~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/UndoableChangeClef.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.models.TGTrack;
 
15
 
 
16
public class UndoableChangeClef implements UndoableEdit{
 
17
        private int doAction;
 
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;
 
26
        
 
27
        private UndoableChangeClef(){
 
28
                super();
 
29
        }
 
30
        
 
31
        public void redo() throws CannotRedoException {
 
32
                if(!canRedo()){
 
33
                        throw new CannotRedoException();
 
34
                }
 
35
                TuxGuitar.instance().getSongManager().getTrackManager().changeClef(this.track,this.position,this.redoableClef,this.toEnd);
 
36
                TuxGuitar.instance().fireUpdate();
 
37
                this.redoCaret.update();
 
38
                
 
39
                this.doAction = UNDO_ACTION;
 
40
        }
 
41
        
 
42
        public void undo() throws CannotUndoException {
 
43
                if(!canUndo()){
 
44
                        throw new CannotUndoException();
 
45
                }
 
46
                TuxGuitar.instance().getSongManager().getTrackManager().changeClef(this.track,this.position,this.undoableClef,this.toEnd);
 
47
                if(this.toEnd){
 
48
                        Iterator it = this.nextClefPositions.iterator();
 
49
                        while(it.hasNext()){
 
50
                                ClefPosition ksp = (ClefPosition)it.next();
 
51
                                TuxGuitar.instance().getSongManager().getTrackManager().changeClef(this.track,ksp.getPosition(),ksp.getClef(),true);
 
52
                        }
 
53
                }
 
54
                TuxGuitar.instance().fireUpdate();
 
55
                this.undoCaret.update();
 
56
                
 
57
                this.doAction = REDO_ACTION;
 
58
        }
 
59
        
 
60
        public boolean canRedo() {
 
61
                return (this.doAction == REDO_ACTION);
 
62
        }
 
63
        
 
64
        public boolean canUndo() {
 
65
                return (this.doAction == UNDO_ACTION);
 
66
        }
 
67
        
 
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();
 
77
                
 
78
                int prevClef = undoable.undoableClef;
 
79
                Iterator it = caret.getTrack().getMeasures();
 
80
                while(it.hasNext()){
 
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);
 
87
                                }
 
88
                                prevClef = currClef;
 
89
                        }
 
90
                }
 
91
                
 
92
                return undoable;
 
93
        }
 
94
        
 
95
        public UndoableChangeClef endUndo(int clef,boolean toEnd){
 
96
                this.redoCaret = new UndoableCaretHelper();
 
97
                this.redoableClef = clef;
 
98
                this.toEnd = toEnd;
 
99
                return this;
 
100
        }
 
101
        
 
102
        private static Caret getCaret(){
 
103
                return TuxGuitar.instance().getTablatureEditor().getTablature().getCaret();
 
104
        }
 
105
        
 
106
        private class ClefPosition{
 
107
                private long position;
 
108
                private int clef;
 
109
                
 
110
                public ClefPosition(long position,int clef) {
 
111
                        this.position = position;
 
112
                        this.clef = clef;
 
113
                }
 
114
                
 
115
                public long getPosition() {
 
116
                        return this.position;
 
117
                }
 
118
                
 
119
                public int getClef() {
 
120
                        return this.clef;
 
121
                }
 
122
        }
 
123
}