Use workaround to force _factory to be non-null.
[potlatch2.git] / net / systemeD / potlatch2 / mapfeatures / editors / ChoiceEditor.mxml
1 <?xml version="1.0" encoding="utf-8"?>
2 <edit:SingleTagEditor
3         xmlns:fx="http://ns.adobe.com/mxml/2009"
4         xmlns:mx="library://ns.adobe.com/flex/mx"
5         xmlns:s="library://ns.adobe.com/flex/spark"
6         xmlns:edit="net.systemeD.potlatch2.mapfeatures.editors.*"
7         toolTip="{fieldDescription}"
8     direction="{fieldDirection}" styleName="titledEditor">
9
10   <mx:Label text="{fieldName}:"/>
11   <s:DropDownList id="inputBox" dataProvider="{choices}" selectedItem="{selectFromTag}"
12       change="value = inputBox.selectedItem.value" labelField="label">
13       <s:itemRenderer>
14         <fx:Component>
15         <mx:HBox toolTip="{data.description}" horizontalScrollPolicy="off">
16           <mx:Image source="{Choice(data).icon}"/>
17           <mx:Label htmlText="{data.label}"/>
18         </mx:HBox>
19         </fx:Component>
20       </s:itemRenderer>
21   </s:DropDownList>
22
23   <fx:Script><![CDATA[
24       import mx.collections.*;
25       import mx.utils.*;
26       import net.systemeD.potlatch2.mapfeatures.Feature;
27       
28       private var _choices:ArrayCollection = null;
29       private var _unknownChoice:Choice = null;
30
31       // ** FIXME:
32       // currently the raw htmlText for the selected item is shown.
33       // see http://stackoverflow.com/questions/269773/flex-custom-item-renderer-for-the-displayed-item-in-the-combobox/280859#280859
34       // (via http://stackoverflow.com/questions/4051822/flex-itemrenderer-issue-with-dropdown-control)
35
36       [Bindable(event="factory_set")]
37       protected function get choices():ArrayCollection {
38           var dummy:String=_factory.key;                // otherwise _factory is null, for some not yet discovered reason.
39           if ( _choices == null ) {
40               _choices = new ArrayCollection();
41               for each ( var choice:Choice in ChoiceEditorFactory(_factory).choices ) {
42                   _choices.addItem(choice);
43               }
44               _choices.addItem(createUnsetChoice());
45           }
46           return _choices;
47       }
48       
49       [Bindable(event="tag_changed")]
50       protected function get selectFromTag():Object {
51           var tagValue:String = value;
52           for each(var choice:Choice in choices) {
53               if ( choice.isTagMatch(tagValue) )
54                   return choice;
55           }
56           
57           // set up the unknown choice
58           if ( _unknownChoice == null ) {
59               _unknownChoice = new Choice();
60               _unknownChoice.description = "Unknown value";
61               _choices.addItem(_unknownChoice);
62           }
63           _unknownChoice.label = "<i>Unknown:</i> "+Feature.htmlEscape(tagValue);
64           _unknownChoice.icon = null;
65           _unknownChoice.value = tagValue;
66           
67           return _unknownChoice;
68       }
69       
70       private function createUnsetChoice():Choice {
71           var choice:Choice = new Choice();
72           choice.icon = null;
73           choice.description = "Field not set";
74           choice.label = "<font color='#a0a0a0'><i>Unset</i></font>";
75           choice.value = null;
76           return choice;
77       }
78   ]]></fx:Script>
79 </edit:SingleTagEditor>
80