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

« back to all changes in this revision

Viewing changes to TuxGuitar/src/org/herac/tuxguitar/gui/undo/undoables/custom/UndoableChangeKeySignature.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
package org.herac.tuxguitar.gui.undo.undoables.custom;
 
2
 
 
3
import java.util.ArrayList;
 
4
import java.util.Iterator;
 
5
import java.util.List;
 
6
 
 
7
import org.herac.tuxguitar.gui.TuxGuitar;
 
8
import org.herac.tuxguitar.gui.editors.tab.Caret;
 
9
import org.herac.tuxguitar.gui.editors.tab.TGMeasureImpl;
 
10
import org.herac.tuxguitar.gui.undo.CannotRedoException;
 
11
import org.herac.tuxguitar.gui.undo.CannotUndoException;
 
12
import org.herac.tuxguitar.gui.undo.UndoableEdit;
 
13
import org.herac.tuxguitar.gui.undo.undoables.UndoableCaretHelper;
 
14
import org.herac.tuxguitar.song.models.TGTrack;
 
15
 
 
16
public class UndoableChangeKeySignature implements UndoableEdit{
 
17
        private int doAction;
 
18
        private UndoableCaretHelper undoCaret;
 
19
        private UndoableCaretHelper redoCaret;
 
20
        private long position;
 
21
        private int redoableKeySignature;
 
22
        private int undoableKeySignature;
 
23
        private List nextKeySignaturePositions;
 
24
        private boolean toEnd;
 
25
        private TGTrack track;
 
26
        
 
27
        private UndoableChangeKeySignature(){
 
28
                super();
 
29
        }
 
30
        
 
31
        public void redo() throws CannotRedoException {
 
32
                if(!canRedo()){
 
33
                        throw new CannotRedoException();
 
34
                }
 
35
                TuxGuitar.instance().getSongManager().getTrackManager().changeKeySignature(this.track,this.position,this.redoableKeySignature,this.toEnd);
 
36
                TuxGuitar.instance().fireUpdate();
 
37
                this.redoCaret.update();
 
38
                
 
39
                this.doAction = UNDO_ACTION;
 
40
        }
 
41
        
 
42
        public void undo() throws CannotUndoException {
 
43
                if(!canUndo()){
 
44
                        throw new CannotUndoException();
 
45
                }
 
46
                TuxGuitar.instance().getSongManager().getTrackManager().changeKeySignature(this.track,this.position,this.undoableKeySignature,this.toEnd);
 
47
                if(this.toEnd){
 
48
                        Iterator it = this.nextKeySignaturePositions.iterator();
 
49
                        while(it.hasNext()){
 
50
                                KeySignaturePosition ksp = (KeySignaturePosition)it.next();
 
51
                                TuxGuitar.instance().getSongManager().getTrackManager().changeKeySignature(this.track,ksp.getPosition(),ksp.getKeySignature(),true);
 
52
                        }
 
53
                }
 
54
                TuxGuitar.instance().fireUpdate();
 
55
                this.undoCaret.update();
 
56
                
 
57
                this.doAction = REDO_ACTION;
 
58
        }
 
59
        
 
60
        public boolean canRedo() {
 
61
                return (this.doAction == REDO_ACTION);
 
62
        }
 
63
        
 
64
        public boolean canUndo() {
 
65
                return (this.doAction == UNDO_ACTION);
 
66
        }
 
67
        
 
68
        public static UndoableChangeKeySignature startUndo(){
 
69
                UndoableChangeKeySignature undoable = new UndoableChangeKeySignature();
 
70
                Caret caret = getCaret();
 
71
                undoable.doAction = UNDO_ACTION;
 
72
                undoable.undoCaret = new UndoableCaretHelper();
 
73
                undoable.position = caret.getPosition();
 
74
                undoable.undoableKeySignature = caret.getMeasure().getKeySignature();
 
75
                undoable.track = caret.getTrack();
 
76
                undoable.nextKeySignaturePositions = new ArrayList();
 
77
                
 
78
                int prevKeySignature = undoable.undoableKeySignature;
 
79
                Iterator it = caret.getTrack().getMeasures();
 
80
                while(it.hasNext()){
 
81
                        TGMeasureImpl measure = (TGMeasureImpl)it.next();
 
82
                        if(measure.getStart() > undoable.position){
 
83
                                int currKeySignature = measure.getKeySignature();
 
84
                                if(prevKeySignature != currKeySignature){
 
85
                                        KeySignaturePosition tsp = undoable.new KeySignaturePosition(measure.getStart(),currKeySignature);
 
86
                                        undoable.nextKeySignaturePositions.add(tsp);
 
87
                                }
 
88
                                prevKeySignature = currKeySignature;
 
89
                        }
 
90
                }
 
91
                
 
92
                return undoable;
 
93
        }
 
94
        
 
95
        public UndoableChangeKeySignature endUndo(int keySignature,boolean toEnd){
 
96
                this.redoCaret = new UndoableCaretHelper();
 
97
                this.redoableKeySignature = keySignature;
 
98
                this.toEnd = toEnd;
 
99
                return this;
 
100
        }
 
101
        
 
102
        private static Caret getCaret(){
 
103
                return TuxGuitar.instance().getTablatureEditor().getTablature().getCaret();
 
104
        }
 
105
        
 
106
        private class KeySignaturePosition{
 
107
                private long position;
 
108
                private int keySignature;
 
109
                
 
110
                public KeySignaturePosition(long position,int keySignature) {
 
111
                        this.position = position;
 
112
                        this.keySignature = keySignature;
 
113
                }
 
114
                
 
115
                public long getPosition() {
 
116
                        return this.position;
 
117
                }
 
118
                
 
119
                public int getKeySignature() {
 
120
                        return this.keySignature;
 
121
                }
 
122
        }
 
123
}