Removed executable flag from files which don't need it.
[potlatch2.git] / net / systemeD / potlatch2 / mapfeatures / Feature.as
index 425768ce8772988857afc3abb5ea4b2829396b1c..0ad71358eeab6050181ffff3ced5bed50e1a5c01 100644 (file)
@@ -2,49 +2,86 @@ package net.systemeD.potlatch2.mapfeatures {
 
     import flash.events.EventDispatcher;
     import flash.events.Event;
 
     import flash.events.EventDispatcher;
     import flash.events.Event;
+    import flash.net.*;
+    import flash.utils.ByteArray;
+    import mx.core.BitmapAsset;
+    import mx.graphics.codec.PNGEncoder;
+    
     import net.systemeD.halcyon.connection.Entity;
     import net.systemeD.halcyon.connection.Entity;
+    import net.systemeD.potlatch2.utils.CachedDataLoader;
 
        public class Feature extends EventDispatcher {
         private var mapFeatures:MapFeatures;
         private var _xml:XML;
         private static var variablesPattern:RegExp = /[$][{]([^}]+)[}]/g;
         private var _tags:Array;
 
        public class Feature extends EventDispatcher {
         private var mapFeatures:MapFeatures;
         private var _xml:XML;
         private static var variablesPattern:RegExp = /[$][{]([^}]+)[}]/g;
         private var _tags:Array;
+               private var _withins:Array;
         private var _editors:Array;
 
         private var _editors:Array;
 
+        [Embed(source="../../../../embedded/missing_icon.png")]
+        [Bindable]
+        public var missingIconCls:Class;
+
+
         public function Feature(mapFeatures:MapFeatures, _xml:XML) {
             this.mapFeatures = mapFeatures;
             this._xml = _xml;
         public function Feature(mapFeatures:MapFeatures, _xml:XML) {
             this.mapFeatures = mapFeatures;
             this._xml = _xml;
-            parseTags();
+            parseConditions();
             parseEditors();
         }
         
             parseEditors();
         }
         
-        private function parseTags():void {
-            _tags = new Array();
+        private function parseConditions():void {
+            _tags = [];
+                       _withins = [];
             
             
+                       // parse tags
             for each(var tag:XML in definition.tag) {
             for each(var tag:XML in definition.tag) {
-                var tagObj:Object = new Object();
-                tagObj["k"] = tag.@k;
-                tagObj["v"] = tag.@v;
-                _tags.push(tagObj);
+                _tags.push( { k:tag.@k, v:tag.@v} );
+            }
+
+                       // parse 'within'
+            for each(var within:XML in definition.within) {
+                               var obj:Object= { entity:within.@entity, k:within.@k };
+                               if (within.attribute('v'      ).length()>0) { obj['v'      ]=within.@v;       }
+                               if (within.attribute('minimum').length()>0) { obj['minimum']=within.@minimum; }
+                               if (within.attribute('role'   ).length()>0) { obj['role'   ]=within.@role;    }
+                _withins.push(obj);
             }
         }
         
         private function parseEditors():void {
             _editors = new Array();
             
             }
         }
         
         private function parseEditors():void {
             _editors = new Array();
             
-            for each(var inputXML:XML in definition.input) {
-                var inputType:String = inputXML.@type;
-                var presenceStr:String = inputXML.@presence;
-                var sortOrderStr:String = inputXML.@priority;
-                var editor:EditorFactory = EditorFactory.createFactory(inputType, inputXML);
-                if ( editor != null ) {
-                    editor.presence = Presence.getPresence(presenceStr);
-                    editor.sortOrder = EditorFactory.getPriority(sortOrderStr);
-                    _editors.push(editor);
+            addEditors(definition);
+            
+            _editors.sortOn(["sortOrder", "name"], [Array.DESCENDING | Array.NUMERIC, Array.CASEINSENSITIVE]);
+        }
+        
+        private function addEditors(xml:XML):void {
+            var inputXML:XML;
+            
+            for each(var inputSetRef:XML in xml.inputSet) {
+                var setName:String = String(inputSetRef.@ref);
+                for each (inputXML in mapFeatures.definition.inputSet.(@id==setName)) {
+                    addEditors(inputXML);
                 }
             }
             
                 }
             }
             
-            _editors.sortOn(["sortOrder", "name"], [Array.DESCENDING | Array.NUMERIC, Array.CASEINSENSITIVE]);
+            for each(inputXML in xml.input) {
+                addEditor(inputXML);
+            }
+        }
+        
+        private function addEditor(inputXML:XML):void {
+            var inputType:String = inputXML.@type;
+            var presenceStr:String = inputXML.@presence;
+            var sortOrderStr:String = inputXML.@priority;
+            var editor:EditorFactory = EditorFactory.createFactory(inputType, inputXML);
+            if ( editor != null ) {
+                editor.presence = Presence.getPresence(presenceStr);
+                editor.sortOrder = EditorFactory.getPriority(sortOrderStr);
+                _editors.push(editor);
+            }
         }
         
         public function get editors():Array {
         }
         
         public function get editors():Array {
@@ -57,33 +94,61 @@ package net.systemeD.potlatch2.mapfeatures {
     
         [Bindable(event="nameChanged")]
         public function get name():String {
     
         [Bindable(event="nameChanged")]
         public function get name():String {
-            return _xml.@name;
+                       if (_xml.attribute('name').length()>0) { return _xml.@name; }
+                       return null;
         }
     
         [Bindable(event="imageChanged")]
         }
     
         [Bindable(event="imageChanged")]
-        public function get image():String {
+        public function get image():ByteArray {
             var icon:XMLList = _xml.icon;
             var icon:XMLList = _xml.icon;
+            var imageURL:String = null;
+            var img:ByteArray;
 
             if ( icon.length() > 0 && icon[0].hasOwnProperty("@image") )
 
             if ( icon.length() > 0 && icon[0].hasOwnProperty("@image") )
-                return icon[0].@image;
-            else
-                return null;
+                imageURL = icon[0].@image;
+
+            if ( imageURL != null ) {
+                img = CachedDataLoader.loadData(imageURL, imageLoaded);
+            }
+            if (img) {
+              return img;
+            }
+            var bitmap:BitmapAsset = new missingIconCls() as BitmapAsset;
+            return new PNGEncoder().encode(bitmap.bitmapData);
+        }
+        
+        private function imageLoaded(url:String, data:ByteArray):void {
+            dispatchEvent(new Event("imageChanged"));
         }
         
         public function htmlDetails(entity:Entity):String {
             var icon:XMLList = _xml.icon;
         }
         
         public function htmlDetails(entity:Entity):String {
             var icon:XMLList = _xml.icon;
+            return makeHTMLIcon(icon, entity);
+        }   
+
+        public static function makeHTMLIcon(icon:XMLList, entity:Entity):String {
             if ( icon == null )
                 return "";
             if ( icon == null )
                 return "";
-
+            
             var txt:String = icon.children().toXMLString();
             var replaceTag:Function = function():String {
                 var value:String = entity.getTag(arguments[1]);
             var txt:String = icon.children().toXMLString();
             var replaceTag:Function = function():String {
                 var value:String = entity.getTag(arguments[1]);
-                return value == null ? "" : value;
+                return value == null ? "" : htmlEscape(value);
             };
             txt = txt.replace(variablesPattern, replaceTag);
             return txt;
         }
         
             };
             txt = txt.replace(variablesPattern, replaceTag);
             return txt;
         }
         
+        public static function htmlEscape(str:String):String {
+            var newStr:String = str.replace(/&/g, "&");
+            newStr = newStr.replace(/</g, "&lt;");
+            newStr = newStr.replace(/>/g, "&gt;");
+            newStr = newStr.replace(/"/g, "&quot;");   // "
+            newStr = newStr.replace(/'/g, "&apos;");   // '
+            return newStr;
+        }
+      
+
         public function isInCategory(category:String):Boolean {
             var cats:XMLList = _xml.category;
             if ( cats.length() == 0 )
         public function isInCategory(category:String):Boolean {
             var cats:XMLList = _xml.category;
             if ( cats.length() == 0 )
@@ -98,6 +163,10 @@ package net.systemeD.potlatch2.mapfeatures {
         public function get tags():Array {
             return _tags;
         }
         public function get tags():Array {
             return _tags;
         }
+
+        public function get withins():Array {
+            return _withins;
+        }
         
         public function findFirstCategory():Category {
             for each( var cat:Category in mapFeatures.categories ) {
         
         public function findFirstCategory():Category {
             for each( var cat:Category in mapFeatures.categories ) {
@@ -106,6 +175,22 @@ package net.systemeD.potlatch2.mapfeatures {
             }
             return null;
         }
             }
             return null;
         }
+        
+        public function isType(type:String):Boolean {
+            if (type=='area') {
+                           return (_xml.elements(type).length() > 0) || (_xml.elements('line').length() > 0);
+            } else {
+                           return _xml.elements(type).length() > 0;
+                       }
+        }
+        
+        public function hasHelpURL():Boolean {
+            return _xml.help.length() > 0;
+        }
+        
+        public function get helpURL():String {
+            return _xml.help;
+        }
     }
 }
 
     }
 }