Make splitway reversible. TODO - fix relations bug
[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 function clearActions():void {
18             actions = [];
19         }
20         
21         public override function doAction():uint {
22             if ( actionsDone )
23                 return UndoableAction.FAIL;
24                 
25             var somethingDone:Boolean = false;
26             for ( var i:int = 0; i < actions.length; i++ ) {
27                 var action:UndoableAction = actions[i];
28                 
29                 var result:uint = action.doAction();
30                 if ( result == UndoableAction.NO_CHANGE ) {
31                     // splice this one out as it doesn't do anything
32                     actions.splice(i, 1)
33                     i --;
34                 } else if ( result == UndoableAction.FAIL ) {
35                     undoFrom(i);
36                     return UndoableAction.FAIL;
37                 } else {
38                     somethingDone = true;
39                 }
40             }
41             actionsDone = true;
42             return somethingDone ? UndoableAction.SUCCESS : UndoableAction.NO_CHANGE;
43         }
44         
45         public override function undoAction():uint {
46             if ( !actionsDone )
47                 return UndoableAction.FAIL;
48                 
49             undoFrom(actions.length);
50             return UndoableAction.SUCCESS;
51         }
52         
53         public function undoFrom(index:int):void {
54             for ( var i:int = index - 1; i >= 0; i-- ) {
55                 var action:UndoableAction = actions[i];
56                 trace("going to do "+action);
57                 
58                 action.undoAction();
59             }
60             actionsDone = false;
61         }
62         
63         public function toString():String {
64             var str:String = " {" + actions.join(",") + "}";
65             return name + str;
66         }
67     }
68
69 }
70