Archive for category actionscript
Flex & ActionScript Regex List
Posted by brianr in actionscript, flex, regex, tutorial on September 1st, 2010
I’m not great with regex since I don’t use it a ton so I constantly find myself looking up simple, reusable regex statements…thought I’d just start listing them as I use them in case someone else finds them useful. This will start with just a couple examples that we’ll continue to add to.
Assume the following vars:
var myString:String; var pattern:RegExp; var resultString:String; var isSuccess:Boolean;
Remove All Spaces
myString = "The quick brown fox."; pattern = /\s+/g; resultString = myString.replace(pattern, ""); trace(resultString); // Thequickbrownfox.
Remove Special Characters & Spaces
myString = "The 123 quick 456 brown !@#$% fox."; pattern = /\W/g; resultString = myString.replace(pattern, ""); trace(resultString); // The123quick456brownfox
Remove Special Characters & Numbers & Spaces
myString = "The 123 quick 456 brown !@#$% fox."; pattern = /[^a-zA-Z]+/g; resultString = myString.replace(pattern, ""); trace(resultString); // Thequickbrownfox
Test URL String
myString = "http://yahoo.com"; pattern = /^http(s)?:\/\/((\d+\.\d+\.\d+\.\d+)|(([\w-]+\.)+([a-z,A-Z][\w-]*)))(:[1-9][0-9]*)?(\/([\w-.\/:%+@&=]+[\w- .\/?:%+@&=]*)?)?(#(.*))?$/i; isSuccess = pattern.test(myString); trace(isSuccess); // true myString = "htt://yahoo.com"; isSuccess = pattern.test(myString); trace(isSuccess); // false
Trim String (With Tabs & Returns): Courtesy of Jeff Channel
myString = myString = "The quick brown fox. "; pattern = /^\s+|\s+$/gs; resultString = myString.replace(pattern, ""); trace(resultString); // "The quick brown fox."
Unable to export SWC oem
Posted by admin in actionscript, flash builder, flex on April 7th, 2010
Been switching environments a bit lately from Flash Builder to Flex Builder and back and using different SDKs.
When importing a new project that someone had checked in from a different version, the .flexLibProperties file was causing this issue.
After much scouring, I found something that worked.
Open .flexLibProperties and find the section <includeResource/>. Remove everything in that section.
Made the problem disappear.
Dynamic Classes Gotcha!
Posted by admin in Uncategorized, actionscript, flex, flex builder on April 6th, 2010
We recently started using dynamic classes to assist with unknown data formats. It’s a pretty powerful feature of the language. It comes with a price in that you don’t get strong typing nor intellisense on the dynamic properties. But, there are cases where it can serve great purpose.
One of the things you have to keep in mind though is that since the class is dynamic, the compiler can’t provide the linkage for you to do something like:
var someValue:SomeClass = someObject as SomeDynamicClass;
The compiler won’t complain, but, you will as you scratch your head trying to understand why someValue == null all the time. The lack of information at compile time is the obvious culprit.
But, I have found a decent workaround. Provide a constructor for the dynamic class that takes and Object as an optional parameter. If you pass in a parameter, call the helper function to populate the object. Here I’m using a form of reflection to do this dynamically so I don’t have to hand write all the properties.
dynamic public class SomeDynamicClass
public function SomeDynamicClass(obj:Object=null)
{
if ( obj )
this.populateMe(obj);
}
private function populateMe(source:Object):void
{
var destinationList:XML = describeType(this);
for each ( var propName:XML in destinationList..variable )
{
if ( source.hasOwnProperty( propName.@name ) )
{
var cls:Class = getDefinitionByName(propName.@type) as Class;
this[propName.@name] = source[propName.@name] as cls;
}
}
}
Later on, when you get some generic Object, like from ArrayCollection.getItem, you don’t do the normal cast but instead create a new instance passing in the Object.
var someValue:SomeClass = new SomeDynamicClass(someObject);
Hope that helps.
Dude, Where’s My Chart Labels?
Posted by joes in actionscript, data visualization, flex, flex builder on November 19th, 2009
Wondering where your Flex Chart labels are? Well, you’re not alone. Turns out it may be related to your use of Embedded Fonts and Modules.
Surfacing a charting component in a Module works just fine when not making use of Embedded Fonts. Embed fonts in your main application or the Module itself and Presto! Your labels will disappear.
A number of bug tickets have been opened and closed with Adobe, i.e. http://bugs.adobe.com/jira/browse/FLEXDMV-1883, indicating that the issue was resolved with DMV 3.3.0. Currently using DMV 3.4.0.9271, however, and the problem seems to have re-emerged.
Opening another bug ticket with Adobe and in the meantime commenting out the use of Embedded Fonts.
More to follow…
Axiis Drill Down Example
Posted by admin in actionscript, axiis, data visualization, flex, tutorial on November 2nd, 2009
I’ve seen a number of requests for a drill down version of an Axiis chart. We have a current client that will need that functionality as well so I figured I’d give it a shot.
The approach I’m taking is pretty straight forward. Put a drill down layout on the stage and make it invisible initially. When the user clicks on an item in the primary chart that we want to drill in, capture the event, set a dataProvider specific to the portion of the data that represents the drill down, hide the primary layout and show the drill down layout.
Building off the familiar approach for the Axiis examples, add a few objects for the drill down layout to use
<mx:Object id=“dataProvider”/> <mx:String id=“verticalField”/> <mx:String id=“dataField”>date</mx:String> <mx:String id=“labelField”>date.value</mx:String> <mx:Object id=“drilldataProvider”/> <mx:String id=“drilldataField”/> <mx:String id=“drilllabelField”/>
The drill down layout can be whatever type you choose. Here I’m just going to display the data as a simple bar chart.
<axiis:HBoxLayout
id=“drillDownLayout”
x=“25”
y=“0”
showDataTips=“true”
height=“{dc.height-70}”
width=“{dc.width-25}”
visible=“false”
dataProvider=“{drilldataProvider}”
percentGap=“0”
dataField=“{drilldataField}”
labelField=“{drilllabelField}”
itemClick=“this.itemClick(event);”>
<axiis:drawingGeometries>
<degrafa:RegularRectangle
id=“drillRectangle”
x=“{drillDownLayout.currentReference.x}”
y=“{drillDownLayout.height-vScale.valueToLayout(drillDownLayout.currentValue)}”
width=“{drillDownLayout.currentReference.width}”
height=“{vScale.valueToLayout(drillDownLayout.currentValue)}”
stroke=“{colStroke}”/>
<degrafa:RasterText
text=“{drillDownLayout.currentLabel}”
fontFamily=“Arial”
align=“center”
x=“{drillDownLayout.currentReference.x}”
width=“{drillDownLayout.currentReference.width}”
y=“{drillDownLayout.height+5}”/>
</axiis:drawingGeometries>
</axiis:HBoxLayout>
The only real piece of interest in this is the addition of a handler for the itemClick event.
itemClick="this.itemClick(event)"
Here’s the code that will handle the click of an item in the layout. Note that it will check to see which layout is currently visible so we can get back to the primary chart.
public function itemClick(evt:LayoutItemEvent):void
{
// Switch back to main view
if ( this.drillDownLayout.visible )
{
this.drilldataProvider = null;
this.drillDownLayout.visible = false;
this.hLayout.visible = true;
dc.invalidateDisplayList();
}else {
var ac:ArrayCollection = evt.item.layout.dataProvider as ArrayCollection;
this.hLayout.visible = false;
this.drillDownLayout.visible = true;
this.drilldataField = &quot;value&quot;;
this.drilllabelField = &quot;value&quot;;
this.drillRectangle.fill = ( evt.item.data.type == "receivables" ) ? this.receivablesStack.fill : this.payablesStack.fill;
this.drilldataProvider = ac;
dc.invalidateDisplayList();
}
}
Axiis Examples with Lots of Comments
Posted by admin in actionscript, air, data visualization, flex, tutorial on October 17th, 2009
We’ve begun using Axiis on a current client’s project. The application is loaded with graphs and charts and when we hand off the project, we needed to provide a framework for the development staff to quickly build and add new graphing components. Some of the charts are simple but there are some more complicated ones we need to build and will be built in the future. The current Flex DataViz components work fine for simple tasks. But, anyone who has had to go beyond the basics understands the pain that you quickly begin to feel.
Fortunately, I caught Tom Gonzalez’s, (BrightPoint Consulting) lecture at MAX this month and was blown away. The things they’re doing with the Axiis framework and Degrafa are off the charts! (pun intended)
I’ll leave the real discovery to you to check out the Axiis site, but, Axiis is a ’specialized framework that implements specific design patterns that can be used to create your own visualizations’. It’s not a pre-built collection of charting components. They’ve developed a great way of abstracting the basic building blocks of doing data visualization.
Axiis is currently in beta release. There are a lot of great examples on their site so check them out. If you’re new to Degrafa, it’s worth your while to do a bit of reading and check out the cool examples they have on their site as well since many of the examples use Degrafa.
Being in beta, the documentation is a work in progress. The examples from the site are great and really cover a lot of space, but I did struggle with some of the concepts at first. So I’ve taken three of the first examples we needed to borrow from and hyper-commented them for clarity.
I’ll continue to add to the collection over time.
Workaround to Flex SDK 3.4 Bug 22333 :: HTTPService Responders Are Called Twice…Here’s a Fix!
Posted by brianr in actionscript, flex on September 29th, 2009
This one kicked my team’s ass for a couple of hours so I’m hoping it’ll help somone else…so we updated our Flex SDKs to 3.4 and all of a sudden all of our Delegates’ result handlers were getting called twice. We thought it was due to our service orchestration object (we have a base object that allows us to make sequential Cairngorm service calls) and somehow we were calling the services 2x, but nope…wasn’t it. Seemed our AbstractDelegate that sets up the responders for all of our service calls like so had the issue:
public function executeHTTPService(requestDTO:AbstractRequestDTO=null):void
{
logger.debug("executeHTTPService");
var token:AsyncToken;
var responder:mx.rpc.Responder;
// create a responder for the service
responder = new mx.rpc.Responder(result, fault); // WTF mate?
// execute the service
token = this.service.send(requestDTO);
token.addResponder(responder); // why yall busted up now???
}
Apparently, SDK 3.4 reintroduces an old defect that causes this implementation of setting up the responder with the async token to call the result handler twice…bummer, eh? So what to do…well, a simple workaround until they fix this is to do the following:
this.service.addEventListener(ResultEvent.RESULT, result); this.service.addEventListener(FaultEvent.FAULT, fault);
Just create your handlers with good-ole fashioned event handlers…DONE.
Get more info on the defect here.
Java byte[] to ActionScript String - UPDATED
Posted by brianr in actionscript, blazeds, java on July 30th, 2009
I recently needed to send a Java byte[] to Flex that ultimately needed to be converted to an ActionScript String on the Flex side. I wrestled with it for a bit before I realized that I needed to make the byte[] on the Java side encoded with UTF-8.
Here’s a quick EchoService I created in Java that takes a String parameter from Flex, converts it into a byte[], and then sends it back to Flex, as well as the accompanying Flex code that handles the service’s result and transforms the byte[] back into a String in ActionScript.
package com.wasi.services.test;
import java.io.UnsupportedEncodingException;
public class EchoService
{
public String echo(String param)
{
return param;
}
public int echo(int param)
{
return param;
}
public byte[] echoStringToByte(String param)
{
System.out.println("echoStringToByte: " + param);
byte[] byteArray = null;
try
{
byteArray = param.getBytes("UTF-8");
}
catch (UnsupportedEncodingException e)
{
e.printStackTrace();
}
return byteArray;
}
}
And on the Flex side:
import mx.rpc.remoting.mxml.RemoteObject;
import mx.rpc.events.FaultEvent;
import mx.rpc.events.ResultEvent;
private var echoStringToByteService:RemoteObject;
private function init():void
{
trace("init");
this.echoStringToByteService = new RemoteObject();
this.echoStringToByteService.destination = "echoService";
this.echoStringToByteService.endpoint = "http://localhost:8080/bds/messagebroker/amf";
this.echoStringToByteService.showBusyCursor = true;
this.echoStringToByteService.addEventListener(ResultEvent.RESULT, onEchoStringToByteServiceResult);
this.echoStringToByteService.addEventListener(FaultEvent.FAULT, onEchoStringToByteServiceFault);
}
private function onEchoServiceSubmit(evt:MouseEvent):void
{
trace("onEchoServiceSubmit");
var str:String;
str = this.echoStringTextInput.text;
this.echoStringToByteService.echoStringToByte(str);
}
private function onEchoStringToByteServiceResult(evt:ResultEvent):void
{
trace("onEchoStringToByteServiceResult");
var byteArray:ByteArray;
byteArray = evt.result as ByteArray;
// both the readUTFBytes() and toString() methods give return you the string
trace("onEchoStringToByteServiceResult readUTFBytes = " + byteArray.readUTFBytes(byteArray.bytesAvailable));
trace("onEchoStringToByteServiceResult toString = " + byteArray.toString());
this.echoServiceResultText.text = byteArray.readUTFBytes(byteArray.bytesAvailable);
}
private function onEchoStringToByteServiceFault(evt:FaultEvent):void
{
trace("onEchoStringToByteServiceFault");
}
In order to run this you’ll need to:
- Copy and paste the ActionScript listed above into a Flex Application tat calls the init() method on creationComplete.
- Have BlazeDS or LCDS running on a server (as you can see in the definition of my RemoteObject in Flex I created a small Java web-app with a context root of /bds).
- Configure remoting-config.xml by adding in the following destination node listed below.
<destination id="echoService"> <properties> <source>com.wasi.services.test.EchoService</source> <scope>application</scope> </properties> </destination>
NOTE: I removed a couple things for clarity and this code has not been retested to verify that it still runs, but you should get the idea.
Wordpress Plugin SyntaxHighlighter Test On ActionScript 3
Posted by brianr in actionscript, wordpress on June 29th, 2009
This is a test post to determine if I like SyntaxHighlighter as a source code plugin for Wordpress. Here’s two ActionScript 3 examples:
private var foo:Bar = new Bar();
/**
* Web App Solution Confidential Information
* Copyright 2009, Web App Solution, Inc.
*
* @author Brian Riley
* @date June, 29, 2009
*/
package com.wasi.test
{
import com.wasi.test.interfaces.IFooBar;
/**
* This class rocks my world.
*/
public class Foo extends Bar implements IFooBar
{
/**
* Constant happy value.
*/
public var myLongString:String = "this is a really, really long string to illustrate what happens when one line of code is tooooo long";
/**
* Constant happy value.
*/
private static const MY_CONST:String = "myPrivateStaticConstant";
/**
* Constructor.
*
* param myFoo A String vlaue that's happy.
* param myBar A Number vlaue that's sad.
*/
public function Foo(myFoo:String, myFoo:Number)
{
// TODO
}
}
}
Just wrap your source code in the following syntax:
[sourceCodeAlias]
my source code
[/sourceCodeAlias]
where sourceCodeAlias = as3 or actionscript3. Here’s the full list of possible code syntax highlighters at your disposal.