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