Merge branch 'master' of github.com:systemed/potlatch2
[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, node2:Node;
19                         while ((i=nodeList.indexOf(node))>-1) {
20                                 // remove the node from the way
21                                 nodeList.splice(i,1);
22                                 nodeRemovedFrom.push([node,i]);
23                                 entity.dispatchEvent(new WayNodeEvent(Connection.WAY_NODE_REMOVED, node, Way(entity), i));
24
25                                 // remove any repeated nodes that have occurred as a result (i.e. removing B from ABA)
26                                 while (i>0 && nodeList[i-1]==nodeList[i]) {
27                                         node2=nodeList.splice(i,1)[0];
28                                         nodeRemovedFrom.push([node2,i]);
29                                         entity.dispatchEvent(new WayNodeEvent(Connection.WAY_NODE_REMOVED, node2, Way(entity), i));
30                                 }
31                         }
32                         
33                         if ( nodeRemovedFrom.length > 0 ) {
34                             node.removeParent(entity);
35                             entity.deleted = nodeList.length == 0;
36                             markDirty();
37                             return SUCCESS;
38                         }
39             
40             return NO_CHANGE;
41         }
42             
43         public override function undoAction():uint {
44             node.addParent(entity);
45             
46             for (var i:int = nodeRemovedFrom.length - 1; i >= 0; i--) {
47                 var removal:Array = nodeRemovedFrom[i];
48                 var reinstate:Node = removal[0];
49                 var index:int = removal[1];
50                 nodeList.splice(index, 0, reinstate);
51                 entity.dispatchEvent(new WayNodeEvent(Connection.WAY_NODE_ADDED, reinstate, Way(entity), index));
52             }
53             
54             entity.deleted = nodeList.length == 0;
55             markClean();
56             
57             return SUCCESS;
58         }
59     }
60 }
61