//////////////////////////////////////////////////////////////////////////////// // // ADOBE SYSTEMS INCORPORATED // Copyright 2008 Adobe Systems Incorporated // All Rights Reserved. // // NOTICE: Adobe permits you to use, modify, and distribute this file // in accordance with the terms of the license agreement accompanying it. // //////////////////////////////////////////////////////////////////////////////// package spark.components { import adobe.utils.CustomActions; import flash.display.DisplayObject; import flash.display.InteractiveObject; import flash.events.Event; import flash.events.FocusEvent; import flash.events.KeyboardEvent; import flash.events.MouseEvent; import flash.ui.Keyboard; import flashx.textLayout.operations.CutOperation; import flashx.textLayout.operations.DeleteTextOperation; import flashx.textLayout.operations.FlowOperation; import flashx.textLayout.operations.InsertTextOperation; import mx.core.IIMESupport; import mx.core.mx_internal; import mx.events.FlexEvent; import mx.styles.StyleProxy; import spark.components.supportClasses.DropDownListBase; import spark.components.supportClasses.ListBase; import spark.core.NavigationUnit; import spark.events.DropDownEvent; import spark.events.TextOperationEvent; import spark.utils.LabelUtil; use namespace mx_internal; /** * Bottom inset, in pixels, for the text in the * prompt area of the control. * * @default 3 * * @langversion 3.0 * @playerversion Flash 10 * @playerversion AIR 1.5 * @productversion Flex 4 */ [Style(name="paddingBottom", type="Number", format="Length", inherit="no")] /** * Left inset, in pixels, for the text in the * prompt area of the control. * * @default 3 * * @langversion 3.0 * @playerversion Flash 10 * @playerversion AIR 1.5 * @productversion Flex 4 */ [Style(name="paddingLeft", type="Number", format="Length", inherit="no")] /** * Right inset, in pixels, for the text in the * prompt area of the control. * * @default 3 * * @langversion 3.0 * @playerversion Flash 10 * @playerversion AIR 1.5 * @productversion Flex 4 */ [Style(name="paddingRight", type="Number", format="Length", inherit="no")] /** * Top inset, in pixels, for the text in the * prompt area of the control. * * @default 5 * * @langversion 3.0 * @playerversion Flash 10 * @playerversion AIR 1.5 * @productversion Flex 4 */ [Style(name="paddingTop", type="Number", format="Length", inherit="no")] //-------------------------------------- // Other metadata //-------------------------------------- [AccessibilityClass(implementation="spark.accessibility.ComboBoxAccImpl")] [IconFile("ComboBox.png")] /** * Because this component does not define a skin for the mobile theme, Adobe * recommends that you not use it in a mobile application. Alternatively, you * can define your own mobile skin for the component. For more information, * see Spark Skinning. */ [DiscouragedForProfile("mobileDevice")] /** * The ComboBox control is a child class of the DropDownListBase control. * Like the DropDownListBase control, when the user selects an item from * the drop-down list in the ComboBox control, the data item appears * in the prompt area of the control. * *
One difference between the controls is that the prompt area of * the ComboBox control is implemented by using the TextInput control, * instead of the Label control for the DropDownList control. * Therefore, a user can edit the prompt area of the control to enter * a value that is not one of the predefined options.
* *For example, the DropDownList control only lets the user select * from a list of predefined items in the control. * The ComboBox control lets the user either select a predefined item, * or enter a new item into the prompt area. * Your application can recognize that a new item has been entered and, * optionally, add it to the list of items in the control.
* *The ComboBox control also searches the item list as the user * enters characters into the prompt area. As the user enters characters, * the drop-down area of the control opens. * It then and scrolls to and highlights the closest match in the item list.
* *To use this component in a list-based component, such as a List or DataGrid, * create an item renderer. * For information about creating an item renderer, see * * Custom Spark item renderers.
* *Note: The Spark list-based controls (the Spark ListBase class and its subclasses
* such as ButtonBar, ComboBox, DropDownList, List, and TabBar) do not support the BasicLayout class
* as the value of the layout property.
* Do not use BasicLayout with the Spark list-based controls.
The ComboBox control has the following default characteristics:
*| Characteristic | *Description | *
|---|---|
| Default size | *146 pixels wide by 23 pixels high | *
| Minimum size | *20 pixels wide by 23 pixels high | *
| Maximum size | *10000 pixels wide and 10000 pixels high | *
| Default skin class | *spark.skins.spark.ComboBoxSkin
* spark.skins.spark.ComboBoxTextInputSkin |
*
The <s:ComboBox> tag inherits all of the tag
* attributes of its superclass and adds the following tag attributes:
* <s:ComboBox * Properties * itemMatchingFunction="null" * labelToItemFunction="null" * maxChars="0" * openOnInput="true" * prompt="null" * restrict="" * * Styles * paddingBottom="3" * paddingLeft="3" * paddingRight="3" * paddingTop="5" * /> ** * @see spark.skins.spark.ComboBoxSkin * @see spark.skins.spark.ComboBoxTextInputSkin * @includeExample examples/ComboBoxExample.mxml * * @langversion 3.0 * @playerversion Flash 10 * @playerversion AIR 1.5 * @productversion Flex 4 */ public class ComboBox extends DropDownListBase implements IIMESupport { //-------------------------------------------------------------------------- // // Skin Parts // //-------------------------------------------------------------------------- /** * Optional skin part that holds the input text or the selectedItem text. * * @langversion 3.0 * @playerversion Flash 10 * @playerversion AIR 1.5 * @productversion Flex 4 */ [SkinPart(required="false")] public var textInput:TextInput; //-------------------------------------------------------------------------- // // Class mixins // //-------------------------------------------------------------------------- /** * @private * Placeholder for mixin by ComboBoxAccImpl. */ mx_internal static var createAccessibilityImplementation:Function; /** * Constructor. * * @langversion 3.0 * @playerversion Flash 10 * @playerversion AIR 1.5 * @productversion Flex 4 */ public function ComboBox() { super(); addEventListener(KeyboardEvent.KEY_DOWN, capture_keyDownHandler, true); allowCustomSelectedItem = true; } //-------------------------------------------------------------------------- // // Static Variables // //-------------------------------------------------------------------------- /** * Static constant representing the value of the
selectedIndex property
* when the user enters a value into the prompt area, and the value is committed.
*
* @langversion 3.0
* @playerversion Flash 10
* @playerversion AIR 1.5
* @productversion Flex 4
*/
public static const CUSTOM_SELECTED_ITEM:int = ListBase.CUSTOM_SELECTED_ITEM;
//--------------------------------------------------------------------------
//
// Variables
//
//--------------------------------------------------------------------------
private var isTextInputInFocus:Boolean;
private var actualProposedSelectedIndex:Number = NO_SELECTION;
private var userTypedIntoText:Boolean;
private var previousTextInputText:String = "";
//--------------------------------------------------------------------------
//
// Properties
//
//--------------------------------------------------------------------------
//--------------------------------------------------------------------------
// itemMatchingFunction
//--------------------------------------------------------------------------
/**
* Specifies a callback function used to search the item list as the user
* enters characters into the prompt area.
* As the user enters characters, the drop-down area of the control opens.
* It then and scrolls to and highlights the closest match in the item list.
*
* The function referenced by this property takes an input string and returns * the index of the items in the data provider that match the input. * The items are returned as a Vector.<int> of indices in the data provider.
* *The callback function must have the following signature:
* *
* function myMatchingFunction(comboBox:ComboBox, inputText:String):Vector.<int>
*
* If the value of this property is null, the ComboBox finds matches * using the default algorithm. * By default, if an input string of length n is equivalent to the first n characters * of an item (ignoring case), then it is a match to that item. For example, 'aRiz' * is a match to "Arizona" while 'riz' is not.
* *To disable search, create a callback function that returns an empty Vector.<int>.
* * @default null * * @langversion 3.0 * @playerversion Flash 10 * @playerversion AIR 1.5 * @productversion Flex 4 */ public var itemMatchingFunction:Function = null; //-------------------------------------------------------------------------- // labelToItemFunction //-------------------------------------------------------------------------- private var _labelToItemFunction:Function; private var labelToItemFunctionChanged:Boolean = false; /** * Specifies a callback function to convert a new value entered * into the prompt area to the same data type as the data items in the data provider. * The function referenced by this properly is called when the text in the prompt area * is committed, and is not found in the data provider. * *The callback function must have the following signature:
* *
* function myLabelToItem(value:String):Object
*
* Where value is the String entered in the prompt area.
* The function returns an Object that is the same type as the items
* in the data provider.
The default callback function returns value.
true, the drop-down list opens when the user edits the prompt area.
*
* @default true
*
* @langversion 3.0
* @playerversion Flash 10
* @playerversion AIR 1.5
* @productversion Flex 4
*/
public var openOnInput:Boolean = true;
//----------------------------------
// prompt
//----------------------------------
private var _prompt:String;
private var promptChanged:Boolean;
[Inspectable(category="General")]
/**
* Text to be displayed if/when the input text is null.
*
* Prompt text appears when the control is first created. Prompt text disappears * when the control gets focus, when the input text is non-null, or when an item in the list is selected. * Prompt text reappears when the control loses focus, but only if no text was entered * (if the value of the text field is null or the empty string).
* *You can change the style of the prompt text with CSS. If the control has prompt text
* and is not disabled, the style is defined by the normalWithPrompt pseudo selector.
* If the control is disabled, then the styles defined by the disabledWithPrompt pseudo selector are used.
The following example CSS changes the color of the prompt text in TextInput controls. The ComboBox control uses * a TextInput control as a subcomponent for the prompt text and input, so its prompt text changes when you use this CSS: *
* @namespace s "library://ns.adobe.com/flex/spark";
* s|TextInput:normalWithPrompt {
* color: #CCCCFF;
* }
*
*
*
* @default null
*
* @langversion 3.0
* @playerversion Flash 10.2
* @playerversion AIR 2.0
* @productversion Flex 4.5
*/
public function get prompt():String
{
return _prompt;
}
/**
* @private
*/
public function set prompt(value:String):void
{
_prompt = value;
promptChanged = true;
invalidateProperties();
}
//--------------------------------------------------------------------------
// restrict
//--------------------------------------------------------------------------
private var _restrict:String;
private var restrictChanged:Boolean;
[Inspectable(category="General", defaultValue="")]
/**
* Specifies the set of characters that a user can enter into the prompt area.
* By default, the user can enter any characters, corresponding to a value of
* an empty string.
*
* @default ""
*
* @langversion 3.0
* @playerversion Flash 10
* @playerversion AIR 1.5
* @productversion Flex 4
*/
public function set restrict(value:String):void
{
if (value == _restrict)
return;
_restrict = value;
restrictChanged = true;
invalidateProperties();
}
/**
* @private
*/
public function get restrict():String
{
return _restrict;
}
//--------------------------------------------------------------------------
//
// Overridden Properties
//
//--------------------------------------------------------------------------
//--------------------------------------------------------------------------
// baselinePosition
//--------------------------------------------------------------------------
/**
* @private
*/
override public function get baselinePosition():Number
{
return getBaselinePositionForPart(textInput);
}
//--------------------------------------------------------------------------
// selectedIndex
//--------------------------------------------------------------------------
[Inspectable(category="General", defaultValue="-1")]
/**
* @private
*/
override public function set selectedIndex(value:int):void
{
super.selectedIndex = value;
actualProposedSelectedIndex = value;
}
//--------------------------------------------------------------------------
// typicalItem
//--------------------------------------------------------------------------
private var typicalItemChanged:Boolean = false;
[Inspectable(category="Data")]
/**
* @private
*/
override public function set typicalItem(value:Object):void
{
if (value == typicalItem)
return;
super.typicalItem = value;
typicalItemChanged = true;
invalidateProperties();
}
//--------------------------------------------------------------------------
// userProposedSelectedIndex
//--------------------------------------------------------------------------
/**
* @private
*/
override mx_internal function set userProposedSelectedIndex(value:Number):void
{
super.userProposedSelectedIndex = value;
actualProposedSelectedIndex = value;
}
//--------------------------------------------------------------------------
//
// Methods
//
//--------------------------------------------------------------------------
/**
* @private
*/
private function processInputField():void
{
var matchingItems:Vector.