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 UndoableChangeClef implements UndoableEdit{
18
private UndoableCaretHelper undoCaret;
19
private UndoableCaretHelper redoCaret;
20
private long position;
21
private int redoableClef;
22
private int undoableClef;
23
private List nextClefPositions;
24
private boolean toEnd;
25
private TGTrack track;
27
private UndoableChangeClef(){
31
public void redo() throws CannotRedoException {
33
throw new CannotRedoException();
35
TuxGuitar.instance().getSongManager().getTrackManager().changeClef(this.track,this.position,this.redoableClef,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().changeClef(this.track,this.position,this.undoableClef,this.toEnd);
48
Iterator it = this.nextClefPositions.iterator();
50
ClefPosition ksp = (ClefPosition)it.next();
51
TuxGuitar.instance().getSongManager().getTrackManager().changeClef(this.track,ksp.getPosition(),ksp.getClef(),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 UndoableChangeClef startUndo(){
69
UndoableChangeClef undoable = new UndoableChangeClef();
70
Caret caret = getCaret();
71
undoable.doAction = UNDO_ACTION;
72
undoable.undoCaret = new UndoableCaretHelper();
73
undoable.position = caret.getPosition();
74
undoable.undoableClef = caret.getMeasure().getClef();
75
undoable.track = caret.getTrack();
76
undoable.nextClefPositions = new ArrayList();
78
int prevClef = undoable.undoableClef;
79
Iterator it = caret.getTrack().getMeasures();
81
TGMeasureImpl measure = (TGMeasureImpl)it.next();
82
if(measure.getStart() > undoable.position){
83
int currClef = measure.getClef();
84
if(prevClef != currClef){
85
ClefPosition tsp = undoable.new ClefPosition(measure.getStart(),currClef);
86
undoable.nextClefPositions.add(tsp);
95
public UndoableChangeClef endUndo(int clef,boolean toEnd){
96
this.redoCaret = new UndoableCaretHelper();
97
this.redoableClef = clef;
102
private static Caret getCaret(){
103
return TuxGuitar.instance().getTablatureEditor().getTablature().getCaret();
106
private class ClefPosition{
107
private long position;
110
public ClefPosition(long position,int clef) {
111
this.position = position;
115
public long getPosition() {
116
return this.position;
119
public int getClef() {