Adobe Flex 3 Help

Creating your own override classes

Flex lets you create a view state by specifying overrides to add or remove child components, to set style and property values, or to set state-specific event handlers. However, your application may require you to define your own overrides in addition to the ones supplied by Flex.

You can define custom overrides by creating a class that implements the IOverride interface. You can then use your override class in the same way that you use AddChild, RemoveChild, SetProperty, and the other override classes. For example, you can create a class that adds a bitmap filter, such as a filter that blurs an object.

The IOverride interface contains the following methods:

Method

Description

initialize()

Initializes the override.

apply()

Saves the original value and applies the override.

remove()

Restores the original value.

The following example shows an AddBlur override class that applies the Flash BlurFilter class to blur the target component.

package myOverrides
{
    import flash.display.*;
    import flash.filters.*;
    import mx.core.*;
    import mx.states.*;

    /* State override that adds a Blur effect to a component. */
    public class AddBlur implements IOverride
    {
        /* Constructor. */
        public function AddBlur(
            target:DisplayObject = null)
        {
            this.target = target;
        }
    
        /* The object to blur. */
        public var target:DisplayObject;
    
        /* The initialize() method is empty for this example. */
        public function initialize():void   {
        }

        /* The apply() method adds a BlurFilter to the filters array. */
        public function apply(parent:UIComponent):void {
            var obj:DisplayObject = target ? target : parent;
            var filters:Array = obj.filters;
        
            filters.push(new BlurFilter());
            obj.filters = filters;
        }
    
        /* The remove() method removes the BlurFilter 
           from the filters array. */
        public function remove(parent:UIComponent):void {
            var obj:DisplayObject = target ? target : parent;
            var filters:Array = obj.filters;
        
            filters.pop();
            obj.filters = filters;
        }
    }
}

You can then use your custom override class in an application, as the following example shows:

<?xml version="1.0"?>
<!-- states\AddBlurApp.mxml -->
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
    xmlns:myOverride="myOverrides.*">

    <mx:states>
        <mx:State name="State1">
            <myOverride:AddBlur target="{b1}"/> 
        </mx:State>       
    </mx:states>

    <mx:Button id="b1" 
        label="Click Me: Base State"/>
    
    <mx:Button  
        label="Set State 1" 
        click="currentState='State1';"/>

    <mx:Button 
        label="Set Base State" 
        click="currentState='';"/>
</mx:Application>

The executing SWF file for the previous example is shown below: