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

« back to all changes in this revision

Viewing changes to src/org/herac/tuxguitar/gui/undo/undoables/track/UndoableTrackGeneric.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.track;
2
 
 
3
 
import org.herac.tuxguitar.gui.TuxGuitar;
4
 
import org.herac.tuxguitar.gui.editors.tab.Caret;
5
 
import org.herac.tuxguitar.gui.undo.CannotRedoException;
6
 
import org.herac.tuxguitar.gui.undo.CannotUndoException;
7
 
import org.herac.tuxguitar.gui.undo.UndoableEdit;
8
 
import org.herac.tuxguitar.gui.undo.undoables.UndoableCaretHelper;
9
 
import org.herac.tuxguitar.song.models.SongTrack;
10
 
 
11
 
public class UndoableTrackGeneric implements UndoableEdit{
12
 
        private int doAction;
13
 
        private UndoableCaretHelper undoCaret;
14
 
        private UndoableCaretHelper redoCaret;  
15
 
        private UndoTrack undoTrack;
16
 
        private RedoTrack redoTrack;
17
 
        
18
 
        private UndoableTrackGeneric(){
19
 
                
20
 
        }
21
 
        
22
 
        public void redo() throws CannotRedoException { 
23
 
                if(!canRedo()){
24
 
                        throw new CannotRedoException();
25
 
                }        
26
 
                redoTrack.redo();
27
 
                this.redoCaret.update();
28
 
                this.doAction = UNDO_ACTION;
29
 
        }
30
 
 
31
 
        public void undo() throws CannotUndoException {
32
 
                if(!canUndo()){
33
 
                        throw new CannotUndoException();
34
 
                }                       
35
 
                undoTrack.undo();               
36
 
                this.undoCaret.update();
37
 
                this.doAction = REDO_ACTION;
38
 
        }
39
 
 
40
 
    public boolean canRedo() {
41
 
        return (doAction == REDO_ACTION);
42
 
    }
43
 
 
44
 
    public boolean canUndo() {
45
 
        return (doAction == UNDO_ACTION);
46
 
    }
47
 
   
48
 
    
49
 
    public static UndoableTrackGeneric startUndo(SongTrack track){
50
 
        Caret caret = getCaret(); 
51
 
        UndoableTrackGeneric undoable = new UndoableTrackGeneric();             
52
 
        undoable.doAction = UNDO_ACTION;
53
 
        undoable.undoCaret = new UndoableCaretHelper();
54
 
        undoable.undoTrack = undoable.new UndoTrack(track);     
55
 
        
56
 
        return undoable;
57
 
    }
58
 
    
59
 
    public UndoableTrackGeneric endUndo(SongTrack track){    
60
 
        Caret caret = getCaret(); 
61
 
        this.redoCaret = new UndoableCaretHelper();
62
 
                this.redoTrack = new RedoTrack(track);  
63
 
                return this;
64
 
    }
65
 
    
66
 
    
67
 
    private static Caret getCaret(){
68
 
        return TuxGuitar.instance().getTablatureEditor().getTablature().getCaret();
69
 
    }
70
 
    
71
 
    private class UndoTrack{
72
 
        private SongTrack track;
73
 
        
74
 
        public UndoTrack(SongTrack track){
75
 
                if(track != null){
76
 
                        this.track = (SongTrack)track.clone(TuxGuitar.instance().getSongManager().getMeasureHeaders());
77
 
                }
78
 
        }
79
 
        
80
 
        public void undo(){    
81
 
                if(this.track != null){
82
 
                        TuxGuitar.instance().getSongManager().replaceTrack(track);                      
83
 
                        TuxGuitar.instance().fireUpdate();   
84
 
                        TuxGuitar.instance().getMixer().update();
85
 
                }
86
 
        }
87
 
    }
88
 
    
89
 
    private class RedoTrack{
90
 
        private SongTrack track;
91
 
        
92
 
        public RedoTrack(SongTrack track){
93
 
                if(track != null){
94
 
                        this.track = (SongTrack)track.clone(TuxGuitar.instance().getSongManager().getMeasureHeaders());
95
 
                }
96
 
        }
97
 
        
98
 
        public void redo(){
99
 
                if(this.track != null){
100
 
                        TuxGuitar.instance().getSongManager().replaceTrack(track);                      
101
 
                        TuxGuitar.instance().fireUpdate();
102
 
                        TuxGuitar.instance().getMixer().update();
103
 
                }
104
 
        }
105
 
    }
106
 
}