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

« back to all changes in this revision

Viewing changes to TuxGuitar/src/org/herac/tuxguitar/gui/undo/UndoableManager.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 UndoableManager {
 
17
        private static final int LIMIT = 100;
 
18
        private int indexOfNextAdd;
 
19
        private List edits;
 
20
        
 
21
        public UndoableManager() {
 
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
                try{
 
35
                        edit.undo();
 
36
                }catch(Throwable throwable){
 
37
                        throw new CannotUndoException(throwable);
 
38
                }
 
39
                this.indexOfNextAdd--;
 
40
        }
 
41
        
 
42
        public synchronized void redo() throws CannotRedoException {
 
43
                UndoableEdit edit = editToBeRedone();
 
44
                if (edit == null) {
 
45
                        throw new CannotRedoException();
 
46
                }
 
47
                try{
 
48
                        edit.redo();
 
49
                }catch(Throwable throwable){
 
50
                        throw new CannotRedoException();
 
51
                }
 
52
                this.indexOfNextAdd++;
 
53
        }
 
54
        
 
55
        public synchronized boolean canUndo() {
 
56
                boolean canUndo = false;
 
57
                UndoableEdit edit = editToBeUndone();
 
58
                if (edit != null) {
 
59
                        canUndo = edit.canUndo();
 
60
                }
 
61
                return canUndo;
 
62
        }
 
63
        
 
64
        public synchronized boolean canRedo() {
 
65
                boolean canRedo = false;
 
66
                UndoableEdit edit = editToBeRedone();
 
67
                if (edit != null) {
 
68
                        canRedo = edit.canRedo();
 
69
                }
 
70
                return canRedo;
 
71
        }
 
72
        
 
73
        public synchronized void addEdit(UndoableEdit anEdit) {
 
74
                checkForUnused();
 
75
                checkForLimit();
 
76
                this.edits.add(this.indexOfNextAdd, anEdit);
 
77
                this.indexOfNextAdd++;
 
78
        }
 
79
        
 
80
        private void checkForUnused() {
 
81
                while (this.edits.size() > this.indexOfNextAdd) {
 
82
                        UndoableEdit edit = (UndoableEdit) this.edits.get(this.indexOfNextAdd);
 
83
                        remove(edit);
 
84
                }
 
85
        }
 
86
        
 
87
        private void checkForLimit() {
 
88
                while (this.edits.size() >= LIMIT) {
 
89
                        UndoableEdit edit = (UndoableEdit) this.edits.get(0);
 
90
                        remove(edit);
 
91
                        this.indexOfNextAdd--;
 
92
                }
 
93
        }
 
94
        
 
95
        private void remove(UndoableEdit edit) {
 
96
                this.edits.remove(edit);
 
97
        }
 
98
        
 
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);
 
103
                }
 
104
                return null;
 
105
        }
 
106
        
 
107
        private UndoableEdit editToBeRedone() {
 
108
                int index = this.indexOfNextAdd;
 
109
                if (index >= 0 && index < this.edits.size()) {
 
110
                        return (UndoableEdit) this.edits.get(index);
 
111
                }
 
112
                return null;
 
113
        }
 
114
        
 
115
        private void init() {
 
116
                this.indexOfNextAdd = 0;
 
117
                this.edits = new ArrayList();
 
118
        }
 
119
        
 
120
        private void reset() {
 
121
                this.indexOfNextAdd = 0;
 
122
                this.edits.clear();
 
123
        }
 
124
        
 
125
}