Prevent single-node ways from being created by undo.
[potlatch2.git] / net / systemeD / halcyon / connection / actions / DeleteWayAction.as
1 package net.systemeD.halcyon.connection.actions {
2
3     import net.systemeD.halcyon.connection.*;
4     
5     public class DeleteWayAction extends UndoableEntityAction {
6         private var setDeleted:Function;
7         private var effects:CompositeUndoableAction;
8         private var nodeList:Array;
9         private var oldNodeList:Array;
10         
11         public function DeleteWayAction(way:Way, setDeleted:Function, nodeList:Array) {
12             super(way, "Delete");
13             this.setDeleted = setDeleted;
14             this.nodeList = nodeList;
15         }
16             
17         public override function doAction():uint {
18             var way:Way = entity as Way;
19             if ( way.isDeleted() )
20                 return NO_CHANGE;
21
22             effects = new CompositeUndoableAction("Delete refs");            
23                         var node:Node;
24                         way.suspend();
25                         way.removeFromParents(effects.push);
26                         oldNodeList = nodeList.slice();
27                         while (nodeList.length > 0) {
28                                 node=nodeList.pop();
29                                 node.removeParent(way);
30                                 way.dispatchEvent(new WayNodeEvent(Connection.WAY_NODE_REMOVED, node, way, 0));
31                 if (!node.hasParents && !node.hasInterestingTags()) { //need to trigger redraw of new POIs?
32                   node.remove(effects.push);
33                 }
34                         }
35                         effects.doAction();
36                         setDeleted(true);
37             
38             // see note in DeleteNodeAction
39             if (way.id < 0) {
40               markClean();
41             } else {
42               markDirty();
43             }
44                         way.dispatchEvent(new EntityEvent(Connection.WAY_DELETED, way));        // delete WayUI
45                         way.resume();
46
47             return SUCCESS;
48         }
49             
50         public override function undoAction():uint {
51             var way:Way = entity as Way;
52                         way.suspend();
53             setDeleted(false);
54             if (way.id < 0) {
55               markDirty();
56             } else {
57               markClean();
58             }
59             entity.connection.dispatchEvent(new EntityEvent(Connection.NEW_WAY, way));
60             if (effects) effects.undoAction();
61             for each(var node:Node in oldNodeList) {
62                 nodeList.push(node);
63                 node.addParent(way);
64                 way.dispatchEvent(new WayNodeEvent(Connection.WAY_NODE_ADDED, node, way, 0));
65             }
66                         way.resume();
67             return SUCCESS;
68         }
69     }
70 }
71