~ubuntu-branches/ubuntu/oneiric/tuxguitar/oneiric

« back to all changes in this revision

Viewing changes to src/org/herac/tuxguitar/gui/undo/UndoManager.java

  • Committer: Bazaar Package Importer
  • Author(s): Philippe Coval
  • Date: 2008-06-19 00:30:30 UTC
  • mto: (5.1.2 sid)
  • mto: This revision was merged to the branch mainline in revision 3.
  • Revision ID: james.westby@ubuntu.com-20080619003030-h719szrhsngou7c6
Tags: upstream-1.0
ImportĀ upstreamĀ versionĀ 1.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 * Created on 08-ago-2005
3
 
 *
4
 
 * TODO To change the template for this generated file go to
5
 
 * Window - Preferences - Java - Code Style - Code Templates
6
 
 */
7
 
package org.herac.tuxguitar.gui.undo;
8
 
 
9
 
import java.util.ArrayList;
10
 
import java.util.List;
11
 
/**
12
 
 * @author julian
13
 
 * 
14
 
 * TODO To change the template for this generated type comment go to Window - Preferences - Java - Code Style - Code Templates
15
 
 */
16
 
public class UndoManager {
17
 
    private static final int LIMIT = 100;
18
 
    private int indexOfNextAdd;
19
 
    private List edits;
20
 
    
21
 
    public UndoManager() {
22
 
        this.init();
23
 
    }
24
 
 
25
 
    public void discardAllEdits() {
26
 
        this.reset();
27
 
    }
28
 
 
29
 
    public synchronized void undo() throws CannotUndoException {
30
 
        UndoableEdit edit = editToBeUndone();
31
 
        if (edit == null) {
32
 
            throw new CannotUndoException();
33
 
        }
34
 
        edit.undo();
35
 
        indexOfNextAdd--;
36
 
    }
37
 
 
38
 
    public synchronized void redo() throws CannotRedoException {
39
 
        UndoableEdit edit = editToBeRedone();
40
 
        if (edit == null) {
41
 
            throw new CannotRedoException();
42
 
        }
43
 
        edit.redo();
44
 
        indexOfNextAdd++;
45
 
    }
46
 
 
47
 
    public synchronized boolean canUndo() {
48
 
        boolean canUndo = false;
49
 
        UndoableEdit edit = editToBeUndone();
50
 
        if (edit != null) {
51
 
            canUndo = edit.canUndo();
52
 
        }
53
 
        return canUndo;
54
 
    }
55
 
 
56
 
    public synchronized boolean canRedo() {
57
 
        boolean canRedo = false;
58
 
        UndoableEdit edit = editToBeRedone();
59
 
        if (edit != null) {
60
 
            canRedo = edit.canRedo();
61
 
        }
62
 
        return canRedo;
63
 
    }
64
 
 
65
 
    public synchronized void addEdit(UndoableEdit anEdit) {
66
 
        checkForUnused();
67
 
        checkForLimit();
68
 
        edits.add(indexOfNextAdd, anEdit);
69
 
        indexOfNextAdd++;
70
 
    }
71
 
 
72
 
    public boolean shift() {                
73
 
        if(!edits.isEmpty()){
74
 
            UndoableEdit edit = (UndoableEdit) edits.get(0);            
75
 
            remove(edit);
76
 
            indexOfNextAdd--;
77
 
            return true;
78
 
        }        
79
 
        return false;
80
 
    }    
81
 
 
82
 
 
83
 
    private void checkForUnused() {
84
 
        while (edits.size() > indexOfNextAdd) {
85
 
            UndoableEdit edit = (UndoableEdit) edits.get(indexOfNextAdd);
86
 
            remove(edit);
87
 
        }
88
 
    }
89
 
 
90
 
    
91
 
    private void checkForLimit() {
92
 
        while (edits.size() >= LIMIT) {
93
 
            UndoableEdit edit = (UndoableEdit) edits.get(0);
94
 
            remove(edit);
95
 
            indexOfNextAdd--;
96
 
        }
97
 
    }
98
 
 
99
 
    private void remove(UndoableEdit edit) {
100
 
        edits.remove(edit);
101
 
    }
102
 
 
103
 
    private UndoableEdit editToBeUndone() {
104
 
        int index = indexOfNextAdd - 1;
105
 
        if (index >= 0 && index < edits.size()) {
106
 
            return (UndoableEdit) edits.get(index);
107
 
        }
108
 
        return null;
109
 
    }
110
 
 
111
 
    private UndoableEdit editToBeRedone() {
112
 
        int index = indexOfNextAdd;
113
 
        if (index >= 0 && index < edits.size()) {
114
 
            return (UndoableEdit) edits.get(index);
115
 
        }
116
 
        return null;
117
 
    }
118
 
 
119
 
    private void init() {
120
 
        this.indexOfNextAdd = 0;
121
 
        this.edits = new ArrayList();        
122
 
    }    
123
 
    
124
 
    private void reset() {
125
 
        this.indexOfNextAdd = 0;
126
 
        this.edits.clear();                
127
 
    }
128
 
 
129
 
}
130