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

« back to all changes in this revision

Viewing changes to src/org/herac/tuxguitar/gui/undo/undoables/measure/UndoableMeasureGeneric.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.measure;
2
 
 
3
 
import org.herac.tuxguitar.gui.TuxGuitar;
4
 
import org.herac.tuxguitar.gui.editors.tab.Caret;
5
 
import org.herac.tuxguitar.gui.editors.tab.SongTrackCoords;
6
 
import org.herac.tuxguitar.gui.undo.CannotRedoException;
7
 
import org.herac.tuxguitar.gui.undo.CannotUndoException;
8
 
import org.herac.tuxguitar.gui.undo.UndoableEdit;
9
 
import org.herac.tuxguitar.gui.undo.undoables.UndoableCaretHelper;
10
 
import org.herac.tuxguitar.song.models.Measure;
11
 
import org.herac.tuxguitar.song.models.MeasureHeader;
12
 
 
13
 
public class UndoableMeasureGeneric implements UndoableEdit{
14
 
        private int doAction;
15
 
        private int trackNumber;
16
 
        private UndoableCaretHelper undoCaret;
17
 
        private UndoableCaretHelper redoCaret;          
18
 
        private Measure undoMeasure;
19
 
        private Measure redoMeasure;
20
 
        
21
 
        private UndoableMeasureGeneric(){
22
 
                
23
 
        }
24
 
        
25
 
        public void redo() throws CannotRedoException { 
26
 
                if(!canRedo()){
27
 
                        throw new CannotRedoException();
28
 
                }        
29
 
                this.replace(this.redoMeasure); 
30
 
                this.redoCaret.update();
31
 
                this.doAction = UNDO_ACTION;
32
 
        }
33
 
 
34
 
        public void undo() throws CannotUndoException {
35
 
                if(!canUndo()){
36
 
                        throw new CannotUndoException();
37
 
                }                       
38
 
                this.replace(this.undoMeasure);         
39
 
                this.undoCaret.update();
40
 
                this.doAction = REDO_ACTION;
41
 
        }
42
 
 
43
 
    public boolean canRedo() {
44
 
        return (doAction == REDO_ACTION);
45
 
    }
46
 
 
47
 
    public boolean canUndo() {
48
 
        return (doAction == UNDO_ACTION);
49
 
    }
50
 
   
51
 
    private void replace(Measure replace){
52
 
                SongTrackCoords track = getCaret().getSongCoords().getTrack(trackNumber);    
53
 
                if(track != null && replace != null){
54
 
                        MeasureHeader header = TuxGuitar.instance().getSongManager().getMeasureHeader(replace.getNumber());
55
 
                        Measure measure = (Measure)replace.clone(header);
56
 
                        measure = TuxGuitar.instance().getSongManager().getTrackManager().replaceMeasure(track.getTrack(),measure);
57
 
                        TuxGuitar.instance().getTablatureEditor().getTablature().getViewLayout().fireUpdate(measure.getNumber(),false);                         
58
 
                }       
59
 
    }
60
 
    
61
 
    public static UndoableMeasureGeneric startUndo(){
62
 
        UndoableMeasureGeneric undoable = new UndoableMeasureGeneric();
63
 
        Caret caret = getCaret();               
64
 
        undoable.doAction = UNDO_ACTION;
65
 
        undoable.trackNumber = caret.getSongTrackCoords().getTrack().getNumber();
66
 
        undoable.undoCaret = new UndoableCaretHelper();         
67
 
        undoable.undoMeasure = (Measure)caret.getMeasureCoords().getMeasure().clone((MeasureHeader)caret.getMeasureCoords().getMeasure().getHeader().clone());                  
68
 
        return undoable;
69
 
    }
70
 
    
71
 
    public UndoableMeasureGeneric endUndo(){    
72
 
        Caret caret = getCaret(); 
73
 
        this.redoCaret = new UndoableCaretHelper();
74
 
        this.redoMeasure = (Measure)caret.getMeasureCoords().getMeasure().clone((MeasureHeader)caret.getMeasureCoords().getMeasure().getHeader().clone());      
75
 
                return this;
76
 
    }
77
 
    
78
 
    
79
 
    private static Caret getCaret(){
80
 
        return TuxGuitar.instance().getTablatureEditor().getTablature().getCaret();
81
 
    }
82
 
   
83
 
}