Add some ASDoc comments to mapfeature-related classes. Probably best if someone quick...
[potlatch2.git] / net / systemeD / potlatch2 / mapfeatures / Feature.as
1 package net.systemeD.potlatch2.mapfeatures {
2
3     import flash.events.EventDispatcher;
4     import flash.events.Event;
5     import flash.net.*;
6     import flash.utils.ByteArray;
7     import mx.core.BitmapAsset;
8     import mx.graphics.codec.PNGEncoder;
9
10     import net.systemeD.halcyon.connection.Entity;
11     import net.systemeD.potlatch2.utils.CachedDataLoader;
12
13         /** A "map feature" is sort of a template for a map entity. It consists of a few crucial key/value pairs that define the feature, so that
14          * entities can be recognised. It also contains optional keys, with associated editing controls, that are defined as being appropriate
15          * for the feature. */
16         public class Feature extends EventDispatcher {
17         private var mapFeatures:MapFeatures;
18         private var _xml:XML;
19         private static var variablesPattern:RegExp = /[$][{]([^}]+)[}]/g;
20         private var _tags:Array;
21         private var _withins:Array;
22         private var _editors:Array;
23
24         [Embed(source="../../../../embedded/missing_icon.png")]
25         [Bindable]
26         public var missingIconCls:Class;
27
28
29         public function Feature(mapFeatures:MapFeatures, _xml:XML) {
30             this.mapFeatures = mapFeatures;
31             this._xml = _xml;
32             parseConditions();
33             parseEditors();
34         }
35
36         private function parseConditions():void {
37             _tags = [];
38            _withins = [];
39
40                         // parse tags
41             for each(var tag:XML in definition.tag) {
42                 _tags.push( { k:String(tag.@k), v:String(tag.@v)} );
43             }
44
45                         // parse 'within'
46             for each(var within:XML in definition.within) {
47                                 var obj:Object= { entity:within.@entity, k:within.@k };
48                                 if (within.attribute('v'      ).length()>0) { obj['v'      ]=within.@v;       }
49                                 if (within.attribute('minimum').length()>0) { obj['minimum']=within.@minimum; }
50                                 if (within.attribute('role'   ).length()>0) { obj['role'   ]=within.@role;    }
51                 _withins.push(obj);
52             }
53         }
54
55         private function parseEditors():void {
56             _editors = new Array();
57
58             addEditors(definition);
59
60             _editors.sortOn(["sortOrder", "name"], [Array.DESCENDING | Array.NUMERIC, Array.CASEINSENSITIVE]);
61         }
62
63         private function addEditors(xml:XML):void {
64             var inputXML:XML;
65
66             for each(var inputSetRef:XML in xml.inputSet) {
67                 var setName:String = String(inputSetRef.@ref);
68                 for each (inputXML in mapFeatures.definition.inputSet.(@id==setName)) {
69                     addEditors(inputXML);
70                 }
71             }
72
73             for each(inputXML in xml.input) {
74                 addEditor(inputXML);
75             }
76         }
77
78         private function addEditor(inputXML:XML):void {
79             var inputType:String = inputXML.@type;
80             var presenceStr:String = inputXML.@presence;
81             var sortOrderStr:String = inputXML.@priority;
82             var editor:EditorFactory = EditorFactory.createFactory(inputType, inputXML);
83             if ( editor != null ) {
84                 editor.presence = Presence.getPresence(presenceStr);
85                 editor.sortOrder = EditorFactory.getPriority(sortOrderStr);
86                 _editors.push(editor);
87             }
88         }
89
90         /** List of editing controls associated with this feature. */
91         public function get editors():Array {
92             return _editors;
93         }
94
95         /** The XML subtree that this feature was loaded from. */
96         public function get definition():XML {
97             return _xml;
98         }
99
100         [Bindable(event="nameChanged")]
101         /** The human-readable name of the feature, or null if none. */
102         public function get name():String {
103                         if (_xml.attribute('name').length()>0) { return _xml.@name; }
104                         return null;
105         }
106
107         [Bindable(event="imageChanged")]
108         /** An icon for the feature. If none is defined, return default "missing icon". */
109         public function get image():ByteArray {
110             var icon:XMLList = _xml.icon;
111             var imageURL:String = null;
112             var img:ByteArray;
113
114             if ( icon.length() > 0 && icon[0].hasOwnProperty("@image") )
115                 imageURL = icon[0].@image;
116
117             if ( imageURL != null ) {
118                 img = CachedDataLoader.loadData(imageURL, imageLoaded);
119             }
120             if (img) {
121               return img;
122             }
123             var bitmap:BitmapAsset = new missingIconCls() as BitmapAsset;
124             return new PNGEncoder().encode(bitmap.bitmapData);
125         }
126
127         private function imageLoaded(url:String, data:ByteArray):void {
128             dispatchEvent(new Event("imageChanged"));
129         }
130
131         public function htmlDetails(entity:Entity):String {
132             var icon:XMLList = _xml.icon;
133             return makeHTMLIcon(icon, entity);
134         }
135
136         public static function makeHTMLIcon(icon:XMLList, entity:Entity):String {
137             if ( icon == null )
138                 return "";
139
140             var txt:String = icon.children().toXMLString();
141             var replaceTag:Function = function():String {
142                 var value:String = entity.getTag(arguments[1]);
143                 return value == null ? "" : htmlEscape(value);
144             };
145             txt = txt.replace(variablesPattern, replaceTag);
146             return txt;
147         }
148
149         public static function htmlEscape(str:String):String {
150             var newStr:String = str.replace(/&/g, "&");
151             newStr = newStr.replace(/</g, "&lt;");
152             newStr = newStr.replace(/>/g, "&gt;");
153             newStr = newStr.replace(/"/g, "&quot;");    // "
154             newStr = newStr.replace(/'/g, "&apos;");    // '
155             return newStr;
156         }
157
158         /** Whether this feature belongs to the given category or not, as defined by its definition in the XML file. */
159         public function isInCategory(category:String):Boolean {
160             var cats:XMLList = _xml.category;
161             if ( cats.length() == 0 )
162                 return false;
163
164             for each( var cat:XML in cats )
165                 if ( cat.text()[0] == category )
166                     return true;
167             return false;
168         }
169
170
171         /** List of {k, v} pairs that define the feature. */
172         public function get tags():Array {
173             return _tags;
174         }
175
176         /** List of "withins" which further restrict the applicability of the feature. Each within is a {entity, k, ?v, ?minimum, ?role} object. */
177         public function get withins():Array {
178             return _withins;
179         }
180
181         /** The first category that the feature belongs to, as defined by the order of the map features XML file. */
182         public function findFirstCategory():Category {
183             for each( var cat:Category in mapFeatures.categories ) {
184                 if ( isInCategory(cat.id) )
185                     return cat;
186             }
187             return null;
188         }
189
190         /** Whether the feature is of the given type (point, line/area, relation). */
191         public function isType(type:String):Boolean {
192             if (type=='area') {
193                             return (_xml.elements(type).length() > 0) || (_xml.elements('line').length() > 0);
194             } else {
195                             return _xml.elements(type).length() > 0;
196                         }
197         }
198
199         /** Whether there is a help string defined. */
200         public function hasHelpURL():Boolean {
201             return _xml.help.length() > 0;
202         }
203
204         /** The defined help string, if any. */
205         public function get helpURL():String {
206             return _xml.help;
207         }
208     }
209 }
210