Fix issue where removing node B from an ABA way would leave two As. Needs reasonably...
[potlatch2.git] / net / systemeD / halcyon / connection / actions / RemoveNodeByIndexAction.as
1 package net.systemeD.halcyon.connection.actions {
2
3     import net.systemeD.halcyon.connection.*;
4     
5     public class RemoveNodeByIndexAction extends UndoableEntityAction {
6     
7         private var way:Way;
8         private var nodeList:Array;
9         private var index:uint;
10         private var removed:Array;
11         private var fireEvent:Boolean;
12     
13         public function RemoveNodeByIndexAction(way:Way, nodeList:Array, index:uint, fireEvent:Boolean=true) {
14             super(way, "Remove node " + nodeList[index].id + " from position " + index);
15             this.way = way;
16             this.nodeList = nodeList;
17             this.index = index;
18             this.fireEvent = fireEvent;
19         }
20         
21                 public override function doAction():uint {
22                         var preceding:Node=(index>1) ? nodeList[index-1] : null;
23                         var node:Node=nodeList[index];
24                         removed=[];
25
26                         while (nodeList[index]==node || nodeList[index]==preceding) {
27                                 var removedNode:Node=nodeList.splice(index, 1)[0];
28                                 removed.push(removedNode);
29                                 if (nodeList.indexOf(removedNode)==-1) { removedNode.removeParent(way); }
30                                 if (fireEvent) {
31                                    entity.dispatchEvent(new WayNodeEvent(Connection.WAY_NODE_REMOVED, removedNode, way, index));
32                                 }
33                         }
34                         way.deleted = nodeList.length == 0;
35                         markDirty();
36                         return SUCCESS;
37                 }
38                 
39                 public override function undoAction():uint {
40                         for (var i:uint=removed.length-1; i>=0; i--) {
41                                 nodeList.splice(index, 0, removed[i]);
42                                 removed[i].addParent(way);
43                                 if (fireEvent) {
44                                         entity.dispatchEvent(new WayNodeEvent(Connection.WAY_NODE_ADDED, removed[i], way, index));
45                                 }
46                         }
47                         way.deleted = nodeList.length == 0;
48                         markClean();
49                         return SUCCESS;
50         }
51     }
52 }