first stage of undo/redo functionality -- there are still a number of actions that...
[potlatch2.git] / net / systemeD / halcyon / connection / CompositeUndoableAction.as
1 package net.systemeD.halcyon.connection {
2
3     public class CompositeUndoableAction extends UndoableAction {
4         
5         private var name:String;
6         private var actions:Array = [];
7         private var actionsDone:Boolean = false;
8     
9         public function CompositeUndoableAction(name:String) {
10             this.name = name;
11         }
12         
13         public function push(action:UndoableAction):void {
14             actions.push(action);
15         }
16         
17         public override function doAction():uint {
18             if ( actionsDone )
19                 return UndoableAction.FAIL;
20                 
21             var somethingDone:Boolean = false;
22             for ( var i:int = 0; i < actions.length; i++ ) {
23                 var action:UndoableAction = actions[i];
24                 
25                 var result:uint = action.doAction();
26                 if ( result == UndoableAction.NO_CHANGE ) {
27                     // splice this one out as it doesn't do anything
28                     actions.splice(i, 1)
29                     i --;
30                 } else if ( result == UndoableAction.FAIL ) {
31                     undoFrom(i);
32                     return UndoableAction.FAIL;
33                 } else {
34                     somethingDone = true;
35                 }
36             }
37             actionsDone = true;
38             return somethingDone ? UndoableAction.SUCCESS : UndoableAction.NO_CHANGE;
39         }
40         
41         public override function undoAction():uint {
42             if ( !actionsDone )
43                 return UndoableAction.FAIL;
44                 
45             undoFrom(actions.length);
46             return UndoableAction.SUCCESS;
47         }
48         
49         public function undoFrom(index:int):void {
50             for ( var i:int = index - 1; i >= 0; i-- ) {
51                 var action:UndoableAction = actions[i];
52                 trace("going to do "+action);
53                 
54                 action.undoAction();
55             }
56             actionsDone = false;
57         }
58         
59         public function toString():String {
60             var str:String = " {" + actions.join(",") + "}";
61             return name + str;
62         }
63     }
64
65 }
66