/[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 1343 - (show annotations) (download)
Tue Sep 11 15:38:28 2007 UTC (16 years, 7 months ago) by iliev
File size: 16483 byte(s)
* JS Classic: Added new button to sampler channels
  for starting an instrument editor
* The last used instrument selection method is now
  saved for the next session
* Fantasia: Removed the lock border from non-editable
  text fields in properties panes and LS Console

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

  ViewVC Help
Powered by ViewVC