~ubuntu-branches/ubuntu/oneiric/tuxguitar/oneiric

« back to all changes in this revision

Viewing changes to src/org/herac/tuxguitar/gui/undo/undoables/custom/UndoableChangeKeySignature.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.MeasureCoords;
10
 
import org.herac.tuxguitar.gui.editors.tab.SongCoords;
11
 
import org.herac.tuxguitar.gui.undo.CannotRedoException;
12
 
import org.herac.tuxguitar.gui.undo.CannotUndoException;
13
 
import org.herac.tuxguitar.gui.undo.UndoableEdit;
14
 
import org.herac.tuxguitar.gui.undo.undoables.UndoableCaretHelper;
15
 
import org.herac.tuxguitar.song.models.SongTrack;
16
 
 
17
 
public class UndoableChangeKeySignature implements UndoableEdit{
18
 
        private int doAction;
19
 
        private UndoableCaretHelper undoCaret;
20
 
        private UndoableCaretHelper redoCaret;          
21
 
        private long position;          
22
 
        private int redoableKeySignature;
23
 
        private int undoableKeySignature;
24
 
        private List nextKeySignaturePositions;
25
 
        private boolean toEnd;
26
 
        private SongTrack track;
27
 
        
28
 
        private UndoableChangeKeySignature(){
29
 
                
30
 
        }
31
 
        
32
 
        public void redo() throws CannotRedoException { 
33
 
                if(!canRedo()){
34
 
                        throw new CannotRedoException();
35
 
                }        
36
 
                TuxGuitar.instance().getSongManager().getTrackManager().changeKeySignature(track,position,redoableKeySignature,toEnd);
37
 
                TuxGuitar.instance().fireUpdate();
38
 
                this.redoCaret.update();
39
 
                
40
 
                this.doAction = UNDO_ACTION;
41
 
        }
42
 
 
43
 
        public void undo() throws CannotUndoException {
44
 
                if(!canUndo()){
45
 
                        throw new CannotUndoException();
46
 
                }       
47
 
                SongCoords songCoords = getCaret().getSongCoords();
48
 
                TuxGuitar.instance().getSongManager().getTrackManager().changeKeySignature(track,position,undoableKeySignature,this.toEnd);
49
 
                if(this.toEnd){
50
 
                        Iterator it = this.nextKeySignaturePositions.iterator();
51
 
                        while(it.hasNext()){            
52
 
                                KeySignaturePosition ksp = (KeySignaturePosition)it.next();
53
 
                                TuxGuitar.instance().getSongManager().getTrackManager().changeKeySignature(track,ksp.getPosition(),ksp.getKeySignature(),true);
54
 
                        }
55
 
                }
56
 
                TuxGuitar.instance().fireUpdate();
57
 
                this.undoCaret.update();
58
 
                
59
 
                this.doAction = REDO_ACTION;
60
 
        }
61
 
 
62
 
    public boolean canRedo() {
63
 
        return (doAction == REDO_ACTION);
64
 
    }
65
 
 
66
 
    public boolean canUndo() {
67
 
        return (doAction == UNDO_ACTION);
68
 
    }
69
 
      
70
 
    public static UndoableChangeKeySignature startUndo(){
71
 
        UndoableChangeKeySignature undoable = new UndoableChangeKeySignature();
72
 
        Caret caret = getCaret();               
73
 
        undoable.doAction = UNDO_ACTION;
74
 
        undoable.undoCaret = new UndoableCaretHelper();
75
 
        undoable.position = caret.getPosition();
76
 
        undoable.undoableKeySignature = caret.getMeasureCoords().getMeasure().getKeySignature();
77
 
        undoable.track = caret.getSongTrackCoords().getTrack();
78
 
        undoable.nextKeySignaturePositions = new ArrayList();           
79
 
        
80
 
        int prevKeySignature = undoable.undoableKeySignature;
81
 
        Iterator it = caret.getSongTrackCoords().getMeasuresCoords().iterator();
82
 
        while(it.hasNext()){
83
 
                MeasureCoords measureCoords = (MeasureCoords)it.next();
84
 
                if(measureCoords.getMeasure().getStart() > undoable.position){
85
 
                        int currKeySignature = measureCoords.getMeasure().getKeySignature();            
86
 
                if(prevKeySignature != currKeySignature){
87
 
                        KeySignaturePosition tsp = undoable.new KeySignaturePosition(measureCoords.getMeasure().getStart(),currKeySignature);
88
 
                        undoable.nextKeySignaturePositions.add(tsp);
89
 
                }                       
90
 
                prevKeySignature = currKeySignature;
91
 
                }
92
 
        }
93
 
        
94
 
        return undoable;
95
 
    }
96
 
    
97
 
    public UndoableChangeKeySignature endUndo(int keySignature,boolean toEnd){
98
 
        this.redoCaret = new UndoableCaretHelper();
99
 
        this.redoableKeySignature = keySignature;
100
 
        this.toEnd = toEnd;
101
 
        return this;
102
 
    }
103
 
    
104
 
    private static Caret getCaret(){
105
 
        return TuxGuitar.instance().getTablatureEditor().getTablature().getCaret();
106
 
    }  
107
 
    
108
 
    
109
 
    private class KeySignaturePosition{
110
 
        private long position;
111
 
        private int keySignature;
112
 
        
113
 
                public KeySignaturePosition(long position,int keySignature) {
114
 
                        this.position = position;
115
 
                        this.keySignature = keySignature;
116
 
                }
117
 
                public long getPosition() {
118
 
                        return position;
119
 
                }
120
 
                public int getKeySignature() {
121
 
                        return keySignature;
122
 
                }       
123
 
    }
124
 
}