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 org.eclipse.swt.SWT;
13
import org.eclipse.swt.events.PaintEvent;
14
import org.eclipse.swt.events.PaintListener;
15
import org.eclipse.swt.events.SelectionAdapter;
16
import org.eclipse.swt.events.SelectionEvent;
17
import org.eclipse.swt.events.TypedEvent;
18
import org.eclipse.swt.graphics.Color;
19
import org.eclipse.swt.graphics.RGB;
20
import org.eclipse.swt.layout.GridData;
21
import org.eclipse.swt.layout.GridLayout;
22
import org.eclipse.swt.widgets.Button;
23
import org.eclipse.swt.widgets.ColorDialog;
24
import org.eclipse.swt.widgets.Combo;
25
import org.eclipse.swt.widgets.Composite;
26
import org.eclipse.swt.widgets.Group;
27
import org.eclipse.swt.widgets.Label;
28
import org.eclipse.swt.widgets.Shell;
29
import org.eclipse.swt.widgets.Spinner;
30
import org.eclipse.swt.widgets.Text;
31
import org.herac.tuxguitar.gui.TuxGuitar;
32
import org.herac.tuxguitar.gui.actions.Action;
33
import org.herac.tuxguitar.gui.editors.TGPainter;
34
import org.herac.tuxguitar.gui.editors.tab.TGTrackImpl;
35
import org.herac.tuxguitar.gui.helper.SyncThread;
36
import org.herac.tuxguitar.gui.undo.undoables.UndoableJoined;
37
import org.herac.tuxguitar.gui.undo.undoables.track.UndoableTrackInfo;
38
import org.herac.tuxguitar.gui.undo.undoables.track.UndoableTrackInstrument;
39
import org.herac.tuxguitar.gui.undo.undoables.track.UndoableTrackGeneric;
40
import org.herac.tuxguitar.gui.util.DialogUtils;
41
import org.herac.tuxguitar.gui.util.TGMusicKeyUtils;
42
import org.herac.tuxguitar.player.base.MidiInstrument;
43
import org.herac.tuxguitar.song.managers.TGSongManager;
44
import org.herac.tuxguitar.song.models.TGColor;
45
import org.herac.tuxguitar.song.models.TGString;
46
import org.herac.tuxguitar.song.models.TGTrack;
51
* TODO To change the template for this generated type comment go to Window - Preferences - Java - Code Style - Code Templates
53
public class TrackPropertiesAction extends Action {
54
public static final String NAME = "action.track.properties";
56
private static final String[] NOTE_NAMES = TGMusicKeyUtils.getSharpKeyNames(TGMusicKeyUtils.PREFIX_TUNING);
57
private static final int MINIMUN_LEFT_CONTROLS_WIDTH = 180;
58
private static final int MINIMUN_BUTTON_WIDTH = 80;
59
private static final int MINIMUN_BUTTON_HEIGHT = 25;
60
private static final int MAX_STRINGS = 7;
61
private static final int MIN_STRINGS = 4;
62
private static final int MAX_OCTAVES = 10;
63
private static final int MAX_NOTES = 12;
65
protected Shell dialog;
66
protected Text nameText;
67
protected TGColor trackColor;
68
protected List tempStrings;
69
protected Spinner stringCountSpinner;
70
protected Combo[] stringCombos = new Combo[MAX_STRINGS];
71
protected Combo offsetCombo;
72
protected int stringCount;
73
protected Combo instrumentCombo;
74
protected Button percussionCheckBox;
76
public TrackPropertiesAction() {
77
super(NAME, AUTO_LOCK | AUTO_UNLOCK | AUTO_UPDATE | DISABLE_ON_PLAYING | KEY_BINDING_AVAILABLE);
80
protected int execute(TypedEvent e){
81
showDialog(getEditor().getTablature().getShell());
85
public void showDialog(Shell shell) {
86
TGTrackImpl track = getEditor().getTablature().getCaret().getTrack();
88
this.stringCount = track.getStrings().size();
89
this.trackColor = track.getColor().clone(getSongManager().getFactory());
90
this.initTempStrings(track.getStrings());
92
this.dialog = DialogUtils.newDialog(shell, SWT.DIALOG_TRIM | SWT.APPLICATION_MODAL);
93
this.dialog.setLayout(new GridLayout(2,false));
94
this.dialog.setText(TuxGuitar.getProperty("track.properties"));
96
Composite left = new Composite(this.dialog,SWT.NONE);
97
left.setLayout(new GridLayout());
98
left.setLayoutData(new GridData(SWT.FILL,SWT.FILL,true,true));
100
Composite right = new Composite(this.dialog,SWT.NONE);
101
right.setLayout(new GridLayout());
102
right.setLayoutData(new GridData(SWT.FILL,SWT.FILL,true,true));
104
Composite bottom = new Composite(this.dialog, SWT.NONE);
105
bottom.setLayout(new GridLayout(2,false));
106
bottom.setLayoutData(new GridData(SWT.END,SWT.FILL,true,true,2,1));
109
initTrackInfo(makeGroup(left,1,TuxGuitar.getProperty("track.properties.general")), track);
112
initInstrumentFields(makeGroup(left,1,TuxGuitar.getProperty("instrument.instrument")), track);
115
initTuningInfo(makeGroup(right,2,TuxGuitar.getProperty("tuning")), track);
120
updateTuningGroup(!track.isPercussionTrack());
122
DialogUtils.openDialog(this.dialog, DialogUtils.OPEN_STYLE_CENTER | DialogUtils.OPEN_STYLE_PACK | DialogUtils.OPEN_STYLE_WAIT);
126
private Group makeGroup(Composite parent,int horizontalSpan,String text){
127
Group group = new Group(parent, SWT.SHADOW_ETCHED_IN);
128
group.setLayoutData(makeGridData(horizontalSpan));
134
private GridData makeGridData(int horizontalSpan){
135
GridData data = new GridData(SWT.FILL,SWT.FILL,true,true);
136
data.horizontalSpan = horizontalSpan;
140
public GridData getButtonsData(){
141
GridData data = new GridData(SWT.FILL,SWT.FILL,true,true);
142
data.minimumWidth = MINIMUN_BUTTON_WIDTH;
143
data.minimumHeight = MINIMUN_BUTTON_HEIGHT;
147
private void initTrackInfo(Composite composite,TGTrackImpl track) {
148
composite.setLayout(new GridLayout(/*2,false*/));
149
Composite top = new Composite(composite, SWT.NONE);
150
top.setLayout(new GridLayout(/*2,false*/));
151
top.setLayoutData(new GridData(SWT.FILL,SWT.TOP,true,true));
152
Composite bottom = new Composite(composite, SWT.NONE);
153
bottom.setLayout(new GridLayout(/*2,false*/));
154
bottom.setLayoutData(new GridData(SWT.FILL,SWT.BOTTOM,true,true));
156
//-----------------------NAME---------------------------------
157
Label nameLabel = new Label(top, SWT.NONE);
158
nameLabel.setLayoutData(new GridData(SWT.FILL,SWT.CENTER,true,true));
159
nameLabel.setText(TuxGuitar.getProperty("track.name") + ":");
161
this.nameText = new Text(top, SWT.BORDER);
162
this.nameText.setLayoutData(getAlignmentData(MINIMUN_LEFT_CONTROLS_WIDTH,SWT.FILL));
163
this.nameText.setText(track.getName());
165
//-----------------------COLOR---------------------------------
166
Label colorLabel = new Label(bottom, SWT.NONE);
167
colorLabel.setText(TuxGuitar.getProperty("track.color") + ":");
168
colorLabel.setLayoutData(new GridData(SWT.FILL,SWT.CENTER,true,true));
170
final Button colorButton = new Button(bottom, SWT.PUSH);
171
colorButton.setLayoutData(getAlignmentData(MINIMUN_LEFT_CONTROLS_WIDTH,SWT.FILL));
172
colorButton.addSelectionListener(new SelectionAdapter() {
173
public void widgetSelected(SelectionEvent event) {
174
ColorDialog dlg = new ColorDialog(TrackPropertiesAction.this.dialog);
175
dlg.setRGB(TrackPropertiesAction.this.dialog.getDisplay().getSystemColor(SWT.COLOR_BLACK).getRGB());
176
dlg.setText(TuxGuitar.getProperty("choose-color"));
177
RGB rgb = dlg.open();
179
TrackPropertiesAction.this.trackColor.setR(rgb.red);
180
TrackPropertiesAction.this.trackColor.setG(rgb.green);
181
TrackPropertiesAction.this.trackColor.setB(rgb.blue);
182
colorButton.redraw();
186
colorButton.addPaintListener(new PaintListener() {
187
public void paintControl(PaintEvent e) {
188
Color color = new Color(TrackPropertiesAction.this.dialog.getDisplay(), TrackPropertiesAction.this.trackColor.getR(), TrackPropertiesAction.this.trackColor.getG(), TrackPropertiesAction.this.trackColor.getB());
189
TGPainter painter = new TGPainter(e.gc);
190
painter.setBackground(color);
191
painter.initPath(TGPainter.PATH_FILL);
192
painter.addRectangle(5,5,colorButton.getSize().x - 10,colorButton.getSize().y - 10);
200
private void initTuningInfo(Composite composite,TGTrackImpl track) {
201
composite.setLayout(new GridLayout(2,true));
202
initTuningData(composite,track);
203
initTuningCombos(composite);
206
private void initTuningCombos(Composite parent) {
207
Composite composite = new Composite(parent, SWT.NONE);
208
composite.setLayout(new GridLayout(/*MAX_STRINGS, false*/));
209
composite.setLayoutData(new GridData(SWT.RIGHT,SWT.FILL,true,true));
210
String[] tuningTexts = getAllValueNames();
211
for (int i = 0; i < MAX_STRINGS; i++) {
212
this.stringCombos[i] = new Combo(composite, SWT.DROP_DOWN | SWT.READ_ONLY);
213
this.stringCombos[i].setItems(tuningTexts);
217
private void initTuningData(Composite parent,TGTrackImpl track) {
218
Composite composite = new Composite(parent, SWT.NONE);
219
composite.setLayout(new GridLayout());
220
composite.setLayoutData(new GridData(SWT.FILL,SWT.TOP,true,true));
222
Composite top = new Composite(composite, SWT.NONE);
223
top.setLayout(new GridLayout());
224
top.setLayoutData(new GridData(SWT.FILL,SWT.TOP,true,true));
226
Composite bottom = new Composite(composite, SWT.NONE);
227
bottom.setLayout(new GridLayout());
228
bottom.setLayoutData(new GridData(SWT.FILL,SWT.BOTTOM,true,true));
230
//---------------------------------STRING--------------------------------
231
Label stringCountLabel = new Label(top, SWT.NONE);
232
stringCountLabel.setText(TuxGuitar.getProperty("tuning.strings") + ":");
233
stringCountLabel.setLayoutData(new GridData(SWT.LEFT,SWT.CENTER,true,true));
235
this.stringCountSpinner = new Spinner(top, SWT.BORDER);
236
this.stringCountSpinner.setLayoutData(getAlignmentData(80,SWT.FILL));
237
this.stringCountSpinner.setMinimum(MIN_STRINGS);
238
this.stringCountSpinner.setMaximum(MAX_STRINGS);
239
this.stringCountSpinner.setSelection(this.stringCount);
240
this.stringCountSpinner.addSelectionListener(new SelectionAdapter() {
241
public void widgetSelected(SelectionEvent e) {
242
TrackPropertiesAction.this.stringCount = TrackPropertiesAction.this.stringCountSpinner.getSelection();
244
updateTuningGroup(!TrackPropertiesAction.this.percussionCheckBox.getSelection());
248
//---------------------------------OFFSET--------------------------------
249
Label offsetLabel = new Label(bottom, SWT.NONE);
250
offsetLabel.setText(TuxGuitar.getProperty("tuning.offset") + ":");
251
offsetLabel.setLayoutData(new GridData(SWT.LEFT,SWT.CENTER,true,true));
253
this.offsetCombo = new Combo(bottom, SWT.DROP_DOWN | SWT.READ_ONLY);
254
this.offsetCombo.setLayoutData(getAlignmentData(80,SWT.LEFT));
255
for(int i = TGTrack.MIN_OFFSET;i <= TGTrack.MAX_OFFSET;i ++){
256
this.offsetCombo.add(Integer.toString(i));
257
if(i == track.getOffset()){
258
this.offsetCombo.select(i - TGTrack.MIN_OFFSET);
263
private GridData getAlignmentData(int minimumWidth,int horizontalAlignment){
264
GridData data = new GridData();
265
data.minimumWidth = minimumWidth;
266
data.horizontalAlignment = horizontalAlignment;
267
data.verticalAlignment = SWT.DEFAULT;
268
data.grabExcessHorizontalSpace = true;
269
data.grabExcessVerticalSpace = true;
273
private void initButtons(final Composite parent) {
274
Button buttonOK = new Button(parent, SWT.PUSH);
275
buttonOK.setText(TuxGuitar.getProperty("ok"));
276
buttonOK.setLayoutData(getButtonsData());
277
buttonOK.addSelectionListener(new SelectionAdapter() {
278
public void widgetSelected(SelectionEvent arg0) {
279
updateTrackProperties();
280
//--------------update and redraw------------------------------------------------------
281
new SyncThread(new Runnable() {
283
if(!TuxGuitar.isDisposed()){
285
TrackPropertiesAction.this.dialog.dispose();
292
Button buttonCancel = new Button(parent, SWT.PUSH);
293
buttonCancel.setText(TuxGuitar.getProperty("cancel"));
294
buttonCancel.setLayoutData(getButtonsData());
295
buttonCancel.addSelectionListener(new SelectionAdapter() {
296
public void widgetSelected(SelectionEvent arg0) {
297
TrackPropertiesAction.this.dialog.dispose();
301
this.dialog.setDefaultButton( buttonOK );
304
private void initInstrumentFields(Composite composite,TGTrackImpl track) {
305
composite.setLayout(new GridLayout());
307
Composite top = new Composite(composite, SWT.NONE);
308
top.setLayout(new GridLayout());
309
top.setLayoutData(new GridData(SWT.FILL,SWT.TOP,true,true));
311
Composite bottom = new Composite(composite, SWT.NONE);
312
bottom.setLayout(new GridLayout());
313
bottom.setLayoutData(new GridData(SWT.FILL,SWT.BOTTOM,true,true));
315
//------------Instrument Combo-------------------------------------
316
Label instrumentLabel = new Label(top, SWT.NONE);
317
instrumentLabel.setText(TuxGuitar.getProperty("instrument.instrument") + ":");
318
instrumentLabel.setLayoutData(new GridData(SWT.FILL,SWT.CENTER,true,true));
320
this.instrumentCombo = new Combo(top, SWT.DROP_DOWN | SWT.READ_ONLY);
321
this.instrumentCombo.setLayoutData(getAlignmentData(MINIMUN_LEFT_CONTROLS_WIDTH,SWT.FILL));
322
MidiInstrument[] instruments = TuxGuitar.instance().getPlayer().getInstruments();
323
if (instruments != null) {
324
int count = instruments.length;
328
for (int i = 0; i < count; i++) {
329
this.instrumentCombo.add(instruments[i].getName());
331
this.instrumentCombo.select(track.getChannel().getInstrument());
333
this.instrumentCombo.setEnabled(!track.isPercussionTrack() && instruments != null);
335
//--------------------Precusion CheckBox-------------------------------
336
this.percussionCheckBox = new Button(bottom, SWT.CHECK);
337
this.percussionCheckBox.setText(TuxGuitar.getProperty("instrument.percussion-track"));
338
this.percussionCheckBox.setLayoutData(new GridData(SWT.FILL,SWT.CENTER,true,true));
339
if (instruments != null) {
340
this.percussionCheckBox.setSelection(track.isPercussionTrack());
341
this.percussionCheckBox.addSelectionListener(new SelectionAdapter() {
342
public void widgetSelected(SelectionEvent arg0) {
343
TrackPropertiesAction.this.instrumentCombo.setEnabled(!TrackPropertiesAction.this.percussionCheckBox.getSelection());
346
updateTuningGroup(!TrackPropertiesAction.this.percussionCheckBox.getSelection());
350
this.percussionCheckBox.setEnabled(false);
352
//-----------------------------------------------------------
355
protected void updateTrackProperties() {
356
TGTrackImpl track = getEditor().getTablature().getCaret().getTrack();
358
String trackName = this.nameText.getText();
360
List strings = new ArrayList();
361
for (int i = 0; i < this.stringCount; i++) {
362
strings.add(TGSongManager.newString(getSongManager().getFactory(),(i + 1), this.stringCombos[i].getSelectionIndex()));
365
boolean percussion = this.percussionCheckBox.getSelection();
366
int offset = ((percussion)?0:TGTrack.MIN_OFFSET + this.offsetCombo.getSelectionIndex());
367
int instrument = ((this.instrumentCombo.getSelectionIndex() >= 0)?this.instrumentCombo.getSelectionIndex():0);
369
boolean infoChanges = hasInfoChanges(track,trackName,this.trackColor,offset);
370
boolean tuningChanges = hasTuningChanges(track,strings);
371
boolean instrumentChanges = hasInstrumentChanges(track,instrument,percussion);
373
if(infoChanges || tuningChanges || instrumentChanges){
374
TuxGuitar.instance().getFileHistory().setUnsavedFile();
375
UndoableJoined undoable = new UndoableJoined();
377
UndoableTrackGeneric undoableGeneric = null;
379
undoableGeneric = UndoableTrackGeneric.startUndo(track);
382
//--------------------------------------info---------------------------------------
384
UndoableTrackInfo undoableInfo = null;
386
undoableInfo = UndoableTrackInfo.startUndo(track);
388
getSongManager().getTrackManager().changeInfo(track,trackName,this.trackColor,offset);
389
if(!tuningChanges && undoableInfo != null){
390
undoable.addUndoableEdit(undoableInfo.endUndo(track));
393
//--------------------------------------tuning---------------------------------------
395
getSongManager().getTrackManager().changeInstrumentStrings(track,strings);
397
//-----------------------------instrument----------------------------------------------
398
if(instrumentChanges){
399
UndoableTrackInstrument undoableInstrument = null;
401
undoableInstrument = UndoableTrackInstrument.startUndo(track);
403
getSongManager().getTrackManager().changeInstrument(track,instrument,percussion);
404
if(!tuningChanges && undoableInstrument != null){
405
undoable.addUndoableEdit(undoableInstrument.endUndo(track));
408
if(tuningChanges && undoableGeneric != null){
409
undoable.addUndoableEdit(undoableGeneric.endUndo(track));
411
addUndoableEdit(undoable.endUndo());
415
protected boolean hasInfoChanges(TGTrackImpl track,String name,TGColor color,int offset){
416
if(!name.equals(track.getName())){
419
if(!color.isEqual(track.getColor())){
422
if(offset != track.getOffset()){
428
protected boolean hasTuningChanges(TGTrackImpl track,List newStrings){
429
List oldStrings = track.getStrings();
430
//check the number of strings
431
if(oldStrings.size() != newStrings.size()){
434
//check the tuning of strings
435
for(int i = 0;i < oldStrings.size();i++){
436
TGString oldString = (TGString)oldStrings.get(i);
437
boolean stringExists = false;
438
for(int j = 0;j < newStrings.size();j++){
439
TGString newString = (TGString)newStrings.get(j);
440
if(newString.isEqual(oldString)){
451
protected boolean hasInstrumentChanges(TGTrackImpl track,int instrument,boolean percussion){
452
return ((track.getChannel().getInstrument() != instrument) || (track.isPercussionTrack() != percussion));
455
protected void updateTuningGroup(boolean enabled) {
456
for (int i = 0; i < this.tempStrings.size(); i++) {
457
TGString string = (TGString)this.tempStrings.get(i);
458
this.stringCombos[i].select(string.getValue());
459
this.stringCombos[i].setVisible(true);
460
this.stringCombos[i].setEnabled(enabled);
463
for (int i = this.tempStrings.size(); i < MAX_STRINGS; i++) {
464
this.stringCombos[i].select(0);
465
this.stringCombos[i].setVisible(false);
467
this.offsetCombo.setEnabled(enabled);
470
protected void initTempStrings(List realStrings) {
471
this.tempStrings = new ArrayList();
472
for (int i = 0; i < realStrings.size(); i++) {
473
TGString realString = (TGString) realStrings.get(i);
474
this.tempStrings.add(realString.clone(getSongManager().getFactory()));
478
protected void setDefaultTuning() {
479
this.tempStrings.clear();
480
if (this.percussionCheckBox.getSelection()) {
481
for (int i = 1; i <= this.stringCount; i++) {
482
this.tempStrings.add(TGSongManager.newString(getSongManager().getFactory(),i, 0));
486
switch (this.stringCount) {
488
this.tempStrings.add(TGSongManager.newString(getSongManager().getFactory(),1, 64));
489
this.tempStrings.add(TGSongManager.newString(getSongManager().getFactory(),2, 59));
490
this.tempStrings.add(TGSongManager.newString(getSongManager().getFactory(),3, 55));
491
this.tempStrings.add(TGSongManager.newString(getSongManager().getFactory(),4, 50));
492
this.tempStrings.add(TGSongManager.newString(getSongManager().getFactory(),5, 45));
493
this.tempStrings.add(TGSongManager.newString(getSongManager().getFactory(),6, 40));
494
this.tempStrings.add(TGSongManager.newString(getSongManager().getFactory(),7, 35));
497
this.tempStrings.add(TGSongManager.newString(getSongManager().getFactory(),1, 64));
498
this.tempStrings.add(TGSongManager.newString(getSongManager().getFactory(),2, 59));
499
this.tempStrings.add(TGSongManager.newString(getSongManager().getFactory(),3, 55));
500
this.tempStrings.add(TGSongManager.newString(getSongManager().getFactory(),4, 50));
501
this.tempStrings.add(TGSongManager.newString(getSongManager().getFactory(),5, 45));
502
this.tempStrings.add(TGSongManager.newString(getSongManager().getFactory(),6, 40));
505
this.tempStrings.add(TGSongManager.newString(getSongManager().getFactory(),1, 43));
506
this.tempStrings.add(TGSongManager.newString(getSongManager().getFactory(),2, 38));
507
this.tempStrings.add(TGSongManager.newString(getSongManager().getFactory(),3, 33));
508
this.tempStrings.add(TGSongManager.newString(getSongManager().getFactory(),4, 28));
509
this.tempStrings.add(TGSongManager.newString(getSongManager().getFactory(),5, 23));
512
this.tempStrings.add(TGSongManager.newString(getSongManager().getFactory(),1, 43));
513
this.tempStrings.add(TGSongManager.newString(getSongManager().getFactory(),2, 38));
514
this.tempStrings.add(TGSongManager.newString(getSongManager().getFactory(),3, 33));
515
this.tempStrings.add(TGSongManager.newString(getSongManager().getFactory(),4, 28));
521
protected String[] getAllValueNames() {
522
String[] valueNames = new String[MAX_NOTES * MAX_OCTAVES];
523
for (int i = 0; i < valueNames.length; i++) {
524
valueNames[i] = NOTE_NAMES[ (i - ((i / MAX_NOTES) * MAX_NOTES) ) ] + (i / MAX_NOTES);
b'\\ No newline at end of file'