package CheckboxTree {
import flash.events.Event;
import flash.events.MouseEvent;
import flash.geom.Rectangle;
import mx.collections.ICollectionView;
import mx.collections.IList;
import mx.collections.IViewCursor;
import mx.controls.Alert;
import mx.controls.CheckBox;
import mx.controls.Tree;
import mx.controls.treeClasses.ITreeDataDescriptor;
import mx.controls.treeClasses.TreeItemRenderer;
import mx.controls.treeClasses.TreeListData;
import mx.events.FlexEvent;
import mx.events.ListEvent;
public class CheckTreeRenderer extends TreeItemRenderer
{
protected var myCheckBox:CheckBox;
private var myTree:CheckTree;
/**
* STATE_SCHRODINGER : 部分子项选中
* STATE_CHECKED : 全部子项选中
* STATE_UNCHECKED : 全部子项未选中
*/
static private var STATE_SCHRODINGER:int=2;
static private var STATE_CHECKED:int=1;
static private var STATE_UNCHECKED:int=0;
/**
*记录当前选中的大区的ID
*
*/
private var defaultAreaId:int = 0 ;
public function CheckTreeRenderer()
{
super();
}
/**
* 初始化完成时处理复选框和图片对象
*
*/
override protected function createChildren():void
{
myCheckBox = new CheckBox();
addChild(myCheckBox);
myCheckBox.addEventListener(MouseEvent.CLICK, checkBoxToggleHandler);
// myCheckBox.addEventListener(FlexEvent.CREATION_COMPLETE,asdas);
myTree = this.owner as CheckTree;
super.createChildren();
}
// protected function asdas(event:Event):void
// {
// checkBoxState();
//
// }
protected function onPropertyChange(e:ListEvent=null):void
{
this.updateDisplayList(unscaledWidth,unscaledHeight);
}
override public function set data(value:Object):void
{
if (value != null)
{
super.data=value;
setCheckState(myCheckBox, value, value.state);
// checkBoxState();
}
}
/**
*如果当前的PID为且为选中或部分选中状态则记录该大区ID
* @param checkBox
* @param value
* @param state
*
*/
private function setCheckState(checkBox:CheckBox, value:Object, state:int):void
{
if (state == STATE_CHECKED)
{
checkBox.selected=true;
if( (this.owner as CheckTree).isSelectTwoArea == false && value.PID == 0 )
{
defaultAreaId = value.Id;
}
}
else if (state == STATE_UNCHECKED)
checkBox.selected=false;
else if (state == STATE_SCHRODINGER)
{
checkBox.selected=false;
if((this.owner as CheckTree).isSelectTwoArea == false && value.PID == 0)
{
defaultAreaId = value.Id;
}
}
}
private function toggleParents (item:Object, tree:Tree, state:int):void
{
if (item == null)
{
return;
}
else
{
if((this.owner as CheckTree).isSelectTwoArea = false && item.PID == 0 && defaultAreaId != 0 && defaultAreaId != item.Id)
{
Alert.show("不能同时选择两个大区","提示");
return;
}
var tmpTree:IList=myTree.dataProvider as IList;
var oldValue:Number=item.state as Number;
var newValue:Number=state as Number;
item.state = state;
tmpTree.itemUpdated(item,item.state,oldValue,newValue);
toggleParents(tree.getParentItem(item), tree, getState (tree, tree.getParentItem(item)));
}
}
/**
* // TODO : 设置项目的状态和子项的状态
* @param item 项目
* @param tree 树对象
* @param state 目标状态
*
*/
private function toggleChildren(item:Object, tree:Tree, state:int):void
{
if (item == null)
return ;
else
{
var tmpTree:IList=myTree.dataProvider as IList;
var oldValue:Number=item.state as Number;
var newValue:Number=state as Number;
item.state = state;
tmpTree.itemUpdated(item,item.state,oldValue,newValue);
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):int
{
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;
}
/**
* //TODO:设置项目的父项状态和子项状态
* @param event 事件
*
*/
private function checkBoxState():void
{
if (data)
{
var myListData:TreeListData=TreeListData(this.listData);
var selectedNode:Object=myListData.item;
myTree=myListData.owner as CheckTree;
var toggle:Boolean=myCheckBox.selected;
if (toggle)
{
toggleChildren(data, myTree, STATE_CHECKED);
myTree.expandChildrenOf(data, true);
}
else
{
toggleChildren(data, myTree, STATE_UNCHECKED);
myTree.expandChildrenOf(data, false);
}
var parent:Object = myTree.getParentItem(data);
if(null != parent)
{
toggleParents (parent, myTree, getState (myTree, parent));
}
}
}
/**
* //TODO:设置项目的父项状态和子项状态
* @param event 事件
*
*/
private function checkBoxToggleHandler(event:MouseEvent):void
{
checkBoxState();
}
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 + 1;
myCheckBox.y=(height - myCheckBox.height) / 2 - 2;
super.icon.x=myCheckBox.x + myCheckBox.width + 15;
super.label.x=super.icon.x + super.icon.width + 3;
}
else
{
myCheckBox.x=super.label.x + 10;
myCheckBox.y=(height - myCheckBox.height) / 2 - 2;
super.label.x=myCheckBox.x + myCheckBox.width + 15;
}
setCheckState(myCheckBox, data, data.state);
if(data.state == STATE_SCHRODINGER)
{
myCheckBox.setStyle("chromeColor","#5b5691");
}
else
{
myCheckBox.setStyle("chromeColor","#CCCCCC");
}
}
}
}
}