Merge branch 'master' of github.com:systemed/potlatch2
[potlatch2.git] / net / systemeD / halcyon / connection / actions / DeleteNodeAction.as
1 package net.systemeD.halcyon.connection.actions {
2
3     import net.systemeD.halcyon.connection.*;
4     
5     public class DeleteNodeAction extends UndoableEntityAction {
6         private var setDeleted:Function;
7         private var effects:CompositeUndoableAction;
8         
9         public function DeleteNodeAction(node:Node, setDeleted:Function) {
10             super(node, "Delete");
11             this.setDeleted = setDeleted;
12         }
13             
14         public override function doAction():uint {
15             var node:Node = entity as Node;
16             if ( node.isDeleted() )
17                 return NO_CHANGE;
18
19             effects = new CompositeUndoableAction("Delete refs");            
20             node.removeFromParents(effects.push);
21             effects.doAction();
22             setDeleted(true);
23             
24             // The Delete[entity]Action is unusual, since it can be called both to delete an entity, and is also used to undo its creation
25             // (hence preserving the negative id, if the creation is subsequently redone). Normally a deletion would mark the entity dirty, but
26             // if a newly created entity is being deleted, the entity is now clean. 
27             // When the creation is "redone", it's actually an undo on the deletion of the new entity (see below),
28             // and so the connection will need to be considered dirty again. Usually it's an existing object that's deleted and restored,
29             // which would make things clean.
30             // See also CreateEntityAction
31             
32             if (node.id < 0) {
33               markClean();
34             } else {
35               markDirty();
36             }
37             node.dispatchEvent(new EntityEvent(Connection.NODE_DELETED, node)); // delete NodeUI
38             
39             return SUCCESS;
40         }
41             
42         public override function undoAction():uint {
43             var node:Node = entity as Node;
44             setDeleted(false);
45             
46             // See note above
47             if (node.id < 0) {
48               markDirty();
49             } else {
50               markClean();
51             }
52             node.connection.dispatchEvent(new EntityEvent(Connection.NEW_NODE, entity));
53             if ( effects != null )
54                 effects.undoAction();
55             return SUCCESS;
56         }
57     }
58 }
59