Merge branch 'master' into history
[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                 } else {
34                   if (!node.hasParentWays) node.connection.registerPOI(node);
35                 }
36                         }
37                         effects.doAction();
38                         setDeleted(true);
39             
40             // see note in DeleteNodeAction
41             if (way.id < 0) {
42               markClean();
43             } else {
44               markDirty();
45             }
46                         way.dispatchEvent(new EntityEvent(Connection.WAY_DELETED, way));        // delete WayUI
47                         way.resume();
48
49             return SUCCESS;
50         }
51             
52         public override function undoAction():uint {
53             var way:Way = entity as Way;
54                         way.suspend();
55             setDeleted(false);
56             if (way.id < 0) {
57               markDirty();
58             } else {
59               markClean();
60             }
61             entity.connection.dispatchEvent(new EntityEvent(Connection.NEW_WAY, way));
62             if (effects) effects.undoAction();
63             for each(var node:Node in oldNodeList) {
64                 nodeList.push(node);
65                 node.addParent(way);
66                 node.connection.unregisterPOI(node);
67                 way.dispatchEvent(new WayNodeEvent(Connection.WAY_NODE_ADDED, node, way, 0));
68             }
69                         way.resume();
70             return SUCCESS;
71         }
72     }
73 }
74