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.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;
16
public class UndoableChangeKeySignature implements UndoableEdit{
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;
27
private UndoableChangeKeySignature(){
31
public void redo() throws CannotRedoException {
33
throw new CannotRedoException();
35
TuxGuitar.instance().getSongManager().getTrackManager().changeKeySignature(this.track,this.position,this.redoableKeySignature,this.toEnd);
36
TuxGuitar.instance().fireUpdate();
37
this.redoCaret.update();
39
this.doAction = UNDO_ACTION;
42
public void undo() throws CannotUndoException {
44
throw new CannotUndoException();
46
TuxGuitar.instance().getSongManager().getTrackManager().changeKeySignature(this.track,this.position,this.undoableKeySignature,this.toEnd);
48
Iterator it = this.nextKeySignaturePositions.iterator();
50
KeySignaturePosition ksp = (KeySignaturePosition)it.next();
51
TuxGuitar.instance().getSongManager().getTrackManager().changeKeySignature(this.track,ksp.getPosition(),ksp.getKeySignature(),true);
54
TuxGuitar.instance().fireUpdate();
55
this.undoCaret.update();
57
this.doAction = REDO_ACTION;
60
public boolean canRedo() {
61
return (this.doAction == REDO_ACTION);
64
public boolean canUndo() {
65
return (this.doAction == UNDO_ACTION);
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();
78
int prevKeySignature = undoable.undoableKeySignature;
79
Iterator it = caret.getTrack().getMeasures();
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);
88
prevKeySignature = currKeySignature;
95
public UndoableChangeKeySignature endUndo(int keySignature,boolean toEnd){
96
this.redoCaret = new UndoableCaretHelper();
97
this.redoableKeySignature = keySignature;
102
private static Caret getCaret(){
103
return TuxGuitar.instance().getTablatureEditor().getTablature().getCaret();
106
private class KeySignaturePosition{
107
private long position;
108
private int keySignature;
110
public KeySignaturePosition(long position,int keySignature) {
111
this.position = position;
112
this.keySignature = keySignature;
115
public long getPosition() {
116
return this.position;
119
public int getKeySignature() {
120
return this.keySignature;