Custom Module Loader in Flex

I know it’s too late to post this now, but I just found something which I created in October 2011, when I started cleaning up my hard disk.

package 
{

   import flash.display.DisplayObject;
   import flash.system.ApplicationDomain;
   import flash.utils.ByteArray;
   import flash.utils.Dictionary;
   import mx.controls.Alert;
   import mx.controls.ProgressBar;
   import mx.events.FlexEvent;
   import mx.events.ModuleEvent;
   import mx.modules.IModuleInfo;
   import mx.modules.Module;
   import mx.modules.ModuleLoader;
   import mx.modules.ModuleManager;
   import mx.utils.UIDUtil;

   public class CustomModuleLoader extends ModuleLoader{

        private var ichild:IModuleCommunication;
	private var loadRequested:Boolean = false;
	private var map:Dictionary = new Dictionary();
	private var module:IModuleInfo;
	private var progressBar:ProgressBar = null;
        private var _url:String = null;
        private var _dashBoardChildName:String;

	public function CustomModuleLoader(){
	    super();
	}
	
	public function get dashBoardChildName():String{
	    return _dashBoardChildName;
	}

        public function set dashBoardChildName(value:String):void{
	    _dashBoardChildName = value;
	}
	
	override public function get url():String{
	    return _url;
	}

        override public function set url(value:String):void{
	  if (value == _url){
	     return;
	   }
          if (module){
             unloadModule();
	     return;
	   }
          if (value == null || value == ""){
	     unloadModule();
	     return;
	   }
	_url = value;
	dispatchEvent(new FlexEvent(FlexEvent.URL_CHANGED));
	removeAllChildren();

	if (_url != null && _url != "" && loadRequested){
	   if (!map[_url]){
	      loadModule();
	    }
	    else{
	      child = map[_url];
	      addChild(child);
	     }
	   }
	}

        override public function createComponentsFromDescriptors(recurse:Boolean = true):void{
	   super.createComponentsFromDescriptors(recurse);
	   loadRequested = true;
	}

	override protected function measure():void{
	   super.measure();
	   trace("CustomModuleLoader Width--------------------------------------------- " + this.width);
	   trace("CustomModuleLoader Height--------------------------------------------- " + this.height);
	   this.percentWidth = 100;
	   this.percentHeight = 100;

	   if (progressBar){
	     progressBar.top = 200;
	     progressBar.left = 100;
	     progressBar.x = 200;
	     progressBar.y = 100;
	     progressBar.verticalCenter = 0;
	     trace("progressBar Width--------------------------------------------- " + progressBar.width);
	     trace("progressBar Height--------------------------------------------- " + progressBar.height);
	     trace("progressBar X--------------------------------------------- " + progressBar.x);
	     trace("progressBar Y--------------------------------------------- " + progressBar.y);
             trace("progressBar verticalCenter--------------------------------------------- " +  progressBar.verticalCenter);
             trace("progressBar horizontalCenter--------------------------------------------- " + progressBar.horizontalCenter);
	     trace("progressBar top--------------------------------------------- " + progressBar.top);
 	     trace("progressBar left--------------------------------------------- " + progressBar.left);
	   }
	}

	override public function loadModule(url:String = null, bytes:ByteArray = null):void{
	   if (url != null){
	      _url = url;
	     }
           if (_url == null){
	      return;
	    }
           if (map[_url]){
	      //trace("loadModule() - already created the child");
	      return;
	    }

	   if (module){
	      //trace("loadModule() - load already initiated");
	      return;
	    }
	   showProgressBar();
	   dispatchEvent(new FlexEvent(FlexEvent.LOADING));
	   module = mx.modules.ModuleManager.getModule(_url + '?cacheClear=' + UIDUtil.createUID());
	   module.addEventListener(ModuleEvent.PROGRESS, moduleProgressHandler);
	   module.addEventListener(ModuleEvent.SETUP, moduleSetupHandler);
	   module.addEventListener(ModuleEvent.READY, moduleReadyHandler);
	   module.addEventListener(ModuleEvent.ERROR, moduleErrorHandler);
	   module.addEventListener(ModuleEvent.UNLOAD, moduleUnloadHandler);
	   module.load(ApplicationDomain.currentDomain, null, bytes);
	}

        override public function unloadModule():void{
	  if (map[_url]){
	     delete map[_url];
 	  }
          if (child && contains(child)){
	     ichild.handleDispose();
	     removeChild(child);
	     child = null;
	     ichild = null;
	     _url = null;
	   }
          if (module){
	     module.removeEventListener(ModuleEvent.PROGRESS, moduleProgressHandler);
	     module.removeEventListener(ModuleEvent.SETUP, moduleSetupHandler);
	     module.removeEventListener(ModuleEvent.READY, moduleReadyHandler);
	     module.removeEventListener(ModuleEvent.ERROR, moduleErrorHandler);
	     module.removeEventListener(ModuleEvent.UNLOAD, moduleUnloadHandler);
	     module.release();
	     module.unload();
	     module = null;
	  }
	     applicationDomain = null;
	     removeProgressBar();
	 }

	private function moduleErrorHandler(event:ModuleEvent):void{
	    removeProgressBar();
	    dispatchEvent(event);
	}

	private function moduleProgressHandler(event:ModuleEvent):void{
	    dispatchEvent(event);
	}

	private function moduleReadyHandler(event:ModuleEvent):DisplayObject{
	    removeProgressBar();
            child = module.factory.create() as DisplayObject;
            ichild = child as IModuleCommunication;
            ichild.setModuleParent(this.parentApplication);

	    if (url == "DashboardModule.swf"){
	      ichild.setDashboardChild(dashBoardChildName);
	    }
 	   dispatchEvent(event);
           if (child){
		addChild(child);
		map[url] = child;
	    }
	   return child;
	}

	private function moduleSetupHandler(event:ModuleEvent):void{
	    dispatchEvent(event);
	}

	private function moduleUnloadHandler(event:ModuleEvent):void{
	    removeProgressBar();
	    dispatchEvent(event);
	}

	private function removeProgressBar():void{
	    if (progressBar)
	    {
		removeChild(progressBar);
	        progressBar = null;
	    }
	}

	private function showProgressBar():void{
	 if (!progressBar)
	  {
	    progressBar = new ProgressBar();
	    progressBar.labelPlacement = "top";
	    progressBar.source = module;
	    progressBar.verticalCenter = 0;
	    progressBar.indeterminate = true;
	    addChild(progressBar);
	}
     }
  }
}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s