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

« back to all changes in this revision

Viewing changes to src/org/herac/tuxguitar/gui/editors/chord/ChordSelector.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 02-ene-2006
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.editors.chord;
8
 
 
9
 
import java.util.ArrayList;
10
 
 
11
 
import org.eclipse.swt.SWT;
12
 
import org.eclipse.swt.events.SelectionAdapter;
13
 
import org.eclipse.swt.events.SelectionEvent;
14
 
import org.eclipse.swt.layout.GridData;
15
 
import org.eclipse.swt.layout.GridLayout;
16
 
import org.eclipse.swt.widgets.Composite;
17
 
import org.eclipse.swt.widgets.List;
18
 
import org.herac.tuxguitar.song.models.Chord;
19
 
 
20
 
/**
21
 
 * @author julian
22
 
 *
23
 
 * TODO To change the template for this generated type comment go to
24
 
 * Window - Preferences - Java - Code Style - Code Templates
25
 
 */
26
 
public class ChordSelector extends Composite{
27
 
    private ChordEditor editor;
28
 
    private ChordList list;
29
 
    private int[] tunning;
30
 
    private List tonicList;
31
 
    private List chordList;
32
 
    private List alterationList;    
33
 
    
34
 
    public ChordSelector(Composite parent,int style) {        
35
 
        super(parent,style);
36
 
        this.setLayout(new GridLayout(2,false));
37
 
        this.setLayoutData(new GridData(SWT.FILL,SWT.FILL,true,true));
38
 
        this.init();
39
 
    }
40
 
 
41
 
    
42
 
    public void init(){
43
 
        Composite leftComposite = new Composite(this,SWT.NONE);
44
 
        Composite rightComposite = new Composite(this,SWT.NONE);
45
 
        leftComposite.setLayout(new GridLayout());
46
 
        rightComposite.setLayout(new GridLayout());
47
 
        
48
 
        leftComposite.setLayoutData(new GridData(SWT.FILL,SWT.FILL,true,true));
49
 
        rightComposite.setLayoutData(new GridData(SWT.FILL,SWT.FILL,true,true));
50
 
        
51
 
        this.tonicList = new List(leftComposite,SWT.BORDER);
52
 
        this.tonicList.setLayoutData(makeGridData());
53
 
        String[] tonicNames = getTonicNames();
54
 
        for(int i = 0;i < tonicNames.length;i++){
55
 
            this.tonicList.add(tonicNames[i]);
56
 
        }
57
 
        this.tonicList.setSelection(0);
58
 
               
59
 
        this.chordList = new List(rightComposite,SWT.BORDER);
60
 
        this.chordList.setLayoutData(makeGridData());
61
 
        String[] chordNames = getChordNames();
62
 
        for(int i = 0;i < chordNames.length;i++){
63
 
            this.chordList.add(chordNames[i]);
64
 
        }        
65
 
        this.chordList.setSelection(0);
66
 
        
67
 
        this.alterationList = new List(rightComposite,SWT.BORDER);
68
 
        this.alterationList.setLayoutData(makeGridData());
69
 
        String[] alterationNames = getAlterationNames();
70
 
        for(int i = 0;i < alterationNames.length;i++){
71
 
            this.alterationList.add(alterationNames[i]);
72
 
        }        
73
 
        this.alterationList.setSelection(0);        
74
 
        
75
 
        this.tonicList.addSelectionListener(new SelectionAdapter() {
76
 
            public void widgetSelected(SelectionEvent e) {
77
 
                if(editor != null && list != null){                 
78
 
                    showChord(tonicList.getSelectionIndex() + 1,chordList.getSelectionIndex() + 1,alterationList.getSelectionIndex() + 1);
79
 
                    list.redraw();
80
 
                }
81
 
            }
82
 
        });  
83
 
        
84
 
        this.chordList.addSelectionListener(new SelectionAdapter() {
85
 
            public void widgetSelected(SelectionEvent e) {
86
 
                if(editor != null && list != null){                 
87
 
                    showChord(tonicList.getSelectionIndex() + 1,chordList.getSelectionIndex() + 1,alterationList.getSelectionIndex() + 1);
88
 
                    list.redraw();
89
 
                }
90
 
            }
91
 
        });         
92
 
        
93
 
        this.alterationList.addSelectionListener(new SelectionAdapter() {
94
 
            public void widgetSelected(SelectionEvent e) {
95
 
                if(editor != null && list != null){                 
96
 
                    showChord(tonicList.getSelectionIndex() + 1,chordList.getSelectionIndex() + 1,alterationList.getSelectionIndex() + 1);
97
 
                    list.redraw();
98
 
                }
99
 
            }
100
 
        });           
101
 
    }
102
 
    
103
 
    private GridData makeGridData(){
104
 
        GridData data = new GridData(SWT.FILL,SWT.FILL,true,true);
105
 
        data.minimumWidth = 50;
106
 
        
107
 
        return data;
108
 
    }
109
 
    
110
 
    public void setEditor(ChordEditor editor){
111
 
        this.editor = editor;
112
 
    }
113
 
    
114
 
    public void setList(ChordList list){
115
 
        this.list = list;
116
 
    }    
117
 
    
118
 
    public void setTunning(int[] tunning){
119
 
        this.tunning = tunning;
120
 
    }
121
 
    
122
 
    private String[] getTonicNames(){
123
 
        String[] names = new String[12];
124
 
     
125
 
        names[0] = "C";
126
 
        names[1] = "C#";
127
 
        names[2] = "D";
128
 
        names[3] = "D#";
129
 
        names[4] = "E";
130
 
        names[5] = "F";
131
 
        names[6] = "F#";
132
 
        names[7] = "G";
133
 
        names[8] = "G#";
134
 
        names[9] = "A";
135
 
        names[10] = "A#";
136
 
        names[11] = "B";
137
 
        
138
 
        return names;
139
 
    }
140
 
    
141
 
    private String[] getChordNames(){
142
 
        String[] names = new String[4];
143
 
     
144
 
        names[0] = "M";
145
 
        names[1] = "m";
146
 
        names[2] = "sus2";
147
 
        names[3] = "sus4";
148
 
        
149
 
        return names;
150
 
    }    
151
 
 
152
 
    private String[] getAlterationNames(){
153
 
        String[] names = new String[5];
154
 
     
155
 
        names[0] = "Normal";
156
 
        names[1] = "7";
157
 
        names[2] = "7M";
158
 
        names[3] = "6";
159
 
        names[4] = "5";
160
 
        
161
 
        return names;
162
 
    }  
163
 
    
164
 
    private void showChord(int tonic,int chordType,int alterationType){
165
 
        int requiredNotes[] = getRequiredNotes(chordType,alterationType);
166
 
        
167
 
        //entre cegillas 1 y 4
168
 
        int fret1 = 0;
169
 
        int fret2 = 3;
170
 
 
171
 
        java.util.List chords = new ArrayList();
172
 
        ChordCreatorUtil chordCreator = null;
173
 
        for(int i = 0;i < 10;i++){
174
 
            chordCreator = new ChordCreatorUtil(this.tunning,requiredNotes,tonic,fret1 + i,fret2 + i);
175
 
            Chord chord = chordCreator.getChord();
176
 
            if(chord != null){
177
 
                chords.add(chord);
178
 
            }
179
 
        }
180
 
        this.list.setChords(chords);
181
 
    }
182
 
 
183
 
    private int[] getRequiredNotes(int chordType,int alterationType){               
184
 
        int requiredNotes[] = null;
185
 
        
186
 
        int alterationRequired = getAlterationRequired(alterationType);
187
 
        int moveLength = (alterationRequired > 0)?1:0;
188
 
        
189
 
        switch(chordType){
190
 
                case 1:             
191
 
                    requiredNotes = new int[3 + moveLength]; 
192
 
                    requiredNotes[0 + moveLength] = 1;
193
 
                    requiredNotes[1 + moveLength] = 5;
194
 
                    requiredNotes[2 + moveLength] = 8;
195
 
                    break;
196
 
                case 2:
197
 
                    requiredNotes = new int[3 + moveLength]; 
198
 
                    requiredNotes[0 + moveLength] = 1;
199
 
                    requiredNotes[1 + moveLength] = 4;
200
 
                    requiredNotes[2 + moveLength] = 8;
201
 
                    break;
202
 
                case 3:
203
 
                    requiredNotes = new int[4 + moveLength]; 
204
 
                    requiredNotes[0 + moveLength] = 3;
205
 
                    requiredNotes[1 + moveLength] = 1;
206
 
                    requiredNotes[2 + moveLength] = 5;
207
 
                    requiredNotes[3 + moveLength] = 8;
208
 
                    break;
209
 
                case 4:
210
 
                    requiredNotes = new int[4 + moveLength]; 
211
 
                    requiredNotes[0 + moveLength] = 6;
212
 
                    requiredNotes[1 + moveLength] = 1;
213
 
                    requiredNotes[2 + moveLength] = 8;
214
 
                    requiredNotes[3 + moveLength] = 5;
215
 
                    break;                  
216
 
        }
217
 
        if(alterationRequired > 0){
218
 
            requiredNotes[0] = alterationRequired;
219
 
        }
220
 
        
221
 
        return requiredNotes;
222
 
    }
223
 
    
224
 
    private int getAlterationRequired(int alterationType){
225
 
        int requiredNote = 0;
226
 
        
227
 
        switch(alterationType){
228
 
        case 1:
229
 
            requiredNote = 0;
230
 
            break;
231
 
        case 2:
232
 
            requiredNote = 11;
233
 
            break;
234
 
        case 3:
235
 
            requiredNote = 12;
236
 
            break;          
237
 
        case 4:
238
 
            requiredNote = 10;
239
 
            break;
240
 
        case 5:
241
 
            requiredNote = 8;
242
 
            break;          
243
 
        }       
244
 
        return requiredNote;
245
 
    }    
246
 
}