/[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 1352 - (show annotations) (download)
Sun Sep 16 23:24:15 2007 UTC (16 years, 7 months ago) by iliev
File size: 16546 byte(s)
* instruments db: slashes-in-names are now escaped with \x2f
* some bugfixes

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 = new JComboBox();
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 = new JComboBox();
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 JSPrefs
284 preferences() { return CC.getViewConfig().preferences(); }
285
286 private void
287 orchestraChanged() {
288 OrchestraModel om = (OrchestraModel)cbOrchestras.getSelectedItem();
289 String s = om == null ? null : om.getDescription();
290 if(s != null && s.length() == 0) s = null;
291 cbOrchestras.setToolTipText(s);
292
293 cbInstruments.removeAllItems();
294 if(om == null || om.getInstrumentCount() == 0) {
295 cbInstruments.setEnabled(false);
296 return;
297 }
298
299 cbInstruments.setEnabled(true);
300
301 for(int i = 0; i < om.getInstrumentCount(); i++) {
302 cbInstruments.addItem(om.getInstrument(i));
303 }
304 }
305
306 private void
307 instrumentChanged() {
308 Instrument instr = (Instrument)cbInstruments.getSelectedItem();
309 String s = instr == null ? null : instr.getDescription();
310 if(s != null && s.length() == 0) s = null;
311 cbInstruments.setToolTipText(s);
312
313 btnOk.setEnabled(instr != null);
314 }
315
316
317
318 private JPanel
319 createDbPane() {
320 JPanel p = new JPanel();
321 p.setLayout(new BoxLayout(p, BoxLayout.X_AXIS));
322 p.add(cbDbInstrument);
323 p.add(Box.createRigidArea(new Dimension(6, 0)));
324 btnBrowseDb.setMargin(new Insets(0, 0, 0, 0));
325 p.add(btnBrowseDb);
326
327
328
329 cbDbInstrument.setPreferredSize (
330 new Dimension(200, cbDbInstrument.getPreferredSize().height)
331 );
332
333 btnBrowseDb.addActionListener(new ActionListener() {
334 public void
335 actionPerformed(ActionEvent e) { onBrowseDb(); }
336 });
337
338 p.setAlignmentX(LEFT_ALIGNMENT);
339
340 return p;
341 }
342
343 private JPanel
344 createFilePane() {
345 GridBagLayout gridbag = new GridBagLayout();
346 GridBagConstraints c = new GridBagConstraints();
347
348 JPanel filePane = new JPanel();
349
350 filePane.setLayout(gridbag);
351
352 c.fill = GridBagConstraints.NONE;
353
354 c.gridx = 0;
355 c.gridy = 0;
356 c.anchor = GridBagConstraints.EAST;
357 c.insets = new Insets(3, 3, 3, 3);
358 gridbag.setConstraints(lFilename, c);
359 filePane.add(lFilename);
360
361 c.gridx = 0;
362 c.gridy = 1;
363 gridbag.setConstraints(lIndex, c);
364 filePane.add(lIndex);
365
366 btnBrowse.setMargin(new Insets(0, 0, 0, 0));
367 btnBrowse.setToolTipText(i18n.getLabel("JSInstrumentChooser.btnBrowse"));
368 c.gridx = 2;
369 c.gridy = 0;
370 gridbag.setConstraints(btnBrowse, c);
371 filePane.add(btnBrowse);
372
373 c.fill = GridBagConstraints.HORIZONTAL;
374 c.gridx = 1;
375 c.gridy = 0;
376 c.weightx = 1.0;
377 c.anchor = GridBagConstraints.WEST;
378 gridbag.setConstraints(cbFilename, c);
379 filePane.add(cbFilename);
380
381 c.gridx = 1;
382 c.gridy = 1;
383 gridbag.setConstraints(spinnerIndex, c);
384 filePane.add(spinnerIndex);
385
386 cbFilename.setPreferredSize (
387 new Dimension(200, cbFilename.getPreferredSize().height)
388 );
389
390 btnBrowse.addActionListener(new ActionListener() {
391 public void
392 actionPerformed(ActionEvent e) { onBrowse(); }
393 });
394
395 filePane.setAlignmentX(LEFT_ALIGNMENT);
396 return filePane;
397 }
398
399 protected void
400 onOk() {
401 if(!btnOk.isEnabled()) return;
402
403 String s = "lastUsedInstrumentSelectionMethod";
404
405 if(rbSelectFromOrchestra.isSelected()) {
406 Instrument instr = (Instrument)cbInstruments.getSelectedItem();
407 instrumentFile = instr.getPath();
408 instrumentIndex = instr.getInstrumentIndex();
409 engine = instr.getEngine();
410 setVisible(false);
411
412 int i = cbOrchestras.getSelectedIndex();
413 if(i >= 0) preferences().setIntProperty("lastUsedOrchestraIndex", i);
414
415 i = cbInstruments.getSelectedIndex();
416 if(i >= 0) {
417 preferences().setIntProperty("lastUsedOrchestraInstrumentIndex", i);
418 }
419
420 preferences().setStringProperty(s, "fromOrchestra");
421
422 return;
423 }
424
425 if(rbSelectFromFile.isSelected()) {
426 instrumentFile = cbFilename.getSelectedItem().toString();
427 instrumentIndex = Integer.parseInt(spinnerIndex.getValue().toString());
428
429 StdUtils.updateRecentElements("recentInstrumentFiles", instrumentFile);
430 preferences().setStringProperty(s, "fromFile");
431 setVisible(false);
432 return;
433 }
434
435 if(!rbSelectFromDb.isSelected()) return;
436
437 preferences().setStringProperty(s, "fromDb");
438
439 String instr = cbDbInstrument.getSelectedItem().toString();
440 preferences().setStringProperty("lastUsedDbInstrument", instr);
441 final InstrumentsDb.GetInstrument t = new InstrumentsDb.GetInstrument(instr);
442
443 StdUtils.updateRecentElements("recentDbInstruments", instr);
444
445 t.addTaskListener(new TaskListener() {
446 public void
447 taskPerformed(TaskEvent e) {
448 updateState();
449 if(t.doneWithErrors()) return;
450
451 instrumentFile = t.getResult().getFilePath();
452 instrumentIndex = t.getResult().getInstrumentIndex();
453 engine = t.getResult().getFormatFamily(); // TODO: fix this
454 setVisible(false);
455 }
456 });
457
458 btnOk.setEnabled(false);
459 CC.getTaskQueue().add(t);
460 }
461
462 protected void
463 onCancel() { setVisible(false); }
464
465 private void
466 onBrowse() {
467 if(!rbSelectFromFile.isSelected()) rbSelectFromFile.doClick(0);
468 String s = preferences().getStringProperty("lastInstrumentLocation");
469 JFileChooser fc = new JFileChooser(s);
470 int result = fc.showOpenDialog(this);
471 if(result == JFileChooser.APPROVE_OPTION) {
472 cbFilename.setSelectedItem(toEscapedString(fc.getSelectedFile().getPath()));
473 btnOk.requestFocusInWindow();
474
475 String path = fc.getCurrentDirectory().getAbsolutePath();
476 preferences().setStringProperty("lastInstrumentLocation", path);
477 }
478 }
479
480 private void
481 onBrowseDb() {
482 if(!rbSelectFromDb.isSelected()) rbSelectFromDb.doClick(0);
483 JSDbInstrumentChooser dlg;
484 dlg = new JSDbInstrumentChooser(JSInstrumentChooser.this);
485 Object o = cbDbInstrument.getSelectedItem();
486 if(o != null && o.toString().length() > 0) dlg.setSelectedInstrument(o.toString());
487 else {
488 String s = preferences().getStringProperty("lastUsedDbInstrument", "");
489 if(s.length() > 0) dlg.setSelectedInstrument(s);
490 else dlg.setSelectedDirectory("/");
491 }
492 dlg.setVisible(true);
493 if(dlg.isCancelled()) return;
494 cbDbInstrument.setSelectedItem(dlg.getSelectedInstrument());
495 cbDbInstrument.requestFocus();
496 }
497
498 /**
499 * Gets the name of the selected instrument file.
500 * @return The name of the selected instrument file.
501 */
502 public String
503 getInstrumentFile() { return instrumentFile; }
504
505 /**
506 * Gets the index of the instrument in the instrument file.
507 * @return The index of the instrument in the instrument file.
508 */
509 public int
510 getInstrumentIndex() { return instrumentIndex; }
511
512 public String
513 getEngine() { return engine; }
514
515 private void
516 updateState() {
517 boolean b = false;
518 if(rbSelectFromOrchestra.isSelected()) {
519 b = cbInstruments.getSelectedItem() != null;
520 } else if(rbSelectFromDb.isSelected()) {
521 Object o = cbDbInstrument.getSelectedItem();
522 b = o != null && o.toString().length() > 0;
523 } else if(rbSelectFromFile.isSelected()) {
524 Object o = cbFilename.getSelectedItem();
525 if(o == null) b = false;
526 else b = o.toString().length() > 0;
527 }
528
529 btnOk.setEnabled(b);
530 }
531
532 private final EventHandler eventHandler = new EventHandler();
533
534 private EventHandler
535 getHandler() { return eventHandler; }
536
537 private class EventHandler extends FocusAdapter
538 implements ActionListener, DocumentListener {
539
540 public void
541 actionPerformed(ActionEvent e) {
542 updateState();
543 }
544
545 // DocumentListener
546 public void
547 insertUpdate(DocumentEvent e) { updateState(); }
548
549 public void
550 removeUpdate(DocumentEvent e) { updateState(); }
551
552 public void
553 changedUpdate(DocumentEvent e) { updateState(); }
554
555 // FocusListener
556 public void
557 focusGained(FocusEvent e) {
558 Object src = e.getSource();
559 if(src == cbInstruments || src == cbOrchestras) {
560 if(!rbSelectFromOrchestra.isSelected()) {
561 rbSelectFromOrchestra.doClick(0);
562 }
563 } else if(src == cbDbInstrument) {
564 if(!rbSelectFromDb.isSelected()) rbSelectFromDb.doClick(0);
565 } else if(src == cbFilename) {
566 if(!rbSelectFromFile.isSelected()) rbSelectFromFile.doClick(0);
567 }
568 }
569 }
570 }

  ViewVC Help
Powered by ViewVC