Merge branch 'master' of github.com:systemed/potlatch2
[potlatch2.git] / net / systemeD / halcyon / connection / actions / AddNodeToWayAction.as
1 package net.systemeD.halcyon.connection.actions {
2
3     import net.systemeD.halcyon.connection.*;
4     
5     public class AddNodeToWayAction extends UndoableEntityAction {
6         private var node:Node;
7         private var nodeList:Array;
8         private var index:int;
9         private var firstNode:Node;
10         private var autoDelete:Boolean;         /* automatically delete way when undoing addition of node 2? */
11         
12         public function AddNodeToWayAction(way:Way, node:Node, nodeList:Array, index:int, autoDelete:Boolean=true) {
13             super(way, "Add node "+node.id+" to");
14             this.node = node;
15             this.nodeList = nodeList;
16             this.index = index;
17             this.autoDelete = autoDelete;
18         }
19             
20         public override function doAction():uint {
21             var way:Way = entity as Way;
22
23                         // undelete way if it was deleted before (only happens on redo)
24                         if (way.deleted) {
25                                 way.setDeletedState(false);
26                                 if (!firstNode.hasParentWays) firstNode.connection.unregisterPOI(firstNode);
27                                 firstNode.addParent(way);
28                                 way.connection.dispatchEvent(new EntityEvent(Connection.NEW_WAY, way));
29                         }
30
31                         // add node
32             if ( index == -1 ) index = nodeList.length;
33             node.addParent(way);
34             nodeList.splice(index, 0, node);
35             markDirty();
36                         way.expandBbox(node);
37             way.dispatchEvent(new WayNodeEvent(Connection.WAY_NODE_ADDED, node, way, index));
38             
39             return SUCCESS;
40         }
41             
42         public override function undoAction():uint {
43             var way:Way = entity as Way;
44
45                         // ** FIXME: if the user undoes adding the 2nd node, then we delete the way and create a POI from the
46                         //           one remaining node (see below). _However_, when we delete the way, we also need to remove 
47                         //           it from any relations... and to do that, this needs to be a CompositeUndoableAction.
48                         //           Which it isn't (because we want all the markDirty/markClean stuff). So, for now, we'll
49                         //           simply refuse to undo adding the 2nd node if the way is in any relations. (This should
50                         //           be a vanishingly small case anyway, because usually the AddMemberToRelationAction will
51                         //           have been undone already.)
52                         if (autoDelete && way.length==2 && way.parentRelations.length) return FAIL;
53
54                         // remove node
55             var removed:Array=nodeList.splice(index, 1);
56                         if (nodeList.indexOf(removed[0])==-1) { removed[0].removeParent(way); }
57                         markClean();
58             way.dispatchEvent(new WayNodeEvent(Connection.WAY_NODE_REMOVED, removed[0], way, index));
59             
60                         // delete way if it's now 1-length, and convert the one remaining node to a POI
61                         if (autoDelete && way.length==1) {
62                                 way.setDeletedState(true);
63                                 way.dispatchEvent(new EntityEvent(Connection.WAY_DELETED, way));
64                                 firstNode=way.getNode(0);
65                                 firstNode.removeParent(way);
66                                 if (!firstNode.hasParentWays) firstNode.connection.registerPOI(firstNode);
67                                 MainUndoStack.getGlobalStack().removeLastIfAction(BeginWayAction);
68                         }
69                         return SUCCESS;
70         }
71     }
72 }