Products
Technologies

Developer resources

Overriding framework default implementations for mx.managers classes

Avg. Rating 3.7

Problem

You want to customize framework managers available in Flex/AIR. For example, You would like to change the way PopUpManager manager displays popup windows.

Solution

This entry describes how to change standard manager implementation at applications startup. This change does not affect way how managers are used, in application You can still use mx.managers.* classes. You don't have to rebuild all code in addition to change single manager.

Detailed explanation

Most common solution to this problem is creating custom manager class and using this in code instead of default manager class. For example lets take a look at PopUpManager. If You want to customize addPopup method, You create Your own manager, lets call it PopManager, and later on You use it instead of standard PopUpManager class. Main advantage of using method described here is, that You can extend manager , and still use standard class to call all methods. This allows You to change the way the manager works without refactoring all Your code.

In further discussion I use PopUpManager as an example. PopUpManager is class with all public methods defined as statics. Internally to handle popup windows it uses instance of a mx.core.PopUpManagerImpl class, IPopUpManager interface implementator. Implementation class is stored in singleton registry (mx.core.Singleton). All implementation classes for managers are registered at application startup by an instance of SystemManager class. To create singleton registry of classes mx.core.Singleton static method registerClass is used. Only one implementation per manager can be registered. The registration policy used is "first class in wins", all further calls are ignored. To use custom implementation You need to register it before SystemManager registers framework default ones. Default implementations are registered in SystemManager.mx_internal::docFrameHandler method. It is called before the instance of main application class in created.

The best (and seems to be the only possible) way to override framework implementations, is to implement IPreloaderDisplay in Your application and use it to register custom classes. IPreloaderDisplay interface implementor DownloadProgressBar is used as a visual preloader for a Flex/AIR applications. It can be set with use of Application.preloader property. Preloader is created and initialized by SystemManager (simplified description), before it registers default implementations for framework managers. To override any of implementations use initialize method of IPreloaderDisplay interface (other methods can be used as well).

package my.domain {
 public class MyPreloader extends DownloadProgressBar
 {
   override public function initialize():void
   {
     super.initalize();
     Singleton.registerClass("mx.core.IPopUpManager",
                             Class( my.domain.managers.MyPopUpManager ) );
   };
 } 
}

From now on, custom IPopUpManager implementation will be used when static method of the PopUpManager is called. And an example code for custom IPopUpManager implementation

package my.domain.managers {
  public function MyPopUpManager extends PopUpManagerImpl
 {
   private var instance : IPopUpManager;
 
   static public function getInstance():IPopUpManager
   {
       if ( !instance ) instance = new MyPopUpManager();
     return instance;
   };
  
   override public function addPopUp( ... ):void
   {
     super.addPopUp(...);
    /* custom code */
   }
}
}

One static method always needs to implemented in Your class - getInstance. This method is used by Singleton class to add an instance of Your class to the registry. In the same way You can overload managers listed below, as it can be seen not all framework managers can be changed.


BrowserManager
CursorManager
HistoryManager
LayoutManager
ToolTipManager
DragManager

Working example can be found at segfaultlabs.com/blog/post/overriding-default-mx-managers

Report abuse

Related recipes