最近要用flex实现权限管理军火库装配,记下来吧,流失得太多了。
主要是角色应该拥有的资源,spring security 3.x里面需要的,角色列表,某角色拥有的资源,资源树checkbox的操作。
代码如下:
package com.gwtjs.renderer.tree
{
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 CheckTreeRenderer extends TreeItemRenderer
{
protected var myImage:Image;
private var imageWidth:Number = 6;
private var imageHeight:Number = 6;
private var inner:String ;
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 CheckTreeRenderer ()
{
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;
}
}
}
}
package com.gwtjs.renderer.tree
{
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);
}
}
}
>;
[Bindable]
public var folderCollection:XMLListCollection;
//保存数据
protected function saveNode(event:MouseEvent):void
{
saveBtn.enabled = false;
if("Update"==action){
}else{
}
//取树勾选的项
getTreeCheckboxState(folderList);
Alert.show(getResouceToString());
PopUpManager.removePopUp(this);
}
//重置表单
protected function resetNode(event:MouseEvent):void
{
if("Update"==action){
}else{
}
}
//遍历tree的数据,如果某用户拥有角色标识,应该勾选上。
public function isExists(str:String,trees:XMLList):void{
//trace(str);
//Alert.show(trees.toString()+":"+str,"Test");
for each(var t:XML in trees){
//Alert.show(t.@id+":"+str,"期待"+str+"出现");
if([email protected]()==str){
t.@state = "checked";//勾选上
}
if (t.hasComplexContent()){
arguments.callee(str,t.children());//递归找下去,不能错过一个。
}
}
}
protected function creationCompleteHandler(event:FlexEvent):void
{
Alert.show(action);
if("Update"==action){
//遍历数据打上拥有标记
var ids:Array = role.resource.split(",");
for(var i:int=0;i