rendered paste bodypackage com.jwopitz.controls {
//
import flash.events.Event;
//
import mx.events.FlexEvent;
import mx.controls.ComboBox;
//
import com.jwopitz.utils.ArrayCollectionUtil;
//
[Event (name="selectedDataChanged")]
public class ComboBox extends mx.controls.ComboBox {
//
//properties
//-----------------------------------------------------------//
public var propertyToSearchBy:String = "label";
//this acts as an MXML proxy for binding of the selectedItem.
private var _selectedData:Object;
private var _tempTextInputString:String = "";
//is true, autoPrompt will select the prompt as the current index if the proposed selectedData was not found in the dataprovider.
private var _autoPrompt:Boolean = false;
private var _autoFind:Boolean = true;
//
//constructor
//-----------------------------------------------------------//
public function ComboBox (){
//add listeners for uic init events
this.addEventListener (FlexEvent.INITIALIZE, onInitialize);
this.addEventListener (FlexEvent.CREATION_COMPLETE, onCreationComplete);
this.addEventListener (Event.CHANGE, onChange);
super ();
}
//
//methods
//-----------------------------------------------------------//
protected function onInitialize (evt:Object = null):void {
//trace ('onInitialize event: ' + this.name);
}
//
protected function onCreationComplete (evt:Object = null):void {
//trace ('onCreationComplete event: ' + this.name);
}
//
protected function onChange (evt:Object = null):void {
//trace ('onChange event: ' + this.name);
if (this.editable == true)
if (this._autoFind == true)
findMatchByText ();
}
//
protected function findMatchByText ():void {
/*_tempTextInputString = this.text;
if (typeof dataProvider.getItemAt(0) == 'object'){
this.selectedData = ArrayCollectionUtil.findItem (dataProvider, this.text, this.labelField);
} else {
this.selectedData = ArrayCollectionUtil.findItem (dataProvider, this.text);
}*/
}
//
protected function findMatch ():void {
/*if (typeof dataProvider.getItemAt(0) == 'object'){
this.selectedData = ArrayCollectionUtil.findItem (dataProvider, this.text, this.propertyToSearchBy);
} else {
this.selectedData = ArrayCollectionUtil.findItem (dataProvider, this.text);
}*/
}
//
//getters & setters
//-----------------------------------------------------------//
//write-only
public function set selectedData (value:Object):void {
_selectedData = value;
var i:uint = 0;
var l:uint = this.dataProvider.length
for (i; i < l; i++) {
if (this._selectedData == this.dataProvider[i]){
this.selectedIndex = i;
break;
} else {
if (_autoPrompt)
this.selectedIndex = -1;
}
}
dispatchEvent (new Event ("selectedDataChanged"));
}
//
public function set autoPrompt (value:Boolean):void {
this._autoPrompt = value;
this._autoFind = !value;
}
//
public function set autoFind (value:Boolean):void {
this._autoFind = value;
this._autoPrompt = !value;
}
}
}