Skip to main content

Posts

Showing posts with the label ActionScript

Preserving open nodes in AdvancedDataGrid with HierarchicalCollectionView

My dataProvider is an ArrayCollection, which is populated using XML. var initXML:XML = <items>    <item id="News" label="News" value_1="unchecked" value_2="unchecked" value_3="checked"/>    <item id="BBC" label="The Web" value_1="unchecked" value_2="unchecked" value_3="checked">        <item id="BBC1" label="BBC Homepage" value_1="unchecked" value_2="unchecked" value_3="checked">       <item id="YouTube" label="YouTube" value_1="unchecked" value_2="unchecked" value_3="checked"/>       <item id="GoogleMaps" label="Google Maps" value_1="unchecked" value_2="unchecked" value_3="checked"/>       <item id="MSN" label="MSN" value_1="unchecked" value_2="unchecked" valu...

Flash trace log file in Windows 7

In my recent project, I need to look at the Trace output from the Flex application outside Flash Builder. I have Windows 7 as my operating System. Following are the steps to setup trace log file: Download and install Flash player debug version. Check at www.flashplayerversion.com for the version details. Create a new file named mm.cfg in your home directory. To figure out home directory open a command wondow and type echo %HOMEDRIVE%     TraceOutputFileEnable=1     ErrorReportingEnable=1     MaxWarnings=500     TraceOutPutFileName=C:\Users\(username)\AppData\Roaming\Macromedia\Flash Player\Logs\flashlog.txt Last line is optional if you are fine with the output file location. Open web browser and your Flash application.4. Use a tool like baretail to check the log file.

Spark TextInput for Currency values

In my project, I wanted to have an spark TextInput to show currency values. While formatting it should show unformatted number. The way I did it is by creating a new CurrencyTextInput class extending spark TextInput. Code for the component is (CurrencyTextInput.as): package com.abcd.component {     import flash.events.Event;     import flash.events.FocusEvent;     import spark.components.TextInput;     import spark.formatters.CurrencyFormatter;     public class CurrencyTextInput extends TextInput     {         public function CurrencyTextInput()         {             super();             formatter = new CurrencyFormatter();             formatter.currencySymbol ...

Check if a string converted to a number is actually a number in ActionScript (Flex) and setting scale of a number

An easy way to check if a String is actually a number of not is to actually convert the string to a Number and test if it's NaN. Flex API reference mentions that the top-level function Number(str) returns NaN if the string passed to the method cannot be converted to a Number. The problem with this approach is that if you take Number(null) or Number(undefined) or Number(""), all will return 0 and will evaluate to "is a number". The correct way to do this is shown in following code snippet. It also sets scale of the converted number to two decimal digits. public static function getNumberFromString(val:*, scale:uint = 2):* {     if ((val === null || StringUtil.trim(val) === "" || isNaN(val)) == false)         return setScale(Number(val)).toFixed(scale);     else         return ""; } public static function setScale(number:Number, scale:uint = 2):Number {      scale ...

Download a file using FileReference in Flex

Following function gives an example of how to doanload a file in Flex. "file" object passed to the function is a simple object having following structure: file.fileName = "Abc.png" file.fileType = "image/png" file.url = "/documents/Abc.png" A pattern match-replacement is used to determine file extension, in case fileName does not have it. public function downloadFile(file:Object):void { if (file != null) { var fileName:String = (file.fileName != null && file.fileName.length > 0) ? file.fileName : (new Date()).time.toString(); var fileExtension:String = file.fileExtension; if (fileName.indexOf(".") = 0) fileExtension = fileExtension.slice(fileExtension.lastIndexOf("."), fileExtension.length); else if (fileExtension.lastIndexOf("/") >= 0) fileExtension = "." + fileExtension.slice(fileExtension.lastIndexOf("/...

Flex ByteArray to Java ByteArray

While trying to import XLS files from Flex front-end, we needed to pass the file contents to Java middle tier, where Apache POI library was used to parse the contents. The Java method expects a Java ByteArray containing the file contents, but when we pass a Flex ByteArray to it, it throws error. To overcome this instead of passing Flex ByteArray, we should pass an Array of bytes, as depicted in following code snippet: public static function byteArrayToArrayOfBytes(ba:ByteArray):Array {     var arrayOfBytes:Array = [];     ba.position = 0;     while(ba.bytesAvailable > 0)         arrayOfBytes.push(ba.readByte());     ba.position = 0;     return arrayOfBytes; } public function onFileSelectionComplete(fileReference:FileReference) {     var payload:Object = new Object();     payload.byteArray = byteArrayToArrayOfBytes(fileReference.data);  ...

Flex: ToolTip on Spark DataGrid Rows

We can use Spark DataGrid's showDataTips and dataTipFunction properties to show customized tool tips on the rows. An example showing this: <?xml version="1.0" encoding="utf-8"?> <s:Application initialize="onInitialize()" xmlns:fx="http://ns.adobe.com/mxml/2009" xmlns:s="library://ns.adobe.com/flex/spark"> <fx:Script> <![CDATA[ import mx.collections.ArrayCollection; import mx.utils.StringUtil; private static function fnDataTip(item:Object, column:GridColumn):String { var toolTip:String = item[column.dataTipField != null ? column.dataTipField : column.dataField]; if (toolTip == null || StringUtil.trim(toolTip) == "") toolTip = ""; if (item.hasOwnProperty("toolTipComment") && item.toolTipComment != null && item.toolTipComment....