slash and burn trace statements, and fix areas on vector background layers
[potlatch2.git] / net / systemeD / halcyon / connection / MainUndoStack.as
1 package net.systemeD.halcyon.connection {
2
3     import flash.events.*;
4
5     public class MainUndoStack extends EventDispatcher {
6         private static const GLOBAL_INSTANCE:MainUndoStack = new MainUndoStack();
7         
8         public static function getGlobalStack():MainUndoStack {
9             return GLOBAL_INSTANCE;
10         }
11         
12         private var undoActions:Array = [];
13         private var redoActions:Array = [];
14
15         /*
16          * Performs the action, then puts it on the undo stack.
17          *
18          * If you want to delay execution don't put it on this
19          * stack -- find another one.
20          */
21         public function addAction(action:UndoableAction):void {
22             var result:uint = action.doAction();
23             
24             switch ( result ) {
25             
26             case UndoableAction.FAIL:
27                 throw new Error("Failure performing "+action);
28                 
29             case UndoableAction.NO_CHANGE:
30                 // nothing to do, and don't add to stack
31                 break;
32                 
33             case UndoableAction.SUCCESS:
34             default:
35                 if ( undoActions.length > 0 ) {
36                     var previous:UndoableAction = undoActions[undoActions.length - 1];
37                     var isMerged:Boolean = action.mergePrevious(previous);
38                     if ( isMerged ) {
39                                                 UndoableEntityAction(action).wasDirty = UndoableEntityAction(previous).wasDirty;
40                                                 UndoableEntityAction(action).connectionWasDirty = UndoableEntityAction(previous).connectionWasDirty;
41                         undoActions.pop();
42                                         }
43                 }
44                 undoActions.push(action);
45                 redoActions = [];
46                 dispatchEvent(new Event("new_undo_item"));
47                 dispatchEvent(new Event("new_redo_item"));
48                 break;
49                 
50             }
51         }
52         
53         /*
54          * Call to kill the undo queue -- the user will not be able to undo
55          * anything they previously did after this is called.
56          */
57         public function breakUndo():void {
58             undoActions = [];
59             redoActions = [];
60         }
61         
62         [Bindable(event="new_undo_item")]
63         public function canUndo():Boolean {
64             return undoActions.length > 0;
65         }
66         
67         [Bindable(event="new_redo_item")]
68         public function canRedo():Boolean {
69             return redoActions.length > 0;
70         }
71         
72         public function undo():void {
73                         if (!undoActions.length) { return; }
74             var action:UndoableAction = undoActions.pop();
75             action.undoAction();
76             redoActions.push(action);
77             dispatchEvent(new Event("new_undo_item"));
78             dispatchEvent(new Event("new_redo_item"));
79         }
80         
81         public function redo():void {
82                         if (!redoActions.length) { return; }
83             var action:UndoableAction = redoActions.pop();
84             action.doAction();
85             undoActions.push(action);
86             dispatchEvent(new Event("new_undo_item"));
87             dispatchEvent(new Event("new_redo_item"));
88         }
89        
90     }
91 }