~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/UndoableChangeTempo.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.undo.CannotRedoException;
 
9
import org.herac.tuxguitar.gui.undo.CannotUndoException;
 
10
import org.herac.tuxguitar.gui.undo.UndoableEdit;
 
11
import org.herac.tuxguitar.gui.undo.undoables.UndoableCaretHelper;
 
12
import org.herac.tuxguitar.song.models.TGMeasureHeader;
 
13
import org.herac.tuxguitar.song.models.TGTempo;
 
14
 
 
15
public class UndoableChangeTempo implements UndoableEdit{
 
16
        private int doAction;
 
17
        private UndoableCaretHelper undoCaret;
 
18
        private UndoableCaretHelper redoCaret;
 
19
        private List undoableTempos;
 
20
        private List redoableTempos;
 
21
        
 
22
        private UndoableChangeTempo(){
 
23
                super();
 
24
                this.undoableTempos = new ArrayList();
 
25
                this.redoableTempos = new ArrayList();
 
26
        }
 
27
        
 
28
        public void redo() throws CannotRedoException {
 
29
                if(!canRedo()){
 
30
                        throw new CannotRedoException();
 
31
                }
 
32
                this.setTempos(this.redoableTempos);
 
33
                this.redoCaret.update();
 
34
                
 
35
                this.doAction = UNDO_ACTION;
 
36
        }
 
37
        
 
38
        public void undo() throws CannotUndoException {
 
39
                if(!canUndo()){
 
40
                        throw new CannotUndoException();
 
41
                }
 
42
                this.setTempos(this.undoableTempos);
 
43
                this.undoCaret.update();
 
44
                
 
45
                this.doAction = REDO_ACTION;
 
46
        }
 
47
        
 
48
        public boolean canRedo() {
 
49
                return (this.doAction == REDO_ACTION);
 
50
        }
 
51
        
 
52
        public boolean canUndo() {
 
53
                return (this.doAction == UNDO_ACTION);
 
54
        }
 
55
        
 
56
        public static UndoableChangeTempo startUndo(){
 
57
                UndoableChangeTempo undoable = new UndoableChangeTempo();
 
58
                undoable.doAction = UNDO_ACTION;
 
59
                undoable.undoCaret = new UndoableCaretHelper();
 
60
                undoable.getTempos(undoable.undoableTempos);
 
61
                return undoable;
 
62
        }
 
63
        
 
64
        public UndoableChangeTempo endUndo(){
 
65
                this.redoCaret = new UndoableCaretHelper();
 
66
                this.getTempos(this.redoableTempos);
 
67
                return this;
 
68
        }
 
69
        
 
70
        private void getTempos(List list){
 
71
                Iterator it = TuxGuitar.instance().getSongManager().getSong().getMeasureHeaders();
 
72
                while(it.hasNext()){
 
73
                        TGMeasureHeader header = (TGMeasureHeader)it.next();
 
74
                        list.add(header.getTempo().clone(TuxGuitar.instance().getSongManager().getFactory()));
 
75
                }
 
76
        }
 
77
        
 
78
        private void setTempos(List tempos){
 
79
                int length = tempos.size();
 
80
                if(length != TuxGuitar.instance().getSongManager().getSong().countMeasureHeaders()){
 
81
                        return;
 
82
                }
 
83
                for(int i =0; i < length; i ++){
 
84
                        TGTempo tempo = ((TGTempo)tempos.get(i)).clone(TuxGuitar.instance().getSongManager().getFactory());
 
85
                        TuxGuitar.instance().getSongManager().changeTempo(TuxGuitar.instance().getSongManager().getMeasureHeader(i + 1),tempo);
 
86
                }
 
87
                TuxGuitar.instance().fireUpdate();
 
88
        }
 
89
}