CeLabeledComboBox.mxml

<?xml version="1.0" encoding="utf-8"?>
<mx:Canvas xmlns:mx="http://www.adobe.com/2006/mxml" xmlns:ce="ce.controls.*" width="240" height="24"
	creationComplete="creationCompleteHandler(event)"
	disabledOverlayAlpha="0">
	<mx:Metadata>
		[Event(name="focusIn", type="flash.events.FocusEvent")]
		[Event(name="focusOut", type="flash.events.FocusEvent")]
		
		[Event(name="change", type="mx.events.ListEvent")]
		[Event(name="close", type="mx.events.DropdownEvent")]
		[Event(name="enter", type="mx.events.FlexEvent")]
		[Event(name="itemRollOut", type="mx.events.ListEvent")]
		[Event(name="itemRollOver", type="mx.events.ListEvent")]
		[Event(name="open", type="mx.events.DropdownEvent")]
	
		/**
		 *  标签颜色
		 * 
		 *  @default 0x0B333C
		 */
		[Style(name="captionColor", type="uint", format="Color", inherit="yes")]
		
		/**
		 *  星号颜色
		 * 
		 *  @default 0xFF0000
		 */
		[Style(name="starColor", type="uint", format="Color", inherit="yes")]
	</mx:Metadata>
	<!--
	带标签的下拉框
	
	@author ZengWenFeng
	-->
	<mx:Script>
		<![CDATA[
			import mx.events.FocusRequestDirection;
			import mx.collections.ArrayCollection;
			import mx.events.ScrollEvent;
			import mx.events.DropdownEvent;
			import mx.events.ListEvent;
			import mx.controls.List;
			import mx.controls.listClasses.ListBase;
			import mx.events.FlexEvent;
			
			/**
			 * 星号标签的y坐标
			 */
			public static const STAR_Y_POS:int = 5;
			
			//--------------------------------------------------------------------------
			//
			//  Variables
			//
			//--------------------------------------------------------------------------
			
			/**
			 *  @private
			 *  保存编辑器的事件侦听器对象
			 */
			private var editorEventListeners:Array = [];
			
			//--------------------------------------------------------------------------
			//
			//  New Properties
			//
			//--------------------------------------------------------------------------
			
			//----------------------------------
			//  captionWidth
			//----------------------------------
			
			[Bindable]
			[Inspectable(category="Common", defaultValue="65", type="Number")]
			
			/**
			 *  标签的长度
			 */
			public var captionWidth:Number = 65;
			
			//----------------------------------
			//  gap
			//----------------------------------
			
			[Bindable]
			
			/**
			 *  @private
			 *  间距
			 */
			public var gap:Number = 5;
			
			//----------------------------------
			//  postfix
			//----------------------------------
			
			[Bindable]
			
			/**
			 *  后缀。位于编辑器后面的标签,可用于设置单位
			 */
			public var postfix:String = "";
			
			//----------------------------------
			//  required
			//----------------------------------
			
			[Bindable]
			[Inspectable(category="Common", defaultValue="false", type="Boolean")]
			
			/**
			 *  是否必填项,用于控制是否显示星号
			 */
			public var required:Boolean = false;
			
			//----------------------------------
			//  starWidth
			//----------------------------------
			
			[Bindable]
			
			/**
			 *  星号标签的长度
			 */
			public var starWidth:Number = 10;
			
			//----------------------------------
			//  labelEnableFollow
			//----------------------------------
			
			private var _labelEnableFollow:Boolean = false;
			private var labelEnableFollowChanged:Boolean = false;
			
			[Bindable]
			[Inspectable(category="Common", defaultValue="false", type="Boolean")]
			
			/**
			 *  标签、后缀、星号标签是否继承启用属性。若为是,当禁用时所有标签也被禁用,否则标签永远为启用状态
			 */
			public function get labelEnableFollow():Boolean
			{
				return _labelEnableFollow;
			}
			
			/**
			 * 
			 */
			public function set labelEnableFollow(value:Boolean):void
			{
				_labelEnableFollow = value;
				
				if (editor != null)
				{
					if (_labelEnableFollow)
					{
						lblCaption.enabled = enabled;
						lblPostfix.enabled = enabled;
						lblStar.enabled = enabled;
					}
					else
					{
						lblCaption.enabled = true;
						lblPostfix.enabled = true;
						lblStar.enabled = true;
					}
				}
				else
				{
					labelEnableFollowChanged = true;
				}
			}
			
			//----------------------------------
			//  enterToTab
			//----------------------------------
			
			private var _enterToTab:Boolean = true;
			
			[Bindable]
			[Inspectable(category="Common", defaultValue="true", type="Boolean")]
			
			/**
			 *  按回车时是否切换到下一个控件
			 */
			public function get enterToTab():Boolean
			{
				return _enterToTab;
			}
			
			/**
			 * 
			 */
			public function set enterToTab(value:Boolean):void
			{
				_enterToTab = value;
				
				if (value)
				{
					this.addEventListener(KeyboardEvent.KEY_DOWN, enterHandler);
				}
				else
				{
					this.removeEventListener(KeyboardEvent.KEY_DOWN, enterHandler);
				}
			}
			
			//--------------------------------------------------------------------------
			//
			//  Overridden Properties
			//
			//--------------------------------------------------------------------------
			
			//----------------------------------
			//  enabled
			//----------------------------------
			
			private var _enabled:Boolean = true;
			private var enabledChanged:Boolean = false;
			
			[Inspectable(category="General", enumeration="true,false", defaultValue="true")]
			
			override public function get enabled():Boolean
			{
				return editor != null ? editor.enabled : _enabled;
			}
			
			override public function set enabled(value:Boolean):void
			{
				if (editor != null)
				{
					editor.enabled = value;
					_enabled = value;  // 因为UIComponent构造函数会设置enabled属性,所以必须同步数据
					
					if (_labelEnableFollow)
					{
						lblCaption.enabled = value;
						lblPostfix.enabled = value;
						lblStar.enabled = value;
					}
				}
				else
				{
					_enabled = value;
					enabledChanged = true;
				}
			}
			
			//----------------------------------
			//  errorString
			//----------------------------------
			
			private var _errorString:String = "";
			private var errorStringChanged:Boolean = false;
			
			[Bindable("errorStringChanged")]
			
			override public function get errorString():String
			{
				return editor != null ? editor.errorString : _errorString;
			}
			
			override public function set errorString(value:String):void
			{
				if (editor != null)
				{
					editor.errorString = value;
				}
				else
				{
					_errorString = value;
					errorStringChanged = true;
				}
			}
			
			//----------------------------------
			//  toolTip
			//----------------------------------
			
			private var _toolTip:String;
			private var toolTipChanged:Boolean = false;
			
			[Bindable("toolTipChanged")]
			[Inspectable(category="General", defaultValue="null")]
			
			override public function get toolTip():String
			{
				return editor != null ? editor.toolTip : _toolTip;
			}
			
			override public function set toolTip(value:String):void
			{
				if (editor != null)
				{
					editor.toolTip = value;
				}
				else
				{
					_toolTip = value;
					toolTipChanged = true;
				}
			}
			
			//--------------------------------------------------------------------------
			//
			//  Delegated Properties
			//
			//--------------------------------------------------------------------------
			
			//----------------------------------
			//  dataProvider
			//----------------------------------
			
			private var _dataProvider:Object = new ArrayCollection();
			private var dataProviderChanged:Boolean = false;
			
			[Bindable("collectionChange")]
			[Inspectable(category="Data")]
			
			public function get dataProvider():Object
			{
				return editor != null ? editor.dataProvider : _dataProvider;
			}
			
			public function set dataProvider(value:Object):void
			{
				if (editor != null)
				{
					editor.dataProvider = value;
				}
				else
				{
					_dataProvider = value;
					dataProviderChanged = true;
				}
			}
			
			//----------------------------------
			//  editable
			//----------------------------------
			
			private var _editable:Boolean = false;
			private var editableChanged:Boolean = false;
			
			[Bindable("editableChanged")]
			[Inspectable(category="General", defaultValue="false")]
			
			public function get editable():Boolean
			{
				return _editable;
			}
			
			public function set editable(value:Boolean):void
			{
				_editable = value;
				
			    dispatchEvent(new Event("editableChanged"));
			}
			
			//----------------------------------
			//  imeMode
			//----------------------------------
		
			private var _imeMode:String = null;
			private var imeModeChanged:Boolean = false;
			
			public function get imeMode():String
			{
				return editor != null ? editor.imeMode : _imeMode;
			}
			
			public function set imeMode(value:String):void
			{
				if (editor != null)
				{
					editor.imeMode = value;
				}
				else
				{
					_imeMode = value;
					imeModeChanged = true;
				}
			}
			
			//----------------------------------
			//  maxChars
			//----------------------------------
			
			private var _maxChars:int = 0;
			private var maxCharsChanged:Boolean = false;
			
			[Bindable("maxCharsChanged")]
			[Inspectable(category="General", defaultValue="0")]
			
			public function get maxChars():int
			{
				return editor != null ? editor.maxChars : _maxChars;
			}
			
			public function set maxChars(value:int):void
			{
				if (editor != null)
				{
					editor.maxChars = value;
				}
				else
				{
					_maxChars = value;
					maxCharsChanged = true;
				}
			}
			
			//----------------------------------
			//  readonly
			//----------------------------------
			
			/**
			 *  @private
			 */
			private var _readonly:Boolean = false;
			
			[Bindable("readonlyChanged")]
			[Inspectable(category="General", defaultValue="0")]
			
			/**
			 *  是否只读。只读时文本框不可编辑、下拉按钮不可用。
			 * 
			 *  @default 0
			 */
			public function get readonly():Boolean
			{
				return _readonly;
			}
			
			/**
			 *  @private
			 */
			public function set readonly(value:Boolean):void
			{
			    _readonly = value;
				
			    dispatchEvent(new Event("readonlyChanged"));
			}
			
			//----------------------------------
			//  restrict
			//----------------------------------
			
			private var _restrict:String;
			private var restrictChanged:Boolean = false;
			
			[Bindable("restrictChanged")]
			[Inspectable(category="Other")]
			
			public function get restrict():String
			{
				return editor != null ? editor.restrict : _restrict;
			}
			
			public function set restrict(value:String):void
			{
				if (editor != null)
				{
					editor.restrict = value;
				}
				else
				{
					_restrict = value;
					restrictChanged = true;
				}
			}
			
			//----------------------------------
			//  selectedIndex
			//----------------------------------
			
			private var _selectedIndex:int = -1;
			private var selectedIndexChanged:Boolean = false;
			
			[Bindable("change")]
			[Bindable("valueCommit")]
			[Inspectable(category="General", defaultValue="-1")]
			
			public function get selectedIndex():int
			{
				return editor != null ? editor.selectedIndex : _selectedIndex;
			}
			
			public function set selectedIndex(value:int):void
			{
				if (editor != null)
				{
					editor.selectedIndex = value;
				}
				else
				{
					_selectedIndex = value;
					selectedIndexChanged = true;
				}
			}
			
			//----------------------------------
			//  selectedItem
			//----------------------------------
			
			private var _selectedItem:Object;
			private var selectedItemChanged:Boolean = false;
			
			[Bindable("change")]
			[Bindable("valueCommit")]
			[Inspectable(environment="none")]
			
			public function get selectedItem():Object
			{
				return editor != null ? editor.selectedItem : _selectedItem;
			}
			
			public function set selectedItem(value:Object):void
			{
				if (editor != null)
				{
					editor.selectedItem = value;
				}
				else
				{
					_selectedItem = value;
					selectedItemChanged = true;
				}
			}
			
			//----------------------------------
			//  text
			//----------------------------------
			
			private var _text:String = "";
			private var textChanged:Boolean = false;
			
			[Bindable("collectionChange")]
			[Bindable("valueCommit")]
			[Inspectable(category="General", defaultValue="")]
			[NonCommittingChangeEvent("change")]
			
			public function get text():String
			{
				return editor != null ? editor.text : _text;
			}
			
			public function set text(value:String):void
			{
				if (editor != null)
				{
					editor.text = value;
				}
				else
				{
					_text = value;
					textChanged = true;
				}
			}
			
			//----------------------------------
			//  dropdown
			//----------------------------------
		
			public function get dropdown():ListBase
			{
				return editor != null ? editor.dropdown : null;
			}
			
			//----------------------------------
			//  dropdownFactory
			//----------------------------------
			
			private var _dropdownFactory:IFactory = new ClassFactory(List);
			private var dropdownFactoryChanged:Boolean = false;
			
			[Bindable("dropdownFactoryChanged")]
			
			public function get dropdownFactory():IFactory
			{
				return editor != null ? editor.dropdownFactory : _dropdownFactory;
			}
			
			public function set dropdownFactory(value:IFactory):void
			{
				if (editor != null)
				{
					editor.dropdownFactory = value;
				}
				else
				{
					_dropdownFactory = value;
					dropdownFactoryChanged = true;
				}
			}
			
			//----------------------------------
			//  dropdownWidth
			//----------------------------------
			
			private var _dropdownWidth:Number = 100;
			private var dropdownWidthChanged:Boolean = false;
			
			[Bindable("dropdownWidthChanged")]
			[Inspectable(category="Size", defaultValue="100")]
			
			public function get dropdownWidth():Number
			{
				return editor != null ? editor.dropdownWidth : _dropdownWidth;
			}
			
			public function set dropdownWidth(value:Number):void
			{
				if (editor != null)
				{
					editor.dropdownWidth = value;
				}
				else
				{
					_dropdownWidth = value;
					dropdownWidthChanged = true;
				}
			}
			
			//----------------------------------
			//  itemRenderer
			//----------------------------------
			
			private var _itemRenderer:IFactory;
			private var itemRendererChanged:Boolean = false;
			
			[Inspectable(category="Data")]
			
			public function get itemRenderer():IFactory
			{
				return editor != null ? editor.itemRenderer : _itemRenderer;
			}
			
			public function set itemRenderer(value:IFactory):void
			{
				if (editor != null)
				{
					editor.itemRenderer = value;
				}
				else
				{
					_itemRenderer = value;
					itemRendererChanged = true;
				}
			}
			
			//----------------------------------
			//  labelField
			//----------------------------------
			
			private var _labelField:String = "label";
			private var labelFieldChanged:Boolean = false;
			
			[Bindable("labelFieldChanged")]
			[Inspectable(category="Data", defaultValue="label")]
			
			public function get labelField():String
			{
				return editor != null ? editor.labelField : _labelField;
			}
			
			public function set labelField(value:String):void
			{
				if (editor != null)
				{
					editor.labelField = value;
				}
				else
				{
					_labelField = value;
					labelFieldChanged = true;
				}
			}
			
			//----------------------------------
			//  labelFunction
			//----------------------------------
			
			private var _labelFunction:Function;
			private var labelFunctionChanged:Boolean = false;
			
			[Bindable("labelFunctionChanged")]
			[Inspectable(category="Data")]
			
			public function get labelFunction():Function
			{
				return editor != null ? editor.labelFunction : _labelFunction;
			}
			
			public function set labelFunction(value:Function):void
			{
				if (editor != null)
				{
					editor.labelFunction = value;
				}
				else
				{
					_labelFunction = value;
					labelFunctionChanged = true;
				}
			}
			
			//----------------------------------
			//  prompt
			//----------------------------------
			
			private var _prompt:String;
			private var promptChanged:Boolean = false;
			
			[Inspectable(category="General")]
			
			public function get prompt():String
			{
				return editor != null ? editor.prompt : _prompt;
			}
			
			public function set prompt(value:String):void
			{
				if (editor != null)
				{
					editor.prompt = value;
				}
				else
				{
					_prompt = value;
					promptChanged = true;
				}
			}
			
			//----------------------------------
			//  rowCount
			//----------------------------------
			
			private var _rowCount:int = 5;
			private var rowCountChanged:Boolean = false;
			
			[Bindable("resize")]
			[Inspectable(category="General", defaultValue="5")]
			
			public function get rowCount():int
			{
				return editor != null ? editor.rowCount : _rowCount;
			}
			
			public function set rowCount(value:int):void
			{
				if (editor != null)
				{
					editor.rowCount = value;
				}
				else
				{
					_rowCount = value;
					rowCountChanged = true;
				}
			}
			
			//----------------------------------
			//  selectedLabel
			//----------------------------------
		
			public function get selectedLabel():String
			{
				return editor != null ? editor.selectedLabel : "";
			}
			
			//--------------------------------------------------------------------------
			//
			//  Overridden methods
			//
			//--------------------------------------------------------------------------
			
			override public function addEventListener(
											type:String, listener:Function,
											useCapture:Boolean = false,
											priority:int = 0,
											useWeakReference:Boolean = false):void
			{
				super.addEventListener(type, listener, useCapture,
									   priority, useWeakReference);
		
				// If we are ComboBox event, then add event listener to editor
				if (type == ListEvent.CHANGE ||
					type == DropdownEvent.CLOSE ||
					type == FlexEvent.ENTER ||
					type == ListEvent.ITEM_ROLL_OUT ||
					type == ListEvent.ITEM_ROLL_OVER ||
					type == DropdownEvent.OPEN ||
					type == ScrollEvent.SCROLL)
				{
					if (editor != null)
					{
						editor.addEventListener(type, listener, useCapture,
												priority, useWeakReference);
					}
					else
					{
						editorEventListeners.push({type:type, listener:listener, useCapture:useCapture, priority:priority, useWeakReference:useWeakReference});
					}
				}
			}
			
			override public function removeEventListener(
											type:String, listener:Function,
											useCapture:Boolean = false):void
			{
				super.removeEventListener(type, listener, useCapture);
		
				// If we are ComboBox event, then remove event listener from editor
				if (type == ListEvent.CHANGE ||
					type == DropdownEvent.CLOSE ||
					type == FlexEvent.ENTER ||
					type == ListEvent.ITEM_ROLL_OUT ||
					type == ListEvent.ITEM_ROLL_OVER ||
					type == DropdownEvent.OPEN ||
					type == ScrollEvent.SCROLL)
				{
					if (editor != null)
					{
						editor.removeEventListener(type, listener, useCapture);
					}
				}
			}
			
			override public function setFocus():void
			{
				editor.setFocus();
			}
			
			//--------------------------------------------------------------------------
			//
			//  Methods
			//
			//--------------------------------------------------------------------------
			
			/**
			 *  将保存的事件侦听器对象添加到编辑器
			 * 
			 *  @author ZengWenFeng
			 */
			private function addEditorEventListeners():void
			{
				while (editorEventListeners.length > 0)
				{
					var el:Object = editorEventListeners.pop();
					editor.addEventListener(el.type, el.listener, el.useCapture,
											el.priority, el.useWeakReference);
				}
			}
			
			//--------------------------------------------------------------------------
			//
			//  Event handlers
			//
			//--------------------------------------------------------------------------
			
			private function creationCompleteHandler(event:FlexEvent):void
			{
				editor.labelField = 'label';
				
				// 如果子项的属性已改变,则设置子项的属性
				if (labelEnableFollowChanged)
				{
					if (_labelEnableFollow)
					{
						lblCaption.enabled = enabled;
						lblPostfix.enabled = enabled;
						lblStar.enabled = enabled;
					}
					else
					{
						lblCaption.enabled = true;
						lblPostfix.enabled = true;
						lblStar.enabled = true;
					}
					labelEnableFollowChanged = false;
				}
				
				if (enabledChanged)
				{
					editor.enabled = _enabled;
					enabledChanged = false;
				}
				
				if (errorStringChanged)
				{
					editor.errorString = _errorString;
					errorStringChanged = false;
				}
				
				if (toolTipChanged)
				{
					editor.toolTip = _toolTip;
					toolTipChanged = false;
				}
				
				if (dataProviderChanged)
				{
					editor.dataProvider = _dataProvider;
					dataProviderChanged = false;
				}
				
				if (editableChanged)
				{
					editor.editable = _editable;
					editableChanged = false;
				}
				
				if (imeModeChanged)
				{
					editor.imeMode = _imeMode;
					imeModeChanged = false;
				}
				
				if (maxCharsChanged)
				{
					editor.maxChars = _maxChars;
					maxCharsChanged = false;
				}
				
				if (restrictChanged)
				{
					editor.restrict = _restrict;
					restrictChanged = false;
				}
				
				if (selectedIndexChanged)
				{
					editor.selectedIndex = _selectedIndex;
					selectedIndexChanged = false;
				}
				
				if (selectedItemChanged)
				{
					editor.selectedItem = _selectedItem;
					selectedItemChanged = false;
				}
				
				if (textChanged)
				{
					editor.text = _text;
					textChanged = false;
				}
				
				if (dropdownFactoryChanged)
				{
					editor.dropdownFactory = _dropdownFactory;
					dropdownFactoryChanged = false;
				}
				
				if (dropdownWidthChanged)
				{
					editor.dropdownWidth = _dropdownWidth;
					dropdownWidthChanged = false;
				}
				
				if (itemRendererChanged)
				{
					editor.itemRenderer = _itemRenderer;
					itemRendererChanged = false;
				}
				
				if (labelFieldChanged)
				{
					editor.labelField = _labelField;
					labelFieldChanged = false;
				}
				
				if (labelFunctionChanged)
				{
					editor.labelFunction = _labelFunction;
					labelFunctionChanged = false;
				}
				
				if (promptChanged)
				{
					editor.prompt = _prompt;
					promptChanged = false;
				}
				
				if (rowCountChanged)
				{
					editor.rowCount = _rowCount;
					rowCountChanged = false;
				}
				
				//
				addEditorEventListeners();
				
				// 如果回车需要切换到下一个控件但还没添加事件,则添加回车监听器
				if (enterToTab && !this.hasEventListener(KeyboardEvent.KEY_DOWN))
				{
					this.addEventListener(KeyboardEvent.KEY_DOWN, enterHandler);
				}
			}
			
			private function enterHandler(event:KeyboardEvent):void
			{
				if (event.keyCode == Keyboard.ENTER)
				{
					if (event.shiftKey)
					{
						this.focusManager.moveFocus(FocusRequestDirection.BACKWARD, editor);
					}
					else
					{
						this.focusManager.moveFocus(FocusRequestDirection.FORWARD, editor);
					}
				}
			}
		]]>
	</mx:Script>
	<mx:Label id="lblCaption" text="{label != '' ? label + ':' : ''}" verticalCenter="0" width="{captionWidth}" 
		textAlign="right" color="{getStyle('captionColor') != null ? getStyle('captionColor') : 0x0B333C}" fontWeight="normal"/>
	<ce:CeComboBox id="editor" editable="{editable}" readonly="{readonly}"
		x="{captionWidth}" width="{width - captionWidth - gap - (postfix != '' ? lblPostfix.width : 0) - starWidth}" height="{height}" labelField="name"/>
	<mx:Label id="lblPostfix" text="{postfix}" visible="{postfix != ''}" right="{starWidth}" verticalCenter="0" textAlign="center" color="#0B333C"/>
	<mx:Label id="lblStar" text="*" visible="{required}" right="0" y="{STAR_Y_POS}" height="{height - STAR_Y_POS}" width="{starWidth}" textAlign="left"
		color="{getStyle('starColor') != null ? getStyle('starColor') : 0xFF0000}"
		truncateToFit="false"/>
</mx:Canvas>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值