1 package tim.prune.undo;
3 import java.util.Stack;
6 * Class to hold an undo operation together with a counter
10 public UndoOperation _undoOperation = null;
11 public int _undoCounter = 0;
13 public UndoOpWithState(UndoOperation inOp, int inCounter)
15 _undoOperation = inOp;
16 _undoCounter = inCounter;
21 * Stack of undo operations
22 * which also remembers how many undos have been performed
24 public class UndoStack extends Stack<UndoOpWithState>
26 /** Number of undos (and clears) already performed */
27 private int _numUndos = 0;
36 /** Add an undo operation to the stack */
37 public synchronized boolean add(UndoOperation inOp)
39 return super.add(new UndoOpWithState(inOp, _numUndos));
42 /** Pop the latest operation from the stack */
43 public synchronized UndoOperation popOperation()
46 return super.pop()._undoOperation;
49 /** Get the operation at the given index */
50 public UndoOperation getOperationAt(int inIndex)
52 return super.elementAt(inIndex)._undoOperation;
55 /** @return number of undos */
56 public int getNumUndos()
58 if (isEmpty()) {return 0;}
59 // Get the number of undos stored by the last operation on the stack
60 return peek()._undoCounter;