1 package net.systemeD.potlatch2.mapfeatures {
3 import flash.events.EventDispatcher;
4 import flash.events.Event;
6 import flash.utils.ByteArray;
7 import mx.core.BitmapAsset;
8 import mx.graphics.codec.PNGEncoder;
10 import net.systemeD.halcyon.connection.Entity;
11 import net.systemeD.potlatch2.utils.CachedDataLoader;
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
16 public class Feature extends EventDispatcher {
17 private var mapFeatures:MapFeatures;
19 private static var variablesPattern:RegExp = /[$][{]([^}]+)[}]/g;
20 private var _tags:Array;
21 private var _withins:Array;
22 private var _editors:Array;
24 [Embed(source="../../../../embedded/missing_icon.png")]
26 public var missingIconCls:Class;
29 public function Feature(mapFeatures:MapFeatures, _xml:XML) {
30 this.mapFeatures = mapFeatures;
36 private function parseConditions():void {
41 for each(var tag:XML in definition.tag) {
42 _tags.push( { k:String(tag.@k), v:String(tag.@v)} );
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; }
55 private function parseEditors():void {
56 _editors = new Array();
58 addEditors(definition);
60 _editors.sortOn(["sortOrder", "name"], [Array.DESCENDING | Array.NUMERIC, Array.CASEINSENSITIVE]);
63 private function addEditors(xml:XML):void {
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)) {
73 for each(inputXML in xml.input) {
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);
90 /** List of editing controls associated with this feature. */
91 public function get editors():Array {
95 /** The XML subtree that this feature was loaded from. */
96 public function get definition():XML {
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; }
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;
114 if ( icon.length() > 0 && icon[0].hasOwnProperty("@image") )
115 imageURL = icon[0].@image;
117 if ( imageURL != null ) {
118 img = CachedDataLoader.loadData(imageURL, imageLoaded);
123 var bitmap:BitmapAsset = new missingIconCls() as BitmapAsset;
124 return new PNGEncoder().encode(bitmap.bitmapData);
127 private function imageLoaded(url:String, data:ByteArray):void {
128 dispatchEvent(new Event("imageChanged"));
131 public function htmlDetails(entity:Entity):String {
132 var icon:XMLList = _xml.icon;
133 return makeHTMLIcon(icon, entity);
136 public static function makeHTMLIcon(icon:XMLList, entity:Entity):String {
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);
145 txt = txt.replace(variablesPattern, replaceTag);
149 public static function htmlEscape(str:String):String {
150 var newStr:String = str.replace(/&/g, "&");
151 newStr = newStr.replace(/</g, "<");
152 newStr = newStr.replace(/>/g, ">");
153 newStr = newStr.replace(/"/g, """); // "
154 newStr = newStr.replace(/'/g, "'"); // '
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 )
164 for each( var cat:XML in cats )
165 if ( cat.text()[0] == category )
171 /** List of {k, v} pairs that define the feature. */
172 public function get tags():Array {
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 {
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) )
190 /** Whether the feature is of the given type (point, line/area, relation). */
191 public function isType(type:String):Boolean {
193 return (_xml.elements(type).length() > 0) || (_xml.elements('line').length() > 0);
195 return _xml.elements(type).length() > 0;
199 /** Whether there is a help string defined. */
200 public function hasHelpURL():Boolean {
201 return _xml.help.length() > 0;
204 /** The defined help string, if any. */
205 public function get helpURL():String {