| Subcribe via RSS

IFlexModuleFactory – create and examine loaded Flex Modules

August 17th, 2008 | 1 Comment | Posted in Flex, Tutorials

In an earlier post I described how you can load in Flex modules using the ModuleManager class and the IModuleInfo interface, but I didn’t really mention IFlexModuleFactory which is pretty fundamental to the whole process.

The factory is responsible for knowing how to create an instance of the module, and also provides access to certain properties of the module. It can get a little more complicated as the methods exposed by the factory become available at different stages of the load process, so you have to be careful not to try and retrieve information before it is available.

The factory is retrieved used by accessing the factory property of the IModuleInfo object, returned by ModuleManager.getModule (). For more information on this, see the previous post. The factory will be of type IFlexModuleFactory, which behind the scenes is implemented by mx.managers.SystemManager. You don’t really need to worry about this detail though, it’s enough to know about the two public methods exposed by the factory.

They are:

  • IFlexModuleFactory.create – This is really the most important method, and provides the mechanism for creating an instance of your loaded module. When enough of the module has been downloaded for this method to be called, the IModuleInfo object will dispatch a ModuleEvent.READY event, and the public property IModuleInfo.ready will return true. The method allows you to optionally pass in one or more parameters, using the “…rest” mechanism. These parameters are passed to your module’s constructor. Calling this method will create a new instance of your module, and the new instace of the method will be returned. The return type is specified as Object, so you will need to manually cast the returned object to the correct type in order to work with it.
  • IFlexModuleFactory.info – This module allows you to find out a bit of information about the module which is being loaded. When enough of the module has been downloaded for this method to be called, the IModuleInfo object will dispatch a ModuleEvent.SETUP event, and the public property IModuleInfo.setup will return true. The method returns an Object, containing a variety of name/value pairs providing bits of information about the module. Examples of the kind of information that can be contained in the object includes width, height, a list of mixins used by the module, a list of RSLs used by the modules, a list of fonts embedded within the module, and information about the modules Main class name. The method can be useful for finding out the module’s layout information before you create an instance of it and add it to the display list.
  • Just to mention a quick ‘gotcha’ that I encountered when first working with Modules… If you find that the call to IModuleData.factory.create for some reason returns null, or at some point in the process you get an Error #1009, most likely your module is using some classes which for some reason are not accessible once the SWFs have been compiled and optimised. A simple fix for this can be to reference the offending classes in some way in the main application to force them to be compiled. Common offenders are the ‘manager’ classes, for example DragManager, PopupManager and SystemManager.

    Tags: ,

ModuleManager and IModuleInfo – loading Flex Modules dynamically

August 17th, 2008 | 8 Comments | Posted in Actionscript 2.0, Flex, Tutorials

When you’re working with Flex Modules, most of the time the ModuleLoader component will be enough to get you up and running. It will quickly and easily let you load your Modules and add them to the display list, it’ll even dispatch an event to let you know when the Module is ready. Lovely stuff. It’s very similar to the SWFLoader component with some added Module-related goodness thrown in.

However, there may be times when the ModuleLoader component may not be appropriate for your needs, and you’ll have to get your hands dirty and get involved with the lower-level ModuleManager class. Some examples of when ModuleLoader might not be suitable include:

  • Your Module is entirely non-visual, and therefore shouldn’t be added to the display list
  • Performance is really important to your application, and the extra overhead of the ModuleLoader container is something you need to remove to try and speed things up. (ModuleLoader essentially wraps your Module’s content in an extra container, incurring a slight performance hit).
  • You need greater control over how and when your modules are loaded and unloaded.

How does ModuleManager work?

ModuleManager maintains a map of modules, indexed by each module’s URL. Information about the module is stored as an object implementing IModuleInfo, and is retrieved by calling the ModuleManager.getModule method, passing in the module’s URL as the method’s only parameter. Once you’ve retrieved the IModuleInfo object, you can call its load method to starting loading in the module. While the module is loading, you can monitor the load progress using a variety of different events (of type ModuleEvent). Finally, when the module is loaded and available, you can create an instance of that module using someModuleInfo.factory.create ().

Continue reading ModuleManager and IModuleInfo – loading Flex Modules dynamically »

Tags: ,

NativeWindow – using AIR windows with Actionscript (part 3 of 3)

August 10th, 2008 | 3 Comments | Posted in AIR, Actionscript 3.0, Tutorials

In the first part of this post I covered each public property exposed by an instance of NativeWindow, before continuing to look at each public method in part two. In this final part of the series I’ll go through each of the different events dispatched by a NativeWindow during its lifetime.

Event.ACTIVATE

This event is sent out when the window is activated. The event doesn’t go through a capture phase, and it won’t bubble up the Display List. Some examples of when your window may dispatch this event include:

  • When the NativeWindow is first created and activated
  • When your application contains multiple NativeWindows and the user clicks on a window which previously was not the active window, giving it focus and bringing it to the front, the window will dispatch Event.ACTIVATE
  • When you switch to another running application elsewhere on the system (an AIR application, or otherwise) and then switch back to this window, the event will be dispatched

Continue reading NativeWindow – using AIR windows with Actionscript (part 3 of 3) »

Tags: , ,

NativeWindow – using AIR windows with Actionscript (part 2 of 3)

August 10th, 2008 | No Comments | Posted in AIR, Actionscript 3.0, Tutorials

In the previous post I listed the public properties exposed by an instance of NativeWindow. In this post, I’ll be going through NativeWindow’s public methods. The third and final part of this series will explain the Events dispatched by a NativeWindow during its lifetime.

Once a NativeWindow has been created, you can call methods to move, resize, reposition, activate and close the window. All these methods are fairly simple, with the odd gotcha thrown in for good measure. So, here’s a description of each of these methods…

Continue reading NativeWindow – using AIR windows with Actionscript (part 2 of 3) »

Tags: , ,

NativeWindow – using AIR windows with Actionscript (part 1 of 3)

August 9th, 2008 | No Comments | Posted in AIR, Actionscript 3.0, Tutorials

In a previous post I described how to create and activate an application window when creating an Actionscript AIR project in Flex Builder 3. I then went on to describe in a little more detail the various properties of the NativeWindowInitOptions class, and how setting some or all of these properties can affect the style and behaviour of the window that will be created. I guess the final step is to describe how to work with the window once it has been created, starting with an explanation of the flash.display.NativeWindow class. There’s quite a bit of functionality contained within the class, so I’ll break this post up into three. Part 1 will describe the properties of a NativeWindow, part 2 will list the public methods and finally part 3 will discuss the events dispatched by the Window throughout its lifetime.

As previously mentioned, an application window is created and displayed in Adobe AIR by creating a new instance of the NativeWindow class, and then calling this instance’s activate method. Content is added to the window by adding DisplayObjects to the stage property of the NativeWindow instance.

Here are descriptions of each of the NativeWindow’s public properties…

Continue reading NativeWindow – using AIR windows with Actionscript (part 1 of 3) »

Tags: , ,