Archive for September, 2008

Flex in RIA Development

The session detailed about Flex in RIA Development. The PPT contents of the session is available here.

Using IViewCursor for XML Traversing

The source code for using IViewCursor for XMLListCollection Navigation is here.

Flex 3 Vs Gumbo

This link describes the most differences of Flex3 and Gumbo

Welcome the user with his name

Guessing the end-user name, for AIR Applications.
;)
var nameStr:String = File.userDirectory.name;

Eval mathematical Expression using AS3

The below code is example for how to use MathParser.as used for evaluating maths expressions using flex. Inspired from flashmaths

<?xml version=”1.0″ encoding=”utf-8″?>
<mx:Application xmlns:mx=”http://www.adobe.com/2006/mxml&#8221; backgroundColor=”#333333″ creationComplete=”calculate()”>
<mx:Script>
<![CDATA[
[Bindable]
private var evalStr:String;
private var mpExp:MathParser = new MathParser([ ]);
private function calculate ():void {
var compobjVal:Object = mpExp.doCompile(mathStr.text);
evalStr = String(mpExp.doEval(compobjVal.PolishArray, []));
}
]]>
</mx:Script>
<mx:TextInput id=”mathStr” text=”((7*(1-8))*9)-6+7″ change=”calculate()” />
<mx:Text id=”resVal” color=”White” text=”{evalStr}” />
</mx:Application>

Using garbage collection

The below component can be used for monitoring/debugging the memory usage in your application. This also portrays about how to force the garbage collection or emptying the memory usage.

<?xml version=”1.0″ encoding=”utf-8″?>
<mx:Application xmlns:mx=”http://www.adobe.com/2006/mxml&#8221; layout=”horizontal” >
<mx:Script>
<![CDATA[
import flash.events.TimerEvent;
[Bindable]
private var minmem:Number = 0;
[Bindable]
private var totalmem:Number = 0;
[Bindable]
private var maxmem:Number = 0;
private var timer:Timer;
private function doGarbageCollection() : void
{
var lc1:LocalConnection = new LocalConnection();
var lc2:LocalConnection = new LocalConnection();
try
{
lc1.connect(“empty”);
lc2.connect(“empty”);
}
catch (e:Error) {}
}
public function start() : void
{
timer = new Timer(500);
timer.addEventListener(TimerEvent.TIMER, timerHandler, false, 0, true);
timer.start();
}
public function stop() : void
{
totalmem = 0;
timer.stop();
timer.removeEventListener(TimerEvent.TIMER, timerHandler);
timer = null;
}
private function timerHandler(e:TimerEvent) : void
{
totalmem = System.totalMemory;
maxmem = Math.max(maxmem, totalmem);
if (minmem == 0)
{
minmem = totalmem;
}
else
{
minmem = Math.min(minmem, totalmem);
}
}
]]>
</mx:Script>
<mx:Form>
<mx:FormItem label=”Minimum : “>
<mx:TextInput id=”minimum” editable=”false” text=”{minmem}” />
</mx:FormItem>
<mx:FormItem label=”Current : “>
<mx:TextInput id=”current” editable=”false” text=”{totalmem}”/>
</mx:FormItem>
<mx:FormItem label=”Maximum : “>
<mx:TextInput id=”maximum” editable=”false” text=”{maxmem}”/>
</mx:FormItem>
<mx:HBox>
<mx:Button label=”Monitor” id=”monitors” toggle=”true” click=”{monitors.selected ? start() : stop() }”/>
<mx:Button label=”Clear” click=”doGarbageCollection()”/>
</mx:HBox>
</mx:Form>
</mx:Application>

Debug Flash Player Capabilities

The below code debugs the capabilities of system, might be useful for debugging.

<?xml version=”1.0″ encoding=”utf-8″?>
<mx:Application xmlns:mx=”http://www.adobe.com/2006/mxml&#8221; layout=”horizontal”
initialize=”init()”>
<mx:Script>
<![CDATA[
import flash.system.Capabilities;
import mx.utils.ObjectUtil;
private function init() : void
{
var _capObj:Array = new Array();
capability.dataProvider = objectToArray(Capabilities);
}
private function objectToArray(cap:Class) : Array
{
var capObj:Object = ObjectUtil.getClassInfo(cap);
var _capArr:Array = new Array();
for each (var _capPropObj:Object in capObj.properties)
{
if (_capPropObj.localName != "prototype")
{
var capProp:Object = new Object();
capProp.Capability = _capPropObj.localName;
capProp.Value =Capabilities[capProp.Capability]
_capArr.push(capProp);
}
}
return _capArr;
}
]]>
</mx:Script>
<mx:DataGrid id=”capability” width=”100%” height=”100%” />
</mx:Application>

RegExp Validation

The below function validates the email ID string using regular expression.

private function validateEmail(mailid:String):Boolean {
        var emailExpression:RegExp = /^[a-z][\w.-]+@\w[\w.-]+\.[\w.-]*[a-z][a-z]$/i;
        return emailExpression.test(mailid);
}

//e-mail option 2:

/[a-zA-Z0-9!$&*.=^`|~#%'+\/?_{}-]+@([\w_-]+\.)+[a-zA-Z]{2,4}/;

//URL:

/(https?|ftp|svn)(:\/\/[\w-_.:;!?~*\'()\/\@&=+\$,%#]+)/;

//Color:

/#([0-9a-fA-F]{6}|[0-9a-fA-F]{3})/;

//String Quote:

/(“|’)(.+)?\1/;

IP evaluation:

var ipPattern:RegExp = /\b(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b/;

Data Filter

Check the demo for data filtering xml output with LOGICAL AND, updating the Datagrid dynamically.
The source is available over here. It uses data filter component developed by Jack Herrington.

Call USA free on VOIP

Using AS3 and VOIP protocol call USA for free Dial Now
;)

Using IViewCursor for Navigation

The Below code is example for using IViewCursor for ArrayCollection Navigation

<?xml version=”1.0″?>
<mx:Application xmlns:mx=”http://www.adobe.com/2006/mxml&#8221; initialize=”run();”>
<mx:Script>
<![CDATA[
import mx.collections.*;
private var myCursor:IViewCursor;
[Bindable]
public var myAC:ArrayCollection;
public var myArray:Array = [ { label:1, data:"One" }, { label:2, data:"Two" }, { label:3, data:"Three" }, { label:4, data:"Four" }, { label:5, data:"Five" },
{ label:6, data:"Six" }, { label:7, data:"Seven" }, { label:8, data:"Eight" }, { label:9, data:"Nine" }, {label:10, data:"Ten"}];

public function run():void {
myAC = new ArrayCollection(myArray);
myCursor=myAC.createCursor();
var sort:Sort = new Sort();
sort.fields = [new SortField("label")];
myAC.sort=sort;
myAC.refresh();
first.enabled = false;
back.enabled = false;
}
public function countLast(theCursor:IViewCursor):int {
var counter:int=0;
var mark:CursorBookmark=theCursor.bookmark;
while (theCursor.moveNext()) {
counter++;
}
theCursor.seek(mark);
return counter;
}
public function countFromSelection():void {
myCursor.findAny(myCB.selectedItem);
var count:int = countLast(myCursor);
ta1.text = “cursor is at: ” + myCursor.current.label;
enableDisableButtons();
}
public function nextCollection():void {
if(! myCursor.afterLast) {
myCursor.moveNext();
ta1.text=”cursor is at: ” + myCursor.current.label;
}
}
public function backCollection():void {
if(!myCursor.beforeFirst) {
myCursor.movePrevious();
ta1.text=”cursor is at: ” + myCursor.current.label;
}
}
public function firstCollection():void {
myCursor.seek(CursorBookmark.FIRST);
ta1.text=”cursor is at: ” + myCursor.current.label;
}
public function lastCollection():void {
myCursor.seek(CursorBookmark.LAST);
ta1.text=”cursor is at: ” + myCursor.current.label;
}
public function navigate(event:MouseEvent):void {
switch(event.currentTarget.label) {
case ‘first’:
firstCollection();
break;
case ‘back’:
backCollection();
break;
case ‘next’:
nextCollection();
break;
case ‘last’:
lastCollection();
break;
}
enableDisableButtons()
}
public function enableDisableButtons():void{
var firstInCollection:Boolean = myAC.getItemAt(0) == myCursor.current;
first.enabled = back.enabled = !firstInCollection
var lastInCollection:Boolean = myAC.getItemAt(myAC.length – 1) == myCursor.current;
last.enabled = next.enabled = !lastInCollection;
}
]]>
</mx:Script>
<mx:ComboBox id=”myCB” dataProvider=”{myAC}” change=”countFromSelection();”/>
<mx:TextArea id=”ta1″ height=”200″ width=”175″/>
<mx:HBox>
<mx:Button label=”first” id=”first” click=”navigate(event)”/>
<mx:Button label=”back” id=”back” click=”navigate(event)”/>
<mx:Button label=”next” id=”next” click=”navigate(event)”/>
<mx:Button label=”last” id=”last” click=”navigate(event)”/>
</mx:HBox>
</mx:Application>

Follow

Get every new post delivered to your Inbox.

Join 436 other followers