Hi all, I need a tree renderer that creates a checkbox at certain nodes in my tree.  I found a sample online that helped a lot, but not 100%.  Currently I have it working with a checkbox at every node, but I get some crazy behavior.  When the tree is rendered the checkboxes are all selected which is good, but when I uncheck one and scroll down other checkboxes are being unchecked randomly as well.  Does anyone have any suggestions on how to fix this issue?  Here is my itemRenderer.

 

myTreeItemRenderer.as

 

package components

{

    import mx.core.Container;

    import mx.core.IDataRenderer;

    import mx.controls.CheckBox;

    import mx.controls.treeClasses.*;

    import mx.collections.*;

    import flash.xml.*;

    import flash.events.Event;

    import flash.events.MouseEvent;

    import mx.controls.Alert;

    import mx.controls.listClasses.*;

 

 

    public class myTreeItemRenderer extends TreeItemRenderer

    {

        private var i:uint=0;

        // myCheckBox: holds the CheckBox we are adding to the tree nodes

        protected var myCheckBox:CheckBox;

       

        // set the margin between the image we are adding, and the label

        private var cbToLabelMargin:Number = 2;

       

       

        // show default branch icon?

        private var showDefaultBranchIcon:Boolean = false;

        // show default leaf icon?

        private var showDefaultLeafIcon:Boolean = false;

       

       

       

        public function myTreeItemRenderer()

        {

            super();

            // InteractiveObject variables.

            mouseEnabled = false;

        }

       

        public function openBranch(evt:Event):void

        {

            // get the TreeListData

            var myListData:TreeListData = TreeListData(this.listData);

           

            // get the selected node

//var selectedNode:Object = myListData.node;

            var selectedNode:Object = myListData.item;

           

            var selectedNodeXML:XMLList = new XMLList(selectedNode);

           

            // get the tree that owns us

            var theTree:Tree = Tree(myListData.owner);

           

            // find out if the selected branch is already open

            //var isBranchOpen:Boolean = theTree.getIsOpen( selectedNode );

           

            //mx.controls.Alert.show(selectedNode.toString());

            // if the selected branch is open, let's close it

            // and if it's closed, let's open it

            //var isBranchOpen:Boolean = isBranchOpen ? false : true;

            //theTree.setIsOpen( selectedNode, isBranchOpen, true, false );

            if(theTree.id=="soTree")

            {

                 this.parentApplication.soCheckBoxChanged(evt,selectedNodeXML);

                //mx.controls.Alert.show(selectedNodeXML.attribute('id')+" "+selectedNodeXML.attribute('type')+" False");

            }

            else if(theTree.id=="ntaTree")

            {

                       

            }

        }

       

        override protected function createChildren():void

        {

           // create a new CheckBox() to hold the CheckBox we'll add to the tree item

           myCheckBox = new CheckBox();

          

           myCheckBox.setStyle( "verticalAlign", "middle" );

           myCheckBox.selected=true

                                   

           // and apply it to the tree item

           addChild(myCheckBox);

          

           // add the event listener to the whole tree item

           // this will let us click anywhere on the branch item to expose the children of this branch

           myCheckBox.addEventListener( MouseEvent.CLICK, openBranch  );

           super.createChildren();

        }   

       

        override public function set data(value:Object):void

        {

            if(value==null)

            {

                        return;

            }

            else

            {

                super.data = "">

                var currentNodeXMLList:XMLList = new XMLList(TreeListData(super.listData).item);

                //var nodeType:String = currentNodeXMLList.attribute('type');                 

                // get the tree that owns us

                var _tree:Tree = Tree(this.parent.parent);

                       

                 // if the current node is a branch node

                 if(TreeListData(super.listData).hasChildren)

                 {

                      // set styles...

                     setStyle("color", 0x000000);

                     setStyle("fontWeight", 'bold');

                     // if we don't want to show the default branch icons, let's empty them

                     if( !showDefaultBranchIcon )

                     {

                          _tree.setStyle("folderClosedIcon", null);

                          _tree.setStyle("folderOpenIcon", null);

                     }

                 }

                 else

                 {

                      // if we are in here, then the current node is a leaf node

                          

                     // set styles...

                    setStyle("color", 0x000000);

                    setStyle("fontWeight", 'normal');

                    // if we don't want to show the default leaf icons, let's empty them

                    if( !showDefaultLeafIcon )

                    {

                        _tree.setStyle("defaultLeafIcon", null);

                    }

                 }

             }         

        }

       override protected function updateDisplayList(unscaledWidth:Number,unscaledHeight:Number):void

       {       

            super.updateDisplayList(unscaledWidth, unscaledHeight);

            if(super.data)

            {

                // if the current node is a branch

                graphics.clear();

                if(TreeListData(super.listData).hasChildren)

                {

                    // get the current node and it's children as XMLList

                    var currentNodeXMLList:XMLList = new XMLList(TreeListData(super.listData).item);

 

                    // get the number of children under the current node

                    var numOfImmediateChildren:int = currentNodeXMLList[0].children().length();

   

                    // set the image to be displayed in the branches

                    //myImage.source = branchImage;

                   

                    // set the label text

                    super.label.text =  TreeListData(super.listData).text + "(" + numOfImmediateChildren + ")";

                   

                }

                else

                {

                    // if we are in here, then the current node is a leaf node

                    //mx.controls.Alert.show(super.listData.toString());

                    //myImage.source = leafImage;

                }

                // reset the position of the image to be before the label

                myCheckBox.x = super.label.x;

                        // reset the position of the label to be after the image, plus give it a margin

                        super.label.x = myCheckBox.x + 10 + cbToLabelMargin;               

            }

        }

    }

}

 

 

Thanks, Michael



--
Flexcoders Mailing List
FAQ: http://groups.yahoo.com/group/flexcoders/files/flexcodersFAQ.txt
Search Archives: http://www.mail-archive.com/flexcoders%40yahoogroups.com




SPONSORED LINKS
Web site design development Computer software development Software design and development
Macromedia flex Software development best practice


YAHOO! GROUPS LINKS




Reply via email to