X-Git-Url: http://gitweb.fperrin.net/?p=GpsPrune.git;a=blobdiff_plain;f=tim%2Fprune%2Fundo%2FUndoStack.java;fp=tim%2Fprune%2Fundo%2FUndoStack.java;h=d3e85e02e3f5d078eb49ac612e084528d34eae1c;hp=bca92b11119114e930bd28a95785e2cc1cff2130;hb=0a2480df5845e2d7190dfdec9b2653b1609e853d;hpb=2154b1969ac2995cca46546f217f53c066b0b749 diff --git a/tim/prune/undo/UndoStack.java b/tim/prune/undo/UndoStack.java index bca92b1..d3e85e0 100644 --- a/tim/prune/undo/UndoStack.java +++ b/tim/prune/undo/UndoStack.java @@ -3,22 +3,60 @@ package tim.prune.undo; import java.util.Stack; /** - * Stack of undo operations - * which also remembers how many times it's been cleared + * Class to hold an undo operation together with a counter */ -public class UndoStack extends Stack +class UndoOpWithState { - private int _numTimesDeleted = 0; - - /** @return number of times this stack has been deleted */ - public int getNumTimesDeleted() { - return _numTimesDeleted; + public UndoOperation _undoOperation = null; + public int _undoCounter = 0; + /** Constructor */ + public UndoOpWithState(UndoOperation inOp, int inCounter) + { + _undoOperation = inOp; + _undoCounter = inCounter; } +} + +/** + * Stack of undo operations + * which also remembers how many undos have been performed + */ +public class UndoStack extends Stack +{ + /** Number of undos (and clears) already performed */ + private int _numUndos = 0; @Override public void clear() { - _numTimesDeleted++; + _numUndos++; super.clear(); } + + /** Add an undo operation to the stack */ + public synchronized boolean add(UndoOperation inOp) + { + return super.add(new UndoOpWithState(inOp, _numUndos)); + } + + /** Pop the latest operation from the stack */ + public synchronized UndoOperation popOperation() + { + _numUndos++; + return super.pop()._undoOperation; + } + + /** Get the operation at the given index */ + public UndoOperation getOperationAt(int inIndex) + { + return super.elementAt(inIndex)._undoOperation; + } + + /** @return number of undos */ + public int getNumUndos() + { + if (isEmpty()) {return 0;} + // Get the number of undos stored by the last operation on the stack + return peek()._undoCounter; + } }