Merge branch 'master' of github.com:systemed/potlatch2
[potlatch2.git] / net / systemeD / halcyon / connection / actions / AddMemberToRelationAction.as
1 package net.systemeD.halcyon.connection.actions {
2
3     import net.systemeD.halcyon.connection.*;
4     
5     public class AddMemberToRelationAction extends UndoableEntityAction {
6         private var rel:Relation;
7         private var index:int;
8         private var member:RelationMember;
9         private var memberList:Array;
10         
11         public function AddMemberToRelationAction(rel:Relation, index:int, member:RelationMember, memberList:Array) {
12             super(rel, "Add " + member.entity.getType() + " " + member.entity.id + " at position " + index + " to ");
13             this.rel = rel;
14             this.index = index;
15             this.member = member;
16             this.memberList = memberList;
17         }
18         
19         public override function doAction():uint {
20             if ( index == -1 )
21                 index = memberList.length;
22             memberList.splice(index, 0, member);
23             member.entity.addParent(rel);
24             markDirty();
25             rel.dispatchEvent(new RelationMemberEvent(Connection.RELATION_MEMBER_ADDED, member.entity, rel, index));
26             
27             return SUCCESS;
28         }
29         
30         public override function undoAction():uint {
31             memberList.splice(index, 1);
32             if (rel.findEntityMemberIndex(member.entity)==-1) {
33                 member.entity.removeParent(rel);
34             }
35             markClean();
36             rel.dispatchEvent(new RelationMemberEvent(Connection.RELATION_MEMBER_REMOVED, member.entity, rel, index));
37             
38             return SUCCESS;
39         }
40     }
41 }