Friday 20 May 2011

TreeItem Renderer With Link Buttons

package
{
    import flash.display.DisplayObject;
    import flash.events.MouseEvent;
   
    import mx.collections.*;
    import mx.controls.Image;
    import mx.controls.LinkButton;
    import mx.controls.treeClasses.*;

    public class MyTreeItemRenderer extends TreeItemRenderer
    {
        [Bindable]
        [Embed(source="assets/icons/delete.jpg")]
        private var exceptionIcon:Class;
       
        private var exceptionImage:Image = new Image();
        public var addList1:LinkButton;
        public var addList2:LinkButton;
       
        public function LedgerTreeItemRenderer()
        {
            super();
        }
       
        override public function set data(value:Object):void
        {
            super.data = value;
        }
       
        override protected function createChildren():void
        {
            super.createChildren();
           
            this.addEventListener(MouseEvent.ROLL_OVER, onRollOver);
            this.addEventListener(MouseEvent.ROLL_OUT, onRollOut);
           
            this.addList1= new LinkButton();
            this.addList1.label = "Add To List 1";
            this.addList1.visible = false;
            this.addList1.addEventListener(MouseEvent.CLICK, onListClick1);
           
            this.addList2= new LinkButton();
            this.addList2.label = "Add To List 2";
            this.addList2.visible = false;
            this.addList2.addEventListener(MouseEvent.CLICK, onListClick2);
        }
       
        override protected function updateDisplayList(unscaledWidth:Number, unscaledHeight:Number):void
        {
            super.updateDisplayList(unscaledWidth, unscaledHeight);
           
            if(super.data)
            {
                if(!(TreeListData(super.listData).hasChildren))
                {
                    this.addChild(this.addList1);
                    this.addChild(this.addList2);
                   
                    this.addList1.x = label.x + label.textWidth + 10;
                    this.addList1.width = 142;
                    this.addList1.height = 20;
                       
                    this.addList2.x = addList1.x + addList1.width;
                    this.addList2.width = 142;
                    this.addList2.height = 20;
                       
                    //if(super.data.@value == 0)
                    {
                        this.addChild(this.exceptionImage as DisplayObject);
                           
                        this.exceptionImage.x = super.label.x + super.label.textWidth + 5;
                        this.exceptionImage.y = 0;
                        this.exceptionImage.width = super.label.textHeight;
                        this.exceptionImage.height = super.label.textHeight;
                        //this.exceptionImage.source = exceptionIcon;
                    }
                }
            }
        }
       
        private function onRollOver(evt:MouseEvent):void
        {
            this.addList1.visible = true;
            this.addList2.visible = true;
        }
       
        private function onRollOut(evt:MouseEvent):void
        {
            this.addList1.visible = false;
            this.addList2.visible = false;
        }
       
        private function onListClick1(evt:MouseEvent):void
        {
            var List1Obj:List1LinkButtonEvent= new List1LinkButtonEvent(List1LinkButtonEvent.LIST2_EVENT, true, false);
            dispatchEvent(List1Obj);
        }
       
        private function onListClick2(evt:MouseEvent):void
        {
            var List2Obj:List2LinkButtonEvent = new List2LinkButtonEvent (List2LinkButtonEvent .LIST1_EVENT, true, false);
            dispatchEvent(List2Obj);
        }
    }
}


And add an event listener in the creation complete of the main mxml file...

private function onCreationComplete(evt:FlexEvent):void
            {
                this.addEventListener(List1LinkButtonEvent.LIST1_EVENT, addToList1);
                this.addEventListener(List2LinkButtonEvent.LIST2_EVENT, addToList2);
            }

No comments:

Post a Comment