00001 import gugga.events.EventDispatcher;
00002 import mx.utils.Delegate;
00003
00004 import gugga.common.IProgressiveTask;
00005 import gugga.debug.Assertion;
00006
00007 [Event("start")]
00008 [Event("progress")]
00009 [Event("interrupted")]
00010 [Event("completed")]
00011
00015 class gugga.components.XMLLoader extends EventDispatcher implements IProgressiveTask
00016 {
00017 private var UPDATE_PROGRESS_TIME:Number = 0.2;
00018 private var mUpdateProgressInterval:Number;
00019
00020 private var mIsRunning:Boolean;
00021 private var mInterrupted:Boolean;
00022
00023 private var mPercentsLoaded:Number;
00024
00025 private var mXml:XML;
00026 private var mContentPath:String;
00027
00028 public function XMLLoader()
00029 {
00030 mPercentsLoaded = 0;
00031 mIsRunning = false;
00032 mInterrupted = false;
00033 mXml = new XML();
00034 }
00035
00036 public function get Xml():XML
00037 {
00038 return mXml;
00039 }
00040
00041 public function get ContentPath():String
00042 {
00043 return mContentPath;
00044 }
00045
00046 public function set ContentPath(aValue:String)
00047 {
00048 mContentPath = aValue;
00049 }
00050
00051 public function releaseXML() : Void
00052 {
00053 Assertion.failIfTrue(mIsRunning, "You can't releaseXML of a XMLLoader which is running.", this, arguments);
00054
00055 mXml = new XML();
00056 }
00057
00058 public function start():Void
00059 {
00060 mPercentsLoaded = 0;
00061 mIsRunning = true;
00062 mInterrupted = false;
00063 load();
00064 }
00065
00066 public function isImmediatelyInterruptable() : Boolean
00067 {
00068 return false;
00069 }
00070
00071 public function interrupt() : Void
00072 {
00073 mInterrupted = true;
00074 }
00075
00076 public function load(pContentPath:String):Void
00077 {
00078 if(pContentPath)
00079 {
00080 mContentPath = pContentPath;
00081 }
00082
00083 mUpdateProgressInterval = setInterval(this, "onUpdateProgress", UPDATE_PROGRESS_TIME);
00084
00085 mXml.onLoad = Delegate.create(this, onXmlLoaded);
00086
00087 mXml.load(mContentPath);
00088 dispatchEvent({type:"start", target:this});
00089 }
00090
00091 private function onUpdateProgress():Void
00092 {
00093 var bytesTotal:Number = mXml.getBytesTotal();
00094 var bytesLoaded:Number = mXml.getBytesLoaded();
00095
00096 if(bytesTotal && bytesLoaded)
00097 {
00098 mPercentsLoaded = (bytesLoaded / bytesTotal) * 100;
00099 }
00100 else
00101 {
00102 mPercentsLoaded = 0;
00103 }
00104
00105 dispatchEvent({type:"progress", target:this, xml:mXml, total:bytesTotal, current:bytesLoaded, percents:mPercentsLoaded});
00106 }
00107
00108 private function onXmlLoaded(success:Boolean):Void
00109 {
00110 mIsRunning = false;
00111 clearInterval(mUpdateProgressInterval);
00112 onUpdateProgress();
00113
00114 if(success)
00115 {
00116 if(mInterrupted)
00117 {
00118 dispatchEvent({type:"interrupted", target:this});
00119 }
00120 else
00121 {
00122 dispatchEvent({type:"completed", target:this, xml:mXml});
00123 }
00124 }
00125 else
00126 {
00127 Assertion.warning("XML file '" + ContentPath + "' not loaded ", this, arguments);
00128 dispatchEvent({type:"completed", target:this, xml:mXml, error:true});
00129 }
00130 }
00131
00132 public function getProgress() : Number
00133 {
00134 return mPercentsLoaded;
00135 }
00136
00137 public function isRunning() : Boolean
00138 {
00139 return mIsRunning;
00140 }
00141 }