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

« back to all changes in this revision

Viewing changes to src/org/herac/tuxguitar/gui/undo/undoables/custom/UndoableChangeTripletFeel.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
 
 
16
 
public class UndoableChangeTripletFeel implements UndoableEdit{
17
 
        private int doAction;
18
 
        private UndoableCaretHelper undoCaret;
19
 
        private UndoableCaretHelper redoCaret;          
20
 
        private long position;          
21
 
        private int redoableTripletFeel;
22
 
        private int undoableTripletFeel;
23
 
        private List nextTripletFeelPositions;
24
 
        private boolean toEnd;
25
 
        
26
 
        private UndoableChangeTripletFeel(){
27
 
                
28
 
        }
29
 
        
30
 
        public void redo() throws CannotRedoException { 
31
 
                if(!canRedo()){
32
 
                        throw new CannotRedoException();
33
 
                }        
34
 
                TuxGuitar.instance().getSongManager().changeTripletFeel(position,redoableTripletFeel,toEnd);
35
 
                TuxGuitar.instance().fireUpdate();
36
 
                this.redoCaret.update();
37
 
                
38
 
                this.doAction = UNDO_ACTION;
39
 
        }
40
 
 
41
 
        public void undo() throws CannotUndoException {
42
 
                if(!canUndo()){
43
 
                        throw new CannotUndoException();
44
 
                }       
45
 
                SongCoords songCoords = getCaret().getSongCoords();
46
 
                TuxGuitar.instance().getSongManager().changeTripletFeel(position,undoableTripletFeel,this.toEnd);
47
 
                if(this.toEnd){
48
 
                        Iterator it = this.nextTripletFeelPositions.iterator();
49
 
                        while(it.hasNext()){            
50
 
                                TripletFeelPosition tfp = (TripletFeelPosition)it.next();
51
 
                                TuxGuitar.instance().getSongManager().changeTripletFeel(tfp.getPosition(),tfp.getTripletFeel(),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 (doAction == REDO_ACTION);
62
 
    }
63
 
 
64
 
    public boolean canUndo() {
65
 
        return (doAction == UNDO_ACTION);
66
 
    }
67
 
      
68
 
    public static UndoableChangeTripletFeel startUndo(){
69
 
        UndoableChangeTripletFeel undoable = new UndoableChangeTripletFeel();
70
 
        Caret caret = getCaret();               
71
 
        undoable.doAction = UNDO_ACTION;
72
 
        undoable.undoCaret = new UndoableCaretHelper();
73
 
        undoable.position = caret.getPosition();
74
 
        undoable.undoableTripletFeel = caret.getMeasureCoords().getMeasure().getTripletFeel();
75
 
        undoable.nextTripletFeelPositions = new ArrayList();            
76
 
        
77
 
        int prevTripletFeel = undoable.undoableTripletFeel;
78
 
        Iterator it = caret.getSongCoords().getFirstTrack().getMeasuresCoords().iterator();
79
 
        while(it.hasNext()){
80
 
                MeasureCoords measureCoords = (MeasureCoords)it.next();
81
 
                if(measureCoords.getMeasure().getStart() > undoable.position){
82
 
                        int currTripletFeel = measureCoords.getMeasure().getTripletFeel();                           
83
 
                if(prevTripletFeel != currTripletFeel){
84
 
                        TripletFeelPosition tfp = undoable.new TripletFeelPosition(measureCoords.getMeasure().getStart(),currTripletFeel);
85
 
                        undoable.nextTripletFeelPositions.add(tfp);
86
 
                }                       
87
 
                prevTripletFeel = currTripletFeel;
88
 
                }
89
 
        }       
90
 
        return undoable;
91
 
    }
92
 
    
93
 
    public UndoableChangeTripletFeel endUndo(int tripletFeel,boolean toEnd){
94
 
        this.redoCaret = new UndoableCaretHelper();
95
 
        this.redoableTripletFeel = tripletFeel;
96
 
        this.toEnd = toEnd;
97
 
        return this;
98
 
    }
99
 
    
100
 
    private static Caret getCaret(){
101
 
        return TuxGuitar.instance().getTablatureEditor().getTablature().getCaret();
102
 
    }  
103
 
    
104
 
    
105
 
    private class TripletFeelPosition{
106
 
        private long position;
107
 
        private int tripletFeel;
108
 
        
109
 
                public TripletFeelPosition(long position,int tripletFeel) {
110
 
                        this.position = position;
111
 
                        this.tripletFeel = tripletFeel;
112
 
                }
113
 
                public long getPosition() {
114
 
                        return position;
115
 
                }
116
 
                public int getTripletFeel() {
117
 
                        return tripletFeel;
118
 
                }       
119
 
    }
120
 
}