1
package org.herac.tuxguitar.gui.undo.undoables.custom;
3
import java.util.ArrayList;
4
import java.util.Iterator;
7
import org.herac.tuxguitar.gui.TuxGuitar;
8
import org.herac.tuxguitar.gui.editors.tab.Caret;
9
import org.herac.tuxguitar.gui.editors.tab.MeasureCoords;
10
import org.herac.tuxguitar.gui.editors.tab.SongCoords;
11
import org.herac.tuxguitar.gui.undo.CannotRedoException;
12
import org.herac.tuxguitar.gui.undo.CannotUndoException;
13
import org.herac.tuxguitar.gui.undo.UndoableEdit;
14
import org.herac.tuxguitar.gui.undo.undoables.UndoableCaretHelper;
15
import org.herac.tuxguitar.song.models.SongTrack;
17
public class UndoableChangeKeySignature implements UndoableEdit{
19
private UndoableCaretHelper undoCaret;
20
private UndoableCaretHelper redoCaret;
21
private long position;
22
private int redoableKeySignature;
23
private int undoableKeySignature;
24
private List nextKeySignaturePositions;
25
private boolean toEnd;
26
private SongTrack track;
28
private UndoableChangeKeySignature(){
32
public void redo() throws CannotRedoException {
34
throw new CannotRedoException();
36
TuxGuitar.instance().getSongManager().getTrackManager().changeKeySignature(track,position,redoableKeySignature,toEnd);
37
TuxGuitar.instance().fireUpdate();
38
this.redoCaret.update();
40
this.doAction = UNDO_ACTION;
43
public void undo() throws CannotUndoException {
45
throw new CannotUndoException();
47
SongCoords songCoords = getCaret().getSongCoords();
48
TuxGuitar.instance().getSongManager().getTrackManager().changeKeySignature(track,position,undoableKeySignature,this.toEnd);
50
Iterator it = this.nextKeySignaturePositions.iterator();
52
KeySignaturePosition ksp = (KeySignaturePosition)it.next();
53
TuxGuitar.instance().getSongManager().getTrackManager().changeKeySignature(track,ksp.getPosition(),ksp.getKeySignature(),true);
56
TuxGuitar.instance().fireUpdate();
57
this.undoCaret.update();
59
this.doAction = REDO_ACTION;
62
public boolean canRedo() {
63
return (doAction == REDO_ACTION);
66
public boolean canUndo() {
67
return (doAction == UNDO_ACTION);
70
public static UndoableChangeKeySignature startUndo(){
71
UndoableChangeKeySignature undoable = new UndoableChangeKeySignature();
72
Caret caret = getCaret();
73
undoable.doAction = UNDO_ACTION;
74
undoable.undoCaret = new UndoableCaretHelper();
75
undoable.position = caret.getPosition();
76
undoable.undoableKeySignature = caret.getMeasureCoords().getMeasure().getKeySignature();
77
undoable.track = caret.getSongTrackCoords().getTrack();
78
undoable.nextKeySignaturePositions = new ArrayList();
80
int prevKeySignature = undoable.undoableKeySignature;
81
Iterator it = caret.getSongTrackCoords().getMeasuresCoords().iterator();
83
MeasureCoords measureCoords = (MeasureCoords)it.next();
84
if(measureCoords.getMeasure().getStart() > undoable.position){
85
int currKeySignature = measureCoords.getMeasure().getKeySignature();
86
if(prevKeySignature != currKeySignature){
87
KeySignaturePosition tsp = undoable.new KeySignaturePosition(measureCoords.getMeasure().getStart(),currKeySignature);
88
undoable.nextKeySignaturePositions.add(tsp);
90
prevKeySignature = currKeySignature;
97
public UndoableChangeKeySignature endUndo(int keySignature,boolean toEnd){
98
this.redoCaret = new UndoableCaretHelper();
99
this.redoableKeySignature = keySignature;
104
private static Caret getCaret(){
105
return TuxGuitar.instance().getTablatureEditor().getTablature().getCaret();
109
private class KeySignaturePosition{
110
private long position;
111
private int keySignature;
113
public KeySignaturePosition(long position,int keySignature) {
114
this.position = position;
115
this.keySignature = keySignature;
117
public long getPosition() {
120
public int getKeySignature() {