1 |
/* |
2 |
* JSampler - a java front-end for LinuxSampler |
3 |
* |
4 |
* Copyright (C) 2005-2007 Grigor Iliev <grigor@grigoriliev.com> |
5 |
* |
6 |
* This file is part of JSampler. |
7 |
* |
8 |
* JSampler is free software; you can redistribute it and/or modify |
9 |
* it under the terms of the GNU General Public License version 2 |
10 |
* as published by the Free Software Foundation. |
11 |
* |
12 |
* JSampler is distributed in the hope that it will be useful, |
13 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
14 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
15 |
* GNU General Public License for more details. |
16 |
* |
17 |
* You should have received a copy of the GNU General Public License |
18 |
* along with JSampler; if not, write to the Free Software |
19 |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, |
20 |
* MA 02111-1307 USA |
21 |
*/ |
22 |
|
23 |
package org.jsampler.view.classic; |
24 |
|
25 |
import java.awt.Dimension; |
26 |
import java.awt.Frame; |
27 |
import java.awt.GridBagConstraints; |
28 |
import java.awt.GridBagLayout; |
29 |
import java.awt.Insets; |
30 |
|
31 |
import java.awt.event.ActionEvent; |
32 |
import java.awt.event.ActionListener; |
33 |
|
34 |
import java.awt.event.FocusAdapter; |
35 |
import java.awt.event.FocusEvent; |
36 |
|
37 |
import javax.swing.BorderFactory; |
38 |
import javax.swing.Box; |
39 |
import javax.swing.BoxLayout; |
40 |
import javax.swing.ButtonGroup; |
41 |
import javax.swing.JButton; |
42 |
import javax.swing.JComboBox; |
43 |
import javax.swing.JFileChooser; |
44 |
import javax.swing.JLabel; |
45 |
import javax.swing.JOptionPane; |
46 |
import javax.swing.JPanel; |
47 |
import javax.swing.JRadioButton; |
48 |
import javax.swing.JSpinner; |
49 |
import javax.swing.JTextField; |
50 |
import javax.swing.SpinnerNumberModel; |
51 |
|
52 |
import javax.swing.event.ChangeEvent; |
53 |
import javax.swing.event.ChangeListener; |
54 |
import javax.swing.event.DocumentEvent; |
55 |
import javax.swing.event.DocumentListener; |
56 |
|
57 |
import net.sf.juife.OkCancelDialog; |
58 |
|
59 |
import net.sf.juife.event.TaskEvent; |
60 |
import net.sf.juife.event.TaskListener; |
61 |
|
62 |
import org.jsampler.CC; |
63 |
import org.jsampler.Instrument; |
64 |
import org.jsampler.OrchestraModel; |
65 |
|
66 |
import org.jsampler.task.InstrumentsDb; |
67 |
|
68 |
import static org.jsampler.view.classic.ClassicI18n.i18n; |
69 |
|
70 |
|
71 |
/** |
72 |
* |
73 |
* @author Grigor Iliev |
74 |
*/ |
75 |
public class InstrumentChooser extends OkCancelDialog { |
76 |
private final JRadioButton rbSelectFromOrchestra = |
77 |
new JRadioButton(i18n.getLabel("InstrumentChooser.rbSelectFromOrchestra")); |
78 |
private final JRadioButton rbSelectFromDb = |
79 |
new JRadioButton(i18n.getLabel("InstrumentChooser.rbSelectFromDb")); |
80 |
private final JRadioButton rbSelectFromFile = |
81 |
new JRadioButton(i18n.getLabel("InstrumentChooser.rbSelectFromFile")); |
82 |
|
83 |
|
84 |
private final JLabel lOrchestra = new JLabel(i18n.getLabel("InstrumentChooser.lOrchestra")); |
85 |
|
86 |
private final JLabel lInstrument = |
87 |
new JLabel(i18n.getLabel("InstrumentChooser.lInstrument")); |
88 |
|
89 |
private final JComboBox cbOrchestras = new JComboBox(); |
90 |
private final JComboBox cbInstruments = new JComboBox(); |
91 |
|
92 |
|
93 |
private final JTextField tfDbInstrument = new JTextField(); |
94 |
private final JButton btnBrowseDb = new JButton(Res.iconFolderOpen16); |
95 |
|
96 |
private final JLabel lFilename = new JLabel(i18n.getLabel("InstrumentChooser.lFilename")); |
97 |
private final JLabel lIndex = new JLabel(i18n.getLabel("InstrumentChooser.lIndex")); |
98 |
|
99 |
private final JTextField tfFilename = new JTextField(); |
100 |
private final JSpinner spinnerIndex = new JSpinner(new SpinnerNumberModel(0, 0, 500, 1)); |
101 |
|
102 |
private final JButton btnBrowse = new JButton(Res.iconFolderOpen16); |
103 |
|
104 |
private String instrumentFile = null; |
105 |
private int instrumentIndex = 0; |
106 |
private String engine = null; |
107 |
|
108 |
/** Creates a new instance of InstrumentChooser */ |
109 |
public InstrumentChooser(Frame owner) { |
110 |
super(owner, i18n.getLabel("InstrumentChooser.title")); |
111 |
|
112 |
btnOk.setEnabled(false); |
113 |
|
114 |
ButtonGroup group = new ButtonGroup(); |
115 |
group.add(rbSelectFromOrchestra); |
116 |
group.add(rbSelectFromDb); |
117 |
group.add(rbSelectFromFile); |
118 |
|
119 |
rbSelectFromOrchestra.addActionListener(getHandler()); |
120 |
rbSelectFromDb.addActionListener(getHandler()); |
121 |
rbSelectFromFile.addActionListener(getHandler()); |
122 |
rbSelectFromOrchestra.doClick(0); |
123 |
|
124 |
cbOrchestras.addFocusListener(getHandler()); |
125 |
cbInstruments.addFocusListener(getHandler()); |
126 |
|
127 |
tfDbInstrument.addFocusListener(getHandler()); |
128 |
tfDbInstrument.getDocument().addDocumentListener(getHandler()); |
129 |
|
130 |
tfFilename.addFocusListener(getHandler()); |
131 |
tfFilename.getDocument().addDocumentListener(getHandler()); |
132 |
|
133 |
spinnerIndex.addChangeListener(new ChangeListener() { |
134 |
public void |
135 |
stateChanged(ChangeEvent e) { |
136 |
if(!rbSelectFromFile.isSelected()) rbSelectFromFile.doClick(0); |
137 |
} |
138 |
}); |
139 |
|
140 |
JPanel mainPane = new JPanel(); |
141 |
mainPane.setLayout(new BoxLayout(mainPane, BoxLayout.Y_AXIS)); |
142 |
|
143 |
rbSelectFromOrchestra.setAlignmentX(LEFT_ALIGNMENT); |
144 |
mainPane.add(rbSelectFromOrchestra); |
145 |
|
146 |
JPanel orchestraPane = createOrchestraPane(); |
147 |
orchestraPane.setBorder(BorderFactory.createEmptyBorder(0, 32, 17, 0)); |
148 |
mainPane.add(orchestraPane); |
149 |
|
150 |
rbSelectFromDb.setAlignmentX(LEFT_ALIGNMENT); |
151 |
mainPane.add(rbSelectFromDb); |
152 |
|
153 |
JPanel dbPane = createDbPane(); |
154 |
dbPane.setBorder(BorderFactory.createEmptyBorder(0, 32, 17, 0)); |
155 |
mainPane.add(dbPane); |
156 |
|
157 |
rbSelectFromFile.setAlignmentX(LEFT_ALIGNMENT); |
158 |
mainPane.add(rbSelectFromFile); |
159 |
|
160 |
JPanel filePane = createFilePane(); |
161 |
filePane.setBorder(BorderFactory.createEmptyBorder(0, 32, 0, 0)); |
162 |
mainPane.add(filePane); |
163 |
|
164 |
setMainPane(mainPane); |
165 |
|
166 |
if(!CC.getSamplerModel().getServerInfo().hasInstrumentsDbSupport()) { |
167 |
rbSelectFromDb.setEnabled(false); |
168 |
tfDbInstrument.setEnabled(false); |
169 |
btnBrowseDb.setEnabled(false); |
170 |
} else { |
171 |
btnBrowseDb.requestFocusInWindow(); |
172 |
} |
173 |
} |
174 |
|
175 |
private JPanel |
176 |
createOrchestraPane() { |
177 |
GridBagLayout gridbag = new GridBagLayout(); |
178 |
GridBagConstraints c = new GridBagConstraints(); |
179 |
|
180 |
JPanel p = new JPanel(); |
181 |
|
182 |
p.setLayout(gridbag); |
183 |
|
184 |
c.fill = GridBagConstraints.NONE; |
185 |
|
186 |
c.gridx = 0; |
187 |
c.gridy = 0; |
188 |
c.anchor = GridBagConstraints.EAST; |
189 |
c.insets = new Insets(0, 0, 6, 6); |
190 |
gridbag.setConstraints(lOrchestra, c); |
191 |
p.add(lOrchestra); |
192 |
|
193 |
c.gridx = 0; |
194 |
c.gridy = 1; |
195 |
c.insets = new Insets(0, 0, 0, 6); |
196 |
gridbag.setConstraints(lInstrument, c); |
197 |
p.add(lInstrument); |
198 |
|
199 |
c.gridx = 1; |
200 |
c.gridy = 0; |
201 |
c.weightx = 1.0; |
202 |
c.insets = new Insets(0, 0, 6, 0); |
203 |
c.fill = GridBagConstraints.HORIZONTAL; |
204 |
c.anchor = GridBagConstraints.NORTH; |
205 |
gridbag.setConstraints(cbOrchestras, c); |
206 |
p.add(cbOrchestras); |
207 |
|
208 |
c.gridx = 1; |
209 |
c.gridy = 1; |
210 |
c.insets = new Insets(0, 0, 0, 0); |
211 |
gridbag.setConstraints(cbInstruments, c); |
212 |
p.add(cbInstruments); |
213 |
|
214 |
cbOrchestras.addActionListener(new ActionListener() { |
215 |
public void |
216 |
actionPerformed(ActionEvent e) { orchestraChanged(); } |
217 |
}); |
218 |
|
219 |
cbInstruments.addActionListener(new ActionListener() { |
220 |
public void |
221 |
actionPerformed(ActionEvent e) { instrumentChanged(); } |
222 |
}); |
223 |
|
224 |
for(int i = 0; i < CC.getOrchestras().getOrchestraCount(); i++) { |
225 |
cbOrchestras.addItem(CC.getOrchestras().getOrchestra(i)); |
226 |
} |
227 |
|
228 |
p.setAlignmentX(LEFT_ALIGNMENT); |
229 |
return p; |
230 |
} |
231 |
|
232 |
private void |
233 |
orchestraChanged() { |
234 |
OrchestraModel om = (OrchestraModel)cbOrchestras.getSelectedItem(); |
235 |
String s = om == null ? null : om.getDescription(); |
236 |
if(s != null && s.length() == 0) s = null; |
237 |
cbOrchestras.setToolTipText(s); |
238 |
|
239 |
cbInstruments.removeAllItems(); |
240 |
if(om == null || om.getInstrumentCount() == 0) { |
241 |
cbInstruments.setEnabled(false); |
242 |
return; |
243 |
} |
244 |
|
245 |
cbInstruments.setEnabled(true); |
246 |
|
247 |
for(int i = 0; i < om.getInstrumentCount(); i++) { |
248 |
cbInstruments.addItem(om.getInstrument(i)); |
249 |
} |
250 |
} |
251 |
|
252 |
private void |
253 |
instrumentChanged() { |
254 |
Instrument instr = (Instrument)cbInstruments.getSelectedItem(); |
255 |
String s = instr == null ? null : instr.getDescription(); |
256 |
if(s != null && s.length() == 0) s = null; |
257 |
cbInstruments.setToolTipText(s); |
258 |
|
259 |
btnOk.setEnabled(instr != null); |
260 |
} |
261 |
|
262 |
|
263 |
|
264 |
private JPanel |
265 |
createDbPane() { |
266 |
JPanel p = new JPanel(); |
267 |
p.setLayout(new BoxLayout(p, BoxLayout.X_AXIS)); |
268 |
p.add(tfDbInstrument); |
269 |
p.add(Box.createRigidArea(new Dimension(6, 0))); |
270 |
btnBrowseDb.setMargin(new Insets(0, 0, 0, 0)); |
271 |
p.add(btnBrowseDb); |
272 |
//p.setMaximumSize(new Dimension(Short.MAX_VALUE, p.getPreferredSize().height)); |
273 |
|
274 |
btnBrowseDb.addActionListener(new ActionListener() { |
275 |
public void |
276 |
actionPerformed(ActionEvent e) { |
277 |
if(!rbSelectFromDb.isSelected()) rbSelectFromDb.doClick(0); |
278 |
DbInstrumentChooser dlg; |
279 |
dlg = new DbInstrumentChooser(InstrumentChooser.this); |
280 |
String s = tfDbInstrument.getText(); |
281 |
if(s.length() > 0) dlg.setSelectedInstrument(s); |
282 |
dlg.setVisible(true); |
283 |
if(dlg.isCancelled()) return; |
284 |
tfDbInstrument.setText(dlg.getSelectedInstrument()); |
285 |
} |
286 |
}); |
287 |
|
288 |
p.setAlignmentX(LEFT_ALIGNMENT); |
289 |
|
290 |
return p; |
291 |
} |
292 |
|
293 |
private JPanel |
294 |
createFilePane() { |
295 |
GridBagLayout gridbag = new GridBagLayout(); |
296 |
GridBagConstraints c = new GridBagConstraints(); |
297 |
|
298 |
JPanel filePane = new JPanel(); |
299 |
|
300 |
filePane.setLayout(gridbag); |
301 |
|
302 |
c.fill = GridBagConstraints.NONE; |
303 |
|
304 |
c.gridx = 0; |
305 |
c.gridy = 0; |
306 |
c.anchor = GridBagConstraints.EAST; |
307 |
c.insets = new Insets(3, 3, 3, 3); |
308 |
gridbag.setConstraints(lFilename, c); |
309 |
filePane.add(lFilename); |
310 |
|
311 |
c.gridx = 0; |
312 |
c.gridy = 1; |
313 |
gridbag.setConstraints(lIndex, c); |
314 |
filePane.add(lIndex); |
315 |
|
316 |
btnBrowse.setMargin(new Insets(0, 0, 0, 0)); |
317 |
btnBrowse.setToolTipText(i18n.getLabel("InstrumentChooser.btnBrowse")); |
318 |
c.gridx = 2; |
319 |
c.gridy = 0; |
320 |
gridbag.setConstraints(btnBrowse, c); |
321 |
filePane.add(btnBrowse); |
322 |
|
323 |
c.fill = GridBagConstraints.HORIZONTAL; |
324 |
c.gridx = 1; |
325 |
c.gridy = 0; |
326 |
c.weightx = 1.0; |
327 |
c.anchor = GridBagConstraints.WEST; |
328 |
gridbag.setConstraints(tfFilename, c); |
329 |
filePane.add(tfFilename); |
330 |
|
331 |
c.gridx = 1; |
332 |
c.gridy = 1; |
333 |
gridbag.setConstraints(spinnerIndex, c); |
334 |
filePane.add(spinnerIndex); |
335 |
|
336 |
tfFilename.setPreferredSize ( |
337 |
new Dimension(200, tfFilename.getPreferredSize().height) |
338 |
); |
339 |
|
340 |
btnBrowse.addActionListener(new ActionListener() { |
341 |
public void |
342 |
actionPerformed(ActionEvent e) { onBrowse(); } |
343 |
}); |
344 |
|
345 |
filePane.setAlignmentX(LEFT_ALIGNMENT); |
346 |
return filePane; |
347 |
} |
348 |
|
349 |
protected void |
350 |
onOk() { |
351 |
if(!btnOk.isEnabled()) return; |
352 |
if(rbSelectFromOrchestra.isSelected()) { |
353 |
Instrument instr = (Instrument)cbInstruments.getSelectedItem(); |
354 |
instrumentFile = instr.getPath(); |
355 |
instrumentIndex = instr.getInstrumentIndex(); |
356 |
engine = instr.getEngine(); |
357 |
setVisible(false); |
358 |
return; |
359 |
} |
360 |
|
361 |
if(rbSelectFromFile.isSelected()) { |
362 |
instrumentFile = tfFilename.getText(); |
363 |
instrumentIndex = Integer.parseInt(spinnerIndex.getValue().toString()); |
364 |
setVisible(false); |
365 |
return; |
366 |
} |
367 |
|
368 |
if(!rbSelectFromDb.isSelected()) return; |
369 |
|
370 |
String instr = tfDbInstrument.getText(); |
371 |
final InstrumentsDb.GetInstrument t = new InstrumentsDb.GetInstrument(instr); |
372 |
|
373 |
t.addTaskListener(new TaskListener() { |
374 |
public void |
375 |
taskPerformed(TaskEvent e) { |
376 |
updateState(); |
377 |
if(t.doneWithErrors()) return; |
378 |
|
379 |
instrumentFile = t.getResult().getFilePath(); |
380 |
instrumentIndex = t.getResult().getInstrumentIndex(); |
381 |
engine = t.getResult().getFormatFamily(); // TODO: fix this |
382 |
setVisible(false); |
383 |
} |
384 |
}); |
385 |
|
386 |
btnOk.setEnabled(false); |
387 |
CC.getTaskQueue().add(t); |
388 |
} |
389 |
|
390 |
protected void |
391 |
onCancel() { setVisible(false); } |
392 |
|
393 |
private void |
394 |
onBrowse() { |
395 |
if(!rbSelectFromFile.isSelected()) rbSelectFromFile.doClick(0); |
396 |
JFileChooser fc = new JFileChooser(); |
397 |
int result = fc.showOpenDialog(this); |
398 |
if(result == JFileChooser.APPROVE_OPTION) { |
399 |
tfFilename.setText(fc.getSelectedFile().getPath()); |
400 |
btnOk.requestFocusInWindow(); |
401 |
} |
402 |
} |
403 |
|
404 |
/** |
405 |
* Gets the name of the selected instrument file. |
406 |
* @return The name of the selected instrument file. |
407 |
*/ |
408 |
public String |
409 |
getInstrumentFile() { return instrumentFile; } |
410 |
|
411 |
/** |
412 |
* Gets the index of the instrument in the instrument file. |
413 |
* @return The index of the instrument in the instrument file. |
414 |
*/ |
415 |
public int |
416 |
getInstrumentIndex() { return instrumentIndex; } |
417 |
|
418 |
public String |
419 |
getEngine() { return engine; } |
420 |
|
421 |
private void |
422 |
updateState() { |
423 |
boolean b = false; |
424 |
if(rbSelectFromOrchestra.isSelected()) { |
425 |
b = cbInstruments.getSelectedItem() != null; |
426 |
} else if(rbSelectFromDb.isSelected()) { |
427 |
b = tfDbInstrument.getText().length() > 0; |
428 |
} else if(rbSelectFromFile.isSelected()) { |
429 |
b = tfFilename.getText().length() > 0; |
430 |
} |
431 |
|
432 |
btnOk.setEnabled(b); |
433 |
} |
434 |
|
435 |
private final EventHandler eventHandler = new EventHandler(); |
436 |
|
437 |
private EventHandler |
438 |
getHandler() { return eventHandler; } |
439 |
|
440 |
private class EventHandler extends FocusAdapter |
441 |
implements ActionListener, DocumentListener { |
442 |
|
443 |
public void |
444 |
actionPerformed(ActionEvent e) { |
445 |
updateState(); |
446 |
} |
447 |
|
448 |
// DocumentListener |
449 |
public void |
450 |
insertUpdate(DocumentEvent e) { updateState(); } |
451 |
|
452 |
public void |
453 |
removeUpdate(DocumentEvent e) { updateState(); } |
454 |
|
455 |
public void |
456 |
changedUpdate(DocumentEvent e) { updateState(); } |
457 |
|
458 |
// FocusListener |
459 |
public void |
460 |
focusGained(FocusEvent e) { |
461 |
Object src = e.getSource(); |
462 |
if(src == cbInstruments || src == cbOrchestras) { |
463 |
if(!rbSelectFromOrchestra.isSelected()) { |
464 |
rbSelectFromOrchestra.doClick(0); |
465 |
} |
466 |
} else if(src == tfDbInstrument) { |
467 |
if(!rbSelectFromDb.isSelected()) rbSelectFromDb.doClick(0); |
468 |
} else if(src == tfFilename) { |
469 |
if(!rbSelectFromFile.isSelected()) rbSelectFromFile.doClick(0); |
470 |
} |
471 |
} |
472 |
} |
473 |
} |