1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
|
package tim.prune.undo;
import tim.prune.data.Stack;
import java.util.ArrayList;
import java.util.List;
import tim.prune.cmd.Command;
/**
* Class to hold a command together with a counter
*/
class CommandWithState
{
public final Command _command;
public final int _undoCounter;
/** Constructor */
public CommandWithState(Command inCommand, int inCounter)
{
_command = inCommand;
_undoCounter = inCounter;
}
}
/**
* Stack of undo operations
* which also remembers how many undos have been performed
*/
public class UndoStack extends Stack<CommandWithState>
{
/** Number of undos (and clears) already performed */
private int _numUndos = 0;
public synchronized void clear()
{
_numUndos++;
super.clear();
}
/** Add a command to the stack */
public synchronized void add(Command inCommand) {
super.add(new CommandWithState(inCommand, _numUndos));
}
/** Pop the latest command from the stack */
public synchronized Command popCommand()
{
_numUndos++;
return super.pop()._command;
}
/** Get the list of command descriptions, starting with the most recently added */
public List<String> getDescriptions()
{
ArrayList<String> commands = new ArrayList<>();
for (CommandWithState cws : asList()) {
commands.add(cws._command.getDescription());
}
return commands;
}
/** @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;
}
}
|