/[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 1308 - (show annotations) (download)
Tue Aug 28 17:00:19 2007 UTC (16 years, 8 months ago) by iliev
File size: 15685 byte(s)
* A lists of recently used instrument files, directories,
  database instruments, database directories are now available
  for quick access, where appropriate.

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

  ViewVC Help
Powered by ViewVC