basic multipolygon rendering
[potlatch2.git] / net / systemeD / halcyon / connection / Relation.as
index 02fccdc005e5f24b17a3c281d3908af660b35be8..5cab4047149e9046a8884735b8b5761e34f77b2d 100644 (file)
 package net.systemeD.halcyon.connection {
 
 package net.systemeD.halcyon.connection {
 
+       import net.systemeD.halcyon.connection.actions.*;
+
     public class Relation extends Entity {
         private var members:Array;
                public static var entity_type:String = 'relation';
 
     public class Relation extends Entity {
         private var members:Array;
                public static var entity_type:String = 'relation';
 
-        public function Relation(id:Number, version:uint, tags:Object, members:Array) {
-            super(id, version, tags);
+        public function Relation(id:Number, version:uint, tags:Object, loaded:Boolean, members:Array) {
+            super(id, version, tags, loaded);
             this.members = members;
             this.members = members;
+                       for each (var member:RelationMember in members)
+                           member.entity.addParent(this);
         }
 
         }
 
+        public function update(version:uint, tags:Object, loaded:Boolean, members:Array):void {
+                       var member:RelationMember;
+                       for each (member in this.members)
+                           member.entity.removeParent(this);
+
+                       updateEntityProperties(version,tags,loaded);
+                       this.members=members;
+                       for each (member in members)
+                           member.entity.addParent(this);
+               }
+               
         public function get length():uint {
             return members.length;
         }
 
         public function get length():uint {
             return members.length;
         }
 
+        public function findEntityMemberIndex(entity:Entity):int {
+            for (var index:uint = 0; index < members.length; index++) {
+                var member:RelationMember = members[index];
+                if ( member.entity == entity )
+                    return index;
+            }
+            return -1;
+        }
+
+        public function findEntityMemberIndexes(entity:Entity):Array {
+            var indexes:Array = [];
+            for (var index:uint = 0; index < members.length; index++) {
+                var member:RelationMember = members[index];
+                if ( member.entity == entity )
+                    indexes.push(index);
+            }
+            return indexes;
+        }
+        
         public function getMember(index:uint):RelationMember {
             return members[index];
         }
 
         public function setMember(index:uint, member:RelationMember):void {
         public function getMember(index:uint):RelationMember {
             return members[index];
         }
 
         public function setMember(index:uint, member:RelationMember):void {
-            members.splice(index, 1, member);
+            var oldMember:RelationMember = getMember(index);
+            
+                       members.splice(index, 1, member);
+            oldMember.entity.removeParent(this);
+                       member.entity.addParent(this);
+                       markDirty();
         }
 
         }
 
-        public function insertMember(index:uint, member:RelationMember):void {
-            members.splice(index, 0, member);
+               public function findMembersByRole(role:String):Array {
+                       var a:Array=[];
+            for (var index:uint = 0; index < members.length; index++) {
+                if (members[index].role==role) { a.push(members[index].entity); }
+            }
+                       return a;
+               }
+
+        public function insertMember(index:uint, member:RelationMember, performAction:Function):void {
+            performAction(new AddMemberToRelationAction(this, index, member, members));
         }
 
         public function appendMember(member:RelationMember):uint {
             members.push(member);
         }
 
         public function appendMember(member:RelationMember):uint {
             members.push(member);
+                       member.entity.addParent(this);
+                       markDirty();
+
+                       dispatchEvent(new RelationMemberEvent(Connection.RELATION_MEMBER_ADDED, member.entity, this, members.length-1));
             return members.length;
         }
 
             return members.length;
         }
 
-        public function removeMember(index:uint):void {
-            members.splice(index, 1);
+               public function removeMember(entity:Entity, performAction:Function):void {
+                       performAction(new RemoveEntityFromRelationAction(this, entity, members));
+               }
+
+        public function removeMemberByIndex(index:uint):void {
+            var removed:Array=members.splice(index, 1);
+                       var entity:Entity=removed[0].entity;
+                       
+                       // only remove as parent if this was only reference
+                       if (findEntityMemberIndex(entity)==-1)
+                           entity.removeParent(this);
+                           
+                       markDirty();
+            dispatchEvent(new RelationMemberEvent(Connection.RELATION_MEMBER_REMOVED, entity, this, index));
+        }
+
+               public override function remove(performAction:Function):void {
+                       performAction(new DeleteRelationAction(this, setDeletedState, members));
+               }
+
+               internal override function isEmpty():Boolean {
+                       return (deleted || (members.length==0));
+               }
+
+        public function getDescription():String {
+            var desc:String = "";
+            var relTags:Object = getTagsHash();
+            if ( relTags["type"] ) {
+                desc = relTags["type"];
+                if ( relTags[desc] )
+                    desc += " " + relTags[desc];
+            }
+            if ( relTags["ref"] )
+                desc += " " + relTags["ref"];
+            if ( relTags["name"] )
+                desc += " " + relTags["name"];
+            return desc;
         }
 
                public override function getType():String {
                        return 'relation';
                }
         }
 
                public override function getType():String {
                        return 'relation';
                }
+               
+               public override function toString():String {
+            return "Relation("+id+"@"+version+"): "+members.length+" members "+getTagList();
+        }
+
     }
 
 }
     }
 
 }