first stage of undo/redo functionality -- there are still a number of actions that...
[potlatch2.git] / net / systemeD / halcyon / connection / actions / RemoveNodeFromWayAction.as
1 package net.systemeD.halcyon.connection.actions {
2
3     import net.systemeD.halcyon.connection.*;
4     
5     public class RemoveNodeFromWayAction extends UndoableEntityAction {
6         private var node:Node;
7         private var nodeList:Array;
8         private var nodeRemovedFrom:Array;
9         
10         public function RemoveNodeFromWayAction(way:Way, node:Node, nodeList:Array) {
11             super(way, "Remove node "+node.id+" from ");
12             this.node = node;
13             this.nodeList = nodeList;
14         }
15             
16         public override function doAction():uint {
17             nodeRemovedFrom = [];
18                         var i:int;
19                         while ((i=nodeList.indexOf(node))>-1) {
20                                 nodeList.splice(i,1);
21                                 nodeRemovedFrom.push(i);
22                 entity.dispatchEvent(new WayNodeEvent(Connection.WAY_NODE_REMOVED, node, Way(entity), i));
23                         }
24                         
25                         if ( nodeRemovedFrom.length > 0 ) {
26                             node.removeParent(entity);
27                             markDirty();
28                             return SUCCESS;
29                         }
30             
31             return NO_CHANGE;
32         }
33             
34         public override function undoAction():uint {
35             node.addParent(entity);
36             
37             for (var i:int = nodeRemovedFrom.length - 1; i >= 0; i--) {
38                 var index:int = nodeRemovedFrom[i];
39                 trace("node being added "+index+" "+i+" "+node);
40                 nodeList.splice(index, 0, node);
41                 entity.dispatchEvent(new WayNodeEvent(Connection.WAY_NODE_ADDED, node, Way(entity), index));
42             }
43             
44             markClean();
45             
46             return SUCCESS;
47         }
48     }
49 }
50