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

« back to all changes in this revision

Viewing changes to src/org/herac/tuxguitar/gui/clipboard/MeasureTransferable.java

  • Committer: Bazaar Package Importer
  • Author(s): Philippe Coval
  • Date: 2007-02-04 01:41:23 UTC
  • Revision ID: james.westby@ubuntu.com-20070204014123-9pv7okph0iaiqkvw
Tags: upstream-0.9.1
ImportĀ upstreamĀ versionĀ 0.9.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Created on 09-dic-2005
 
3
 *
 
4
 * TODO To change the template for this generated file go to
 
5
 * Window - Preferences - Java - Code Style - Code Templates
 
6
 */
 
7
package org.herac.tuxguitar.gui.clipboard;
 
8
 
 
9
import java.util.Iterator;
 
10
 
 
11
import org.herac.tuxguitar.gui.TuxGuitar;
 
12
import org.herac.tuxguitar.gui.editors.TablatureEditor;
 
13
import org.herac.tuxguitar.gui.editors.tab.MeasureCoords;
 
14
import org.herac.tuxguitar.gui.editors.tab.SongCoords;
 
15
import org.herac.tuxguitar.gui.editors.tab.SongTrackCoords;
 
16
import org.herac.tuxguitar.gui.undo.undoables.measure.UndoableInsertMeasure;
 
17
import org.herac.tuxguitar.gui.undo.undoables.measure.UndoableReplaceMeasures;
 
18
import org.herac.tuxguitar.song.helpers.TracksMeasures;
 
19
import org.herac.tuxguitar.song.models.MeasureHeader;
 
20
 
 
21
/**
 
22
 * @author julian
 
23
 * 
 
24
 * TODO To change the template for this generated type comment go to Window - Preferences - Java - Code Style - Code Templates
 
25
 */
 
26
public class MeasureTransferable implements Transferable {
 
27
        public static final int TRANSFER_TYPE_REPLACE = 1;
 
28
        public static final int TRANSFER_TYPE_INSERT = 2;
 
29
        
 
30
    private TablatureEditor tablatureEditor;
 
31
    private TracksMeasures tracksMeasures;
 
32
    private long p1;
 
33
    private long p2;
 
34
    private int transferType;
 
35
 
 
36
    public MeasureTransferable(TablatureEditor tablatureEditor, long p1, long p2,boolean allTracks) {
 
37
        this.tablatureEditor = tablatureEditor;
 
38
        this.transferType = TRANSFER_TYPE_REPLACE;
 
39
        this.p1 = p1;
 
40
        this.p2 = p2;        
 
41
        this.getTransfer(p1, p2,allTracks);
 
42
    }
 
43
 
 
44
    private void getTransfer(long p1, long p2,boolean allTracks) {
 
45
        if(allTracks){
 
46
                this.tracksMeasures = TuxGuitar.instance().getSongManager().copyMeasures(p1,p2);
 
47
        }else{
 
48
                SongTrackCoords trackCoords = tablatureEditor.getTablature().getCaret().getSongTrackCoords();
 
49
                this.tracksMeasures = TuxGuitar.instance().getSongManager().copyMeasures(p1,p2,trackCoords.getTrack());
 
50
        }
 
51
        skipMarkers();
 
52
    }
 
53
    
 
54
    public void insertTransfer() throws CannotInsertTransferException {
 
55
        if(this.transferType == TRANSFER_TYPE_REPLACE){
 
56
                replaceMeasures();
 
57
        }else if(this.transferType == TRANSFER_TYPE_INSERT){
 
58
                insertMeasures();
 
59
        }
 
60
    }
 
61
    
 
62
    public void insertMeasures() throws CannotInsertTransferException {
 
63
        MeasureCoords measureCoords = tablatureEditor.getTablature().getCaret().getMeasureCoords();
 
64
        SongCoords songCoords = tablatureEditor.getTablature().getCaret().getSongCoords();
 
65
        if (measureCoords == null || songCoords == null || this.tracksMeasures.isEmpty()) {
 
66
            throw new CannotInsertTransferException();
 
67
        }
 
68
        //comienza el undoable
 
69
        UndoableInsertMeasure undoable = new UndoableInsertMeasure();
 
70
        
 
71
        int fromNumber = measureCoords.getMeasure().getNumber();
 
72
        long theMove = (measureCoords.getMeasure().getStart() - p1);       
 
73
 
 
74
        TuxGuitar.instance().getSongManager().insertMeasures((TracksMeasures)this.tracksMeasures.clone(),fromNumber,theMove);
 
75
        
 
76
        //termia el undoable
 
77
        tablatureEditor.getUndoManager().addEdit(undoable.endUndo((TracksMeasures)this.tracksMeasures.clone(),this.tracksMeasures.getMeasureHeaders().size(),fromNumber,theMove));          
 
78
    }    
 
79
 
 
80
    
 
81
    
 
82
    public void replaceMeasures() throws CannotInsertTransferException {        
 
83
        MeasureCoords measureCoords = tablatureEditor.getTablature().getCaret().getMeasureCoords();
 
84
        SongTrackCoords trackCoords = tablatureEditor.getTablature().getCaret().getSongTrackCoords();
 
85
        SongCoords songCoords = tablatureEditor.getTablature().getCaret().getSongCoords();
 
86
        if (measureCoords == null || songCoords == null || this.tracksMeasures.isEmpty()) {
 
87
            throw new CannotInsertTransferException();
 
88
        }
 
89
        
 
90
        //si no existen los compases los creo        
 
91
        int count = this.tracksMeasures.getMeasureHeaders().size();
 
92
        int current = measureCoords.getMeasure().getNumber();
 
93
        int freeSpace =  (trackCoords.getMeasuresCoords().size()  - (current - 1));
 
94
        long theMove = (measureCoords.getMeasure().getStart() - p1);
 
95
               
 
96
        //comienza el undoable
 
97
        UndoableReplaceMeasures undoable = new UndoableReplaceMeasures(p1 + theMove,p2 + theMove);          
 
98
        
 
99
        for(int i = freeSpace;i < count;i ++){
 
100
                tablatureEditor.getSongManager().addNewMeasureBeforeEnd();
 
101
        }           
 
102
        tablatureEditor.getSongManager().replaceMeasures((TracksMeasures)this.tracksMeasures.clone(),theMove);
 
103
        
 
104
        //Termina el undoable
 
105
        tablatureEditor.getUndoManager().addEdit(undoable.endUndo((TracksMeasures)this.tracksMeasures.clone(),count,freeSpace,theMove));
 
106
    }        
 
107
    
 
108
    public void setTransferType(int transferType){
 
109
        this.transferType = transferType;
 
110
    }
 
111
 
 
112
    private void skipMarkers(){
 
113
        Iterator it = this.tracksMeasures.getMeasureHeaders().iterator();
 
114
        while(it.hasNext()){
 
115
                MeasureHeader header = (MeasureHeader)it.next();
 
116
                header.setMarker(null);
 
117
        }
 
118
    }
 
119
}
 
 
b'\\ No newline at end of file'