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

« back to all changes in this revision

Viewing changes to src/org/herac/tuxguitar/gui/editors/chord/ChordCustomList.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 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.Composite;
16
 
import org.eclipse.swt.widgets.Label;
17
 
import org.eclipse.swt.widgets.List;
18
 
import org.eclipse.swt.widgets.Shell;
19
 
import org.eclipse.swt.widgets.Text;
20
 
import org.herac.tuxguitar.gui.TuxGuitar;
21
 
import org.herac.tuxguitar.gui.util.MessageDialog;
22
 
import org.herac.tuxguitar.song.models.Chord;
23
 
 
24
 
/**
25
 
 * @author julian
26
 
 * 
27
 
 * TODO To change the template for this generated type comment go to Window - Preferences - Java - Code Style - Code Templates
28
 
 */
29
 
public class ChordCustomList extends Composite {
30
 
        private ChordEditor editor;    
31
 
        private List chords;      
32
 
        
33
 
        public ChordCustomList(Composite parent,int style,int height,ChordEditor editor) {        
34
 
                super(parent,style);
35
 
            this.setLayout(new GridLayout());
36
 
            this.setLayoutData(makeGridData(height));
37
 
            this.editor = editor;
38
 
            this.init();
39
 
        }
40
 
 
41
 
        public GridData makeGridData(int height){
42
 
                GridData data = new GridData(SWT.FILL,SWT.TOP,true,true);
43
 
                data.heightHint = height;
44
 
                
45
 
                return data;
46
 
        }
47
 
            
48
 
        public void init(){
49
 
                Composite composite = new Composite(this,SWT.NONE);
50
 
                composite.setLayout(new GridLayout());          
51
 
                composite.setLayoutData(new GridData(SWT.FILL,SWT.FILL,true,true));
52
 
                
53
 
                this.chords = new List(composite,SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL);
54
 
                this.chords.setLayoutData(new GridData(SWT.FILL,SWT.FILL,true,true));
55
 
            this.chords.addSelectionListener(new SelectionAdapter() {
56
 
                public void widgetSelected(SelectionEvent e) {
57
 
                        if(editor != null){                 
58
 
                                showChord(chords.getSelectionIndex());
59
 
                    }
60
 
                }
61
 
            });  
62
 
                
63
 
            
64
 
            //-------------BUTTONS-----------------------------
65
 
            Composite buttons = new Composite(this,SWT.NONE);
66
 
            buttons.setLayout(new GridLayout(3,false));         
67
 
            
68
 
            Button add = new Button(buttons,SWT.PUSH);
69
 
            add.setText(TuxGuitar.getProperty("add"));
70
 
            add.addSelectionListener(new SelectionAdapter(){
71
 
                public void widgetSelected(SelectionEvent e) {
72
 
                        addCustomChord();
73
 
                        }
74
 
            });
75
 
            
76
 
            Button rename = new Button(buttons,SWT.PUSH);
77
 
            rename.setText(TuxGuitar.getProperty("rename"));
78
 
            rename.addSelectionListener(new SelectionAdapter(){
79
 
                public void widgetSelected(SelectionEvent e) {
80
 
                        renameCustomChord(chords.getSelectionIndex());
81
 
                        }
82
 
            });             
83
 
            
84
 
            Button remove = new Button(buttons,SWT.PUSH);
85
 
            remove.setText(TuxGuitar.getProperty("remove"));
86
 
            remove.addSelectionListener(new SelectionAdapter(){
87
 
                public void widgetSelected(SelectionEvent e) {
88
 
                        removeCustomChord(chords.getSelectionIndex());
89
 
                        }
90
 
            });
91
 
 
92
 
            loadChords();
93
 
        }
94
 
 
95
 
        
96
 
        private void loadChords(){
97
 
                int selectionIndex = chords.getSelectionIndex();
98
 
                this.chords.removeAll();
99
 
 
100
 
                for(int i = 0;i < TuxGuitar.instance().getCustomChordManager().countChords();i ++){
101
 
                        Chord chord = TuxGuitar.instance().getCustomChordManager().getChord(i);
102
 
                        if(chord != null){
103
 
                                this.chords.add(chord.getName());
104
 
                        }
105
 
                }
106
 
                
107
 
                if(selectionIndex >= 0 && selectionIndex < this.chords.getItemCount()){
108
 
                        this.chords.select(selectionIndex);
109
 
                }else if(selectionIndex > 0 && (selectionIndex - 1) < this.chords.getItemCount()){
110
 
                        this.chords.select((selectionIndex - 1));
111
 
                }
112
 
        }       
113
 
        
114
 
    private void showChord(int index) {         
115
 
        Chord chord = TuxGuitar.instance().getCustomChordManager().getChord(index);
116
 
        if (chord != null) {                            
117
 
            int firstFret = -1;
118
 
            for (int i = 0; i < chord.getStrings().length; i++) {
119
 
                int fretValue = chord.getFretValue(i);
120
 
                if (firstFret < 0 || (fretValue < firstFret && fretValue >= 0)) {
121
 
                    firstFret = fretValue;
122
 
                }
123
 
            }
124
 
            editor.setFret((short)((firstFret<=0?1:firstFret)));
125
 
            for (int i = 0; i < chord.getStrings().length; i++) {
126
 
                int fretValue = chord.getFretValue(i);
127
 
                editor.addValue(fretValue, i + 1);
128
 
            }                       
129
 
            editor.redraw();
130
 
            
131
 
        }
132
 
    }           
133
 
        
134
 
        private void addCustomChord(){
135
 
                Chord chord = editor.getCurrentChord();         
136
 
                if(chord != null){
137
 
                        String name = new NameDialog().open();
138
 
                        if(name != null){
139
 
                                if(name.length() == 0){
140
 
                                        String title = TuxGuitar.getProperty("error");
141
 
                                        String message = TuxGuitar.getProperty("chord.custom.name-empty-error");
142
 
                                        new MessageDialog(title,message,SWT.ICON_ERROR).show(getShell());
143
 
                                        return;                         
144
 
                                }                       
145
 
                                if(TuxGuitar.instance().getCustomChordManager().existOtherEqualCustomChord(name,-1)){
146
 
                                        String title = TuxGuitar.getProperty("error");
147
 
                                        String message = TuxGuitar.getProperty("chord.custom.name-exist-error");
148
 
                                        new MessageDialog(title,message,SWT.ICON_ERROR).show(getShell());
149
 
                                        return;
150
 
                                }
151
 
                                chord.setName(name);
152
 
                                TuxGuitar.instance().getCustomChordManager().addChord(chord);
153
 
                                loadChords();
154
 
                        }
155
 
                }
156
 
        }    
157
 
        
158
 
        private void renameCustomChord(int index){
159
 
                Chord chord =  TuxGuitar.instance().getCustomChordManager().getChord(index);
160
 
                if(chord != null){                      
161
 
                        String name = new NameDialog(chord.getName()).open();
162
 
                        if(name != null){
163
 
                                if(name.length() == 0){
164
 
                                        String title = TuxGuitar.getProperty("error");
165
 
                                        String message = TuxGuitar.getProperty("chord.custom.name-empty-error");
166
 
                                        new MessageDialog(title,message,SWT.ICON_ERROR).show(getShell());
167
 
                                        return;                         
168
 
                                }
169
 
                                if(TuxGuitar.instance().getCustomChordManager().existOtherEqualCustomChord(name,index)){
170
 
                                        String title = TuxGuitar.getProperty("error");
171
 
                                        String message = TuxGuitar.getProperty("chord.custom.name-exist-error");
172
 
                                        new MessageDialog(title,message,SWT.ICON_ERROR).show(getShell());
173
 
                                        return;
174
 
                                }                                                                       
175
 
                                TuxGuitar.instance().getCustomChordManager().renameChord(index,name);
176
 
                                loadChords();
177
 
                        }
178
 
                }
179
 
        }
180
 
        
181
 
        private void removeCustomChord(int index){
182
 
                if (index >= 0 && index < TuxGuitar.instance().getCustomChordManager().countChords()) {                 
183
 
                        TuxGuitar.instance().getCustomChordManager().removeChord(index);
184
 
                        loadChords();
185
 
                }
186
 
        }
187
 
 
188
 
        private class NameDialog{
189
 
                private String name;
190
 
                
191
 
                public NameDialog(String name){
192
 
                        this.name = name;
193
 
                }
194
 
                
195
 
                public NameDialog(){
196
 
                        this(new String());
197
 
                }
198
 
                
199
 
                public String open(){                           
200
 
                        final Shell dialog = new Shell(getShell(), SWT.DIALOG_TRIM | SWT.APPLICATION_MODAL);
201
 
 
202
 
                dialog.setLayout(new GridLayout());
203
 
                dialog.setText(TuxGuitar.getProperty("chord.name"));
204
 
 
205
 
                //-----------------NAME------------------------
206
 
                Composite composite = new Composite(dialog, SWT.NONE);
207
 
                composite.setLayout(new GridLayout(2,false));
208
 
                composite.setLayoutData(new GridData(SWT.FILL,SWT.FILL,true,true)); 
209
 
                    
210
 
                Label label = new Label(composite, SWT.NULL);
211
 
                label.setText(TuxGuitar.getProperty("chord.name"));
212
 
                                
213
 
                final Text text = new Text(composite, SWT.BORDER);                                    
214
 
                GridData textData = new GridData(SWT.FILL,SWT.FILL,true,true);    
215
 
                textData.minimumWidth = 150;  
216
 
                text.setLayoutData(textData);
217
 
                text.setText(this.name);
218
 
                //------------------BUTTONS--------------------------            
219
 
                Composite buttons = new Composite(dialog, SWT.NONE);
220
 
                buttons.setLayout(new GridLayout(2,false));
221
 
                buttons.setLayoutData(new GridData(SWT.END,SWT.FILL,true,true));        
222
 
                    
223
 
                GridData buttonData = new GridData(SWT.FILL,SWT.FILL,true,true);    
224
 
                buttonData.minimumWidth = 80;
225
 
                buttonData.minimumHeight = 25;     
226
 
                    
227
 
                final Button buttonOK = new Button(buttons, SWT.PUSH);
228
 
                buttonOK.setText(TuxGuitar.getProperty("ok"));
229
 
                buttonOK.setLayoutData(buttonData);
230
 
                buttonOK.addSelectionListener(new SelectionAdapter() {
231
 
                        public void widgetSelected(SelectionEvent arg0) {   
232
 
                        name = text.getText();
233
 
                                dialog.dispose();              
234
 
                    }
235
 
                });
236
 
                
237
 
                Button buttonCancel = new Button(buttons, SWT.PUSH);
238
 
                buttonCancel.setText(TuxGuitar.getProperty("cancel"));
239
 
                buttonCancel.setLayoutData(buttonData);
240
 
                buttonCancel.addSelectionListener(new SelectionAdapter() {
241
 
                        public void widgetSelected(SelectionEvent arg0) {
242
 
                                name = null;
243
 
                                dialog.dispose();
244
 
                        }
245
 
                });
246
 
                    
247
 
                dialog.pack();
248
 
                dialog.open();
249
 
 
250
 
                int x = getShell().getBounds().x + (getShell().getBounds().width - dialog.getSize().x) / 2;
251
 
                int y = getShell().getBounds().y + (getShell().getBounds().height - dialog.getSize().y) / 2;
252
 
                dialog.setLocation(x, y);
253
 
                
254
 
                while (!dialog.isDisposed()) {
255
 
                if (!dialog.getDisplay().readAndDispatch()) {
256
 
                        dialog.getDisplay().sleep();
257
 
                }
258
 
            }           
259
 
                
260
 
                return this.name;
261
 
                        
262
 
                }                               
263
 
                
264
 
        }
265
 
}
 
 
b'\\ No newline at end of file'