2
* Created on 17-dic-2005
4
* TODO To change the template for this generated file go to
5
* Window - Preferences - Java - Code Style - Code Templates
7
package org.herac.tuxguitar.gui.actions.track;
9
import java.util.ArrayList;
10
import java.util.List;
12
import javax.sound.midi.Instrument;
13
import javax.sound.midi.Soundbank;
15
import org.eclipse.swt.SWT;
16
import org.eclipse.swt.events.PaintEvent;
17
import org.eclipse.swt.events.PaintListener;
18
import org.eclipse.swt.events.SelectionAdapter;
19
import org.eclipse.swt.events.SelectionEvent;
20
import org.eclipse.swt.events.TypedEvent;
21
import org.eclipse.swt.graphics.Color;
22
import org.eclipse.swt.graphics.RGB;
23
import org.eclipse.swt.layout.GridData;
24
import org.eclipse.swt.layout.GridLayout;
25
import org.eclipse.swt.widgets.Button;
26
import org.eclipse.swt.widgets.ColorDialog;
27
import org.eclipse.swt.widgets.Combo;
28
import org.eclipse.swt.widgets.Composite;
29
import org.eclipse.swt.widgets.Group;
30
import org.eclipse.swt.widgets.Label;
31
import org.eclipse.swt.widgets.Shell;
32
import org.eclipse.swt.widgets.Spinner;
33
import org.eclipse.swt.widgets.Text;
34
import org.herac.tuxguitar.gui.TuxGuitar;
35
import org.herac.tuxguitar.gui.actions.Action;
36
import org.herac.tuxguitar.gui.editors.TablatureEditor;
37
import org.herac.tuxguitar.gui.editors.tab.SongTrackCoords;
38
import org.herac.tuxguitar.gui.helper.SyncThread;
39
import org.herac.tuxguitar.gui.undo.undoables.JoinedUndoable;
40
import org.herac.tuxguitar.gui.undo.undoables.track.UndoableInfoTrack;
41
import org.herac.tuxguitar.gui.undo.undoables.track.UndoableInstrumentTrack;
42
import org.herac.tuxguitar.gui.undo.undoables.track.UndoableTrackGeneric;
43
import org.herac.tuxguitar.song.models.InstrumentString;
44
import org.herac.tuxguitar.song.models.RGBColor;
45
import org.herac.tuxguitar.song.models.SongTrack;
50
* TODO To change the template for this generated type comment go to Window - Preferences - Java - Code Style - Code Templates
52
public class ChangeTrackPropertiesAction extends Action {
53
public static final String NAME = "action.track.properties";
55
private static final String[] NOTE_NAMES = new String[]{"C","C#","D","D#","E","F","F#","G","G#","A","Bb","B"};
57
//private static final int MINIMUN_WIDTH = 200;
58
//private static final int MINIMUN_HEIGHT = 10;
60
private static final int MINIMUN_LEFT_CONTROLS_WIDTH = 180;
62
private static final int MINIMUN_BUTTON_WIDTH = 80;
63
private static final int MINIMUN_BUTTON_HEIGHT = 25;
65
private static final int MAX_STRINGS = 7;
66
private static final int MIN_STRINGS = 4;
67
private static final int MAX_OCTAVES = 10;
68
private static final int MAX_NOTES = 12;
70
private Text nameText;
71
private RGBColor trackColor;
72
private List tempStrings;
73
private Spinner stringCountSpinner;
74
private Combo[] stringCombos = new Combo[MAX_STRINGS];
75
private Combo offsetCombo;
76
private int stringCount;
77
private Combo instrumentCombo;
78
private Button percussionCheckBox;
80
public ChangeTrackPropertiesAction(TablatureEditor tablatureEditor) {
81
super(NAME,true, tablatureEditor);
84
public boolean doAction(TypedEvent e) {
85
showDialog(getEditor().getTablature().getShell());
89
public void showDialog(Shell shell) {
90
SongTrackCoords trackCoords = getEditor().getTablature().getCaret().getSongTrackCoords();
91
if (trackCoords != null) {
92
this.stringCount = trackCoords.getTrack().getStrings().size();
93
this.trackColor = (RGBColor) trackCoords.getTrack().getColor().clone();
94
this.initTempStrings(trackCoords.getTrack().getStrings());
96
this.dialog = new Shell(shell, SWT.DIALOG_TRIM | SWT.APPLICATION_MODAL);
97
this.dialog.setLayout(new GridLayout());
98
this.dialog.setText(TuxGuitar.getProperty("track.properties"));
100
Composite composite = new Composite(dialog,SWT.NONE);
101
composite.setLayout(new GridLayout(2,false));
102
composite.setLayoutData(new GridData(SWT.FILL,SWT.FILL,true,true));
104
Composite left = new Composite(composite,SWT.NONE);
105
left.setLayout(new GridLayout());
106
left.setLayoutData(new GridData(SWT.FILL,SWT.FILL,true,true));
107
Composite right = new Composite(composite,SWT.NONE);
108
right.setLayout(new GridLayout());
109
right.setLayoutData(new GridData(SWT.FILL,SWT.FILL,true,true));
110
Composite bottom = new Composite(dialog,SWT.NONE);
111
bottom.setLayout(new GridLayout());
112
bottom.setLayoutData(new GridData(SWT.FILL,SWT.FILL,true,true));
115
initTrackInfo(makeGroup(left,1,TuxGuitar.getProperty("track.properties.general")), trackCoords);
118
initInstrumentFields(makeGroup(left,1,TuxGuitar.getProperty("instrument.instrument")), trackCoords);
121
initTunningInfo(makeGroup(right,2,TuxGuitar.getProperty("tunning")), trackCoords);
126
updateTunningGroup(!trackCoords.getTrack().isPercussionTrack());
131
int x = shell.getBounds().x + (shell.getBounds().width - dialog.getSize().x) / 2;
132
int y = shell.getBounds().y + (shell.getBounds().height - dialog.getSize().y) / 2;
133
dialog.setLocation(x, y);
135
while (!dialog.isDisposed()) {
136
if (!dialog.getDisplay().readAndDispatch()) {
137
dialog.getDisplay().sleep();
143
private Group makeGroup(Composite parent,int horizontalSpan,String text){
144
Group group = new Group(parent, SWT.SHADOW_ETCHED_IN);
145
group.setLayoutData(makeGridData(horizontalSpan));
151
private GridData makeGridData(int horizontalSpan){
152
GridData data = new GridData(SWT.FILL,SWT.FILL,true,true);
153
data.horizontalSpan = horizontalSpan;
157
private void initTrackInfo(Composite composite,SongTrackCoords trackCoords) {
158
composite.setLayout(new GridLayout(/*2,false*/));
159
Composite top = new Composite(composite, SWT.NONE);
160
top.setLayout(new GridLayout(/*2,false*/));
161
top.setLayoutData(new GridData(SWT.FILL,SWT.TOP,true,true));
162
Composite bottom = new Composite(composite, SWT.NONE);
163
bottom.setLayout(new GridLayout(/*2,false*/));
164
bottom.setLayoutData(new GridData(SWT.FILL,SWT.BOTTOM,true,true));
166
//-----------------------NAME---------------------------------
167
Label nameLabel = new Label(top, SWT.NONE);
168
nameLabel.setLayoutData(new GridData(SWT.FILL,SWT.CENTER,true,true));
169
nameLabel.setText(TuxGuitar.getProperty("track.name") + ":");
171
this.nameText = new Text(top, SWT.BORDER);
172
nameText.setLayoutData(getAlignmentData(MINIMUN_LEFT_CONTROLS_WIDTH,SWT.FILL));
173
nameText.setText(trackCoords.getTrack().getName());
176
//-----------------------COLOR---------------------------------
177
Label colorLabel = new Label(bottom, SWT.NONE);
178
colorLabel.setText(TuxGuitar.getProperty("track.color") + ":");
179
colorLabel.setLayoutData(new GridData(SWT.FILL,SWT.CENTER,true,true));
181
final Button colorButton = new Button(bottom, SWT.PUSH);
182
colorButton.setLayoutData(getAlignmentData(MINIMUN_LEFT_CONTROLS_WIDTH,SWT.FILL));
183
colorButton.addSelectionListener(new SelectionAdapter() {
184
public void widgetSelected(SelectionEvent event) {
185
ColorDialog dlg = new ColorDialog(dialog);
186
dlg.setRGB(dialog.getDisplay().getSystemColor(SWT.COLOR_BLACK).getRGB());
187
dlg.setText(TuxGuitar.getProperty("choose-color"));
188
RGB rgb = dlg.open();
190
trackColor.setR(rgb.red);
191
trackColor.setG(rgb.green);
192
trackColor.setB(rgb.blue);
193
colorButton.redraw();
197
colorButton.addPaintListener(new PaintListener() {
198
public void paintControl(PaintEvent e) {
199
Color color = new Color(dialog.getDisplay(), trackColor.getR(), trackColor.getG(), trackColor.getB());
200
e.gc.setBackground(color);
201
e.gc.fillRectangle(5,5,colorButton.getSize().x - 10,colorButton.getSize().y - 10);
208
private void initTunningInfo(Composite composite,SongTrackCoords trackCoords) {
209
composite.setLayout(new GridLayout(2,true));
210
initTunningData(composite,trackCoords);
211
initTunningCombos(composite);
214
private void initTunningCombos(Composite parent) {
215
Composite composite = new Composite(parent, SWT.NONE);
216
composite.setLayout(new GridLayout(/*MAX_STRINGS, false*/));
217
composite.setLayoutData(new GridData(SWT.RIGHT,SWT.FILL,true,true));
218
String[] tunningTexts = getAllValueNames();
219
for (int i = 0; i < MAX_STRINGS; i++) {
220
this.stringCombos[i] = new Combo(composite, SWT.DROP_DOWN | SWT.READ_ONLY);
221
this.stringCombos[i].setItems(tunningTexts);
225
private void initTunningData(Composite parent,SongTrackCoords trackCoords) {
226
Composite composite = new Composite(parent, SWT.NONE);
227
composite.setLayout(new GridLayout(/*2, false*/));
228
composite.setLayoutData(new GridData(SWT.FILL,SWT.TOP,true,true));
230
Composite top = new Composite(composite, SWT.NONE);
231
top.setLayout(new GridLayout());
232
top.setLayoutData(new GridData(SWT.FILL,SWT.TOP,true,true));
234
Composite bottom = new Composite(composite, SWT.NONE);
235
bottom.setLayout(new GridLayout());
236
bottom.setLayoutData(new GridData(SWT.FILL,SWT.BOTTOM,true,true));
238
//---------------------------------STRING--------------------------------
239
Label stringCountLabel = new Label(top, SWT.NONE);
240
stringCountLabel.setText(TuxGuitar.getProperty("tunning.strings") + ":");
241
stringCountLabel.setLayoutData(new GridData(SWT.LEFT,SWT.CENTER,true,true));
243
this.stringCountSpinner = new Spinner(top, SWT.BORDER);
244
stringCountSpinner.setLayoutData(getAlignmentData(80,SWT.FILL));
245
stringCountSpinner.setMinimum(MIN_STRINGS);
246
stringCountSpinner.setMaximum(MAX_STRINGS);
247
stringCountSpinner.setSelection(this.stringCount);
248
stringCountSpinner.addSelectionListener(new SelectionAdapter() {
249
public void widgetSelected(SelectionEvent e) {
250
stringCount = stringCountSpinner.getSelection();
252
updateTunningGroup(!percussionCheckBox.getSelection());
257
//---------------------------------OFFSET--------------------------------
258
Label offsetLabel = new Label(bottom, SWT.NONE);
259
offsetLabel.setText(TuxGuitar.getProperty("tunning.offset") + ":");
260
offsetLabel.setLayoutData(new GridData(SWT.LEFT,SWT.CENTER,true,true));
261
//offsetLabel.setLayoutData(new GridData(SWT.RIGHT,SWT.CENTER,true,true));
263
offsetCombo = new Combo(bottom, SWT.DROP_DOWN | SWT.READ_ONLY);
264
offsetCombo.setLayoutData(getAlignmentData(80,SWT.LEFT));
265
//offsetCombo.setLayoutData(new GridData(100,SWT.DEFAULT));
266
for(int i = SongTrack.MIN_OFFSET;i <= SongTrack.MAX_OFFSET;i ++){
267
offsetCombo.add(Integer.toString(i));
268
if(i == trackCoords.getTrack().getOffset()){
269
offsetCombo.select(i - SongTrack.MIN_OFFSET);
274
private GridData getAlignmentData(int minimumWidth,int horizontalAlignment){
275
GridData data = new GridData();
276
data.minimumWidth = minimumWidth;
277
data.horizontalAlignment = horizontalAlignment;
278
data.verticalAlignment = SWT.DEFAULT;
279
data.grabExcessHorizontalSpace = true;
280
data.grabExcessVerticalSpace = true;
284
private void initButtons(final Composite parent) {
285
Composite composite = new Composite(parent, SWT.NONE);
286
composite.setLayout(new GridLayout(2,false));
287
composite.setLayoutData(new GridData(SWT.END,SWT.FILL,true,true));
289
GridData data = new GridData(SWT.FILL,SWT.FILL,true,true);
290
data.minimumWidth = MINIMUN_BUTTON_WIDTH;
291
data.minimumHeight = MINIMUN_BUTTON_HEIGHT;
293
Button buttonOK = new Button(composite, SWT.PUSH);
294
buttonOK.setText(TuxGuitar.getProperty("ok"));
295
buttonOK.setLayoutData(data);
296
buttonOK.addSelectionListener(new SelectionAdapter() {
297
public void widgetSelected(SelectionEvent arg0) {
298
updateTrackProperties();
299
//--------------update and redraw------------------------------------------------------
300
new SyncThread(new Runnable() {
302
if(!TuxGuitar.isDisposed()){
304
TuxGuitar.instance().getMixer().update();
313
Button buttonCancel = new Button(composite, SWT.PUSH);
314
buttonCancel.setText(TuxGuitar.getProperty("cancel"));
315
buttonCancel.setLayoutData(data);
316
buttonCancel.addSelectionListener(new SelectionAdapter() {
317
public void widgetSelected(SelectionEvent arg0) {
324
private void initInstrumentFields(Composite composite,SongTrackCoords trackCoords) {
325
composite.setLayout(new GridLayout(/*2,false*/));
327
Composite top = new Composite(composite, SWT.NONE);
328
top.setLayout(new GridLayout());
329
top.setLayoutData(new GridData(SWT.FILL,SWT.TOP,true,true));
331
Composite bottom = new Composite(composite, SWT.NONE);
332
bottom.setLayout(new GridLayout());
333
bottom.setLayoutData(new GridData(SWT.FILL,SWT.BOTTOM,true,true));
335
Instrument[] instruments = getInstruments();
336
final SongTrack track = trackCoords.getTrack();
337
//------------Instrument Combo-------------------------------------
338
Label instrumentLabel = new Label(top, SWT.NONE);
339
instrumentLabel.setText(TuxGuitar.getProperty("instrument.instrument") + ":");
340
instrumentLabel.setLayoutData(new GridData(SWT.FILL,SWT.CENTER,true,true));
342
this.instrumentCombo = new Combo(top, SWT.DROP_DOWN | SWT.READ_ONLY);
343
this.instrumentCombo.setLayoutData(getAlignmentData(MINIMUN_LEFT_CONTROLS_WIDTH,SWT.FILL));
344
if (instruments != null) {
345
int count = instruments.length;
349
for (int i = 0; i < count; i++) {
350
instruments[i].getName();
351
instrumentCombo.add(instruments[i].getName());
353
instrumentCombo.select(track.getChannel().getInstrument());
355
instrumentCombo.setEnabled(!track.isPercussionTrack() && instruments != null);
357
//--------------------Precusion CheckBox-------------------------------
358
//GridData gridData = new GridData();
359
//gridData.horizontalSpan = 2;
360
this.percussionCheckBox = new Button(bottom, SWT.CHECK);
361
this.percussionCheckBox.setText(TuxGuitar.getProperty("instrument.percussion-track"));
362
this.percussionCheckBox.setLayoutData(new GridData(SWT.FILL,SWT.CENTER,true,true));
363
//percussionCheckBox.setLayoutData(gridData);
364
if (instruments != null) {
365
percussionCheckBox.setSelection(track.isPercussionTrack());
366
percussionCheckBox.addSelectionListener(new SelectionAdapter() {
367
public void widgetSelected(SelectionEvent arg0) {
368
instrumentCombo.setEnabled(!percussionCheckBox.getSelection());
371
updateTunningGroup(!percussionCheckBox.getSelection());
375
percussionCheckBox.setEnabled(false);
377
//-----------------------------------------------------------
380
private Instrument[] getInstruments() {
381
Instrument[] instruments = null;
382
Soundbank sb = TuxGuitar.instance().getPlayer().getSoundbank();
384
instruments = sb.getInstruments();
389
private void updateTrackProperties() {
390
SongTrackCoords trackCoords = getEditor().getTablature().getCaret().getSongTrackCoords();
392
String trackName = this.nameText.getText();
393
RGBColor trakColor = this.trackColor;
395
List strings = new ArrayList();
396
for (int i = 0; i < stringCount; i++) {
397
strings.add(new InstrumentString((i + 1), stringCombos[i].getSelectionIndex()));
400
boolean percussion = percussionCheckBox.getSelection();
401
int offset = ((percussion)?0:SongTrack.MIN_OFFSET + offsetCombo.getSelectionIndex());
402
int instrument = ((instrumentCombo.getSelectionIndex() >= 0)?instrumentCombo.getSelectionIndex():0);
405
boolean infoChanges = hasInfoChanges(trackCoords,trackName,trackColor,offset);
406
boolean tunningChanges = hasTunningChanges(trackCoords,strings);
407
boolean instrumentChanges = hasInstrumentChanges(trackCoords,instrument,percussion);
409
if(infoChanges || tunningChanges || instrumentChanges){
410
JoinedUndoable undoable = new JoinedUndoable();
412
UndoableTrackGeneric undoableGeneric = null;
414
undoableGeneric = UndoableTrackGeneric.startUndo(trackCoords.getTrack());
417
//--------------------------------------info---------------------------------------
419
UndoableInfoTrack undoableInfo = null;
421
undoableInfo = UndoableInfoTrack.startUndo(trackCoords.getTrack());
423
getEditor().getSongManager().getTrackManager().changeInfo(trackCoords.getTrack(),trackName,trackColor,offset);
425
undoable.addUndoableEdit(undoableInfo.endUndo(trackCoords.getTrack()));
428
//--------------------------------------tunning---------------------------------------
430
getEditor().getSongManager().getTrackManager().changeInstrumentStrings(trackCoords.getTrack(),strings);
432
//-----------------------------instrument----------------------------------------------
433
if(instrumentChanges){
434
UndoableInstrumentTrack undoableInstrument = null;
436
undoableInstrument = UndoableInstrumentTrack.startUndo(trackCoords.getTrack());
438
getEditor().getSongManager().getTrackManager().changeInstrument(trackCoords.getTrack(),instrument,percussion);
440
undoable.addUndoableEdit(undoableInstrument.endUndo(trackCoords.getTrack()));
444
undoable.addUndoableEdit(undoableGeneric.endUndo(trackCoords.getTrack()));
446
getEditor().getUndoManager().addEdit(undoable.endUndo());
447
if (TuxGuitar.instance().getPlayer().isRunning()) {
448
TuxGuitar.instance().getPlayer().updatePrograms();
453
private boolean hasInfoChanges(SongTrackCoords trackCoords,String name,RGBColor color,int offset){
454
if(!name.equals(trackCoords.getTrack().getName())){
457
if(!color.isEqual(trackCoords.getTrack().getColor())){
460
if(offset != trackCoords.getTrack().getOffset()){
466
private boolean hasTunningChanges(SongTrackCoords trackCoords,List newStrings){
467
List oldStrings = trackCoords.getTrack().getStrings();
468
//check the number of strings
469
if(oldStrings.size() != newStrings.size()){
472
//check the tunning of strings
473
for(int i = 0;i < oldStrings.size();i++){
474
InstrumentString oldString = (InstrumentString)oldStrings.get(i);
475
boolean stringExists = false;
476
for(int j = 0;j < newStrings.size();j++){
477
InstrumentString newString = (InstrumentString)newStrings.get(j);
478
if(newString.isEqual(oldString)){
489
private boolean hasInstrumentChanges(SongTrackCoords trackCoords,int instrument,boolean percussion){
490
return ((trackCoords.getTrack().getChannel().getInstrument() != instrument) || (trackCoords.getTrack().isPercussionTrack() != percussion));
493
private void updateTunningGroup(boolean enabled) {
495
int selectedIndex = tempStrings.size() - 1;
496
Iterator it = tempStrings.iterator();
497
while (it.hasNext()) {
498
InstrumentString string = (InstrumentString) it.next();
499
stringCombos[selectedIndex].select(string.getValue());
500
stringCombos[selectedIndex].setVisible(true);
501
stringCombos[selectedIndex].setEnabled(enabled);
505
for (int i = 0; i < tempStrings.size(); i++) {
506
InstrumentString string = (InstrumentString)tempStrings.get(i);
507
stringCombos[i].select(string.getValue());
508
stringCombos[i].setVisible(true);
509
stringCombos[i].setEnabled(enabled);
512
for (int i = tempStrings.size(); i < MAX_STRINGS; i++) {
513
stringCombos[i].select(0);
514
stringCombos[i].setVisible(false);
516
offsetCombo.setEnabled(enabled);
519
private void initTempStrings(List realStrings) {
520
this.tempStrings = new ArrayList();
521
for (int i = 0; i < realStrings.size(); i++) {
522
InstrumentString realString = (InstrumentString) realStrings.get(i);
523
tempStrings.add(realString.clone());
527
private void setDefaultTunning() {
528
this.tempStrings.clear();
529
if (percussionCheckBox.getSelection()) {
530
for (int i = 1; i <= stringCount; i++) {
531
this.tempStrings.add(new InstrumentString(i, 0));
535
switch (stringCount) {
537
this.tempStrings.add(new InstrumentString(1, 64));
538
this.tempStrings.add(new InstrumentString(2, 59));
539
this.tempStrings.add(new InstrumentString(3, 55));
540
this.tempStrings.add(new InstrumentString(4, 50));
541
this.tempStrings.add(new InstrumentString(5, 45));
542
this.tempStrings.add(new InstrumentString(6, 40));
543
this.tempStrings.add(new InstrumentString(7, 35));
546
this.tempStrings.add(new InstrumentString(1, 64));
547
this.tempStrings.add(new InstrumentString(2, 59));
548
this.tempStrings.add(new InstrumentString(3, 55));
549
this.tempStrings.add(new InstrumentString(4, 50));
550
this.tempStrings.add(new InstrumentString(5, 45));
551
this.tempStrings.add(new InstrumentString(6, 40));
554
this.tempStrings.add(new InstrumentString(1, 43));
555
this.tempStrings.add(new InstrumentString(2, 38));
556
this.tempStrings.add(new InstrumentString(3, 33));
557
this.tempStrings.add(new InstrumentString(4, 28));
558
this.tempStrings.add(new InstrumentString(5, 23));
561
this.tempStrings.add(new InstrumentString(1, 43));
562
this.tempStrings.add(new InstrumentString(2, 38));
563
this.tempStrings.add(new InstrumentString(3, 33));
564
this.tempStrings.add(new InstrumentString(4, 28));
570
private String[] getAllValueNames() {
571
String[] valueNames = new String[MAX_NOTES * MAX_OCTAVES];
572
for (int i = 0; i < valueNames.length; i++) {
573
valueNames[i] = NOTE_NAMES[ (i - ((i / MAX_NOTES) * MAX_NOTES) ) ] + (i / MAX_NOTES);;
b'\\ No newline at end of file'