Archive for March, 2009

fileReference.load()

Today, i faced a weird problem of fileReference.load() not working in AIR. Even, in the latest 3.3 SDK. To resolve that, you have to install Flex Builder Update. Then, modify your App.xml into air version 1.5. As, to get the benefits of latest AIR Version.

Otherwise, in AIR: Code gets compiled but when we select a file from system, it throws a error.

Advertisements

Video Conference

The link uses Cocomo SDK for achieving the basic video conference.

Date Util Functiions

Utility functions to find First Day of a month, Relative Date information like “Today”, “Yesterday”

public static function getFirstDayofMonth(fullyear:uint,month:uint):uint
{
var tmp:Date = new Date(fullyear,month,1);
return tmp.getDay();
}

public static function isLeapYear( fullyear:uint ):Boolean
{
var flag:Boolean = false;
if(((fullyear%4==0) && (fullyear%100!=0)) || (fullyear%400==0)) flag = true;
return flag;
}

public static function getWeekName( index:uint , type:uint=0 ):String
{
var week:Array;
if(type==0) week = [“SUNDAY”,”MONDAY”,”TUSEDAY”,”WEDNESDAY”,”THURSDAY”,”FRIDAY”,”SATURDAY”];
else if(type==1) week = [“SUN”,”MON”,”TUE”,”WED”,”THU”,”FRI”,”SAT”];
else week = [“Sun”,”Mon”,”Tue”,”Wed”,”Thu”,”Fri”,”Sat”];
return week[index];
}

public static function getMonthName( index:uint , type:uint=0 ):String
{
var month:Array;
if(type==0) month = [“JANUARY”,”FEBRUARY”,”MARCH”,”APRIL”,”MAY”,”JUNE”,”JULY”,”AUGUST”,”SEPTEMBER”,”OCTOBER”,”NOVEMBER”,”DECEMBER”];
else if(type==1) month = [“jan”,”feb”,”mar”,”apr”,”may”,”jun”,”jul”,”aug”,”sep”,”oct”,”nov”,”dec”];
else month = [“Jan”,”Feb”,”Mar”,”Apr”,”May”,”Jun”,”Jul”,”Aug”,”Sep”,”Oct”,”Nov”,”Dec”];
return month[index];
}

public static function getRelativeDateName( date:Date, type:uint=0 ):String
{
var d1:Date = new Date();
d1 = new Date(d1.getFullYear(),d1.getMonth(),d1.getDate());
var d2:Date = new Date(date.getFullYear(),date.getMonth(),date.getDate());
if(d11000*60*60*24*7) return “”;
if(type>8||isNaN(type)) type = 0;
var list:Array;
switch(type) {
case 0: case 3:
list = [“TODAY”,”YESTERDAY”]; break;
case 1: case 4:
list = [“Today”,”Yesterday”]; break;
case 2: case 5:
list = [“today”,”yesterday”]; break;
}
var diff:int = d1.getDate()-d2.getDate();
if(diff<=1) return list[diff];
return getWeekName( d2.getDay(), type );
}

Currency/Numeric Formatter

The below custom functions might be of use where you don’t want to use currency formatter or numeric Formatter of AS3

 
public function currencyFormat( number:Number ):String {
var words:Array = String( number ).split( “” ).reverse(); 
  var results:Array = new Array();
 var l:int = words.length;
  for ( var i:int = 0; i < l; i++ ) {
   results.push( words[i] );
   if ( i % 3 == 2 ) {
     results.push( “,” );
   }
   }
   return results.reverse().join( “” );
}

public function prefixZero( number:Number, figure:int ):String {
   var str:String = String( number );
   for ( var i:int = 0; i < figure; i++ ) {
     str = “0” + str;
   }
   return str.substr( str.length – figure, str.length );
}

ByteArray to BitmapData

The Below code demonstrates the conversion of ByteArray to BitmapData. In the below case, SWF is loaded and converted into BitmapData

private function swfLoad_complete(evt:Event):void {
byteArrayToBitmapData(swf.content.loaderInfo.bytes);
}
private function byteArrayToBitmapData(ba:ByteArray):void {
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, getBitmapData);
loader.loadBytes(ba);
}
private function getBitmapData(e:Event):void {
var content:* = loader.content;
var BMPData:BitmapData = new BitmapData(content.width,content.height);
var UIMatrix:Matrix = new Matrix();
BMPData.draw(content, UIMatrix);
}

Convert Data Types: ArrayCollection, XML, Object, String

The code below demonstrates the data type conversions among basic DataTypes used most in Flex. The ‘init()’ method does to and from conversion for ArrayCollection to XML and XML to ArrayCollection

Object, String, XML, ArrayCollection

 

import mx.collections.ArrayCollection;
import mx.rpc.xml.SimpleXMLEncoder;
import mx.rpc.xml.SimpleXMLDecoder;
import mx.utils.ObjectUtil; 
  private function xmlToArrayCollection(xml:XML):ArrayCollection{                 
            var xmlDoc:XMLDocument = new XMLDocument(xml.toString());
           var decoder:SimpleXMLDecoder = new SimpleXMLDecoder(true);
            var resultObj:Object = decoder.decodeXML(xmlDoc);
            var ac:ArrayCollection =new ArrayCollection(Array(resultObj.root.list.source.item));
            return ac;
}
private function objectToXML(obj:Object):XML {
            var qName:QName = new QName(“root”);
            var xmlDocument:XMLDocument = new XMLDocument();
            var simpleXMLEncoder:SimpleXMLEncoder = new SimpleXMLEncoder(xmlDocument);
            var xmlNode:XMLNode = simpleXMLEncoder.encodeValue(obj, qName, xmlDocument);
            var xml:XML = new XML(xmlDocument.toString());
            return xml;
}
private function objectToArrayCollection(obj:Object):ArrayCollection {
            var ac:ArrayCollection =  new ArrayCollection(obj as Array);
            return ac;

private function arrayCollectionToXML(ac:ArrayCollection):XML{
var xml:XML = objectToXML(ac);
return xml;
}
private function init():void{
  var arr:Array = new Array();
  arr.push({data:0,name:’deva’});
  arr.push({data:1,name:’raj’});
  var ac:ArrayCollection = new ArrayCollection(arr);
  var xml:XML = arrayCollectionToXML(ac);
var newAc:ArrayCollection = xmlToArrayCollection(xml);
trace(newAc[0][0].name);
}
private function objToStr(value:Object,indent:int =0,
                                         refs:Dictionary= null, 
                                         namespaceURIs:Array = null, 
                                         exclude:Array = null):String{
var str:String;
var refCount:int = 0;
if (value is Date)
            {
                return value.toString();
            }
            else if (value is XMLNode)
            {
                return value.toString();
            }
            else if (value is Class)
            {
                return “(” + getQualifiedClassName(value) + “)”;
            }
            else
            {
                var classInfo:Object = ObjectUtil.getClassInfo(value, exclude,
                    { includeReadOnly: true, uris: namespaceURIs });
                var properties:Array = classInfo.properties;
                str = “(” + classInfo.name + “)”;
                if (refs == null)
                    refs = new Dictionary(true);
                var id:Object = refs[value];
                if (id != null)
                {
                    str += “#” + int(id);
                    return str;
                }
                if (value != null)
                {
                    str += “#” + refCount.toString();
                    refs[value] = refCount;
                    refCount++;
                }
                var isArray:Boolean = value is Array;
                var isDict:Boolean = value is Dictionary;
                var prop:*;
                indent += 2;
                for (var j:int = 0; j < properties.length; j++)
                {
                    str = newline(str, indent);
                    prop = properties[j];
                    if (isArray)
                        str += “[“;
                    else if (isDict)
                        str += “{“;
                    if (isDict)
                    {
                        str += objToStr(prop, indent, refs,
                                                namespaceURIs, exclude);
                    }
                    else
                    {
                        str += prop.toString();
                    }
                    if (isArray)
                        str += “] “;
                    else if (isDict)
                        str += “} = “;
                    else
                        str += ” = “;
                    try
                    {
                        str += objToStr(value[prop], indent, refs,
                                                namespaceURIs, exclude);
                    }
                    catch(e:Error)
                    {
                        str += “?”;
                    }
                }
                indent -= 2;
                return str;
            }
}
private static function newline(str:String, n:int = 0):String
   {
       var result:String = str;
       result += “\n”;
       for (var i:int = 0; i < n; i++)
       {
           result += ” “;
       }
       return result;
   }
/* General function for Conversion (thanks, Krystian Bień)
 public function xmlToArrayCollection(xml:XML):ArrayCollection{
            var temp:String = ‘<items>’ + xml.toString() + ‘</items>’;
            xml = XML(temp);
            var xmlDoc:XMLDocument = new XMLDocument(xml.toString());
            var decoder:SimpleXMLDecoder = new SimpleXMLDecoder(true);
            var resultObj:Object = decoder.decodeXML(xmlDoc);
            var ac:ArrayCollection;
            ac = new ArrayCollection();
            ac.addItem(resultObj.items);
            return ac;                
        }*/

TextSelection in Flash

To Select a Static Text in Flash, as like mentioned in LiveDocs.
It doesn’t work, its mentioned in the below bug entry [Link].

The work around for that is to embed the fonts of the text in your flash movie.