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