first stage of undo/redo functionality -- there are still a number of actions that...
[potlatch2.git] / net / systemeD / halcyon / connection / actions / DeleteRelationAction.as
1 package net.systemeD.halcyon.connection.actions {
2
3     import net.systemeD.halcyon.connection.*;
4     
5     public class DeleteRelationAction extends UndoableEntityAction {
6         private var setDeleted:Function;
7         private var effects:CompositeUndoableAction;
8         private var memberList:Array;
9         private var oldMemberList:Array;
10         
11         public function DeleteRelationAction(relation:Relation, setDeleted:Function, memberList:Array) {
12             super(relation, "Delete");
13             this.setDeleted = setDeleted;
14             this.memberList = memberList;
15         }
16             
17         public override function doAction():uint {
18             var relation:Relation = entity as Relation;
19             if ( relation.isDeleted() )
20                 return NO_CHANGE;
21
22             effects = new CompositeUndoableAction("Delete refs");
23                         relation.removeFromParents(effects.push);
24                         oldMemberList = memberList.slice();
25                         for each (var member:RelationMember in memberList) {
26                             member.entity.removeParent(relation);
27                         }
28                         memberList.splice(0, memberList.length);
29                         effects.doAction();
30                         setDeleted(true);
31                         markDirty();
32             relation.dispatchEvent(new EntityEvent(Connection.RELATION_DELETED, relation));
33
34             return SUCCESS;
35         }
36             
37         public override function undoAction():uint {
38             var relation:Relation = entity as Relation;
39             setDeleted(false);
40             markClean();
41             relation.dispatchEvent(new EntityEvent(Connection.NEW_RELATION, relation));
42             effects.undoAction();
43             for each(var member:RelationMember in oldMemberList) {
44                 memberList.push(member);
45                 relation.dispatchEvent(new RelationMemberEvent(
46                         Connection.RELATION_MEMBER_ADDED, member.entity, relation, 0));
47             }
48             
49             return SUCCESS;
50         }
51     }
52 }
53