da515955a64e88afa1cd64da9c165fc1ee6a512b
[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                                 way.dispatchEvent(new WayNodeEvent(Connection.WAY_NODE_REMOVED, node, way, 0));
30                                 node.removeParent(way);
31                                 if (!node.hasParents) { node.remove(effects.push); }
32                         }
33                         effects.doAction();
34                         setDeleted(true);
35             
36             // see note in DeleteNodeAction
37             if (way.id < 0) {
38               markClean();
39             } else {
40               markDirty();
41             }
42             way.dispatchEvent(new EntityEvent(Connection.WAY_DELETED, way));
43                         way.resume();
44
45             return SUCCESS;
46         }
47             
48         public override function undoAction():uint {
49             var way:Way = entity as Way;
50                         way.suspend();
51             setDeleted(false);
52             if (way.id < 0) {
53               markDirty();
54             } else {
55               markClean();
56             }
57             way.dispatchEvent(new EntityEvent(Connection.NEW_WAY, way));
58             effects.undoAction();
59             for each(var node:Node in oldNodeList) {
60                 nodeList.push(node);
61                 way.dispatchEvent(new WayNodeEvent(Connection.WAY_NODE_ADDED, node, way, 0));
62             }
63             way.resume();
64             return SUCCESS;
65         }
66     }
67 }
68