1 | <?xml version="1.0" encoding="utf-8"?>
|
---|
2 | <!-- ValueComboBox.mxml
|
---|
3 | allows value property to be settable
|
---|
4 | based on "Rich Internet Applications with Adobe Flex and Java" page 319
|
---|
5 | -->
|
---|
6 |
|
---|
7 | <mx:ComboBox xmlns:mx="http://www.adobe.com/2006/mxml">
|
---|
8 | <mx:Script>
|
---|
9 | <![CDATA[
|
---|
10 | public var dataField: String = "data";
|
---|
11 |
|
---|
12 | private var candidateValue: Object;
|
---|
13 | private var valueDirty: Boolean = false;
|
---|
14 | private var candidateDataProvider: Object;
|
---|
15 | private var dataProviderDirty: Boolean = false;
|
---|
16 |
|
---|
17 | [Bindable("change")]
|
---|
18 | [Bindable("valueCommit")]
|
---|
19 | [Inspectable(defaultValue="0", category="General", verbose="1")]
|
---|
20 | override public function get value():Object
|
---|
21 | {
|
---|
22 | var item: Object = selectedItem;
|
---|
23 | if ((item == null) || (typeof(item) != "object")) return item;
|
---|
24 | return item[dataField] ? item[dataField] : item.label;
|
---|
25 | }
|
---|
26 |
|
---|
27 | public function set value(val:Object) : void
|
---|
28 | {
|
---|
29 | candidateValue = val;
|
---|
30 | valueDirty = true;
|
---|
31 | invalidateProperties();
|
---|
32 | }
|
---|
33 |
|
---|
34 | private function applyValue(val:Object):void
|
---|
35 | {
|
---|
36 | if ((val != null) && (dataProvider != null)) {
|
---|
37 | for (var i : int = 0; i < dataProvider.length; i++) {
|
---|
38 | if (dataProvider[i] == null) continue;
|
---|
39 |
|
---|
40 | // only checking dataField to avoid accidental selection
|
---|
41 | // (could also check labelField, but not needed for ED app)
|
---|
42 | if (val == dataProvider[i][dataField]) {
|
---|
43 | selectedIndex = i;
|
---|
44 | return;
|
---|
45 | }
|
---|
46 | }
|
---|
47 | }
|
---|
48 | selectedIndex = -1;
|
---|
49 | }
|
---|
50 |
|
---|
51 | override public function set dataProvider(value:Object):void
|
---|
52 | {
|
---|
53 | candidateDataProvider = value;
|
---|
54 | dataProviderDirty = true;
|
---|
55 | invalidateProperties();
|
---|
56 | }
|
---|
57 |
|
---|
58 | override protected function commitProperties():void
|
---|
59 | {
|
---|
60 | super.commitProperties();
|
---|
61 |
|
---|
62 | if (dataProviderDirty) {
|
---|
63 | super.dataProvider = candidateDataProvider;
|
---|
64 | dataProviderDirty = false;
|
---|
65 | }
|
---|
66 |
|
---|
67 | if (valueDirty) {
|
---|
68 | applyValue(candidateValue);
|
---|
69 | valueDirty = false;
|
---|
70 | }
|
---|
71 | }
|
---|
72 | ]]>
|
---|
73 | </mx:Script>
|
---|
74 | </mx:ComboBox>
|
---|