/[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 1286 - (show annotations) (download)
Fri Aug 10 20:24:23 2007 UTC (16 years, 8 months ago) by iliev
File size: 13448 byte(s)
- Updated to version 0.6a. The Fantasia distribution is now
  capable of controlling all features available in LinuxSampler

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.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 JTextField tfDbInstrument = new JTextField();
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 JTextField tfFilename = new JTextField();
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 tfDbInstrument.addFocusListener(getHandler());
134 tfDbInstrument.getDocument().addDocumentListener(getHandler());
135
136 tfFilename.addFocusListener(getHandler());
137 tfFilename.getDocument().addDocumentListener(getHandler());
138
139 spinnerIndex.addChangeListener(new ChangeListener() {
140 public void
141 stateChanged(ChangeEvent e) {
142 if(!rbSelectFromFile.isSelected()) rbSelectFromFile.doClick(0);
143 }
144 });
145
146 JPanel mainPane = new JPanel();
147 mainPane.setLayout(new BoxLayout(mainPane, BoxLayout.Y_AXIS));
148
149 rbSelectFromOrchestra.setAlignmentX(LEFT_ALIGNMENT);
150 mainPane.add(rbSelectFromOrchestra);
151
152 JPanel orchestraPane = createOrchestraPane();
153 orchestraPane.setBorder(BorderFactory.createEmptyBorder(0, 32, 17, 0));
154 mainPane.add(orchestraPane);
155
156 rbSelectFromDb.setAlignmentX(LEFT_ALIGNMENT);
157 mainPane.add(rbSelectFromDb);
158
159 JPanel dbPane = createDbPane();
160 dbPane.setBorder(BorderFactory.createEmptyBorder(0, 32, 17, 0));
161 mainPane.add(dbPane);
162
163 rbSelectFromFile.setAlignmentX(LEFT_ALIGNMENT);
164 mainPane.add(rbSelectFromFile);
165
166 JPanel filePane = createFilePane();
167 filePane.setBorder(BorderFactory.createEmptyBorder(0, 32, 0, 0));
168 mainPane.add(filePane);
169
170 setMainPane(mainPane);
171
172 if(!CC.getSamplerModel().getServerInfo().hasInstrumentsDbSupport()) {
173 rbSelectFromDb.setEnabled(false);
174 tfDbInstrument.setEnabled(false);
175 btnBrowseDb.setEnabled(false);
176 } else {
177 btnBrowseDb.requestFocusInWindow();
178 }
179 }
180
181 private JPanel
182 createOrchestraPane() {
183 GridBagLayout gridbag = new GridBagLayout();
184 GridBagConstraints c = new GridBagConstraints();
185
186 JPanel p = new JPanel();
187
188 p.setLayout(gridbag);
189
190 c.fill = GridBagConstraints.NONE;
191
192 c.gridx = 0;
193 c.gridy = 0;
194 c.anchor = GridBagConstraints.EAST;
195 c.insets = new Insets(0, 0, 6, 6);
196 gridbag.setConstraints(lOrchestra, c);
197 p.add(lOrchestra);
198
199 c.gridx = 0;
200 c.gridy = 1;
201 c.insets = new Insets(0, 0, 0, 6);
202 gridbag.setConstraints(lInstrument, c);
203 p.add(lInstrument);
204
205 c.gridx = 1;
206 c.gridy = 0;
207 c.weightx = 1.0;
208 c.insets = new Insets(0, 0, 6, 0);
209 c.fill = GridBagConstraints.HORIZONTAL;
210 c.anchor = GridBagConstraints.NORTH;
211 gridbag.setConstraints(cbOrchestras, c);
212 p.add(cbOrchestras);
213
214 c.gridx = 1;
215 c.gridy = 1;
216 c.insets = new Insets(0, 0, 0, 0);
217 gridbag.setConstraints(cbInstruments, c);
218 p.add(cbInstruments);
219
220 cbOrchestras.addActionListener(new ActionListener() {
221 public void
222 actionPerformed(ActionEvent e) { orchestraChanged(); }
223 });
224
225 cbInstruments.addActionListener(new ActionListener() {
226 public void
227 actionPerformed(ActionEvent e) { instrumentChanged(); }
228 });
229
230 for(int i = 0; i < CC.getOrchestras().getOrchestraCount(); i++) {
231 cbOrchestras.addItem(CC.getOrchestras().getOrchestra(i));
232 }
233
234 p.setAlignmentX(LEFT_ALIGNMENT);
235 return p;
236 }
237
238 private void
239 orchestraChanged() {
240 OrchestraModel om = (OrchestraModel)cbOrchestras.getSelectedItem();
241 String s = om == null ? null : om.getDescription();
242 if(s != null && s.length() == 0) s = null;
243 cbOrchestras.setToolTipText(s);
244
245 cbInstruments.removeAllItems();
246 if(om == null || om.getInstrumentCount() == 0) {
247 cbInstruments.setEnabled(false);
248 return;
249 }
250
251 cbInstruments.setEnabled(true);
252
253 for(int i = 0; i < om.getInstrumentCount(); i++) {
254 cbInstruments.addItem(om.getInstrument(i));
255 }
256 }
257
258 private void
259 instrumentChanged() {
260 Instrument instr = (Instrument)cbInstruments.getSelectedItem();
261 String s = instr == null ? null : instr.getDescription();
262 if(s != null && s.length() == 0) s = null;
263 cbInstruments.setToolTipText(s);
264
265 btnOk.setEnabled(instr != null);
266 }
267
268
269
270 private JPanel
271 createDbPane() {
272 JPanel p = new JPanel();
273 p.setLayout(new BoxLayout(p, BoxLayout.X_AXIS));
274 p.add(tfDbInstrument);
275 p.add(Box.createRigidArea(new Dimension(6, 0)));
276 btnBrowseDb.setMargin(new Insets(0, 0, 0, 0));
277 p.add(btnBrowseDb);
278 //p.setMaximumSize(new Dimension(Short.MAX_VALUE, p.getPreferredSize().height));
279
280 btnBrowseDb.addActionListener(new ActionListener() {
281 public void
282 actionPerformed(ActionEvent e) {
283 if(!rbSelectFromDb.isSelected()) rbSelectFromDb.doClick(0);
284 JSDbInstrumentChooser dlg;
285 dlg = new JSDbInstrumentChooser(JSInstrumentChooser.this);
286 String s = tfDbInstrument.getText();
287 if(s.length() > 0) dlg.setSelectedInstrument(s);
288 else dlg.setSelectedDirectory("/");
289 dlg.setVisible(true);
290 if(dlg.isCancelled()) return;
291 tfDbInstrument.setText(dlg.getSelectedInstrument());
292 tfDbInstrument.requestFocus();
293 }
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 if(rbSelectFromOrchestra.isSelected()) {
361 Instrument instr = (Instrument)cbInstruments.getSelectedItem();
362 instrumentFile = instr.getPath();
363 instrumentIndex = instr.getInstrumentIndex();
364 engine = instr.getEngine();
365 setVisible(false);
366 return;
367 }
368
369 if(rbSelectFromFile.isSelected()) {
370 instrumentFile = tfFilename.getText();
371 instrumentIndex = Integer.parseInt(spinnerIndex.getValue().toString());
372 setVisible(false);
373 return;
374 }
375
376 if(!rbSelectFromDb.isSelected()) return;
377
378 String instr = tfDbInstrument.getText();
379 final InstrumentsDb.GetInstrument t = new InstrumentsDb.GetInstrument(instr);
380
381 t.addTaskListener(new TaskListener() {
382 public void
383 taskPerformed(TaskEvent e) {
384 updateState();
385 if(t.doneWithErrors()) return;
386
387 instrumentFile = t.getResult().getFilePath();
388 instrumentIndex = t.getResult().getInstrumentIndex();
389 engine = t.getResult().getFormatFamily(); // TODO: fix this
390 setVisible(false);
391 }
392 });
393
394 btnOk.setEnabled(false);
395 CC.getTaskQueue().add(t);
396 }
397
398 protected void
399 onCancel() { setVisible(false); }
400
401 private void
402 onBrowse() {
403 if(!rbSelectFromFile.isSelected()) rbSelectFromFile.doClick(0);
404 JFileChooser fc = new JFileChooser();
405 int result = fc.showOpenDialog(this);
406 if(result == JFileChooser.APPROVE_OPTION) {
407 tfFilename.setText(fc.getSelectedFile().getPath());
408 btnOk.requestFocusInWindow();
409 }
410 }
411
412 /**
413 * Gets the name of the selected instrument file.
414 * @return The name of the selected instrument file.
415 */
416 public String
417 getInstrumentFile() { return instrumentFile; }
418
419 /**
420 * Gets the index of the instrument in the instrument file.
421 * @return The index of the instrument in the instrument file.
422 */
423 public int
424 getInstrumentIndex() { return instrumentIndex; }
425
426 public String
427 getEngine() { return engine; }
428
429 private void
430 updateState() {
431 boolean b = false;
432 if(rbSelectFromOrchestra.isSelected()) {
433 b = cbInstruments.getSelectedItem() != null;
434 } else if(rbSelectFromDb.isSelected()) {
435 b = tfDbInstrument.getText().length() > 0;
436 } else if(rbSelectFromFile.isSelected()) {
437 b = tfFilename.getText().length() > 0;
438 }
439
440 btnOk.setEnabled(b);
441 }
442
443 private final EventHandler eventHandler = new EventHandler();
444
445 private EventHandler
446 getHandler() { return eventHandler; }
447
448 private class EventHandler extends FocusAdapter
449 implements ActionListener, DocumentListener {
450
451 public void
452 actionPerformed(ActionEvent e) {
453 updateState();
454 }
455
456 // DocumentListener
457 public void
458 insertUpdate(DocumentEvent e) { updateState(); }
459
460 public void
461 removeUpdate(DocumentEvent e) { updateState(); }
462
463 public void
464 changedUpdate(DocumentEvent e) { updateState(); }
465
466 // FocusListener
467 public void
468 focusGained(FocusEvent e) {
469 Object src = e.getSource();
470 if(src == cbInstruments || src == cbOrchestras) {
471 if(!rbSelectFromOrchestra.isSelected()) {
472 rbSelectFromOrchestra.doClick(0);
473 }
474 } else if(src == tfDbInstrument) {
475 if(!rbSelectFromDb.isSelected()) rbSelectFromDb.doClick(0);
476 } else if(src == tfFilename) {
477 if(!rbSelectFromFile.isSelected()) rbSelectFromFile.doClick(0);
478 }
479 }
480 }
481 }

  ViewVC Help
Powered by ViewVC