0 Replies Latest reply on May 25, 2006 10:25 AM by michaelhiggins

    item renderer question

    michaelhiggins
      I have an item renderer that adds a checkbox to my tree and checks it. My problem is that when i uncheck the box and scroll down the tree there are random boxes that are unchecked. Does anyone know how to get around this? Here is the renderer, and any help would be greatly appreciated!!

      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 nodeType:String;
      // 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 = value;

      // 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
      if(TreeListData(super.listData).hasChildren)
      {
      // get the current node and it's children as XMLList
      // var currentNodeXMLList:XMLList = new XMLList(TreeListData(super.listData).node);
      var currentNodeXMLList:XMLList = new XMLList(TreeListData(super.listData).item);


      nodeType= currentNodeXMLList.attribute('type');

      // 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

      //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;
      }
      }
      }
      }