CSDN上下载的,该Tree是采用XMLList 绑定数据的
CheckTreeDemoRenderer.as
package com.render
{
import flash.events.MouseEvent;
import flash.xml.*;
import mx.collections.*;
import mx.controls.CheckBox;
import mx.controls.Image;
import mx.controls.Tree;
import mx.controls.listClasses.*;
import mx.controls.treeClasses.*;
public class CheckTreeDemoRenderer extends TreeItemRenderer
{
protected var myImage:Image;
private var imageWidth:Number = 6;
private var imageHeight:Number = 6;
private var inner:String = "com/assets/inner.png";
protected var myCheckBox:CheckBox;
static private var STATE_SCHRODINGER:String = "schrodinger";
static private var STATE_CHECKED:String = "checked";
static private var STATE_UNCHECKED:String = "unchecked";
public function CheckTreeDemoRenderer ()
{
super();
mouseEnabled = false;
}
override protected function createChildren():void
{
super.createChildren();
myCheckBox = new CheckBox();
myCheckBox.setStyle( "verticalAlign", "middle" );
myCheckBox.addEventListener( MouseEvent.CLICK, checkBoxToggleHandler );
addChild(myCheckBox);
myImage = new Image();
myImage.source = inner;
myImage.addEventListener( MouseEvent.CLICK, imageToggleHandler );
myImage.setStyle( "verticalAlign", "middle" );
addChild(myImage);
}
override protected function updateDisplayList(unscaledWidth:Number, unscaledHeight:Number):void
{
super.updateDisplayList(unscaledWidth, unscaledHeight);
if(super.data)
{
if (super.icon != null)
{
myCheckBox.x = super.icon.x;
myCheckBox.y = super.icon.y;
myCheckBox.width = super.icon.width;
myCheckBox.height = super.icon.height;
super.icon.x = myCheckBox.x + myCheckBox.width ;
super.label.x = super.icon.x + super.icon.width;
}
else
{
myCheckBox.x = super.label.x;
myCheckBox.y = super.label.y;
myCheckBox.height = super.label.height;
super.label.x = myCheckBox.x + myCheckBox.width + myCheckBox.height;
}
if (data.@state == STATE_SCHRODINGER)
{
myImage.x = myCheckBox.x + 4;
myImage.y = myCheckBox.y + 4;
myImage.width = imageWidth;
myImage.height = imageHeight;
}
else
{
myImage.x = 0;
myImage.y = 0;
myImage.width = 0;
myImage.height = 0;
}
}
}
override public function set data(value:Object):void
{
super.data = value;
setCheckState (myCheckBox, value, value.@state);
if(TreeListData(super.listData).item.@type == 'dimension')
{
setStyle("fontStyle", 'italic');
}
else
{
if (this.parent != null)
{
var _tree:Tree = Tree(this.parent.parent);
_tree.setStyle("defaultLeafIcon", null);
}
setStyle("fontStyle", 'normal');
}
}
private function toggleParents (item:Object, tree:Tree, state:String):void
{
if (item == null)
{
return;
}
else
{
item.@state = state;
toggleParents(tree.getParentItem(item), tree, getState (tree, tree.getParentItem(item)));
}
}
private function toggleChildren (item:Object, tree:Tree, state:String):void
{
if (item == null)
{
return;
}
else
{
item.@state = state;
var treeData:ITreeDataDescriptor = tree.dataDescriptor;
if (treeData.hasChildren(item))
{
var children:ICollectionView = treeData.getChildren (item);
var cursor:IViewCursor = children.createCursor();
while (!cursor.afterLast)
{
toggleChildren(cursor.current, tree, state);
cursor.moveNext();
}
}
}
}
private function getState(tree:Tree, parent:Object):String
{
// Alert.show("1");
var noChecks:int = 0;
var noCats:int = 0;
var noUnChecks:int = 0;
if (parent != null)
{
var treeData:ITreeDataDescriptor = tree.dataDescriptor;
var cursor:IViewCursor = treeData.getChildren(parent).createCursor();
while (!cursor.afterLast)
{
if (cursor.current.@state == STATE_CHECKED)
{
noChecks++;
}
else if (cursor.current.@state == STATE_UNCHECKED)
{
noUnChecks++
}
else
{
noCats++;
}
cursor.moveNext();
}
}
if ((noChecks > 0 && noUnChecks > 0) || (noCats > 0))
{
return STATE_SCHRODINGER;
}
else if (noChecks > 0)
{
return STATE_CHECKED;
}
else
{
return STATE_UNCHECKED;
}
}
private function imageToggleHandler(event:MouseEvent):void
{
myCheckBox.selected = !myCheckBox.selected;
checkBoxToggleHandler(event);
}
private function checkBoxToggleHandler(event:MouseEvent):void
{
if (data)
{
var myListData:TreeListData = TreeListData(this.listData);
var selectedNode:Object = myListData.item;
var tree:Tree = Tree(myListData.owner);
var toggle:Boolean = myCheckBox.selected;
if (toggle)
{
toggleChildren(data, tree, STATE_CHECKED);
}
else
{
toggleChildren(data, tree, STATE_UNCHECKED);
}
var parent:Object = tree.getParentItem (data);
toggleParents (parent, tree, getState (tree, parent));
tree.parent.dispatchEvent(new TreeCheckBoxEvent(TreeCheckBoxEvent.CHECKBOX_CLICK,false,false,data));
}
}
private function setCheckState (checkBox:CheckBox, value:Object, state:String):void
{
if (state == STATE_CHECKED)
{
checkBox.selected = true;
}
else if (state == STATE_UNCHECKED)
{
checkBox.selected = false;
}
else if (state == STATE_SCHRODINGER)
{
checkBox.selected = false;
}
}
}
}
TreeCheckBoxEvent.as (需要的时候采用)
package com.render
{
import flash.events.Event;
public class TreeCheckBoxEvent extends Event
{
public static const CHECKBOX_CLICK:String = "checkBoxClick";
private var _data:Object;
public function get data():Object{
return this._data;
}
public function set data(data:Object):void{
this._data = data;
}
public function TreeCheckBoxEvent(type:String, bubbles:Boolean=false, cancelable:Boolean=false,data:Object=null)
{
if(data != null) this._data = data;
super(type, bubbles, cancelable);
}
}
}
CheckTreeDemo.mxml
<?xml version="1.0" encoding="iso-8859-1"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" creationComplete="init();" >
<mx:Script>
<![CDATA[
import com.render.TreeCheckBoxEvent;
import mx.collections.*;
import mx.controls.Alert;
[Bindable]
public var folderList:XMLList =
<>
<folder state="unchecked" label="Marketing Collateral" isBranch="true" >
<folder state="unchecked" isBranch="true" label="Media, PR, and Communications" >
<folder state="unchecked" isBranch="false" label="Article Reprint Disclaimers" />
<folder state="unchecked" isBranch="false" label="Articles Reprints" />
<folder state="unchecked" isBranch="false" label="Interviews and Transcripts" />
<folder state="unchecked" isBranch="false" label="Press Kits" />
<folder state="unchecked" isBranch="false" label="Press Releases" />
<folder state="unchecked" isBranch="false" label="Quick Hits" />
<folder state="unchecked" isBranch="false" label="Rep Talking Points" />
<folder state="unchecked" isBranch="false" label="Special Updates" />
<folder state="unchecked" isBranch="false" label="White Papers" />
</folder>
<folder state="unchecked" isBranch="true" label="Forms and Applications" >
<folder state="unchecked" isBranch="false" label="Applications" />
<folder state="checked" isBranch="false" label="Forms" />
</folder>
</folder>
</>;
[Bindable]
public var folderCollection:XMLListCollection;
private function init() : void
{
this.addEventListener(TreeCheckBoxEvent.CHECKBOX_CLICK,treeCheckBoxHandler);
folderCollection = new XMLListCollection(folderList);
checkTree.dataProvider = folderCollection;
}
private function treeCheckBoxHandler(event:TreeCheckBoxEvent):void{
var tempXml:XML = event.data as XML;
Alert.show(tempXml.toXMLString(),"");
}
private function updataDataGrid():void{
Alert.show(checkTree.selectedItem.@label,"");
//在此联动Grid
}
]]>
</mx:Script>
<mx:Tree
id="checkTree"
itemRenderer="com.render.CheckTreeDemoRenderer"
change="updataDataGrid()"
labelField="@label"
width="100%" height="100%" >
</mx:Tree>
</mx:Application>