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 UndoableManager {
17
private static final int LIMIT = 100;
18
private int indexOfNextAdd;
21
public UndoableManager() {
25
public void discardAllEdits() {
29
public synchronized void undo() throws CannotUndoException {
30
UndoableEdit edit = editToBeUndone();
32
throw new CannotUndoException();
36
}catch(Throwable throwable){
37
throw new CannotUndoException(throwable);
39
this.indexOfNextAdd--;
42
public synchronized void redo() throws CannotRedoException {
43
UndoableEdit edit = editToBeRedone();
45
throw new CannotRedoException();
49
}catch(Throwable throwable){
50
throw new CannotRedoException();
52
this.indexOfNextAdd++;
55
public synchronized boolean canUndo() {
56
boolean canUndo = false;
57
UndoableEdit edit = editToBeUndone();
59
canUndo = edit.canUndo();
64
public synchronized boolean canRedo() {
65
boolean canRedo = false;
66
UndoableEdit edit = editToBeRedone();
68
canRedo = edit.canRedo();
73
public synchronized void addEdit(UndoableEdit anEdit) {
76
this.edits.add(this.indexOfNextAdd, anEdit);
77
this.indexOfNextAdd++;
80
private void checkForUnused() {
81
while (this.edits.size() > this.indexOfNextAdd) {
82
UndoableEdit edit = (UndoableEdit) this.edits.get(this.indexOfNextAdd);
87
private void checkForLimit() {
88
while (this.edits.size() >= LIMIT) {
89
UndoableEdit edit = (UndoableEdit) this.edits.get(0);
91
this.indexOfNextAdd--;
95
private void remove(UndoableEdit edit) {
96
this.edits.remove(edit);
99
private UndoableEdit editToBeUndone() {
100
int index = this.indexOfNextAdd - 1;
101
if (index >= 0 && index < this.edits.size()) {
102
return (UndoableEdit) this.edits.get(index);
107
private UndoableEdit editToBeRedone() {
108
int index = this.indexOfNextAdd;
109
if (index >= 0 && index < this.edits.size()) {
110
return (UndoableEdit) this.edits.get(index);
115
private void init() {
116
this.indexOfNextAdd = 0;
117
this.edits = new ArrayList();
120
private void reset() {
121
this.indexOfNextAdd = 0;