/[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 1302 - (show annotations) (download)
Sat Aug 25 15:32:54 2007 UTC (16 years, 8 months ago) by iliev
File size: 14572 byte(s)
* The location of the last loaded instrument is now
  saved for the next session

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.JTextField;
51 import javax.swing.SpinnerNumberModel;
52
53 import javax.swing.event.ChangeEvent;
54 import javax.swing.event.ChangeListener;
55 import javax.swing.event.DocumentEvent;
56 import javax.swing.event.DocumentListener;
57
58 import net.sf.juife.OkCancelDialog;
59
60 import net.sf.juife.event.TaskEvent;
61 import net.sf.juife.event.TaskListener;
62
63 import org.jsampler.CC;
64 import org.jsampler.Instrument;
65 import org.jsampler.JSPrefs;
66 import org.jsampler.OrchestraModel;
67
68 import org.jsampler.task.InstrumentsDb;
69
70 import static org.jsampler.view.std.StdI18n.i18n;
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 JTextField tfDbInstrument = new JTextField();
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 JTextField tfFilename = new JTextField();
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 tfDbInstrument.addFocusListener(getHandler());
135 tfDbInstrument.getDocument().addDocumentListener(getHandler());
136
137 tfFilename.addFocusListener(getHandler());
138 tfFilename.getDocument().addDocumentListener(getHandler());
139
140 spinnerIndex.addChangeListener(new ChangeListener() {
141 public void
142 stateChanged(ChangeEvent e) {
143 if(!rbSelectFromFile.isSelected()) rbSelectFromFile.doClick(0);
144 }
145 });
146
147 JPanel mainPane = new JPanel();
148 mainPane.setLayout(new BoxLayout(mainPane, BoxLayout.Y_AXIS));
149
150 rbSelectFromOrchestra.setAlignmentX(LEFT_ALIGNMENT);
151 mainPane.add(rbSelectFromOrchestra);
152
153 JPanel orchestraPane = createOrchestraPane();
154 orchestraPane.setBorder(BorderFactory.createEmptyBorder(0, 32, 17, 0));
155 mainPane.add(orchestraPane);
156
157 rbSelectFromDb.setAlignmentX(LEFT_ALIGNMENT);
158 mainPane.add(rbSelectFromDb);
159
160 JPanel dbPane = createDbPane();
161 dbPane.setBorder(BorderFactory.createEmptyBorder(0, 32, 17, 0));
162 mainPane.add(dbPane);
163
164 rbSelectFromFile.setAlignmentX(LEFT_ALIGNMENT);
165 mainPane.add(rbSelectFromFile);
166
167 JPanel filePane = createFilePane();
168 filePane.setBorder(BorderFactory.createEmptyBorder(0, 32, 0, 0));
169 mainPane.add(filePane);
170
171 setMainPane(mainPane);
172
173 if(!CC.getSamplerModel().getServerInfo().hasInstrumentsDbSupport()) {
174 rbSelectFromDb.setEnabled(false);
175 tfDbInstrument.setEnabled(false);
176 btnBrowseDb.setEnabled(false);
177 } else {
178 btnBrowseDb.requestFocusInWindow();
179 }
180
181 int i = preferences().getIntProperty("lastUsedOrchestraIndex", 0);
182 if(CC.getOrchestras().getOrchestraCount() > i) {
183 cbOrchestras.setSelectedIndex(i);
184 i = preferences().getIntProperty("lastUsedOrchestraInstrumentIndex", 0);
185 if(cbInstruments.getItemCount() > i) cbInstruments.setSelectedIndex(i);
186 }
187 }
188
189 private JPanel
190 createOrchestraPane() {
191 GridBagLayout gridbag = new GridBagLayout();
192 GridBagConstraints c = new GridBagConstraints();
193
194 JPanel p = new JPanel();
195
196 p.setLayout(gridbag);
197
198 c.fill = GridBagConstraints.NONE;
199
200 c.gridx = 0;
201 c.gridy = 0;
202 c.anchor = GridBagConstraints.EAST;
203 c.insets = new Insets(0, 0, 6, 6);
204 gridbag.setConstraints(lOrchestra, c);
205 p.add(lOrchestra);
206
207 c.gridx = 0;
208 c.gridy = 1;
209 c.insets = new Insets(0, 0, 0, 6);
210 gridbag.setConstraints(lInstrument, c);
211 p.add(lInstrument);
212
213 c.gridx = 1;
214 c.gridy = 0;
215 c.weightx = 1.0;
216 c.insets = new Insets(0, 0, 6, 0);
217 c.fill = GridBagConstraints.HORIZONTAL;
218 c.anchor = GridBagConstraints.NORTH;
219 gridbag.setConstraints(cbOrchestras, c);
220 p.add(cbOrchestras);
221
222 c.gridx = 1;
223 c.gridy = 1;
224 c.insets = new Insets(0, 0, 0, 0);
225 gridbag.setConstraints(cbInstruments, c);
226 p.add(cbInstruments);
227
228 cbOrchestras.addActionListener(new ActionListener() {
229 public void
230 actionPerformed(ActionEvent e) { orchestraChanged(); }
231 });
232
233 cbInstruments.addActionListener(new ActionListener() {
234 public void
235 actionPerformed(ActionEvent e) { instrumentChanged(); }
236 });
237
238 for(int i = 0; i < CC.getOrchestras().getOrchestraCount(); i++) {
239 cbOrchestras.addItem(CC.getOrchestras().getOrchestra(i));
240 }
241
242 p.setAlignmentX(LEFT_ALIGNMENT);
243 return p;
244 }
245
246 protected JSPrefs
247 preferences() { return CC.getViewConfig().preferences(); }
248
249 private void
250 orchestraChanged() {
251 OrchestraModel om = (OrchestraModel)cbOrchestras.getSelectedItem();
252 String s = om == null ? null : om.getDescription();
253 if(s != null && s.length() == 0) s = null;
254 cbOrchestras.setToolTipText(s);
255
256 cbInstruments.removeAllItems();
257 if(om == null || om.getInstrumentCount() == 0) {
258 cbInstruments.setEnabled(false);
259 return;
260 }
261
262 cbInstruments.setEnabled(true);
263
264 for(int i = 0; i < om.getInstrumentCount(); i++) {
265 cbInstruments.addItem(om.getInstrument(i));
266 }
267 }
268
269 private void
270 instrumentChanged() {
271 Instrument instr = (Instrument)cbInstruments.getSelectedItem();
272 String s = instr == null ? null : instr.getDescription();
273 if(s != null && s.length() == 0) s = null;
274 cbInstruments.setToolTipText(s);
275
276 btnOk.setEnabled(instr != null);
277 }
278
279
280
281 private JPanel
282 createDbPane() {
283 JPanel p = new JPanel();
284 p.setLayout(new BoxLayout(p, BoxLayout.X_AXIS));
285 p.add(tfDbInstrument);
286 p.add(Box.createRigidArea(new Dimension(6, 0)));
287 btnBrowseDb.setMargin(new Insets(0, 0, 0, 0));
288 p.add(btnBrowseDb);
289 //p.setMaximumSize(new Dimension(Short.MAX_VALUE, p.getPreferredSize().height));
290
291 btnBrowseDb.addActionListener(new ActionListener() {
292 public void
293 actionPerformed(ActionEvent e) { onBrowseDb(); }
294 });
295
296 p.setAlignmentX(LEFT_ALIGNMENT);
297
298 return p;
299 }
300
301 private JPanel
302 createFilePane() {
303 GridBagLayout gridbag = new GridBagLayout();
304 GridBagConstraints c = new GridBagConstraints();
305
306 JPanel filePane = new JPanel();
307
308 filePane.setLayout(gridbag);
309
310 c.fill = GridBagConstraints.NONE;
311
312 c.gridx = 0;
313 c.gridy = 0;
314 c.anchor = GridBagConstraints.EAST;
315 c.insets = new Insets(3, 3, 3, 3);
316 gridbag.setConstraints(lFilename, c);
317 filePane.add(lFilename);
318
319 c.gridx = 0;
320 c.gridy = 1;
321 gridbag.setConstraints(lIndex, c);
322 filePane.add(lIndex);
323
324 btnBrowse.setMargin(new Insets(0, 0, 0, 0));
325 btnBrowse.setToolTipText(i18n.getLabel("JSInstrumentChooser.btnBrowse"));
326 c.gridx = 2;
327 c.gridy = 0;
328 gridbag.setConstraints(btnBrowse, c);
329 filePane.add(btnBrowse);
330
331 c.fill = GridBagConstraints.HORIZONTAL;
332 c.gridx = 1;
333 c.gridy = 0;
334 c.weightx = 1.0;
335 c.anchor = GridBagConstraints.WEST;
336 gridbag.setConstraints(tfFilename, c);
337 filePane.add(tfFilename);
338
339 c.gridx = 1;
340 c.gridy = 1;
341 gridbag.setConstraints(spinnerIndex, c);
342 filePane.add(spinnerIndex);
343
344 tfFilename.setPreferredSize (
345 new Dimension(200, tfFilename.getPreferredSize().height)
346 );
347
348 btnBrowse.addActionListener(new ActionListener() {
349 public void
350 actionPerformed(ActionEvent e) { onBrowse(); }
351 });
352
353 filePane.setAlignmentX(LEFT_ALIGNMENT);
354 return filePane;
355 }
356
357 protected void
358 onOk() {
359 if(!btnOk.isEnabled()) return;
360
361 if(rbSelectFromOrchestra.isSelected()) {
362 Instrument instr = (Instrument)cbInstruments.getSelectedItem();
363 instrumentFile = instr.getPath();
364 instrumentIndex = instr.getInstrumentIndex();
365 engine = instr.getEngine();
366 setVisible(false);
367
368 int i = cbOrchestras.getSelectedIndex();
369 if(i >= 0) preferences().setIntProperty("lastUsedOrchestraIndex", i);
370
371 i = cbInstruments.getSelectedIndex();
372 if(i >= 0) {
373 preferences().setIntProperty("lastUsedOrchestraInstrumentIndex", i);
374 }
375
376 return;
377 }
378
379 if(rbSelectFromFile.isSelected()) {
380 instrumentFile = tfFilename.getText();
381 instrumentIndex = Integer.parseInt(spinnerIndex.getValue().toString());
382 setVisible(false);
383 return;
384 }
385
386 if(!rbSelectFromDb.isSelected()) return;
387
388 String instr = tfDbInstrument.getText();
389 preferences().setStringProperty("lastUsedDbInstrument", instr);
390 final InstrumentsDb.GetInstrument t = new InstrumentsDb.GetInstrument(instr);
391
392 t.addTaskListener(new TaskListener() {
393 public void
394 taskPerformed(TaskEvent e) {
395 updateState();
396 if(t.doneWithErrors()) return;
397
398 instrumentFile = t.getResult().getFilePath();
399 instrumentIndex = t.getResult().getInstrumentIndex();
400 engine = t.getResult().getFormatFamily(); // TODO: fix this
401 setVisible(false);
402 }
403 });
404
405 btnOk.setEnabled(false);
406 CC.getTaskQueue().add(t);
407 }
408
409 protected void
410 onCancel() { setVisible(false); }
411
412 private void
413 onBrowse() {
414 if(!rbSelectFromFile.isSelected()) rbSelectFromFile.doClick(0);
415 String s = preferences().getStringProperty("lastInstrumentLocation");
416 JFileChooser fc = new JFileChooser(s);
417 int result = fc.showOpenDialog(this);
418 if(result == JFileChooser.APPROVE_OPTION) {
419 tfFilename.setText(fc.getSelectedFile().getPath());
420 btnOk.requestFocusInWindow();
421
422 String path = fc.getCurrentDirectory().getAbsolutePath();
423 preferences().setStringProperty("lastInstrumentLocation", path);
424 }
425 }
426
427 private void
428 onBrowseDb() {
429 if(!rbSelectFromDb.isSelected()) rbSelectFromDb.doClick(0);
430 JSDbInstrumentChooser dlg;
431 dlg = new JSDbInstrumentChooser(JSInstrumentChooser.this);
432 String s = tfDbInstrument.getText();
433 if(s.length() > 0) dlg.setSelectedInstrument(s);
434 else {
435 s = preferences().getStringProperty("lastUsedDbInstrument", "");
436 if(s.length() > 0) dlg.setSelectedInstrument(s);
437 else dlg.setSelectedDirectory("/");
438 }
439 dlg.setVisible(true);
440 if(dlg.isCancelled()) return;
441 tfDbInstrument.setText(dlg.getSelectedInstrument());
442 tfDbInstrument.requestFocus();
443 }
444
445 /**
446 * Gets the name of the selected instrument file.
447 * @return The name of the selected instrument file.
448 */
449 public String
450 getInstrumentFile() { return instrumentFile; }
451
452 /**
453 * Gets the index of the instrument in the instrument file.
454 * @return The index of the instrument in the instrument file.
455 */
456 public int
457 getInstrumentIndex() { return instrumentIndex; }
458
459 public String
460 getEngine() { return engine; }
461
462 private void
463 updateState() {
464 boolean b = false;
465 if(rbSelectFromOrchestra.isSelected()) {
466 b = cbInstruments.getSelectedItem() != null;
467 } else if(rbSelectFromDb.isSelected()) {
468 b = tfDbInstrument.getText().length() > 0;
469 } else if(rbSelectFromFile.isSelected()) {
470 b = tfFilename.getText().length() > 0;
471 }
472
473 btnOk.setEnabled(b);
474 }
475
476 private final EventHandler eventHandler = new EventHandler();
477
478 private EventHandler
479 getHandler() { return eventHandler; }
480
481 private class EventHandler extends FocusAdapter
482 implements ActionListener, DocumentListener {
483
484 public void
485 actionPerformed(ActionEvent e) {
486 updateState();
487 }
488
489 // DocumentListener
490 public void
491 insertUpdate(DocumentEvent e) { updateState(); }
492
493 public void
494 removeUpdate(DocumentEvent e) { updateState(); }
495
496 public void
497 changedUpdate(DocumentEvent e) { updateState(); }
498
499 // FocusListener
500 public void
501 focusGained(FocusEvent e) {
502 Object src = e.getSource();
503 if(src == cbInstruments || src == cbOrchestras) {
504 if(!rbSelectFromOrchestra.isSelected()) {
505 rbSelectFromOrchestra.doClick(0);
506 }
507 } else if(src == tfDbInstrument) {
508 if(!rbSelectFromDb.isSelected()) rbSelectFromDb.doClick(0);
509 } else if(src == tfFilename) {
510 if(!rbSelectFromFile.isSelected()) rbSelectFromFile.doClick(0);
511 }
512 }
513 }
514 }

  ViewVC Help
Powered by ViewVC