Welcome the user with his name
September 22, 2008Guessing the end-user name, for AIR Applications.
![]()
var nameStr:String = File.userDirectory.name;
Eval mathematical Expression using AS3
September 22, 2008The 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” 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
September 18, 2008The 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. Check the demo of this code. Source of the component here.
<?xml version=”1.0″ encoding=”utf-8″?>
<mx:Application xmlns:mx=”http://www.adobe.com/2006/mxml” 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
September 18, 2008The below code debugs the capabilities of system, might be useful for debugging. Check the output here.
<?xml version=”1.0″ encoding=”utf-8″?>
<mx:Application xmlns:mx=”http://www.adobe.com/2006/mxml” 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
September 18, 2008The 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
September 17, 2008Check 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.
Posted by nsdevaraj
Posted by nsdevaraj
Posted by nsdevaraj 