]> gitweb.fperrin.net Git - GpsPrune.git/blob - tim/prune/undo/UndoStack.java
Version 18, July 2015
[GpsPrune.git] / tim / prune / undo / UndoStack.java
1 package tim.prune.undo;
2
3 import java.util.Stack;
4
5 /**
6  * Class to hold an undo operation together with a counter
7  */
8 class UndoOpWithState
9 {
10         public UndoOperation _undoOperation = null;
11         public int           _undoCounter = 0;
12         /** Constructor */
13         public UndoOpWithState(UndoOperation inOp, int inCounter)
14         {
15                 _undoOperation = inOp;
16                 _undoCounter   = inCounter;
17         }
18 }
19
20 /**
21  * Stack of undo operations
22  * which also remembers how many undos have been performed
23  */
24 public class UndoStack extends Stack<UndoOpWithState>
25 {
26         /** Number of undos (and clears) already performed */
27         private int _numUndos = 0;
28
29         @Override
30         public void clear()
31         {
32                 _numUndos++;
33                 super.clear();
34         }
35
36         /** Add an undo operation to the stack */
37         public synchronized boolean add(UndoOperation inOp)
38         {
39                 return super.add(new UndoOpWithState(inOp, _numUndos));
40         }
41
42         /** Pop the latest operation from the stack */
43         public synchronized UndoOperation popOperation()
44         {
45                 _numUndos++;
46                 return super.pop()._undoOperation;
47         }
48
49         /** Get the operation at the given index */
50         public UndoOperation getOperationAt(int inIndex)
51         {
52                 return super.elementAt(inIndex)._undoOperation;
53         }
54
55         /** @return number of undos */
56         public int getNumUndos()
57         {
58                 if (isEmpty()) {return 0;}
59                 // Get the number of undos stored by the last operation on the stack
60                 return peek()._undoCounter;
61         }
62 }