more
[potlatch2.git] / net / systemeD / potlatch2 / mapfeatures / editors / SpeedChoice.as
1 package net.systemeD.potlatch2.mapfeatures.editors {
2
3     import flash.events.*;
4
5         public class SpeedChoice extends EventDispatcher {
6
7         private static const speedRE:RegExp = /^([0-9.]+)\s*(.*)$/;
8         
9         private var _scalar:Number = -1;
10         private var _unit:String = null;
11         private var _description:String = "";
12         private var _value:String = null;
13         private var _isEditable:Boolean = false;
14         
15         public function SpeedChoice(speedStr:String) {
16             value = speedStr;
17         }
18         
19         private function parseSpeedString(speedStr:String):Object {
20             var match:Object = speedRE.exec(speedStr);
21             if ( match == null )
22                 return null;
23             
24             try {
25                 var scalar:Number = Number(match[1]);
26                 var unit:String = match[2].replace(/\s/g, "");
27                 if ( unit == null || unit == "" || unit == "kph" || unit == "kmh" )
28                     unit = "km/h";
29                 return {scalar: scalar, unit: unit};
30             } catch ( exception:Object ) {
31                 return null;
32             }
33             
34             return null;
35         }
36         
37         [Bindable(event="valueChange")]
38         public function get scalar():String { return String(_scalar); }
39
40         [Bindable(event="valueChange")]
41         public function get description():String { return _description; }
42
43         [Bindable(event="valueChange")]
44         public function get value():String { return _value; }
45
46         [Bindable(event="valueChange")]
47         public function get unit():String { return _unit; }
48
49         [Bindable(event="editableChange")]
50         public function get isEditable():Boolean { return _isEditable; }
51
52         public function set isEditable(editable:Boolean):void {
53             _isEditable = editable;
54             dispatchEvent(new Event("editableChange"));
55         }
56         
57         public function isTagMatch(tagValue:String):Boolean {
58             if ( _value == tagValue )
59                 return true;
60             
61             var tagSpeed:Object = parseSpeedString(tagValue);
62             return tagSpeed != null && tagSpeed.scalar == _scalar && tagSpeed.unit == _unit;
63         }
64         
65         public function set value(speedStr:String):void {
66             var speed:Object = parseSpeedString(speedStr);
67             if ( speed != null ) {
68                 _scalar = speed.scalar;
69                 _unit = speed.unit;
70                 _description = String(_scalar) + " "+_unit;
71                 _value = String(_scalar) + (_unit == "km/h" ? "":" "+_unit);
72             } else {
73                 _value = speedStr;
74             }
75             dispatchEvent(new Event("valueChange"));
76         }
77     }
78
79 }
80
81