2
* Created on 02-ene-2006
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.editors.chord;
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.layout.GridData;
13
import org.eclipse.swt.layout.GridLayout;
14
import org.eclipse.swt.widgets.Button;
15
import org.eclipse.swt.widgets.Combo;
16
import org.eclipse.swt.widgets.Composite;
17
import org.eclipse.swt.widgets.Label;
18
import org.eclipse.swt.widgets.List;
19
import org.herac.tuxguitar.gui.TuxGuitar;
20
import org.herac.tuxguitar.gui.helper.SyncThread;
21
import org.herac.tuxguitar.gui.util.TGMusicKeyUtils;
22
import org.herac.tuxguitar.util.TGSynchronizer;
26
* @author Nikola Kolarovic
28
* WIDGET SET that allows complex chord choosing<br>
29
* Chord theory according to <a href="http://www.jazzguitar.be/quick_crd_ref.html">http://www.jazzguitar.be/quick_crd_ref.html</a>.
31
public class ChordSelector extends Composite{
33
public static final String[][] KEY_NAMES = new String[][]{
34
TGMusicKeyUtils.getSharpKeyNames(TGMusicKeyUtils.PREFIX_CHORD),
35
TGMusicKeyUtils.getFlatKeyNames(TGMusicKeyUtils.PREFIX_CHORD),
38
private ChordDialog dialog;
40
private List tonicList;
41
private List chordList;
42
private List alterationList;
43
private Button sharpButton;
44
private Button flatButton;
45
private Combo bassCombo;
46
private Button addCheck;
47
private List plusMinusList;
52
private boolean refresh;
54
public ChordSelector(ChordDialog dialog,Composite parent,int style,int[] tuning) {
56
this.setLayout(new GridLayout(3,false));
57
this.setLayoutData(new GridData(SWT.FILL,SWT.FILL,true,true));
67
Composite tonicComposite = new Composite(this,SWT.NONE);
68
tonicComposite.setLayout(this.dialog.gridLayout(1,false,0,0));
69
tonicComposite.setLayoutData(new GridData(SWT.FILL,SWT.FILL,true,true));
71
this.tonicList = new List(tonicComposite,SWT.BORDER);
72
this.tonicList.setLayoutData(new GridData(SWT.FILL,SWT.FILL,true,true));
74
// sharp & flat buttons
75
Composite buttonsComposite = new Composite(tonicComposite,SWT.NONE);
76
buttonsComposite.setLayout(this.dialog.gridLayout(2,true,0,0));
77
GridData buttonGd = new GridData(SWT.FILL,SWT.TOP,true,false);
78
buttonGd.heightHint = 28;
79
buttonGd.widthHint = 28;
80
this.sharpButton = new Button(buttonsComposite,SWT.TOGGLE);
81
this.sharpButton.setLayoutData(buttonGd);
82
this.flatButton = new Button(buttonsComposite,SWT.TOGGLE);
83
this.flatButton.setLayoutData(buttonGd);
84
// TODO: maybe put an image instead of #,b
85
this.sharpButton.setText("#");
86
this.flatButton.setText("b");
87
this.chordList = new List(this,SWT.BORDER);
88
this.chordList.setLayoutData(new GridData(SWT.FILL,SWT.FILL,true,true));
90
Label separator = new Label(tonicComposite,SWT.SEPARATOR | SWT.HORIZONTAL);
91
separator.setLayoutData(new GridData(SWT.FILL,SWT.BOTTOM,true,true));
92
Button customizeButton = new Button(tonicComposite,SWT.PUSH);
93
customizeButton.setLayoutData(new GridData(SWT.FILL,SWT.BOTTOM,true,false));
94
customizeButton.setText(TuxGuitar.getProperty("settings"));
96
customizeButton.addSelectionListener(new SelectionAdapter() {
97
public void widgetSelected(SelectionEvent arg0) {
98
if(new ChordSettingsDialog().open(ChordSelector.this.getShell())){
99
new SyncThread(new Runnable() {
101
ChordSelector.this.showChord();
102
getChordList().redraw();
112
// fill the List widgets with text
113
insertTonicNames(true);
115
for(int i = 0 ; i < ChordDatabase.length(); i ++) {
116
this.chordList.add( ChordDatabase.get(i).getName() );
119
Iterator chordInfo = ChordCreatorUtil.getChordData().getChords().iterator();
120
while(chordInfo.hasNext()) {
121
this.chordList.add( ((ChordDatabase.ChordInfo)chordInfo.next()).getName() );
125
this.chordList.setSelection(0);
127
String[] alterationNames = getAlterationNames();
128
for(int i = 0;i < alterationNames.length;i++){
129
this.alterationList.add(alterationNames[i]);
131
this.alterationList.setSelection(0);
133
String[] plusMinus = this.getPlusMinus("");
134
for(int i = 0;i < plusMinus.length;i++){
135
this.plusMinusList.add(plusMinus[i]);
137
this.plusMinusList.setSelection(0);
139
String[] plus5Minus = this.getPlusMinus("/5");
140
for(int i = 0;i < plus5Minus.length;i++){
141
this._5List.add(plus5Minus[i]);
143
this._5List.setSelection(0);
144
String[] plus9Minus = this.getPlusMinus("/9");
145
for(int i = 0;i < plus9Minus.length;i++){
146
this._9List.add(plus9Minus[i]);
148
this._9List.setSelection(0);
149
String[] plus11Minus = this.getPlusMinus("/11");
150
for(int i = 0;i < plus11Minus.length;i++){
151
this._11List.add(plus11Minus[i]);
153
this._11List.setSelection(0);
157
this.tonicList.addSelectionListener(new SelectionAdapter() {
158
public void widgetSelected(SelectionEvent e) {
159
if (ChordSelector.this.getRefresh()) {
160
if(getDialog().getEditor() != null && getDialog().getList() != null){
161
getBassCombo().select(getTonicList().getSelectionIndex());
168
this.bassCombo.addSelectionListener(new SelectionAdapter() {
169
public void widgetSelected(SelectionEvent e) {
170
if (ChordSelector.this.getRefresh()) {
171
if(getDialog().getEditor() != null && getDialog().getList() != null){
178
this.chordList.addSelectionListener(new SelectionAdapter() {
179
public void widgetSelected(SelectionEvent e) {
180
if(getDialog().getEditor() != null && getDialog().getList() != null){
181
adjustWidgetAvailability();
182
if (ChordSelector.this.getRefresh()) {
189
this.alterationList.addSelectionListener(new SelectionAdapter() {
190
public void widgetSelected(SelectionEvent e) {
191
if(getDialog().getEditor() != null && getDialog().getList() != null){
192
ChordSelector.this.adjustWidgetAvailability();
193
if (ChordSelector.this.getRefresh()) {
200
this.addCheck.addSelectionListener(new SelectionAdapter() {
201
public void widgetSelected(SelectionEvent arg0) {
202
if(getDialog().getEditor() != null && getDialog().getList() != null){
204
ChordSelector.this.adjustWidgetAvailability();
206
if (getAddCheck().getSelection()) {
207
updateWidget(get_9List(), false);
208
updateWidget(get_11List(), false);
211
if (ChordSelector.this.getRefresh()) {
213
//ChordSelector.this.dialog.getList().redraw();
220
this._5List.addSelectionListener(new SelectionAdapter() {
221
public void widgetSelected(SelectionEvent e) {
222
if(getDialog().getEditor() != null && getDialog().getList() != null){
223
if (ChordSelector.this.getRefresh()) {
230
this._9List.addSelectionListener(new SelectionAdapter() {
231
public void widgetSelected(SelectionEvent e) {
232
if(getDialog().getEditor() != null && getDialog().getList() != null){
233
if (ChordSelector.this.getRefresh()) {
240
this._11List.addSelectionListener(new SelectionAdapter() {
241
public void widgetSelected(SelectionEvent e) {
242
if(getDialog().getEditor() != null && getDialog().getList() != null){
243
if (ChordSelector.this.getRefresh()) {
250
this.plusMinusList.addSelectionListener(new SelectionAdapter() {
251
public void widgetSelected(SelectionEvent e) {
252
if(getDialog().getEditor() != null && getDialog().getList() != null){
253
if (ChordSelector.this.getRefresh()) {
255
//ChordSelector.this.dialog.getList().redraw();
261
this.sharpButton.addSelectionListener(new SelectionAdapter() {
262
public void widgetSelected(SelectionEvent arg0) {
263
insertTonicNames(true);
267
this.flatButton.addSelectionListener(new SelectionAdapter() {
268
public void widgetSelected(SelectionEvent arg0) {
269
insertTonicNames(false);
272
this.adjustWidgetAvailability();
275
protected void initChordWidgets() {
276
Composite alterationComposite = new Composite(this,SWT.NONE);
277
alterationComposite.setLayout(this.dialog.gridLayout(1,true,0,0));
278
alterationComposite.setLayoutData(new GridData(SWT.FILL,SWT.FILL,true,true));
279
Composite aboveComposite = new Composite(alterationComposite,SWT.NONE);
280
aboveComposite.setLayout(this.dialog.gridLayout(2,true,0,0));
281
aboveComposite.setLayoutData(new GridData(SWT.FILL,SWT.FILL,true,true));
282
Composite firstComposite = new Composite(aboveComposite,SWT.NONE);
283
firstComposite.setLayout(this.dialog.gridLayout(1,false,0,0));
284
firstComposite.setLayoutData(new GridData(SWT.FILL,SWT.FILL,true,true));
285
this.alterationList = new List(firstComposite,SWT.BORDER);
286
this.alterationList.setLayoutData(new GridData(SWT.FILL,SWT.FILL,true,true));
287
this.plusMinusList = new List(firstComposite,SWT.BORDER);
288
this.plusMinusList.setLayoutData(new GridData(SWT.FILL,SWT.FILL,true,true));
290
Composite secondComposite = new Composite(aboveComposite,SWT.NONE);
291
secondComposite.setLayout(this.dialog.gridLayout(1,false,0,0));
292
secondComposite.setLayoutData(new GridData(SWT.FILL,SWT.FILL,true,true));
293
this._5List = new List(secondComposite,SWT.BORDER);
294
this._5List.setLayoutData(new GridData(SWT.FILL,SWT.FILL,true,true));
296
this._9List = new List(secondComposite,SWT.BORDER);
297
this._9List.setLayoutData(new GridData(SWT.FILL,SWT.FILL,true,true));
299
this._11List = new List(secondComposite,SWT.BORDER);
300
this._11List.setLayoutData(new GridData(SWT.FILL,SWT.FILL,true,true));
302
Composite bassComposite = new Composite(alterationComposite,SWT.NONE);
303
bassComposite.setLayout(this.dialog.gridLayout(1,true,0,0));
304
bassComposite.setLayoutData(new GridData(SWT.FILL,SWT.BOTTOM,true,true));
305
this.addCheck = new Button(bassComposite, SWT.CHECK | SWT.LEFT);
306
this.addCheck.setText("add");
307
//this.addCheck.setSelection(false);
308
//this.addCheck.setEnabled(false);
309
this.addCheck.setLayoutData(new GridData(SWT.FILL,SWT.BOTTOM,true,true));
311
Label separator = new Label(bassComposite,SWT.SEPARATOR | SWT.HORIZONTAL );
312
separator.setLayoutData(new GridData(SWT.FILL,SWT.BOTTOM,true,true));
314
Label bText = new Label(bassComposite,SWT.LEFT);
315
bText.setLayoutData(new GridData(SWT.FILL,SWT.BOTTOM,true,false));
316
bText.setText(TuxGuitar.getProperty("chord.bass"));
317
this.bassCombo = new Combo(bassComposite, SWT.DROP_DOWN | SWT.READ_ONLY);
318
this.bassCombo.setLayoutData(new GridData(SWT.FILL,SWT.BOTTOM,true,false));
321
protected void insertTonicNames(boolean sharp){
323
String[] names = KEY_NAMES[ sharp?0:1 ];
325
// update the buttons
326
this.flatButton.setSelection(!sharp);
327
this.sharpButton.setSelection(sharp);
328
// keep the old position
329
int indexL = this.tonicList.getSelectionIndex();
330
if (indexL==-1) indexL=0;
331
int indexC = this.bassCombo.getSelectionIndex();
332
if (indexC==-1) indexC=0;
335
this.tonicList.removeAll();
336
this.bassCombo.removeAll();
337
for(int i = 0;i < names.length;i++){
338
this.tonicList.add(names[i]);
339
this.bassCombo.add(names[i]);
341
this.tonicList.setSelection(indexL);
342
this.bassCombo.select(indexC);
345
private String[] getPlusMinus(String text){
346
String[] names = new String[3];
355
private String[] getAlterationNames(){
356
String[] names = new String[4];
366
protected void showChord(){
367
TuxGuitar.instance().loadCursor(getShell(),SWT.CURSOR_WAIT);
368
ChordCreatorListener listener = new ChordCreatorListener() {
369
public void notifyChords(final ChordCreatorUtil instance,final java.util.List chords) {
371
TGSynchronizer.instance().addRunnable(new TGSynchronizer.TGRunnable() {
373
if(instance.isValidProcess() && !getDialog().isDisposed()){
374
getDialog().getList().setChords(chords);
375
TuxGuitar.instance().loadCursor(getShell(),SWT.CURSOR_ARROW);
379
} catch (Throwable e) {
385
ChordCreatorUtil.getChords(listener,
387
this.chordList.getSelectionIndex(),
388
this.alterationList.getSelectionIndex(),
389
this.plusMinusList.getSelectionIndex(),
390
this.addCheck.getSelection(),
391
this._5List.getSelectionIndex(),
392
this._9List.getSelectionIndex(),
393
this._11List.getSelectionIndex(),
394
this.bassCombo.getSelectionIndex(),
395
this.tonicList.getSelectionIndex(),
396
this.sharpButton.getSelection());
399
protected void updateWidget(List widget, boolean enabled) {
400
widget.setEnabled(enabled);
402
widget.setSelection(0);
406
protected void updateWidget(Button widget, boolean enabled) {
407
widget.setEnabled(enabled);
409
widget.setSelection(false);
414
* Sets all the widgets' fields into recognized chord
415
* (tonic, bass, chord, alterations)
417
public void adjustWidgets(int tonic, int chordBasic, int alteration, int bass, int plusMinus, int addBoolean, int index5, int index9, int index11) {
418
this.setRefresh(false);
420
this.tonicList.setSelection(tonic);
421
this.alterationList.setSelection(alteration);
422
this.bassCombo.select(bass);
423
this.plusMinusList.setSelection(plusMinus);
424
this.addCheck.setSelection(addBoolean != 0);
425
this._5List.setSelection(index5);
426
this._9List.setSelection(index9);
427
this._11List.setSelection(index11);
428
this.chordList.setSelection(chordBasic);
429
this.adjustWidgetAvailability();
430
this.setRefresh(true);
435
* adjusts the widgets availability according to chord theory options
437
protected void adjustWidgetAvailability() {
438
String chordName = ChordDatabase.get(getChordList().getSelectionIndex()).getName();
439
if (chordName.equals("dim") || chordName.equals("dim7") || chordName.equals("aug") || chordName.equals("5") ) {
440
updateWidget(getAlterationList(),false);
441
updateWidget(getAddCheck(),false);
442
updateWidget(get_9List(),false);
443
updateWidget(get_11List(),false);
444
updateWidget(getPlusMinusList(),false);
446
if (!chordName.equals("5")){
447
updateWidget(get_5List(),false);//disableWidget(get_5List());
449
updateWidget(get_5List(),true);
453
// enable and don't change the selection index
454
//getAlterationList().setEnabled(true);
455
//get_5List().setEnabled(true);
456
updateWidget(getAlterationList(),true);
457
updateWidget(get_5List(),true);
460
if(this.alterationList.isEnabled()){
461
int currentIndex = this.alterationList.getSelectionIndex();
462
// handle the +- list and ADD checkbox
463
// handle the 9 and 11 list
464
updateWidget(this.plusMinusList,(currentIndex > 0));
465
updateWidget(this.addCheck,(currentIndex > 0));
466
updateWidget(this._9List, (currentIndex >= 2 && !this.addCheck.getSelection() ) );
467
updateWidget(this._11List, (currentIndex >= 3 && !this.addCheck.getSelection() ) );
471
public boolean getRefresh() {
475
public void setRefresh(boolean refresh) {
476
this.refresh = refresh;
479
public void setTuning(int[] tuning){
480
this.tuning = tuning;
483
public int[] getTuning(){
487
protected ChordDialog getDialog() {
491
protected List getTonicList() {
492
return this.tonicList;
495
protected List getChordList() {
496
return this.chordList;
499
protected List getAlterationList() {
500
return this.alterationList;
503
protected Button getSharpButton() {
504
return this.sharpButton;
507
protected Button getFlatButton() {
508
return this.flatButton;
511
protected Combo getBassCombo() {
512
return this.bassCombo;
515
protected Button getAddCheck() {
516
return this.addCheck;
519
protected List getPlusMinusList() {
520
return this.plusMinusList;
523
protected List get_5List() {
527
protected List get_9List() {
531
protected List get_11List() {