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

« back to all changes in this revision

Viewing changes to src/org/herac/tuxguitar/gui/actions/measure/CopyMeasureAction.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
 
/*
2
 
 * Created on 17-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.actions.measure;
8
 
 
9
 
import org.eclipse.swt.SWT;
10
 
import org.eclipse.swt.events.SelectionAdapter;
11
 
import org.eclipse.swt.events.SelectionEvent;
12
 
import org.eclipse.swt.events.TypedEvent;
13
 
import org.eclipse.swt.layout.GridData;
14
 
import org.eclipse.swt.layout.GridLayout;
15
 
import org.eclipse.swt.widgets.Button;
16
 
import org.eclipse.swt.widgets.Composite;
17
 
import org.eclipse.swt.widgets.Label;
18
 
import org.eclipse.swt.widgets.Shell;
19
 
import org.eclipse.swt.widgets.Spinner;
20
 
import org.herac.tuxguitar.gui.TuxGuitar;
21
 
import org.herac.tuxguitar.gui.actions.Action;
22
 
import org.herac.tuxguitar.gui.clipboard.MeasureTransferable;
23
 
import org.herac.tuxguitar.gui.editors.TablatureEditor;
24
 
import org.herac.tuxguitar.gui.editors.tab.Caret;
25
 
import org.herac.tuxguitar.gui.editors.tab.MeasureCoords;
26
 
import org.herac.tuxguitar.gui.editors.tab.SongTrackCoords;
27
 
import org.herac.tuxguitar.song.models.Measure;
28
 
 
29
 
/**
30
 
 * @author julian
31
 
 *
32
 
 * TODO To change the template for this generated type comment go to
33
 
 * Window - Preferences - Java - Code Style - Code Templates
34
 
 */
35
 
public class CopyMeasureAction extends Action{
36
 
    public static final String NAME = "action.measure.copy";
37
 
    
38
 
    private boolean copyAllTracks;
39
 
    
40
 
    public CopyMeasureAction(TablatureEditor tablatureEditor) {
41
 
        super(NAME,true,tablatureEditor);       
42
 
    }
43
 
 
44
 
    public boolean doAction(TypedEvent e) {
45
 
        showDialog(getEditor().getTablature().getShell());
46
 
 
47
 
            return true;
48
 
    }
49
 
 
50
 
    public void showDialog(Shell shell) {
51
 
        SongTrackCoords track = getEditor().getTablature().getCaret().getSongTrackCoords();
52
 
        MeasureCoords measure = getEditor().getTablature().getCaret().getMeasureCoords();
53
 
        if (measure != null) {
54
 
            final Shell dialog = new Shell(shell, SWT.DIALOG_TRIM | SWT.APPLICATION_MODAL);  
55
 
            dialog.setLayout(new GridLayout());
56
 
            dialog.setText(TuxGuitar.getProperty("edit.copy"));
57
 
            
58
 
            //----------------------------------------------------------------------
59
 
            Composite range = new Composite(dialog,SWT.NONE);
60
 
            range.setLayout(new GridLayout(4,false));
61
 
            range.setLayoutData(new GridData(SWT.FILL,SWT.FILL,true,true));   
62
 
            
63
 
            int measureCount = getSongManager().countMeasures();
64
 
 
65
 
            Label fromLabel = new Label(range, SWT.NULL);            
66
 
            fromLabel.setText(TuxGuitar.getProperty("edit.from"));                        
67
 
            final Spinner fromSpinner = new Spinner(range, SWT.BORDER);        
68
 
            fromSpinner.setLayoutData(new GridData(26,SWT.DEFAULT));  
69
 
            fromSpinner.setMinimum(1);
70
 
            fromSpinner.setMaximum(measureCount);     
71
 
            fromSpinner.setSelection(measure.getMeasure().getNumber());
72
 
            
73
 
            Label toLabel = new Label(range, SWT.NULL);
74
 
            toLabel.setText(TuxGuitar.getProperty("edit.to"));
75
 
            final Spinner toSpinner = new Spinner(range, SWT.BORDER);                       
76
 
            toSpinner.setLayoutData(new GridData(26,SWT.DEFAULT));      
77
 
            toSpinner.setMinimum(1);
78
 
            toSpinner.setMaximum(measureCount);     
79
 
            toSpinner.setSelection(measure.getMeasure().getNumber());
80
 
            
81
 
            final int minSelection = 1;
82
 
            final int maxSelection = track.getMeasuresCoords().size();
83
 
            
84
 
            
85
 
            fromSpinner.addSelectionListener(new SelectionAdapter() {           
86
 
                                public void widgetSelected(SelectionEvent e) {
87
 
                                        int fromSelection = fromSpinner.getSelection();
88
 
                                        int toSelection = toSpinner.getSelection();
89
 
                                                                                
90
 
                                        if(fromSelection < minSelection){
91
 
                                                fromSpinner.setSelection(minSelection);
92
 
                                        }else if(fromSelection > toSelection){
93
 
                                                fromSpinner.setSelection(toSelection);
94
 
                                        }
95
 
                                }               
96
 
                        });
97
 
            toSpinner.addSelectionListener(new SelectionAdapter() {             
98
 
                                public void widgetSelected(SelectionEvent e) {                                  
99
 
                                        int toSelection = toSpinner.getSelection();
100
 
                                        int fromSelection = fromSpinner.getSelection();
101
 
                                        if(toSelection < fromSelection){
102
 
                                                toSpinner.setSelection(fromSelection);
103
 
                                        }else if(toSelection > maxSelection){
104
 
                                                toSpinner.setSelection(maxSelection);
105
 
                                        }
106
 
                                }               
107
 
                        });
108
 
            //----------------------------------------------------------------------            
109
 
            copyAllTracks = true;
110
 
            if(getEditor().getSongManager().countTracks() > 1){
111
 
                Composite checkComposites = new Composite(dialog,SWT.NONE);            
112
 
                checkComposites.setLayout(new GridLayout());
113
 
                checkComposites.setLayoutData(new GridData(SWT.FILL,SWT.FILL,true,true));                       
114
 
                
115
 
                final Button allTracks = new Button(checkComposites,SWT.CHECK);
116
 
                allTracks.setText(TuxGuitar.getProperty("edit.all-tracks"));
117
 
                allTracks.setSelection(copyAllTracks);
118
 
                allTracks.addSelectionListener(new SelectionAdapter() {
119
 
                                        public void widgetSelected(SelectionEvent e) {
120
 
                                                copyAllTracks = allTracks.getSelection();
121
 
                                        }                               
122
 
                                });
123
 
            }
124
 
            //------------------BUTTONS--------------------------            
125
 
            Composite buttons = new Composite(dialog, SWT.NONE);
126
 
            buttons.setLayout(new GridLayout(2,false));
127
 
            buttons.setLayoutData(new GridData(SWT.END,SWT.FILL,true,true));            
128
 
            
129
 
            GridData data = new GridData(SWT.FILL,SWT.FILL,true,true);    
130
 
            data.minimumWidth = 80;
131
 
            data.minimumHeight = 25;     
132
 
            
133
 
            final Button buttonOK = new Button(buttons, SWT.PUSH);
134
 
            buttonOK.setText(TuxGuitar.getProperty("ok"));
135
 
            buttonOK.setLayoutData(data);
136
 
            buttonOK.addSelectionListener(new SelectionAdapter() {
137
 
                public void widgetSelected(SelectionEvent arg0) {   
138
 
                        copyMeasures(fromSpinner.getSelection(),toSpinner.getSelection(),copyAllTracks);
139
 
                    dialog.dispose();
140
 
                }
141
 
            });
142
 
 
143
 
            Button buttonCancel = new Button(buttons, SWT.PUSH);            
144
 
            buttonCancel.setText(TuxGuitar.getProperty("cancel"));
145
 
            buttonCancel.setLayoutData(data);
146
 
            buttonCancel.addSelectionListener(new SelectionAdapter() {
147
 
                public void widgetSelected(SelectionEvent arg0) {
148
 
                    dialog.dispose();
149
 
                }
150
 
            });
151
 
 
152
 
            dialog.pack();
153
 
            dialog.open();
154
 
 
155
 
            int x = shell.getBounds().x + (shell.getBounds().width - dialog.getSize().x) / 2;
156
 
            int y = shell.getBounds().y + (shell.getBounds().height - dialog.getSize().y) / 2;
157
 
            dialog.setLocation(x, y);
158
 
            
159
 
                while (!dialog.isDisposed()) {
160
 
                if (!dialog.getDisplay().readAndDispatch()) {
161
 
                        dialog.getDisplay().sleep();
162
 
                }
163
 
 
164
 
            }               
165
 
        }
166
 
    }
167
 
    
168
 
    
169
 
    private void copyMeasures(int m1,int m2,boolean allTracks){
170
 
        if(m1 > 0 && m1 <= m2){         
171
 
                Caret caret = getEditor().getTablature().getCaret();
172
 
                Measure measure1 = getSongManager().getTrackManager().getMeasure(caret.getSongTrackCoords().getTrack(),m1);
173
 
                Measure measure2 = getSongManager().getTrackManager().getMeasure(caret.getSongTrackCoords().getTrack(),m2);
174
 
                if(measure1 != null && measure2 != null){
175
 
                long p1 = measure1.getStart();
176
 
                long p2 = measure2.getStart();
177
 
                MeasureTransferable transferable = new MeasureTransferable(getEditor(),p1,p2,allTracks);
178
 
                getEditor().getClipBoard().addTransferable(transferable);               
179
 
                }
180
 
        }
181
 
    }
182
 
}