/[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 1480 - (show annotations) (download)
Wed Nov 14 21:39:45 2007 UTC (16 years, 5 months ago) by iliev
File size: 16715 byte(s)
* added Windows file path support

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

  ViewVC Help
Powered by ViewVC