File: UndoStack.java

package info (click to toggle)
gpsprune 19.2-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 4,516 kB
  • sloc: java: 42,704; sh: 25; makefile: 24; python: 15
file content (62 lines) | stat: -rw-r--r-- 1,341 bytes parent folder | download | duplicates (5)
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
package tim.prune.undo;

import java.util.Stack;

/**
 * Class to hold an undo operation together with a counter
 */
class UndoOpWithState
{
	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<UndoOpWithState>
{
	/** Number of undos (and clears) already performed */
	private int _numUndos = 0;

	@Override
	public void clear()
	{
		_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;
	}
}