Adjust behaviour of numberEditor - add clear button, and explanitory text for when...
[potlatch2.git] / net / systemeD / potlatch2 / mapfeatures / editors / NumberEditor.mxml
1 <?xml version="1.0" encoding="utf-8"?>
2 <edit:SingleTagEditor
3         xmlns:mx="http://www.adobe.com/2006/mxml" 
4         xmlns:edit="net.systemeD.potlatch2.mapfeatures.editors.*"
5         xmlns:flexlib="flexlib.controls.*"
6         verticalGap="0"
7         width="100%"
8         toolTip="{fieldDescription}"
9     direction="{fieldDirection}" styleName="titledEditor">
10
11         <mx:Label text="{fieldName}:"/>
12     <mx:HBox horizontalGap="3" verticalAlign="middle">
13         <!-- it would be great if the numericStepper showed something other than zero when unset -->
14             <mx:NumericStepper id="input"
15                  minimum="{minimum}" maximum="{maximum}"
16                  stepSize="{stepSize}"
17                  value="{translatedValue}"
18                  enabled="{isValueValid}"
19                                  change="value = input.value.toString()" />
20         <mx:LinkButton label="Clear" click="clear()" enabled="{isValueClearable}" />
21         <mx:Label text="{prompt}" visible="{value == null || value == ''}" includeInLayout="{value == null || value == ''}" styleName="promptText"/>
22         <mx:Label text="{warning}" visible="{!isValueValid}" includeInLayout="{!isValueValid}" styleName="warningText" />
23     </mx:HBox>
24
25   <mx:Script><![CDATA[
26     [Bindable(event="factory_set")]
27     protected function get prompt():String {
28         return _factory == null ? null : NumberEditorFactory(_factory).notPresentText;
29     }
30     
31     [Bindable(event="factory_set")]
32     protected function get warning():String {
33         return _factory == null ? null : NumberEditorFactory(_factory).notValidText;
34     }
35
36     [Bindable(event="factory_set")]
37     public function get minimum():Number {
38         return NumberEditorFactory(_factory) == null ? 0 : NumberEditorFactory(_factory).minimum;
39     }
40
41     [Bindable(event="factory_set")]
42     public function get maximum():Number {
43         return NumberEditorFactory(_factory) == null ? 100 : NumberEditorFactory(_factory).maximum;
44     }
45
46     [Bindable(event="factory_set")]
47     public function get stepSize():Number {
48         return NumberEditorFactory(_factory) == null ? 1 : NumberEditorFactory(_factory).stepSize;
49     }
50
51     [Bindable(event="tag_changed")]
52     private function get translatedValue():Number {
53         var validatedValue:Number = getValidatedValue();
54         return validatedValue;
55     }
56
57     [Bindable(event="tag_changed")]
58     private function get isValueValid():Boolean {
59         var validatedValue:Number = getValidatedValue();
60         return !(isNaN(validatedValue) && value != null && value != "");
61     }
62     
63     [Bindable(event="tag_changed")]
64     private function get isValueClearable():Boolean {
65         var validatedValue:Number = getValidatedValue();
66         return !isNaN(validatedValue);
67     }
68
69     private function getValidatedValue():Number {
70         var valueStr:String = value;
71         if ( valueStr == null || valueStr == "" )
72             return Number.NaN;
73
74         var parsed:Number = parseFloat(value);
75         if ( isNaN(parsed) || parsed < minimum || parsed > maximum )
76             return Number.NaN;
77         return parsed;
78     }
79     
80     private function clear():void {
81         value = null;
82     }
83         
84   ]]></mx:Script>
85 </edit:SingleTagEditor>
86