2
* Created on 08-ago-2005
4
* TODO To change the template for this generated file go to
5
* Window - Preferences - Java - Code Style - Code Templates
7
package org.herac.tuxguitar.gui.undo;
9
import java.util.ArrayList;
10
import java.util.List;
14
* TODO To change the template for this generated type comment go to Window - Preferences - Java - Code Style - Code Templates
16
public class UndoManager {
17
private static final int LIMIT = 100;
18
private int indexOfNextAdd;
21
public UndoManager() {
25
public void discardAllEdits() {
29
public synchronized void undo() throws CannotUndoException {
30
UndoableEdit edit = editToBeUndone();
32
throw new CannotUndoException();
38
public synchronized void redo() throws CannotRedoException {
39
UndoableEdit edit = editToBeRedone();
41
throw new CannotRedoException();
47
public synchronized boolean canUndo() {
48
boolean canUndo = false;
49
UndoableEdit edit = editToBeUndone();
51
canUndo = edit.canUndo();
56
public synchronized boolean canRedo() {
57
boolean canRedo = false;
58
UndoableEdit edit = editToBeRedone();
60
canRedo = edit.canRedo();
65
public synchronized void addEdit(UndoableEdit anEdit) {
68
edits.add(indexOfNextAdd, anEdit);
72
public boolean shift() {
74
UndoableEdit edit = (UndoableEdit) edits.get(0);
83
private void checkForUnused() {
84
while (edits.size() > indexOfNextAdd) {
85
UndoableEdit edit = (UndoableEdit) edits.get(indexOfNextAdd);
91
private void checkForLimit() {
92
while (edits.size() >= LIMIT) {
93
UndoableEdit edit = (UndoableEdit) edits.get(0);
99
private void remove(UndoableEdit edit) {
103
private UndoableEdit editToBeUndone() {
104
int index = indexOfNextAdd - 1;
105
if (index >= 0 && index < edits.size()) {
106
return (UndoableEdit) edits.get(index);
111
private UndoableEdit editToBeRedone() {
112
int index = indexOfNextAdd;
113
if (index >= 0 && index < edits.size()) {
114
return (UndoableEdit) edits.get(index);
119
private void init() {
120
this.indexOfNextAdd = 0;
121
this.edits = new ArrayList();
124
private void reset() {
125
this.indexOfNextAdd = 0;