Merge branch 'master' of github.com:systemed/potlatch2
[potlatch2.git] / net / systemeD / halcyon / connection / actions / MoveNodeAction.as
1 package net.systemeD.halcyon.connection.actions {
2
3     import flash.utils.getTimer;
4
5     import net.systemeD.halcyon.connection.*;
6     
7     public class MoveNodeAction extends UndoableEntityAction {
8         private var createTime:uint;
9         private var oldLat:Number;
10         private var oldLon:Number;
11         private var newLat:Number;
12         private var newLon:Number;        
13         private var setLatLon:Function;
14         
15         public function MoveNodeAction(node:Node, newLat:Number, newLon:Number, setLatLon:Function) {
16             super(node, "Move to "+newLon+","+newLat);
17             this.newLat = newLat;
18             this.newLon = newLon;
19             this.setLatLon = setLatLon;
20             createTime = getTimer();
21         }
22             
23         public override function doAction():uint {
24             var node:Node = entity as Node;
25             oldLat = node.lat;
26             oldLon = node.lon;
27             if ( oldLat == newLat && oldLon == newLon )
28                 return NO_CHANGE;
29             
30             setLatLon(newLat, newLon);
31             markDirty();
32             entity.dispatchEvent(new NodeMovedEvent(Connection.NODE_MOVED, node, oldLat, oldLon));
33             
34             return SUCCESS;
35         }
36             
37         public override function undoAction():uint {
38             setLatLon(oldLat, oldLon);
39             markClean();
40             entity.dispatchEvent(new NodeMovedEvent(Connection.NODE_MOVED, Node(entity), newLat, newLon));
41             return SUCCESS;
42         }
43         
44         public override function mergePrevious(prev:UndoableAction):Boolean {
45             if ( !(prev is MoveNodeAction) )
46                 return false;
47                 
48             var prevMove:MoveNodeAction = prev as MoveNodeAction;
49             if ( prevMove.entity == entity && prevMove.createTime + 1000 > createTime ) {
50                 oldLat = prevMove.oldLat;
51                 oldLon = prevMove.oldLon;
52                 return true;
53             }
54             
55             return false;
56         }
57     }
58 }
59