cd7489d79567bf6da705d8b40d15e7278b60149f
[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="{getStyledLabel(Choice(data))}"/>
18                         <fx:Script><![CDATA[
19                               public function getStyledLabel(choice:Choice):String {
20                                   if (choice.value==null) return "<font color='#a0a0a0'><i>"+choice.label+"</i></font>";
21                                   return choice.label;
22                                   }
23                         ]]></fx:Script>
24         </mx:HBox>
25         </fx:Component>
26       </s:itemRenderer>
27   </s:DropDownList>
28
29   <fx:Script><![CDATA[
30       import mx.collections.*;
31       import mx.utils.*;
32       import net.systemeD.potlatch2.mapfeatures.Feature;
33       
34       private var _choices:ArrayCollection = null;
35       private var _unknownChoice:Choice = null;
36
37       [Bindable(event="factory_set")]
38       protected function get choices():ArrayCollection {
39           var dummy:String=_factory.key;                // otherwise _factory is null, for some not yet discovered reason.
40           if ( _choices == null ) {
41               _choices = new ArrayCollection();
42               for each ( var choice:Choice in ChoiceEditorFactory(_factory).choices ) {
43                   _choices.addItem(choice);
44               }
45               _choices.addItem(createUnsetChoice());
46           }
47           return _choices;
48       }
49       
50       [Bindable(event="tag_changed")]
51       protected function get selectFromTag():Object {
52           var tagValue:String = value;
53           for each(var choice:Choice in choices) {
54               if ( choice.isTagMatch(tagValue) )
55                   return choice;
56           }
57           
58           // set up the unknown choice
59           if ( _unknownChoice == null ) {
60               _unknownChoice = new Choice();
61               _unknownChoice.description = "Unknown value";
62               _choices.addItem(_unknownChoice);
63           }
64           _unknownChoice.label = "<i>Unknown:</i> "+Feature.htmlEscape(tagValue);
65           _unknownChoice.icon = null;
66           _unknownChoice.value = tagValue;
67           
68           return _unknownChoice;
69       }
70
71       private function createUnsetChoice():Choice {
72           var choice:Choice = new Choice();
73           choice.icon = null;
74           choice.description = "Field not set";
75           choice.label = "Unset";
76           choice.value = null;
77           return choice;
78       }
79   ]]></fx:Script>
80 </edit:SingleTagEditor>
81