flex3中有dropdownFactory,可以很好的构建,但在felx4中没有dropdownFactory了,只好自己写,下面的源码
ComboBoxWithTree.as文件:
package com.components.ds {
import flash.display.Sprite;
import flash.geom.Point;
import mx.collections.IList;
import mx.controls.Tree;
import mx.core.FlexGlobals;
import mx.managers.PopUpManager;
import spark.components.ComboBox;
import spark.events.DropDownEvent;
public class DseComboBoxWithTree extends ComboBox {
private var _tree:*;
private var __dataProvider:IList;
private var _dropDownWidth:int;
private var _dropDownHeight:int;
/*private var _selectedItems:Array;*/
/*是否允许多选*/
private var _allowMultipleSelection:Boolean;
private var _dataField:String;
/*private var _selectedItem:Object;*/
override protected function createChildren():void {
addEventListener(DropDownEvent.OPEN, dropDownControllerOpenHandler);
addEventListener(DropDownEvent.CLOSE, dropDownControllerCloseHandler);
super.createChildren();
}
override public function set dataProvider(value:IList):void {
__dataProvider = value;
}
private function dropDownControllerOpenHandler(event:DropDownEvent):void {
trace('open......................')
if (!_tree) {
if (_allowMultipleSelection) {
_tree = new DseCheckTree();
} else {
_tree = new DseTree();
DseTree(_tree).itemClickHandler = itemClickHandler;
}
_tree.dataProvider = __dataProvider;
_tree.labelField = this.labelField;
_tree.width = _dropDownWidth ? _dropDownWidth : this.width;
_tree.height = _dropDownHeight ? _dropDownHeight : 150;
}
popUpTree();
}
/**
* 定位弹出窗口
*
*/
private function popUpTree():void {
trace('position......................');
_tree.x = this.dropDownController.dropDown.x;
_tree.y = this.dropDownController.dropDown.y;
PopUpManager.addPopUp(_tree, this);
}
private function dropDownControllerCloseHandler(event:DropDownEvent):void {
trace('close......................')
if (_tree) {
PopUpManager.removePopUp(_tree);
if (_tree is DseCheckTree && _tree.dataProvider) {
if (_allowMultipleSelection) {
var items:Array = [];
var names:Array = [];
getSelectedValue(_tree.dataProvider, items);
for each (var obj:Object in items) {
names.push(obj[_tree.labelField]);
}
}
this.textInput.text = names.join(',');
} else {
this.textInput.text = _tree.selectedItem ? _tree.selectedItem[_tree.labelField] : '';
}
}
}
private function itemClickHandler(event:ListEvent):void {
this.dropDownController.closeDropDown(false)
}
/**
* 获取树中选择的对象
* @param array
* @param items
*
*/
private function getSelectedValue(array:*, items:*):void {
if (array) {
for each (var obj:Object in array) {
if (obj[_tree.checkBoxStateField] == '1') {
items.push(obj);
}
getSelectedValue(obj.children, items);
}
}
}
public function set dropDownWidth(value:int):void {
_dropDownWidth = value;
}
public function set dropDownHeight(value:int):void {
_dropDownHeight = value;
}
/** 单选时获取当前选择的对象 */
override public function get selectedItem():* {
if (_tree && _tree is DseTree) {
return DseTree(_tree).selectedItem;
}
return super.selectedItem;
}
/** 多选时获取当前选择的对象 */
override public function get selectedItems():Vector.<Object> {
if (_tree && _tree is DseCheckTree) {
var items:Vector.<Object> = new Vector.<Object>();
getSelectedValue(DseCheckTree(_tree).dataProvider, items);
return items;
}
return super.selectedItems;
}
/** 单选时获取当前选择的值 */
public function getValue(name:String = null):String {
return selectedItem ? selectedItem[name ? name : _dataField] : null;
}
/** 多选时获取当前选择的值 */
public function getValues(name:String = null, separator:String = ','):String {
var items:Vector.<Object> = selectedItems;
var v:Array = []
if (items) {
for each (var o:Object in items) {
v.push(o[name ? name : _dataField]);
}
}
return v.join(separator);
}
/** 单选时设置默认值 */
public function setValue(v:String):void {
if (_tree && _tree is DseTree && DseTree(_tree).dataProvider) {
for each (var o:Object in _tree.dataProvider) {
if (o[dataField] == v) {
this.textInput.text = o[labelField];
DseTree(_tree).selectedItem = o;
}
}
}
}
/** 多选时设置默认值 */
public function setValues(v:String, separator:String = ','):void {
var selectedArray:ArrayCollection = new ArrayCollection(v.split(','));
var names:Array = [];
if (_tree && _tree is DseCheckTree && DseCheckTree(_tree).dataProvider) {
setSelectedValue(_tree.dataProvider, names, selectedArray);
}
this.textInput.text = names.join(separator);
}
private function setSelectedValue(array:*, items:Array, selectedArray:ArrayCollection):void {
if (array) {
for each (var obj:Object in array) {
if (obj[dataField] && selectedArray.contains(obj[dataField])) {
obj[_tree.checkBoxStateField] = '1'
items.push(obj[labelField]);
}
setSelectedValue(obj.children, items, selectedArray);
}
}
}
override public function get allowMultipleSelection():Boolean {
return _allowMultipleSelection;
}
override public function set allowMultipleSelection(value:Boolean):void {
_allowMultipleSelection = value;
}
public function get dataField():String {
return _dataField;
}
public function set dataField(value:String):void {
_dataField = value;
}
}
}
import com.dse.common.tree.CheckLineTree;
import com.dse.components.ds.DseComboBoxWithTree;
import flash.events.MouseEvent;
import mx.collections.IList;
import mx.containers.Canvas;
import mx.controls.Tree;
import mx.events.FlexEvent;
import mx.events.FlexMouseEvent;
import mx.events.ListEvent;
import spark.events.DropDownEvent;
class DseTree extends Canvas {
private var _tree:Tree;
private var _dataProvider:IList;
private var _labelField:String;
private var _selectedItem:Object;
private var _itemClickHandler:Function;
function DseTree():void {
_tree = new Tree();
this.addChild(_tree);
_tree.percentHeight = 100;
_tree.percentWidth = 100;
addEventListener(MouseEvent.CLICK, function(e:MouseEvent):void {
e.stopImmediatePropagation();
});
addEventListener(MouseEvent.MOUSE_DOWN, function(e:MouseEvent):void {
e.stopImmediatePropagation();
});
_tree.addEventListener(ListEvent.ITEM_CLICK, function(event:ListEvent):void {
_itemClickHandler(event);
});
}
public function get dataProvider():IList {
return IList(_tree.dataProvider);
}
public function set dataProvider(value:IList):void {
_tree.dataProvider = value;
}
public function get labelField():String {
return _tree.labelField;
}
public function set labelField(value:String):void {
_tree.labelField = value;
}
public function get selectedItem():Object {
return _tree.selectedItem;
}
public function set selectedItem(value:Object):void {
_tree.selectedItem = value;
}
public function set itemClickHandler(value:Function):void {
_itemClickHandler = value;
}
}
class DseCheckTree extends Canvas {
private var _tree:CheckLineTree;
private var _dataProvider:IList;
private var _checkBoxStateField:String;
private var _labelField:String;
function DseCheckTree():void {
_tree = new CheckLineTree();
this.addChild(_tree);
_tree.percentHeight = 100;
_tree.percentWidth = 100;
addEventListener(MouseEvent.CLICK, function(e:MouseEvent):void {
e.stopImmediatePropagation();
});
addEventListener(MouseEvent.MOUSE_DOWN, function(e:MouseEvent):void {
e.stopImmediatePropagation();
});
}
public function set dataProvider(value:IList):void {
_tree.dataProvider = value;
}
public function get dataProvider():IList {
return IList(_tree.dataProvider);
}
public function get checkBoxStateField():String {
return _tree.checkBoxStateField;
}
public function set checkBoxStateField(value:String):void {
_tree.checkBoxStateField = value;
}
public function get labelField():String {
return _tree.labelField;
}
public function set labelField(value:String):void {
_tree.labelField = value;
}
}
CheckLineTree.as文件:
package com.dse.common.tree{
import mx.core.ClassFactory;
import mx.events.ListEvent;
/**
* 三状态复选框树控件
*
*/
public class CheckLineTree extends Tree {
//数据源中状态字段
private var _checkBoxStateField:String = "@checked";
//部分选中的填充色
[Bindable]
private var m_checkBoxBgColor:uint = 0x009900;
//填充色的透明度
[Bindable]
private var m_checkBoxBgAlpha:Number = 1;
//填充色的边距
[Bindable]
private var m_checkBoxBgPadding:Number = 3;
//填充色的四角弧度
[Bindable]
private var m_checkBoxBgElips:Number = 2;
//取消选择是否收回子项
[Bindable]
private var m_checkBoxCloseItemsOnUnCheck:Boolean = true;
//选择项时是否展开子项
[Bindable]
private var m_checkBoxOpenItemsOnCheck:Boolean = false;
//选择框左边距的偏移量
[Bindable]
private var m_checkBoxLeftGap:int = 8;
//选择框右边距的偏移量
[Bindable]
private var m_checkBoxRightGap:int = 10;
//是否显示三状态
[Bindable]
private var m_checkBoxEnableState:Boolean = true;
//子项选择一部份时,是否自动选择父项 m_checkBoxCascadeOnChild=true时,m_checkBoxEnableState失效
[Bindable]
private var m_checkBoxCascadeOnChild:Boolean = false;
//与父项子项关联
[Bindable]
private var m_checkBoxCascadeOnCheck:Boolean = true;
//点击Item时用来判断点击的是否是复选框。
public var ischeckBoxChecked:Boolean = false;
//双击项目
public var itemDClickSelect:Boolean = true;
public function CheckLineTree() {
super();
doubleClickEnabled = true;
}
override protected function createChildren():void {
var myFactory:ClassFactory = new ClassFactory(CheckLineTreeRenderer);
this.itemRenderer = myFactory;
super.createChildren();
addEventListener(ListEvent.ITEM_DOUBLE_CLICK,onItemDClick);
}
public function PropertyChange():void {
dispatchEvent(new ListEvent(mx.events.ListEvent.CHANGE));
}
/**
* 树菜单,双击事件
* @param evt 双击事件源
*
*/
public function onItemDClick(e:ListEvent):void {
if (itemDClickSelect)
OpenItems();
}
/**
* 打开Tree节点函数,被 有打开节点功能的函数调用
* @param item 要打开的节点
*
*/
public function OpenItems():void {
if (this.selectedIndex >= 0 && this.dataDescriptor.isBranch(this.selectedItem))
this.expandItem(this.selectedItem,!this.isItemOpen(this.selectedItem)
,true);
}
/**
* 数据源中状态字段
* @return
*/
[Bindable]
public function get checkBoxStateField():String {
return _checkBoxStateField;
}
public function set checkBoxStateField(v:String):void {
_checkBoxStateField = v;
//PropertyChange();
}
/**
* 部分选中的填充色
* @return
*/
[Bindable]
public function get checkBoxBgColor():uint {
return m_checkBoxBgColor;
}
public function set checkBoxBgColor(v:uint):void {
m_checkBoxBgColor = v;
//PropertyChange();
}
/**
* 填充色的透明度
* @return
*
*/
[Bindable]
public function get checkBoxBgAlpha():Number {
return m_checkBoxBgAlpha;
}
public function set checkBoxBgAlpha(v:Number):void {
m_checkBoxBgAlpha = v;
//PropertyChange();
}
/**
* 填充色的边距
* @return
*/
[Bindable]
public function get checkBoxBgPadding():Number {
return m_checkBoxBgPadding;
}
public function set checkBoxBgPadding(v:Number):void {
m_checkBoxBgPadding = v;
//PropertyChange();
}
/**
* 填充色的四角弧度
* @return
*/
[Bindable]
public function get checkBoxBgElips():Number {
return m_checkBoxBgElips;
}
public function set checkBoxBgElips(v:Number):void {
m_checkBoxBgElips = v;
//PropertyChange();
}
/**
* 取消选择是否收回子项
* @return
*/
[Bindable]
public function get checkBoxCloseItemsOnUnCheck():Boolean {
return m_checkBoxCloseItemsOnUnCheck;
}
public function set checkBoxCloseItemsOnUnCheck(v:Boolean):void {
m_checkBoxCloseItemsOnUnCheck = v;
//PropertyChange();
}
/**
* 选择项时是否展开子项
* @return
*/
[Bindable]
public function get checkBoxOpenItemsOnCheck():Boolean {
return m_checkBoxOpenItemsOnCheck;
}
public function set checkBoxOpenItemsOnCheck(v:Boolean):void {
m_checkBoxOpenItemsOnCheck = v;
//PropertyChange();
}
/**
* 选择框左边距的偏移量
* @return
*/
[Bindable]
public function get checkBoxLeftGap():int {
return m_checkBoxLeftGap;
}
public function set checkBoxLeftGap(v:int):void {
m_checkBoxLeftGap = v;
//PropertyChange();
}
/**
* 选择框右边距的偏移量
* @return
*/
[Bindable]
public function get checkBoxRightGap():int {
return m_checkBoxRightGap;
}
public function set checkBoxRightGap(v:int):void {
m_checkBoxRightGap = v;
//PropertyChange();
}
/**
* 是否显示三状态
* @return
*/
[Bindable]
public function get checkBoxEnableState():Boolean {
return m_checkBoxEnableState;
}
public function set checkBoxEnableState(v:Boolean):void {
m_checkBoxEnableState = v;
//PropertyChange();
}
/**
*
* @return
*/
[Bindable]
public function get checkBoxCascadeOnChild():Boolean {
return m_checkBoxCascadeOnChild;
}
public function set checkBoxCascadeOnChild(v:Boolean):void {
m_checkBoxCascadeOnChild = v;
//PropertyChange();
}
/**
* 与父项子项关联
* @return
*/
[Bindable]
public function get checkBoxCascadeOnCheck():Boolean {
return m_checkBoxCascadeOnCheck;
}
public function set checkBoxCascadeOnCheck(v:Boolean):void {
m_checkBoxCascadeOnCheck = v;
//PropertyChange();
}
}
}
CheckLineTreeRenderer.as文件:
package com.dse.common.tree {
import flash.display.BitmapData;
import flash.display.Graphics;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.geom.Rectangle;
import mx.collections.*;
import mx.controls.CheckBox;
import mx.controls.Tree;
import mx.controls.listClasses.*;
import mx.controls.treeClasses.*;
import mx.events.FlexEvent;
import mx.events.ListEvent;
/**
* Alpha value for the Tree lines.
* @default 1
*/
[Style(name = "lineAlpha", type = "Number", format = "Length", inherit = "no")]
/**
* Color of the Tree lines.
* @default 0x808080
*/
[Style(name = "treeLineColor", type = "uint", format = "Color", inherit = "no")]
/**
* Thickness value for the Tree lines.
* @default 1
*/
[Style(name = "lineThickness", type = "Number", format = "Length", inherit = "no")]
/**
* The Tree line style - none, dotted (default), or solid.
* @default "dotted"
*/
[Style(name = "lineStyle", type = "String", enumeration = "dotted,solid,none", inherit = "no")]
/**
* 三状态复选框树控件
*/
public class CheckLineTreeRenderer extends TreeItemRenderer {
/**
* 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;
public static const DOTTED:String = "dotted"; // default
public static const SOLID:String = "solid";
public static const NONE:String = "none";
protected var myCheckBox:CheckBox;
private var myTree:CheckLineTree;
public function CheckLineTreeRenderer() {
super();
mouseEnabled = true;
}
/**
* 初始化完成时处理复选框和图片对象
*
*/
override protected function createChildren():void {
myCheckBox = new CheckBox();
addChild(myCheckBox);
myCheckBox.addEventListener(MouseEvent.CLICK, checkBoxToggleHandler);
myTree = this.owner as CheckLineTree;
super.createChildren();
myTree.addEventListener(ListEvent.CHANGE, onPropertyChange);
}
protected function onPropertyChange(e:ListEvent = null):void {
this.updateDisplayList(unscaledWidth, unscaledHeight);
}
/**
* 递归设置父项目的状态
* @param item 项目
* @param tree 树对象
* @param state 目标状态
*
*/
private function toggleParents(item:Object, tree:Tree, state:int):void {
if (item == null)
return;
else {
var stateField:String = myTree.checkBoxStateField;
var tmpTree:IList = myTree.dataProvider as IList;
var oldValue:Number = item[stateField] as Number;
var newValue:Number = state as Number;
item[myTree.checkBoxStateField] = state;
tmpTree.itemUpdated(item, stateField, oldValue, newValue);
var parentItem:Object = tree.getParentItem(item);
if (null != parentItem)
toggleParents(parentItem, tree, getState(tree, parentItem));
}
}
/**
* // TODO : 设置项目的状态和子项的状态
* @param item 项目
* @param tree 树对象
* @param state 目标状态
*
*/
private function toggleChildren(item:Object, tree:Tree, state:int):void {
if (item == null)
return;
else {
var stateField:String = myTree.checkBoxStateField;
var tmpTree:IList = myTree.dataProvider as IList;
var oldValue:Number = item[stateField] as Number;
var newValue:Number = state as Number;
item[myTree.checkBoxStateField] = state;
tmpTree.itemUpdated(item, stateField, oldValue, newValue);
var treeData:ITreeDataDescriptor = tree.dataDescriptor;
if (myTree.checkBoxCascadeOnCheck && treeData.hasChildren(item)) {
var children:ICollectionView = treeData.getChildren(item);
var cursor:IViewCursor = children.createCursor();
while (!cursor.afterLast) {
toggleChildren(cursor.current, tree, state);
cursor.moveNext();
}
}
}
}
/**
* 获得parent的状态
* @param tree 树对象
* @param parent 目标项
* @return 状态
*/
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[myTree.checkBoxStateField] == STATE_CHECKED)
noChecks++;
else if (cursor.current[myTree.checkBoxStateField] == STATE_UNCHECKED)
noUnChecks++;
else
noCats++;
cursor.moveNext();
}
}
if (myTree.checkBoxCascadeOnChild) {
return STATE_CHECKED;
} else if ((noChecks > 0 && noUnChecks > 0) || noCats > 0)
return STATE_SCHRODINGER;
else if (noChecks > 0)
return STATE_CHECKED;
else
return STATE_UNCHECKED;
}
/**
* 设置项目的父项状态和子项状态
* @param event 事件
* 点击的是ITME的复选框。设置为true
*/
private function checkBoxToggleHandler(event:MouseEvent):void {
myTree.ischeckBoxChecked = true;
if (data) {
var myListData:TreeListData = TreeListData(this.listData);
var selectedNode:Object = myListData.item;
myTree = myListData.owner as CheckLineTree;
var toggle:Boolean = myCheckBox.selected;
if (toggle) {
toggleChildren(data, myTree, STATE_CHECKED);
} else {
toggleChildren(data, myTree, STATE_UNCHECKED);
}
//如果所有子项选中时需要选中父项则执行以下代码
if (myTree.checkBoxCascadeOnCheck) {
var parent:Object = myTree.getParentItem(data);
if (null != parent)
toggleParents(parent, myTree, getState(myTree, parent));
}
}
myTree.dispatchEvent(new FlexEvent(FlexEvent.DATA_CHANGE));
}
/**
* 设置本项的复选框状态
* @param checkBox 复选框
* @param value
* @param state 状态
*/
private function setCheckState(checkBox:CheckBox, value:Object, state:int):void {
if (state == STATE_CHECKED)
checkBox.selected = true;
else if (state == STATE_UNCHECKED)
checkBox.selected = false;
else if (state == STATE_SCHRODINGER)
checkBox.selected = false;
}
override public function set data(value:Object):void {
if (value != null) {
super.data = value;
setCheckState(myCheckBox, value, value[myTree.checkBoxStateField]);
}
}
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 + myTree.checkBoxLeftGap;
myCheckBox.y = (height - myCheckBox.height) / 2;
super.icon.x = myCheckBox.x + myCheckBox.width + myTree.checkBoxRightGap;
super.label.x = super.icon.x + super.icon.width + 3;
} else {
myCheckBox.x = super.label.x + myTree.checkBoxLeftGap;
myCheckBox.y = (height - myCheckBox.height) / 2;
super.label.x = myCheckBox.x + myCheckBox.width + myTree.checkBoxRightGap;
}
setCheckState(myCheckBox, data, data[myTree.checkBoxStateField]);
if (myTree.checkBoxEnableState && data[myTree.checkBoxStateField] == STATE_SCHRODINGER) {
fillCheckBox(true);
} else
fillCheckBox(false);
}
if ((unscaledWidth > 0) && (unscaledHeight > 0)) {
var tree:Tree = (owner as Tree);
var desc:ITreeDataDescriptor = tree.dataDescriptor;
var currentNode:Object = data;
var parentNode:Object = tree.getParentItem(currentNode);
var levelsUp:int = 0;
var lineStyle:String = getStyle("lineStyle");
var lineColor:uint = getColorStyle("treeLineColor", 0x808080);
var lineAlpha:Number = getNumberStyle("lineAlpha", 1);
var lineThickness:Number = getNumberStyle("lineThickness", 1);
var indentation:Number = tree.getStyle("indentation");
var shift:int = (parentNode == null ? 2 : 6) + lineThickness;
if (icon) {
icon.move(icon.x + shift, icon.y);
}
if (label) {
label.move(label.x + shift, label.y);
}
var g:Graphics = graphics;
g.clear();
if ((lineStyle != NONE) && (lineAlpha > 0) && (lineThickness > 0)) {
while (parentNode != null) {
var children:ICollectionView = desc.getChildren(parentNode);
if (children is IList) {
var itemIndex:int = (children as IList).getItemIndex(currentNode);
var isLast:Boolean = (itemIndex == (children.length - 1));
drawLines(g, unscaledWidth, unscaledHeight, lineStyle, lineColor, lineAlpha, lineThickness, isLast, levelsUp, indentation);
levelsUp++;
currentNode = parentNode;
parentNode = tree.getParentItem(parentNode);
} else {
break;
}
}
}
}
}
protected function fillCheckBox(isFill:Boolean):void {
myCheckBox.graphics.clear();
if (isFill) {
var myRect:Rectangle = getCheckTreeBgRect(myTree.checkBoxBgPadding);
myCheckBox.graphics.beginFill(myTree.checkBoxBgColor, myTree.checkBoxBgAlpha)
myCheckBox.graphics.drawRoundRect(myRect.x, myRect.y, myRect.width, myRect.height, myTree.checkBoxBgElips, myTree.checkBoxBgElips);
myCheckBox.graphics.endFill();
}
}
protected function getCheckTreeBgRect(checkTreeBgPadding:Number):Rectangle {
var myRect:Rectangle = myCheckBox.getBounds(myCheckBox);
myRect.top += checkTreeBgPadding;
myRect.left += checkTreeBgPadding;
myRect.bottom -= checkTreeBgPadding;
myRect.right -= checkTreeBgPadding;
return myRect;
}
protected function drawLines(g:Graphics, w:Number, h:Number, lineStyle:String, lineColor:uint, lineAlpha:Number, lineThickness:Number, isLastItem:Boolean,
levelsUp:int, indentation:Number):void {
var midY:Number = Math.round(h / 2);
var lineX:Number = 0;
if (disclosureIcon) {
lineX = disclosureIcon.x + (disclosureIcon.width / 2);
} else if (icon) {
lineX = icon.x - 8;
} else if (label) {
lineX = label.x - 8;
}
lineX = Math.floor(lineX) - int(lineThickness / 2);
if (levelsUp > 0) {
if (!isNaN(indentation) && (indentation > 0)) {
lineX = lineX - (levelsUp * indentation);
} else {
return;
}
}
var lineY:Number = h;
if (isLastItem) {
lineY = midY;
if (levelsUp > 0) {
return;
}
}
g.lineStyle(0, 0, 0);
if (lineStyle == SOLID) {
g.beginFill(lineColor, lineAlpha);
} else {
var verticalDottedLine:BitmapData = createDottedLine(lineColor, lineAlpha, lineThickness, true);
g.beginBitmapFill(verticalDottedLine);
}
g.drawRect(lineX, 0, lineThickness, lineY);
g.endFill();
if (levelsUp == 0) {
var startX:int = lineX + 1 + int(lineThickness / 2);
var endX:int = startX + 11;
if (isLastItem) {
startX = lineX;
}
var startY:Number = midY - int(lineThickness / 2);
if (lineStyle == SOLID) {
g.beginFill(lineColor, lineAlpha);
} else {
var horizontalDottedLine:BitmapData = createDottedLine(lineColor, lineAlpha, lineThickness, false);
g.beginBitmapFill(horizontalDottedLine);
}
g.drawRect(startX, startY, endX - startX, lineThickness);
g.endFill();
}
}
private function createDottedLine(lineColor:uint, lineAlpha:Number, lineThickness:Number, vertical:Boolean = true):BitmapData {
var w:Number = (vertical ? lineThickness : 2 * lineThickness);
var h:Number = (vertical ? 2 * lineThickness : lineThickness);
var color32:uint = combineColorAndAlpha(lineColor, lineAlpha);
var dottedLine:BitmapData = new BitmapData(w, h, true, 0x00ffffff);
for (var i:int = 0; i < lineThickness; i++) {
for (var j:int = 0; j < lineThickness; j++) {
dottedLine.setPixel32(i, j, color32);
}
}
return dottedLine;
}
private function combineColorAndAlpha(color:uint, alpha:Number):uint {
if (isNaN(alpha)) {
alpha = 1;
} else {
alpha = Math.max(0, Math.min(1, alpha));
}
var alphaColor:Number = alpha * 255;
alphaColor = alphaColor << 24;
var combined:uint = alphaColor | color;
return combined;
}
private function getColorStyle(propName:String, defaultValue:uint):uint {
var color:uint = defaultValue;
if (propName != null) {
var n:Number = getStyle(propName);
if (!isNaN(n)) {
color = uint(n);
}
}
return color;
}
private function getNumberStyle(propName:String, defaultValue:Number):Number {
var number:Number = defaultValue;
if (propName != null) {
var n:Number = getStyle(propName);
if (!isNaN(n)) {
number = n;
}
}
return number;
}
}
}
测试:
<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600" backgroundColor="0xfff000"
xmlns:tree="com.dse.common.tree.*" xmlns:ds="com.dse.components.ds.*">
<s:layout>
<s:VerticalLayout/>
</s:layout>
<fx:Script>
<![CDATA[
import mx.controls.Alert;
protected function button1_clickHandler(event:MouseEvent):void {
var items:Vector.<Object> = combox2.selectedItems;
var ids:Array = [];
var names:Array = [];
for each (var o:Object in items) {
names.push(o.name);
ids.push(o.adcode);
}
Alert.show(names.join(',') + ' ' + ids.join(','));
}
]]>
</fx:Script>
<fx:Script>
<![CDATA[
import com.dse.common.util.ServiceProxy;
import mx.collections.ArrayCollection;
import mx.rpc.events.ResultEvent;
override protected function initializationComplete():void {
super.initializationComplete();
ServiceProxy.execute("sysDistrictService", "getAll", function(e:ResultEvent):void {
var data:ArrayCollection = ArrayCollection(e.result)
combox1.dataProvider = data;
combox2.dataProvider = data;
});
}
]]>
</fx:Script>
<tree:DistrictTreeComboBox/>
<ds:DseComboBoxWithTree id="combox1" labelField="name"/>
<s:Button label="value" click="{Alert.show(combox1.selectedItem?combox1.selectedItem.name+' '+combox1.selectedItem.adcode:'');}"/>
<s:HGroup width="100%">
<s:Label text="22233333333333"/>
<ds:DseComboBoxWithTree id="combox2" labelField="name" allowMultipleSelection="true"/>
<s:Button label="value" click="button1_clickHandler(event)"/>
</s:HGroup>
</s:Application>