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

« back to all changes in this revision

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