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.managers.TGSongManager;
15
import org.herac.tuxguitar.song.models.TGMeasure;
16
import org.herac.tuxguitar.song.models.TGTimeSignature;
17
import org.herac.tuxguitar.song.models.TGTrack;
19
public class UndoableChangeTimeSignature implements UndoableEdit{
21
private UndoableCaretHelper undoCaret;
22
private UndoableCaretHelper redoCaret;
23
private long position;
24
private TGTimeSignature redoableTimeSignature;
25
private TGTimeSignature undoableTimeSignature;
26
private List nextTimeSignaturePositions;
27
private boolean toEnd;
29
private UndoableChangeTimeSignature(){
33
public void redo() throws CannotRedoException {
35
throw new CannotRedoException();
37
TuxGuitar.instance().getSongManager().changeTimeSignature(this.position,this.redoableTimeSignature.clone(TuxGuitar.instance().getSongManager().getFactory()),this.toEnd);
38
TuxGuitar.instance().fireUpdate();
39
this.redoCaret.update();
41
this.doAction = UNDO_ACTION;
44
public void undo() throws CannotUndoException {
46
throw new CannotUndoException();
49
TuxGuitar.instance().getSongManager().changeTimeSignature(this.position,this.undoableTimeSignature.clone(TuxGuitar.instance().getSongManager().getFactory()),this.toEnd);
52
Iterator it = this.nextTimeSignaturePositions.iterator();
54
TimeSignaturePosition tsp = (TimeSignaturePosition)it.next();
55
TuxGuitar.instance().getSongManager().changeTimeSignature(tsp.getPosition(),tsp.getTimeSignature().clone(TuxGuitar.instance().getSongManager().getFactory()),true);
59
if( shouldRemoveRightBeats() ){
60
removeRightBeats( this.position, this.toEnd );
63
TuxGuitar.instance().fireUpdate();
64
this.undoCaret.update();
66
this.doAction = REDO_ACTION;
69
public boolean canRedo() {
70
return (this.doAction == REDO_ACTION);
73
public boolean canUndo() {
74
return (this.doAction == UNDO_ACTION);
77
public static UndoableChangeTimeSignature startUndo(){
78
UndoableChangeTimeSignature undoable = new UndoableChangeTimeSignature();
79
Caret caret = getCaret();
80
undoable.doAction = UNDO_ACTION;
81
undoable.undoCaret = new UndoableCaretHelper();
82
undoable.position = caret.getPosition();
83
undoable.undoableTimeSignature = caret.getMeasure().getTimeSignature().clone(TuxGuitar.instance().getSongManager().getFactory());
84
undoable.nextTimeSignaturePositions = new ArrayList();
86
TGTimeSignature prevTimeSignature = undoable.undoableTimeSignature;
87
Iterator it = TuxGuitar.instance().getSongManager().getFirstTrack().getMeasures();
89
TGMeasureImpl measure = (TGMeasureImpl)it.next();
90
if(measure.getStart() > undoable.position){
91
TGTimeSignature currTimeSignature = measure.getTimeSignature();
92
int numerator = prevTimeSignature.getNumerator();
93
int value = prevTimeSignature.getDenominator().getValue();
94
int currNumerator = currTimeSignature.getNumerator();
95
int currValue = currTimeSignature.getDenominator().getValue();
96
if(numerator != currNumerator || value != currValue){
97
TimeSignaturePosition tsp = undoable.new TimeSignaturePosition(measure.getStart(),currTimeSignature.clone(TuxGuitar.instance().getSongManager().getFactory()));
98
undoable.nextTimeSignaturePositions.add(tsp);
100
prevTimeSignature = currTimeSignature;
107
public UndoableChangeTimeSignature endUndo(TGTimeSignature timeSignature,boolean toEnd){
108
this.redoCaret = new UndoableCaretHelper();
109
this.redoableTimeSignature = timeSignature.clone(TuxGuitar.instance().getSongManager().getFactory());
114
private static Caret getCaret(){
115
return TuxGuitar.instance().getTablatureEditor().getTablature().getCaret();
118
private boolean shouldRemoveRightBeats(){
119
long redoableTime = (this.redoableTimeSignature.getNumerator() * this.redoableTimeSignature.getDenominator().getTime());
120
long undoableTime = (this.undoableTimeSignature.getNumerator() * this.undoableTimeSignature.getDenominator().getTime());
121
return (redoableTime > undoableTime);
124
private void removeRightBeats(long position, boolean toEnd){
125
TGSongManager manager = TuxGuitar.instance().getSongManager();
126
int tracks = manager.getSong().countTracks();
127
for( int i = 1 ; i <= tracks ; i ++ ){
128
TGTrack track = manager.getTrack( i );
129
TGMeasure measure = manager.getTrackManager().getMeasureAt(track, position );
131
manager.getMeasureManager().removeBeatsBeforeEnd(measure, ( measure.getStart() + measure.getLength() ) );
134
while( (measure = manager.getTrackManager().getNextMeasure(measure)) != null ){
135
manager.getMeasureManager().removeBeatsBeforeEnd(measure, ( measure.getStart() + measure.getLength() ) );
141
private class TimeSignaturePosition{
142
private long position;
143
private TGTimeSignature timeSignature;
145
public TimeSignaturePosition(long position, TGTimeSignature timeSignature) {
146
this.position = position;
147
this.timeSignature = timeSignature;
149
public long getPosition() {
150
return this.position;
152
public TGTimeSignature getTimeSignature() {
153
return this.timeSignature;