Posts tagged ‘code’

Item renderer in ActionScript

package myComponents {

    // myComponents/CellField.as
    import mx.controls.*;
    import mx.core.*;
    import mx.controls.dataGridClasses.DataGridListData;

    public class CellField extends TextInput

    {
        // Define the constructor and set properties.
        public function CellField() {
            super();
            height=60;
            width=80;
            setStyle("borderStyle", "none");
            editable=false;
        }

        // Override the set method for the data property.
        override public function set data(value:Object):void {

            super.data = value;

            if (value != null)

            {
                text = value[DataGridListData(listData).dataField];
                if(Number(text) > 100)

                {
                    setStyle("backgroundColor", 0xFF0000);
                }
            }

            else
            {
                // If value is null, clear text.
                text= "";
            }

            super.invalidateDisplayList();
        }

    }
}

Simple item editor

<?xml version="1.0"?>
<!-- itemRenderers\component\myComponents\RendererDGImageSelect.mxml -->

<mx:HBox xmlns:mx="http://www.adobe.com/2006/mxml"
    horizontalAlign="center">

    <mx:Script>
        <![CDATA[

            import mx.events.FlexEvent;

            [(source="saleIcon.jpg")]

            []
            public var sale:Class;

            [(source="noSaleIcon.jpg")]

            []
            public var noSale:Class;            

            override public function set data(value:Object):void {
                if(value != null)  {

                    super.data = value;
                    if (value.SalePrice == true) onSale.source=new sale();
                    else onSale.source=new noSale();
                }
                // Dispatch the dataChange event.

                dispatchEvent(new FlexEvent(FlexEvent.DATA_CHANGE));
            }
        ]]>
    </mx:Script>

    <mx:Image id="onSale" height="20"/>
</mx:HBox>

Creating a subclass from the Event class

package myEvents
{
    import flash.events.Event;

    public class EnableChangeEvent extends Event

    {
        public function EnableChangeEvent(type:String, isEnabled:Boolean=false)

        {
            //Call the constructor of the class
            super(type);

            //Set the new property
            this.isEnabled = isEnabled;
        }

        //Define static constant.
        public static const ENABLE_CHANGED:String = "enableChanged";

        //Define a public variable to hold the state of the enable property.

        public var isEnabled:Boolean;

        //Override the inherited cloe() method.
        override public function clone():Event

        {
            return new EnableChangeEvent(type, isEnabled);
        }

    }
}