first stage of undo/redo functionality -- there are still a number of actions that...
[potlatch2.git] / net / systemeD / halcyon / connection / Relation.as
index f1838ce8cff72d35952f4fc262083660a6515f18..dfa6b45b2098582097ef1070c5af328fa682faaf 100644 (file)
@@ -1,5 +1,7 @@
 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';
@@ -7,14 +9,19 @@ package net.systemeD.halcyon.connection {
         public function Relation(id:Number, version:uint, tags:Object, loaded:Boolean, members:Array) {
             super(id, version, tags, loaded);
             this.members = members;
-                       for each (var member:RelationMember in members) { member.entity.addParent(this); }
+                       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); }
+                       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 {
@@ -29,52 +36,65 @@ package net.systemeD.halcyon.connection {
             }
             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 {
-                       member.entity.addParent(this);
+            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 {
-                       member.entity.addParent(this);
             members.splice(index, 0, member);
+                       member.entity.addParent(this);
                        markDirty();
+                       
+                       dispatchEvent(new RelationMemberEvent(Connection.RELATION_MEMBER_ADDED, member.entity, this, index));
         }
 
         public function appendMember(member:RelationMember):uint {
-                       member.entity.addParent(this);
             members.push(member);
+                       member.entity.addParent(this);
                        markDirty();
+
+                       dispatchEvent(new RelationMemberEvent(Connection.RELATION_MEMBER_ADDED, member.entity, this, members.length-1));
             return members.length;
         }
 
-               public function removeMember(entity:Entity):void {
-                       var i:int;
-                       while ((i=findEntityMemberIndex(entity))>-1) {
-                               members.splice(i, 1);
-                       }
-                       entity.removeParent(this);
-                       markDirty();
+               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;
-                       if (findEntityMemberIndex(entity)==-1) { entity.removeParent(this); }
+                       
+                       // 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():void {
-                       removeFromParents();
-                       for each (var member:RelationMember in members) { member.entity.removeParent(this); }
-                       members=[];
-                       deleted=true;
-            dispatchEvent(new EntityEvent(Connection.RELATION_DELETED, this));
+               public override function remove(performAction:Function):void {
+                       performAction(new DeleteRelationAction(this, setDeletedState, members));
                }
 
                internal override function isEmpty():Boolean {