~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/UndoableChangeTimeSignature.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.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;
 
18
 
 
19
public class UndoableChangeTimeSignature implements UndoableEdit{
 
20
        private int doAction;
 
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;
 
28
        
 
29
        private UndoableChangeTimeSignature(){
 
30
                super();
 
31
        }
 
32
        
 
33
        public void redo() throws CannotRedoException {
 
34
                if(!canRedo()){
 
35
                        throw new CannotRedoException();
 
36
                }
 
37
                TuxGuitar.instance().getSongManager().changeTimeSignature(this.position,this.redoableTimeSignature.clone(TuxGuitar.instance().getSongManager().getFactory()),this.toEnd);
 
38
                TuxGuitar.instance().fireUpdate();
 
39
                this.redoCaret.update();
 
40
                
 
41
                this.doAction = UNDO_ACTION;
 
42
        }
 
43
        
 
44
        public void undo() throws CannotUndoException {
 
45
                if(!canUndo()){
 
46
                        throw new CannotUndoException();
 
47
                }
 
48
                
 
49
                TuxGuitar.instance().getSongManager().changeTimeSignature(this.position,this.undoableTimeSignature.clone(TuxGuitar.instance().getSongManager().getFactory()),this.toEnd);
 
50
                
 
51
                if(this.toEnd){
 
52
                        Iterator it = this.nextTimeSignaturePositions.iterator();
 
53
                        while(it.hasNext()){
 
54
                                TimeSignaturePosition tsp = (TimeSignaturePosition)it.next();
 
55
                                TuxGuitar.instance().getSongManager().changeTimeSignature(tsp.getPosition(),tsp.getTimeSignature().clone(TuxGuitar.instance().getSongManager().getFactory()),true);
 
56
                        }
 
57
                }
 
58
                
 
59
                if( shouldRemoveRightBeats() ){
 
60
                        removeRightBeats( this.position, this.toEnd );
 
61
                }
 
62
                
 
63
                TuxGuitar.instance().fireUpdate();
 
64
                this.undoCaret.update();
 
65
                
 
66
                this.doAction = REDO_ACTION;
 
67
        }
 
68
        
 
69
        public boolean canRedo() {
 
70
                return (this.doAction == REDO_ACTION);
 
71
        }
 
72
        
 
73
        public boolean canUndo() {
 
74
                return (this.doAction == UNDO_ACTION);
 
75
        }
 
76
        
 
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();
 
85
                
 
86
                TGTimeSignature prevTimeSignature = undoable.undoableTimeSignature;
 
87
                Iterator it = TuxGuitar.instance().getSongManager().getFirstTrack().getMeasures();
 
88
                while(it.hasNext()){
 
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);
 
99
                                }
 
100
                                prevTimeSignature = currTimeSignature;
 
101
                        }
 
102
                }
 
103
                
 
104
                return undoable;
 
105
        }
 
106
        
 
107
        public UndoableChangeTimeSignature endUndo(TGTimeSignature timeSignature,boolean toEnd){
 
108
                this.redoCaret = new UndoableCaretHelper();
 
109
                this.redoableTimeSignature = timeSignature.clone(TuxGuitar.instance().getSongManager().getFactory());
 
110
                this.toEnd = toEnd;
 
111
                return this;
 
112
        }
 
113
        
 
114
        private static Caret getCaret(){
 
115
                return TuxGuitar.instance().getTablatureEditor().getTablature().getCaret();
 
116
        }
 
117
        
 
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);
 
122
        }
 
123
        
 
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 );
 
130
                        
 
131
                        manager.getMeasureManager().removeBeatsBeforeEnd(measure, ( measure.getStart() + measure.getLength() ) );
 
132
                        
 
133
                        if( toEnd ){
 
134
                                while( (measure = manager.getTrackManager().getNextMeasure(measure)) != null ){
 
135
                                        manager.getMeasureManager().removeBeatsBeforeEnd(measure, ( measure.getStart() + measure.getLength() ) );
 
136
                                }
 
137
                        }
 
138
                }
 
139
        }
 
140
        
 
141
        private class TimeSignaturePosition{
 
142
                private long position;
 
143
                private TGTimeSignature timeSignature;
 
144
                
 
145
                public TimeSignaturePosition(long position, TGTimeSignature timeSignature) {
 
146
                        this.position = position;
 
147
                        this.timeSignature = timeSignature;
 
148
                }
 
149
                public long getPosition() {
 
150
                        return this.position;
 
151
                }
 
152
                public TGTimeSignature getTimeSignature() {
 
153
                        return this.timeSignature;
 
154
                }
 
155
        }
 
156
}