blog.ashier.com flash / flex / air development
rss rss comment

Archive for the ‘Flex’ Category

Here’s a simple Horizontal Dotted Line Component in Flex. You can implement simple modifications in the source code to create the vertical version of the component.

Demo:

(Either JavaScript is not active or you are using an old version of Adobe Flash Player. Please install the newest Flash Player.)

Right Click to view the source.

David Tucker posted a series of tutorials concerning Cairngorm. It is worth a read.

Donald Martinez extended the datagrid component that will allow you to implicitly page contents in a data grid… You can view his post and download the component or view the demo by clicking this link.

You will need to download the as3corelib classes for this to work.

JSONManager is a wrapper class to simplify JSON service calls. It has a feature that allows you to call service's asynchronously.

Actionscript:
  1. package lib.ashier.managers {
  2.    
  3.     import com.adobe.serialization.json.JSON;
  4.    
  5.     import flash.events.EventDispatcher;
  6.     import flash.utils.Dictionary;
  7.    
  8.     import lib.ashier.events.JSONManagerEvent;
  9.    
  10.     import mx.rpc.events.FaultEvent;
  11.     import mx.rpc.events.ResultEvent;
  12.     import mx.rpc.http.HTTPService;
  13.    
  14.     public class JSONManager extends EventDispatcher{
  15.        
  16.         function JSONManager():void {
  17.             super();
  18.             initializeJSON();
  19.         }
  20.        
  21.         private var service:HTTPService = new HTTPService();
  22.         protected function initializeJSON():void {
  23.             // INITIIALIZE
  24.         }
  25.        
  26.         private function onJSONResult(event:ResultEvent):void {
  27.             if(event.result != "") {
  28.                 var obj:Object = Object(JSON.decode(String(event.result)));
  29.                 obj.url = event.target.url;
  30.                 delete serviceDict[obj.url];
  31.             }else {
  32.                 onJSONFault(new FaultEvent(FaultEvent.FAULT));
  33.                 delete serviceDict[url];
  34.                 return;
  35.             }
  36.             dispatchEvent(new JSONManagerEvent(JSONManagerEvent.RESULT, obj));
  37.         }
  38.        
  39.         private function onJSONFault(event:FaultEvent):void  {
  40.             dispatchEvent(new JSONManagerEvent(JSONManagerEvent.FAULT, {message:((event.fault) ? event.fault.faultString : "Unknown JSON Fault")}));
  41.         }
  42.        
  43.         private var serviceDict:Dictionary = new Dictionary();
  44.         public function send(params:Object = null):void {
  45.             var parameters:String = JSON.encode(params);
  46.            
  47.             if(!serviceDict[_url]) {
  48.            
  49.                 service = new HTTPService();
  50.                 service.useProxy = false;
  51.                 service.contentType = _contentType;
  52.                 service.method = _method;
  53.                 service.url = _url;
  54.                 service.addEventListener(ResultEvent.RESULT, onJSONResult);
  55.                 service.addEventListener(FaultEvent.FAULT, onJSONFault);
  56.                
  57.                 serviceDict[_url] = service
  58.             }
  59.            
  60.             if(params) {
  61.                 service.send(parameters);
  62.             }else {
  63.                 service.send();
  64.             }
  65.             dispatchEvent(new JSONManagerEvent(JSONManagerEvent.SENDING, null));
  66.         }
  67.        
  68.         private var _contentType:String = "text/html";
  69.         public function set contentType(s:String):void {
  70.             _contentType = s;
  71.         }
  72.        
  73.         public function get contentType():String {
  74.             return _contentType;
  75.         }
  76.        
  77.         private var _method:String = "POST";
  78.         public function set method(s:String):void {
  79.             _method = s;
  80.         }
  81.        
  82.         public function get method():String {
  83.             return _method;
  84.         }
  85.        
  86.         private var _url:String = "";
  87.         public function set url(s:String):void {
  88.             _url = s;
  89.         }
  90.        
  91.         public function get url():String {
  92.             return _url;
  93.         }
  94.        
  95.     }
  96.    
  97. }

JSONManagerEvent

Actionscript:
  1. package lib.ashier.events {
  2.    
  3.     import lib.ashier.core.interfaces.IEvent;
  4.     import flash.events.Event;
  5.    
  6.     public class JSONManagerEvent extends Event implements IEvent {
  7.        
  8.        
  9.         public static const SENDING:String = "sending";
  10.         public static const RESULT:String = "result";
  11.         public static const FAULT:String = "fault";
  12.        
  13.         private var value:Object = new Object();
  14.        
  15.         public function JSONManagerEvent(type:String, value:Object) {
  16.                 super(type);
  17.                 this.value = value;
  18.         }
  19.        
  20.         public function get data():Object {
  21.             return this.value;
  22.         }
  23.        
  24.         override public function clone():Event {
  25.             return new JSONManagerEvent(type, value);
  26.         }
  27.  
  28.     }
  29. }

I think this is the best flex development resource so far.

http://blog.flexexamples.com/

I've been trying to develop an ID3 parser and here's what i got so far.

I've update the ID3Parser. It is much faster now.

Actionscript:
  1. package libs.ashier.id3 {
  2.    
  3.     package libs.ashier.id3 {
  4.    
  5.     import flash.events.EventDispatcher;
  6.     import flash.filesystem.File;
  7.     import flash.filesystem.FileMode;
  8.     import flash.filesystem.FileStream;
  9.     import flash.utils.ByteArray;
  10.     import flash.utils.Dictionary;
  11.     import flash.utils.clearInterval;
  12.     import flash.utils.setInterval;
  13.    
  14.     public class ID3Parser extends EventDispatcher{
  15.        
  16.         public function ID3Parser():void {}
  17.        
  18.         /**
  19.          * Bit 7 in the 'ID3v2 flags' indicates whether or not
  20.          * unsynchronisation is applied on all frames.
  21.          */
  22.         private var unsynchronisation:uint;
  23.        
  24.         /**
  25.          * The second bit (bit 6) indicates whether or not the header is
  26.          * followed by an extended header.
  27.          * A set bit indicates the presence of an extended header.
  28.          */
  29.         private var extendedHeader:uint;
  30.        
  31.         /**
  32.          * The third bit (bit 5) is used as an 'experimental indicator'. This
  33.          * flag SHALL always be set when the tag is in an experimental stage.
  34.          */
  35.         private var experimental:uint;
  36.        
  37.         /**
  38.          * FileStream / Bytes
  39.          */       
  40.         private var fs:FileStream;
  41.        
  42.         /**
  43.          * Id3 Version
  44.          */       
  45.         private var version:Number;
  46.        
  47.         /**
  48.          * Available Genres
  49.          */       
  50.         private var genres:Array = [
  51.                                 "Blues", "Classic Rock", "Country", "Dance", "Disco", "Funk",
  52.                                 "Grunge", "Hip-Hop", "Jazz", "Metal", "New Age", "Oldies", "Other",
  53.                                 "Pop", "R&B", "Rap", "Reggae", "Rock", "Techno", "Industrial",
  54.                                 "Alternative", "Ska", "Death Metal", "Pranks", "Soundtrack",
  55.                                 "Euro-Techno", "Ambient", "Trip-Hop", "Vocal", "Jazz+Funk", "Fusion",
  56.                                 "Trance", "Classical", "Instrumental", "Acid", "House", "Game",
  57.                                 "Sound Clip", "Gospel", "Noise", "Alt. Rock", "Bass", "Soul",
  58.                                 "Punk", "Space", "Meditative", "Instrum. Pop", "Instrum. Rock",
  59.                                 "Ethnic", "Gothic", "Darkwave", "Techno-Indust.", "Electronic",
  60.                                 "Pop-Folk", "Eurodance", "Dream", "Southern Rock", "Comedy",
  61.                                 "Cult", "Gangsta", "Top 40", "Christian Rap", "Pop/Funk", "Jungle",
  62.                                 "Native American", "Cabaret", "New Wave", "Psychadelic", "Rave",
  63.                                 "Showtunes", "Trailer", "Lo-Fi", "Tribal", "Acid Punk", "Acid Jazz",
  64.                                 "Polka", "Retro", "Musical", "Rock & Roll", "Hard Rock", "Folk",
  65.                                 "Folk/Rock", "National Folk", "Swing", "Fusion", "Bebob", "Latin",
  66.                                 "Revival", "Celtic", "Bluegrass", "Avantgarde", "Gothic Rock",
  67.                                 "Progress. Rock", "Psychadel. Rock", "Symphonic Rock", "Slow Rock",
  68.                                 "Big Band", "Chorus", "Easy Listening", "Acoustic", "Humour",
  69.                                 "Speech", "Chanson", "Opera", "Chamber Music", "Sonata", "Symphony",
  70.                                 "Booty Bass", "Primus", "Porn Groove", "Satire", "Slow Jam",
  71.                                 "Club", "Tango", "Samba", "Folklore", "Ballad", "Power Ballad",
  72.                                 "Rhythmic Soul", "Freestyle", "Duet", "Punk Rock", "Drum Solo",
  73.                                 "A Capella", "Euro-House", "Dance Hall", "Goa", "Drum & Bass",
  74.                                 "Club-House", "Hardcore", "Terror", "Indie", "BritPop", "Negerpunk",
  75.                                 "Polsk Punk", "Beat", "Christian Gangsta Rap", "Heavy Metal",
  76.                                 "Black Metal", "Crossover", "Contemporary Christian", "Christian Rock",
  77.                                 "Merengue", "Salsa", "Thrash Metal", "Anime", "Jpop", "Synthpop"
  78.                                 ];
  79.        
  80.        
  81.         /**
  82.          * Get ID3 Profile
  83.          *
  84.          */  
  85.         private function getID3Profile():void {
  86.             fs = new FileStream();
  87.             try {
  88.                 fs.open(_file, FileMode.READ);
  89.             }catch (e:Error) {}
  90.             if(_file) {
  91.                 clearInterval(interval);
  92.                 interval = setInterval(checkID3, 10, fs);
  93.             }
  94.         }
  95.        
  96.         /**
  97.          * ID3 Flag
  98.          */       
  99.         private var flags:uint;
  100.         private var interval:uint;
  101.         private var interval2:uint;
  102.        
  103.         /**
  104.          * Check if ID3 is Valid.
  105.          * @param fs
  106.          *
  107.          */
  108.         public function checkID3(fs:FileStream):void {
  109.             clearInterval(interval);
  110.             fs.position = 0;
  111.             var type:String = fs.readUTFBytes(3);
  112.             if(type.toLocaleUpperCase() == "ID3") {
  113.                 version = fs.readByte() + fs.readByte()/100;
  114.                 if(version>= 3) {
  115.                     frameIdSize = 4;
  116.                 }
  117.                
  118.                 flags = fs.readByte();
  119.                 unsynchronisation = flags>> 7;
  120.                 extendedHeader = flags>> 6 && 01;
  121.                 experimental = flags>> 5 && 001;
  122.                 clearInterval(interval2);
  123.                 interval2 = setInterval(readFrames, 10);
  124.             }else {
  125.                 dispatchEvent(new ID3ParserEvent(ID3ParserEvent.ERROR, {message:"Not a valid ID3."}));
  126.             }
  127.         }
  128.        
  129.         /**
  130.          * Declared ID3v2 frames
  131.          */       
  132.         private var frames:Object;
  133.        
  134.         /**
  135.          * First FrameId Size. Default is 3.
  136.          */       
  137.         private var frameIdSize:uint = 3;
  138.        
  139.         /**
  140.          * Parse all types of frames in an ID3.
  141.          *
  142.          */   
  143.        
  144.         private var length:int = 0;
  145.         private var lastFSPosition:int = 0;
  146.        
  147.         private function readFrames():void {
  148.             clearInterval(interval2);
  149.             if(!frames) {
  150.                 frames = new Object();
  151.             }
  152.             length = convertSynchsafe(fs.readUnsignedInt()) + 10;
  153.             parseFrames();
  154.             lastFSPosition = fs.position;
  155.             fs.close();
  156.             dispatchEvent(new ID3ParserEvent(ID3ParserEvent.ID3, ID3Info));
  157.         }
  158.        
  159.          private var regEx:RegExp = new RegExp("" +
  160.                         "/AENC|APIC|PIC|" +
  161.                         "COMM|COMR|" +
  162.                         "ENCR|EQUA|ETCO|" +
  163.                         "GEOB|GRID|" +
  164.                         "IPLS|" +
  165.                         "LINK|" +
  166.                         "MCDI|MLLT|" +
  167.                         "OWNE|" +
  168.                         "PRIV|PCNT|POPM|POSS|" +
  169.                         "RBUF|RVAD|RVRB|" +
  170.                         "SYLT|SYTC|" +
  171.                         "T*" +
  172.                         "UFID|USER|USLT|" +
  173.                         "W*/", "ig");
  174.        
  175.         private function parseFrames():void {
  176.             var id:String = "";
  177.             var size:uint = 0;
  178.             if(fs.position <length) {
  179.                 try {
  180.                     id = fs.readUTFBytes(frameIdSize);
  181.                     size = fs.readUnsignedInt();
  182.                     if (version>= 3) {
  183.                         fs.readByte();
  184.                         fs.readByte();
  185.                     }
  186.                     if(id.match(regEx)) {
  187.                         var obj:Object = new Object();
  188.                         obj.encoding = fs.readByte();
  189.                         obj.text = fs.readUTFBytes(size - 1);
  190.                         frames[id