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

« back to all changes in this revision

Viewing changes to src/org/herac/tuxguitar/gui/undo/undoables/measure/UndoableReplaceMeasures.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 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.undo.CannotRedoException;
10
 
import org.herac.tuxguitar.gui.undo.CannotUndoException;
11
 
import org.herac.tuxguitar.gui.undo.UndoableEdit;
12
 
import org.herac.tuxguitar.gui.undo.undoables.UndoableCaretHelper;
13
 
import org.herac.tuxguitar.song.helpers.TracksMeasures;
14
 
import org.herac.tuxguitar.song.models.Marker;
15
 
 
16
 
public class UndoableReplaceMeasures implements UndoableEdit{
17
 
        private int doAction;
18
 
        private UndoableCaretHelper undoCaret;
19
 
        private UndoableCaretHelper redoCaret;  
20
 
        private UndoMarkers undoMarkers;
21
 
        private TracksMeasures undoTrackMeasures;
22
 
        private TracksMeasures redoTrackMeasures;
23
 
        private int count; 
24
 
        private int freeSpace;
25
 
        private long theMove;
26
 
        
27
 
        public UndoableReplaceMeasures(long p1,long p2){
28
 
        Caret caret = getCaret();               
29
 
        this.doAction = UNDO_ACTION;
30
 
        this.undoCaret = new UndoableCaretHelper();                                     
31
 
        this.undoMarkers = new UndoMarkers();
32
 
        this.undoTrackMeasures = TuxGuitar.instance().getSongManager().copyMeasures(p1,p2);
33
 
        }
34
 
        
35
 
        public void redo() throws CannotRedoException { 
36
 
                if(!canRedo()){
37
 
                        throw new CannotRedoException();
38
 
                }   
39
 
               
40
 
        for(int i = freeSpace;i < count;i ++){           
41
 
                TuxGuitar.instance().getSongManager().addNewMeasureBeforeEnd();
42
 
        }               
43
 
        TuxGuitar.instance().getSongManager().replaceMeasures((TracksMeasures)redoTrackMeasures.clone(),theMove);
44
 
                
45
 
                TuxGuitar.instance().fireUpdate();
46
 
                this.redoCaret.update();
47
 
                
48
 
                this.doAction = UNDO_ACTION;
49
 
        }
50
 
 
51
 
        public void undo() throws CannotUndoException {
52
 
                if(!canUndo()){
53
 
                        throw new CannotUndoException();
54
 
                }                                                       
55
 
 
56
 
        for(int i = freeSpace;i < count;i ++){           
57
 
                TuxGuitar.instance().getSongManager().removeLastMeasure();
58
 
        }                               
59
 
        TuxGuitar.instance().getSongManager().replaceMeasures((TracksMeasures)undoTrackMeasures.clone(),0);
60
 
        
61
 
                TuxGuitar.instance().fireUpdate();
62
 
                this.undoMarkers.undo();        
63
 
                this.undoCaret.update();
64
 
                
65
 
                this.doAction = REDO_ACTION;
66
 
        }
67
 
 
68
 
    public boolean canRedo() {
69
 
        return (doAction == REDO_ACTION);
70
 
    }
71
 
 
72
 
    public boolean canUndo() {
73
 
        return (doAction == UNDO_ACTION);
74
 
    }
75
 
 
76
 
    
77
 
    private static Caret getCaret(){
78
 
        return TuxGuitar.instance().getTablatureEditor().getTablature().getCaret();
79
 
    }  
80
 
 
81
 
    public UndoableReplaceMeasures endUndo(TracksMeasures tracksMeasures,int count,int freeSpace,long theMove){
82
 
        this.redoCaret = new UndoableCaretHelper();
83
 
        this.redoTrackMeasures = tracksMeasures;
84
 
        this.count = count;
85
 
        this.freeSpace = freeSpace;
86
 
        this.theMove = theMove;
87
 
        return this;
88
 
    }
89
 
        
90
 
    
91
 
    private class UndoMarkers{
92
 
        private List markers;
93
 
        
94
 
        public UndoMarkers(){
95
 
                this.markers = new ArrayList();                 
96
 
                Iterator it = TuxGuitar.instance().getSongManager().getMarkers().iterator();
97
 
                while(it.hasNext()){
98
 
                        this.markers.add(((Marker)it.next()).clone());
99
 
                }
100
 
        }
101
 
        
102
 
        public void undo(){    
103
 
                TuxGuitar.instance().getSongManager().removeAllMarkers();
104
 
                Iterator it = this.markers.iterator();
105
 
                while(it.hasNext()){
106
 
                        Marker marker = (Marker)it.next();
107
 
                        TuxGuitar.instance().getSongManager().updateMarker((Marker)marker.clone());
108
 
                }               
109
 
        }
110
 
    }    
111
 
}