fix horrid merge bug (yay!). Implement suspend/resume events for redrawing, to speed...
[potlatch2.git] / net / systemeD / halcyon / connection / Entity.as
index d0e7274..1d2feb4 100644 (file)
@@ -1,17 +1,23 @@
 package net.systemeD.halcyon.connection {
 
     import flash.events.EventDispatcher;
+    import flash.utils.Dictionary;
 
     public class Entity extends EventDispatcher {
         private var _id:Number;
         private var _version:uint;
         private var tags:Object = {};
         private var modified:Boolean = false;
+               private var _loaded:Boolean = true;
+               private var parents:Dictionary = new Dictionary();
+               private var locked:Boolean = false;
+               protected var deleted:Boolean = false;
 
-        public function Entity(id:Number, version:uint, tags:Object) {
+        public function Entity(id:Number, version:uint, tags:Object, loaded:Boolean) {
             this._id = id;
             this._version = version;
             this.tags = tags;
+                       this._loaded = loaded;
             modified = id < 0;
         }
 
@@ -23,12 +29,24 @@ package net.systemeD.halcyon.connection {
             return _version;
         }
 
+        public function get loaded():Boolean {
+            return _loaded;
+        }
+
+               public function updateEntityProperties(version:uint, tags:Object, loaded:Boolean):void {
+                       _version=version; this.tags=tags; _loaded=loaded;
+               }
+
+               // Tag-handling methods
+
         public function hasTags():Boolean {
             for (var key:String in tags)
                 return true;
             return false;
         }
 
+               // ** we could do with hasInterestingTags - don't bother with source, created_by, any TIGER tags, etc.
+
         public function getTag(key:String):String {
             return tags[key];
         }
@@ -40,8 +58,8 @@ package net.systemeD.halcyon.connection {
                     delete tags[key];
                 else
                     tags[key] = value;
-                modified = true;
-                dispatchEvent(new TagEvent(Connection.TAG_CHANGE, this, key, key, old, value));
+                markDirty();
+                dispatchEvent(new TagEvent(Connection.TAG_CHANGED, this, key, key, old, value));
             }
         }
 
@@ -50,8 +68,8 @@ package net.systemeD.halcyon.connection {
             if ( oldKey != newKey ) {
                 delete tags[oldKey];
                 tags[newKey] = value;
-                modified = true;
-                dispatchEvent(new TagEvent(Connection.TAG_CHANGE, this, oldKey, newKey, value, value));
+                markDirty();
+                dispatchEvent(new TagEvent(Connection.TAG_CHANGED, this, oldKey, newKey, value, value));
             }
         }
 
@@ -66,6 +84,12 @@ package net.systemeD.halcyon.connection {
             return copy;
         }
 
+               public function getTagsHash():Object {
+                       // hm, not sure we should be doing this, but for read-only purposes
+                       // it's faster than using getTagsCopy
+                       return tags;
+               }
+
         public function getTagArray():Array {
             var copy:Array = [];
             for (var key:String in tags )
@@ -73,6 +97,8 @@ package net.systemeD.halcyon.connection {
             return copy;
         }
 
+               // Clean/dirty methods
+
         public function get isDirty():Boolean {
             return modified;
         }
@@ -87,6 +113,98 @@ package net.systemeD.halcyon.connection {
             modified = true;
         }
 
+               // Delete entity
+               
+               public function remove():void {
+                       // to be overridden
+               }
+               
+               internal function isEmpty():Boolean {
+                       return false;   // to be overridden
+               }
+
+               protected function removeFromParents():void {
+                       for (var o:Object in parents) {
+                               if (o is Relation) { Relation(o).removeMember(this); }
+                               else if (o is Way) { Way(o).removeNode(Node(this)); }
+                               if (o.isEmpty()) { o.remove(); }
+                       }
+               }
+
+               // Parent handling
+               
+               public function addParent(parent:Entity):void {
+                       parents[parent]=true;
+                       
+                       if ( parent is Relation )
+                           dispatchEvent(new RelationMemberEvent(Connection.ADDED_TO_RELATION, this, parent as Relation, -1));
+               }
+
+               public function removeParent(parent:Entity):void {
+                       delete parents[parent];
+
+                       if ( parent is Relation )
+                           dispatchEvent(new RelationMemberEvent(Connection.REMOVED_FROM_RELATION, this, parent as Relation, -1));
+               }
+               
+               public function get parentWays():Array {
+                       var a:Array=[];
+                       for (var o:Object in parents) {
+                               if (o is Way) { a.push(o); }
+                       }
+                       return a;
+               }
+
+               public function get hasParents():Boolean {
+                       for (var o:Object in parents) { return true; }
+                       return false;
+               }
+               
+               public function get hasParentWays():Boolean {
+                       for (var o:Object in parents) {
+                               if (o is Way) { return true; }
+                       }
+                       return false;
+               }
+               
+               public function get numParentWays():uint {
+                       var i:uint=0;
+                       for (var o:Object in parents) {
+                               if (o is Way) { i++; }
+                       }
+                       return i;
+               }
+               
+               public function get parentRelations():Array {
+                       var a:Array=[];
+                       for (var o:Object in parents) {
+                               if (o is Relation) { a.push(o); }
+                       }
+                       return a;
+               }
+               
+               public function get parentObjects():Array {
+                       var a:Array=[];
+                       for (var o:Object in parents) { a.push(o); }
+                       return a;
+               }
+               
+               public function hasParent(entity:Entity):Boolean {
+            return parents[entity] == true;
+        }
+
+               // Resume/suspend redraw
+               
+               public function suspend():void {
+                       dispatchEvent(new EntityEvent(Connection.SUSPEND_REDRAW, this));
+               }
+               
+               public function resume():void {
+                       dispatchEvent(new EntityEvent(Connection.RESUME_REDRAW, this));
+               }
+
+               // To be overridden
+
         public function getType():String {
             return '';
         }