AS3Commons-stageprocessing offers a convenient registry system for IStageObjectProcessor implementations. An IStageObjectProcessor is meant
to 'post-process' a display object that has been added to the stage.
To keep the system as un-intrusive as possible an IStageObjectProcessor is registered with an associated IObjectSelector implementation.
This IObjectSelector decides if a DisplayObject that was added to the stage will be processed or not. Each IObjectSelector can be
associated with one or more IStageObjectProcessors.
IObjectSelectors in their turn can be associated with a certain DisplayObject that is considered as a 'root view'.
That way different areas of the stage can be associated with different IObjectSelectors and IStageObjectProcessors.
First of all, an instance of FlashStageObjectProcessorRegistry will need to be created and associated with the current stage.
var registry:FlashStageObjectProcessorRegistry = new FlashStageObjectProcessorRegistry(); registry.stage = stage;
When the FlashStageObjectProcessorRegistry is created in a Flex application the stage doesn't have to be set. The library will find the stage reference by itself by looking for the appropriate SystemManager instance.
Creating an IObjectSelector is very straighforward, the interface contains only one method:
public interface IObjectSelector {
function approve(object:Object):Boolean;
}public class MyObjectSelector implements IObjectSelector {
public function approve(object:Object):Boolean {
var className:String = getQualifiedClassName(object);
return (className.substring(0,14) == "com.myclasses.");
}
}As3Commons-stageprocessing already offers these implementations for default usage:
The IStageObjectProcessor interface is equally brief:
public interface IStageObjectProcessor {
function process(displayObject:DisplayObject):DisplayObject;
}Let's imagine a processor that assigns a dataprovider to certain list components that are added to the stage:
public class MystageObjectProcessor implements IStageObjectProcessor {
public function process(displayObject:DisplayObject):DisplayObject {
if (displayObject is List) {
List(displayObject).dataProvider = ['option 1', 'option 2', 'option 3'];
}
}
}Now to register the two implementations takes one line of code:
registry.registerStageObjectProcessor(new MystageObjectProcessor(), new MyObjectSelector());
After all the necessary registrations have been performed its time to activate the FlashStageObjectProcessorRegistry:
registry.initialize();
It is allowed to register an IStageObjectProcessor without providing an IObjectSelector. In this case the defaultObjectSelector property is used to register the IStageObjectProcessor. The type of this defaultObjectSelector is determined by the defaultObjectSelectorClass property. By default the Class is set to AllowAllObjectSelector.
The registerStageObjectProcessor() method takes a third optional argument of type DisplayObject. This DisplayObject
instance will be regarded as the root for the specified IObjectSelectors and their associated IStageObjectProcessors.
In other words, only DisplayObjects that have this root as their parent will be approved and/or processed by these specific IObjectSelectors and their associated IStageObjectProcessors:
registry.registerStageObjectProcessor(new MystageObjectProcessor(), new MyObjectSelector(), registry.stage.getChildAt(4));
To be able to determine in what order IStageObjectProcessors will be executed, a processor can optionally implement the IOrdered interface:
public interface IOrdered {
function get order():int;
function set order(value:int):void;
}The IStageObjectDestroyer interface extends the IStageObjectProcessor interface and is meant to be invoked when an object is removed from the stage.
Just like its counterpart, the interface is a simple one:
public interface IStageObjectDestroyer {
function destroy(displayObject:DisplayObject):DisplayObject;
}