/[svn]/jsampler/trunk/src/org/jsampler/view/std/JSInstrumentChooser.java
ViewVC logotype

Contents of /jsampler/trunk/src/org/jsampler/view/std/JSInstrumentChooser.java

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1540 - (show annotations) (download)
Mon Dec 3 23:22:02 2007 UTC (16 years, 4 months ago) by iliev
File size: 17527 byte(s)
* Fantasia: by default the volume values are now shown in decibels
* Implemented support for retrieving instrument information
  from instrument files
* Some bugfixes and enhancements

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.std;
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.Icon;
42 import javax.swing.JButton;
43 import javax.swing.JComboBox;
44 import javax.swing.JFileChooser;
45 import javax.swing.JLabel;
46 import javax.swing.JOptionPane;
47 import javax.swing.JPanel;
48 import javax.swing.JRadioButton;
49
50 import javax.swing.event.ChangeEvent;
51 import javax.swing.event.ChangeListener;
52 import javax.swing.event.DocumentEvent;
53 import javax.swing.event.DocumentListener;
54
55 import net.sf.juife.OkCancelDialog;
56
57 import net.sf.juife.event.TaskEvent;
58 import net.sf.juife.event.TaskListener;
59
60 import org.jsampler.CC;
61 import org.jsampler.OrchestraInstrument;
62 import org.jsampler.JSPrefs;
63 import org.jsampler.OrchestraModel;
64
65 import org.jsampler.task.Global;
66 import org.jsampler.task.InstrumentsDb;
67
68 import org.linuxsampler.lscp.Instrument;
69
70 import static org.jsampler.view.std.StdI18n.i18n;
71 import static org.linuxsampler.lscp.Parser.*;
72
73 /**
74 *
75 * @author Grigor Iliev
76 */
77 public class JSInstrumentChooser extends OkCancelDialog {
78 private final JRadioButton rbSelectFromOrchestra =
79 new JRadioButton(i18n.getLabel("JSInstrumentChooser.rbSelectFromOrchestra"));
80 private final JRadioButton rbSelectFromDb =
81 new JRadioButton(i18n.getLabel("JSInstrumentChooser.rbSelectFromDb"));
82 private final JRadioButton rbSelectFromFile =
83 new JRadioButton(i18n.getLabel("JSInstrumentChooser.rbSelectFromFile"));
84
85
86 private final JLabel lOrchestra =
87 new JLabel(i18n.getLabel("JSInstrumentChooser.lOrchestra"));
88
89 private final JLabel lInstrument =
90 new JLabel(i18n.getLabel("JSInstrumentChooser.lInstrument"));
91
92 private final JComboBox cbOrchestras = new JComboBox();
93 private final JComboBox cbInstruments = new JComboBox();
94
95
96 private final JComboBox cbDbInstrument = StdUtils.createPathComboBox();
97 private final JButton btnBrowseDb;
98
99 private final JLabel lFilename = new JLabel(i18n.getLabel("JSInstrumentChooser.lFilename"));
100 private final JLabel lIndex = new JLabel(i18n.getLabel("JSInstrumentChooser.lIndex"));
101
102 private final JComboBox cbFilename = StdUtils.createPathComboBox();
103 private final JComboBox cbIndex = new JComboBox();
104
105 private final JButton btnBrowse;
106
107 private String instrumentFile = null;
108 private int instrumentIndex = 0;
109 private String engine = null;
110
111 /**
112 * Creates a new instance of JSInstrumentChooser
113 */
114 public JSInstrumentChooser(Frame owner) {
115 super(owner, i18n.getLabel("JSInstrumentChooser.title"));
116
117 btnOk.setEnabled(false);
118 Icon iconBrowse = CC.getViewConfig().getInstrumentsDbTreeView().getOpenIcon();
119 btnBrowseDb = new JButton(iconBrowse);
120 btnBrowse = new JButton(iconBrowse);
121
122 ButtonGroup group = new ButtonGroup();
123 group.add(rbSelectFromOrchestra);
124 group.add(rbSelectFromDb);
125 group.add(rbSelectFromFile);
126
127 rbSelectFromOrchestra.addActionListener(getHandler());
128 rbSelectFromDb.addActionListener(getHandler());
129 rbSelectFromFile.addActionListener(getHandler());
130 rbSelectFromOrchestra.doClick(0);
131
132 cbOrchestras.addFocusListener(getHandler());
133 cbInstruments.addFocusListener(getHandler());
134
135 cbDbInstrument.addFocusListener(getHandler());
136 cbDbInstrument.addActionListener(getHandler());
137 cbDbInstrument.addActionListener(new ActionListener() {
138 public void
139 actionPerformed(ActionEvent e) {
140 if(!rbSelectFromDb.isSelected()) rbSelectFromDb.doClick(0);
141 }
142 });
143
144 String[] instrs = preferences().getStringListProperty("recentDbInstruments");
145 for(String s : instrs) cbDbInstrument.addItem(s);
146 cbDbInstrument.setSelectedItem(null);
147
148 cbFilename.addFocusListener(getHandler());
149 cbFilename.addActionListener(getHandler());
150 String[] files = preferences().getStringListProperty("recentInstrumentFiles");
151 for(String s : files) cbFilename.addItem(s);
152 cbFilename.setSelectedItem(null);
153
154 cbFilename.addActionListener(new ActionListener() {
155 public void
156 actionPerformed(ActionEvent e) {
157 if(!rbSelectFromFile.isSelected()) rbSelectFromFile.doClick(0);
158 updateFileInstruments();
159 }
160 });
161
162 cbIndex.addFocusListener(getHandler());
163 cbIndex.addActionListener(getHandler());
164 cbFilename.addActionListener(new ActionListener() {
165 public void
166 actionPerformed(ActionEvent e) {
167 if(!rbSelectFromFile.isSelected()) rbSelectFromFile.doClick(0);
168 }
169 });
170
171 for(int i = 0; i < 101; i++) cbIndex.addItem(i);
172
173 JPanel mainPane = new JPanel();
174 mainPane.setLayout(new BoxLayout(mainPane, BoxLayout.Y_AXIS));
175
176 rbSelectFromOrchestra.setAlignmentX(LEFT_ALIGNMENT);
177 mainPane.add(rbSelectFromOrchestra);
178
179 JPanel orchestraPane = createOrchestraPane();
180 orchestraPane.setBorder(BorderFactory.createEmptyBorder(0, 32, 17, 0));
181 mainPane.add(orchestraPane);
182
183 rbSelectFromDb.setAlignmentX(LEFT_ALIGNMENT);
184 mainPane.add(rbSelectFromDb);
185
186 JPanel dbPane = createDbPane();
187 dbPane.setBorder(BorderFactory.createEmptyBorder(0, 32, 17, 0));
188 mainPane.add(dbPane);
189
190 rbSelectFromFile.setAlignmentX(LEFT_ALIGNMENT);
191 mainPane.add(rbSelectFromFile);
192
193 JPanel filePane = createFilePane();
194 filePane.setBorder(BorderFactory.createEmptyBorder(0, 32, 0, 0));
195 mainPane.add(filePane);
196
197 setMainPane(mainPane);
198
199 if(!CC.getSamplerModel().getServerInfo().hasInstrumentsDbSupport()) {
200 rbSelectFromDb.setEnabled(false);
201 cbDbInstrument.setEnabled(false);
202 btnBrowseDb.setEnabled(false);
203 } else {
204 btnBrowseDb.requestFocusInWindow();
205 }
206
207 int i = preferences().getIntProperty("lastUsedOrchestraIndex", 0);
208 if(CC.getOrchestras().getOrchestraCount() > i) {
209 cbOrchestras.setSelectedIndex(i);
210 i = preferences().getIntProperty("lastUsedOrchestraInstrumentIndex", 0);
211 if(cbInstruments.getItemCount() > i) cbInstruments.setSelectedIndex(i);
212 }
213
214 String s = preferences().getStringProperty("lastUsedInstrumentSelectionMethod");
215 if("fromOrchestra".equals(s)) {
216 if(!rbSelectFromOrchestra.isSelected()) rbSelectFromOrchestra.doClick(0);
217 cbInstruments.requestFocusInWindow();
218 } else if("fromDb".equals(s)) {
219 if(!rbSelectFromDb.isSelected()) rbSelectFromDb.doClick(0);
220 } else if("fromFile".equals(s)) {
221 if(!rbSelectFromFile.isSelected()) rbSelectFromFile.doClick(0);
222 btnBrowse.requestFocusInWindow();
223 } else {
224 if(!rbSelectFromOrchestra.isSelected()) rbSelectFromOrchestra.doClick(0);
225 }
226
227 updateState();
228 }
229
230 private JPanel
231 createOrchestraPane() {
232 GridBagLayout gridbag = new GridBagLayout();
233 GridBagConstraints c = new GridBagConstraints();
234
235 JPanel p = new JPanel();
236
237 p.setLayout(gridbag);
238
239 c.fill = GridBagConstraints.NONE;
240
241 c.gridx = 0;
242 c.gridy = 0;
243 c.anchor = GridBagConstraints.EAST;
244 c.insets = new Insets(0, 0, 6, 6);
245 gridbag.setConstraints(lOrchestra, c);
246 p.add(lOrchestra);
247
248 c.gridx = 0;
249 c.gridy = 1;
250 c.insets = new Insets(0, 0, 0, 6);
251 gridbag.setConstraints(lInstrument, c);
252 p.add(lInstrument);
253
254 c.gridx = 1;
255 c.gridy = 0;
256 c.weightx = 1.0;
257 c.insets = new Insets(0, 0, 6, 0);
258 c.fill = GridBagConstraints.HORIZONTAL;
259 c.anchor = GridBagConstraints.NORTH;
260 gridbag.setConstraints(cbOrchestras, c);
261 p.add(cbOrchestras);
262
263 c.gridx = 1;
264 c.gridy = 1;
265 c.insets = new Insets(0, 0, 0, 0);
266 gridbag.setConstraints(cbInstruments, c);
267 p.add(cbInstruments);
268
269 cbOrchestras.addActionListener(new ActionListener() {
270 public void
271 actionPerformed(ActionEvent e) { orchestraChanged(); }
272 });
273
274 cbInstruments.addActionListener(new ActionListener() {
275 public void
276 actionPerformed(ActionEvent e) { instrumentChanged(); }
277 });
278
279 for(int i = 0; i < CC.getOrchestras().getOrchestraCount(); i++) {
280 cbOrchestras.addItem(CC.getOrchestras().getOrchestra(i));
281 }
282
283 p.setAlignmentX(LEFT_ALIGNMENT);
284 return p;
285 }
286
287 protected JSPrefs
288 preferences() { return CC.getViewConfig().preferences(); }
289
290 private void
291 orchestraChanged() {
292 OrchestraModel om = (OrchestraModel)cbOrchestras.getSelectedItem();
293 String s = om == null ? null : om.getDescription();
294 if(s != null && s.length() == 0) s = null;
295 cbOrchestras.setToolTipText(s);
296
297 cbInstruments.removeAllItems();
298 if(om == null || om.getInstrumentCount() == 0) {
299 cbInstruments.setEnabled(false);
300 return;
301 }
302
303 cbInstruments.setEnabled(true);
304
305 for(int i = 0; i < om.getInstrumentCount(); i++) {
306 cbInstruments.addItem(om.getInstrument(i));
307 }
308 }
309
310 private void
311 instrumentChanged() {
312 OrchestraInstrument instr = (OrchestraInstrument)cbInstruments.getSelectedItem();
313 String s = instr == null ? null : instr.getDescription();
314 if(s != null && s.length() == 0) s = null;
315 cbInstruments.setToolTipText(s);
316
317 btnOk.setEnabled(instr != null);
318 }
319
320
321
322 private JPanel
323 createDbPane() {
324 JPanel p = new JPanel();
325 p.setLayout(new BoxLayout(p, BoxLayout.X_AXIS));
326 p.add(cbDbInstrument);
327 p.add(Box.createRigidArea(new Dimension(6, 0)));
328 btnBrowseDb.setMargin(new Insets(0, 0, 0, 0));
329 p.add(btnBrowseDb);
330
331
332
333 cbDbInstrument.setPreferredSize (
334 new Dimension(200, cbDbInstrument.getPreferredSize().height)
335 );
336
337 btnBrowseDb.addActionListener(new ActionListener() {
338 public void
339 actionPerformed(ActionEvent e) { onBrowseDb(); }
340 });
341
342 p.setAlignmentX(LEFT_ALIGNMENT);
343
344 return p;
345 }
346
347 private JPanel
348 createFilePane() {
349 GridBagLayout gridbag = new GridBagLayout();
350 GridBagConstraints c = new GridBagConstraints();
351
352 JPanel filePane = new JPanel();
353
354 filePane.setLayout(gridbag);
355
356 c.fill = GridBagConstraints.NONE;
357
358 c.gridx = 0;
359 c.gridy = 0;
360 c.anchor = GridBagConstraints.EAST;
361 c.insets = new Insets(3, 3, 3, 3);
362 gridbag.setConstraints(lFilename, c);
363 filePane.add(lFilename);
364
365 c.gridx = 0;
366 c.gridy = 1;
367 gridbag.setConstraints(lIndex, c);
368 filePane.add(lIndex);
369
370 btnBrowse.setMargin(new Insets(0, 0, 0, 0));
371 btnBrowse.setToolTipText(i18n.getLabel("JSInstrumentChooser.btnBrowse"));
372 c.gridx = 2;
373 c.gridy = 0;
374 gridbag.setConstraints(btnBrowse, c);
375 filePane.add(btnBrowse);
376
377 c.fill = GridBagConstraints.HORIZONTAL;
378 c.gridx = 1;
379 c.gridy = 0;
380 c.weightx = 1.0;
381 c.anchor = GridBagConstraints.WEST;
382 gridbag.setConstraints(cbFilename, c);
383 filePane.add(cbFilename);
384
385 c.gridx = 1;
386 c.gridy = 1;
387 gridbag.setConstraints(cbIndex, c);
388 filePane.add(cbIndex);
389
390 cbFilename.setPreferredSize (
391 new Dimension(200, cbFilename.getPreferredSize().height)
392 );
393
394 btnBrowse.addActionListener(new ActionListener() {
395 public void
396 actionPerformed(ActionEvent e) { onBrowse(); }
397 });
398
399 filePane.setAlignmentX(LEFT_ALIGNMENT);
400 return filePane;
401 }
402
403 protected void
404 onOk() {
405 if(!btnOk.isEnabled()) return;
406
407 String s = "lastUsedInstrumentSelectionMethod";
408
409 if(rbSelectFromOrchestra.isSelected()) {
410 OrchestraInstrument instr = (OrchestraInstrument)cbInstruments.getSelectedItem();
411 instrumentFile = instr.getFilePath();
412 instrumentIndex = instr.getInstrumentIndex();
413 engine = instr.getEngine();
414 setVisible(false);
415
416 int i = cbOrchestras.getSelectedIndex();
417 if(i >= 0) preferences().setIntProperty("lastUsedOrchestraIndex", i);
418
419 i = cbInstruments.getSelectedIndex();
420 if(i >= 0) {
421 preferences().setIntProperty("lastUsedOrchestraInstrumentIndex", i);
422 }
423
424 preferences().setStringProperty(s, "fromOrchestra");
425
426 return;
427 }
428
429 if(rbSelectFromFile.isSelected()) {
430 instrumentFile = cbFilename.getSelectedItem().toString();
431 instrumentIndex = cbIndex.getSelectedIndex();
432
433 StdUtils.updateRecentElements("recentInstrumentFiles", instrumentFile);
434 preferences().setStringProperty(s, "fromFile");
435 setVisible(false);
436 return;
437 }
438
439 if(!rbSelectFromDb.isSelected()) return;
440
441 preferences().setStringProperty(s, "fromDb");
442
443 String instr = cbDbInstrument.getSelectedItem().toString();
444 preferences().setStringProperty("lastUsedDbInstrument", instr);
445 final InstrumentsDb.GetInstrument t = new InstrumentsDb.GetInstrument(instr);
446
447 StdUtils.updateRecentElements("recentDbInstruments", instr);
448
449 t.addTaskListener(new TaskListener() {
450 public void
451 taskPerformed(TaskEvent e) {
452 updateState();
453 if(t.doneWithErrors()) return;
454
455 instrumentFile = t.getResult().getFilePath();
456 instrumentIndex = t.getResult().getInstrumentIndex();
457 engine = t.getResult().getFormatFamily(); // TODO: fix this
458 setVisible(false);
459 }
460 });
461
462 btnOk.setEnabled(false);
463 CC.getTaskQueue().add(t);
464 }
465
466 protected void
467 onCancel() { setVisible(false); }
468
469 private void
470 onBrowse() {
471 if(!rbSelectFromFile.isSelected()) rbSelectFromFile.doClick(0);
472 String s = preferences().getStringProperty("lastInstrumentLocation");
473 JFileChooser fc = new JFileChooser(s);
474 int result = fc.showOpenDialog(this);
475 if(result != JFileChooser.APPROVE_OPTION) return;
476
477 String path = fc.getSelectedFile().getAbsolutePath();
478 if(java.io.File.separatorChar == '\\') {
479 path = path.replace('\\', '/');
480 }
481 cbFilename.setSelectedItem(toEscapedString(path));
482 btnOk.requestFocusInWindow();
483
484 path = fc.getCurrentDirectory().getAbsolutePath();
485 preferences().setStringProperty("lastInstrumentLocation", path);
486 }
487
488 private void
489 onBrowseDb() {
490 if(!rbSelectFromDb.isSelected()) rbSelectFromDb.doClick(0);
491 JSDbInstrumentChooser dlg;
492 dlg = new JSDbInstrumentChooser(JSInstrumentChooser.this);
493 Object o = cbDbInstrument.getSelectedItem();
494 if(o != null && o.toString().length() > 0) dlg.setSelectedInstrument(o.toString());
495 else {
496 String s = preferences().getStringProperty("lastUsedDbInstrument", "");
497 if(s.length() > 0) dlg.setSelectedInstrument(s);
498 else dlg.setSelectedDirectory("/");
499 }
500 dlg.setVisible(true);
501 if(dlg.isCancelled()) return;
502 cbDbInstrument.setSelectedItem(dlg.getSelectedInstrument());
503 cbDbInstrument.requestFocus();
504 }
505
506 /**
507 * Gets the name of the selected instrument file.
508 * @return The name of the selected instrument file.
509 */
510 public String
511 getInstrumentFile() { return instrumentFile; }
512
513 /**
514 * Gets the index of the instrument in the instrument file.
515 * @return The index of the instrument in the instrument file.
516 */
517 public int
518 getInstrumentIndex() { return instrumentIndex; }
519
520 public String
521 getEngine() { return engine; }
522
523 private void
524 updateState() {
525 boolean b = false;
526 if(rbSelectFromOrchestra.isSelected()) {
527 b = cbInstruments.getSelectedItem() != null;
528 } else if(rbSelectFromDb.isSelected()) {
529 Object o = cbDbInstrument.getSelectedItem();
530 b = o != null && o.toString().length() > 0;
531 } else if(rbSelectFromFile.isSelected()) {
532 Object o = cbFilename.getSelectedItem();
533 if(o == null) b = false;
534 else b = o.toString().length() > 0;
535 o = cbIndex.getSelectedItem();
536 if(o == null || o.toString().length() == 0) b = false;
537 }
538
539 btnOk.setEnabled(b);
540 }
541
542 private void
543 updateFileInstruments() {
544 Object o = cbFilename.getSelectedItem();
545 if(o == null) return;
546 String s = o.toString();
547 final Global.GetFileInstruments t = new Global.GetFileInstruments(s);
548
549 t.addTaskListener(new TaskListener() {
550 public void
551 taskPerformed(TaskEvent e) {
552 Instrument[] instrs = t.getResult();
553 if(instrs == null) {
554 cbIndex.removeAllItems();
555 for(int i = 0; i < 101; i++) cbIndex.addItem(i);
556 return;
557 }
558
559 cbIndex.removeAllItems();
560 for(int i = 0; i < instrs.length; i++) {
561 cbIndex.addItem(i + " - " + instrs[i].getName());
562 }
563 }
564 });
565
566 CC.getTaskQueue().add(t);
567 }
568
569 private final EventHandler eventHandler = new EventHandler();
570
571 private EventHandler
572 getHandler() { return eventHandler; }
573
574 private class EventHandler extends FocusAdapter
575 implements ActionListener, DocumentListener {
576
577 public void
578 actionPerformed(ActionEvent e) {
579 updateState();
580 }
581
582 // DocumentListener
583 public void
584 insertUpdate(DocumentEvent e) { updateState(); }
585
586 public void
587 removeUpdate(DocumentEvent e) { updateState(); }
588
589 public void
590 changedUpdate(DocumentEvent e) { updateState(); }
591
592 // FocusListener
593 public void
594 focusGained(FocusEvent e) {
595 Object src = e.getSource();
596 if(src == cbInstruments || src == cbOrchestras) {
597 if(!rbSelectFromOrchestra.isSelected()) {
598 rbSelectFromOrchestra.doClick(0);
599 }
600 } else if(src == cbDbInstrument) {
601 if(!rbSelectFromDb.isSelected()) rbSelectFromDb.doClick(0);
602 } else if(src == cbFilename || src == cbIndex) {
603 if(!rbSelectFromFile.isSelected()) rbSelectFromFile.doClick(0);
604 }
605 }
606 }
607 }

  ViewVC Help
Powered by ViewVC