/[svn]/jsampler/trunk/src/org/jsampler/view/std/JSNewMidiInstrumentWizard.java
ViewVC logotype

Contents of /jsampler/trunk/src/org/jsampler/view/std/JSNewMidiInstrumentWizard.java

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1871 - (show annotations) (download)
Sun Mar 22 18:11:39 2009 UTC (15 years, 1 month ago) by iliev
File size: 26915 byte(s)
* Mac OS integration, work in progress:
* Added option to use native file choosers
  (choose Edit/Preferences, then click the `View' tab)

1 /*
2 * JSampler - a java front-end for LinuxSampler
3 *
4 * Copyright (C) 2005-2009 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.BorderLayout;
26 import java.awt.GridBagConstraints;
27 import java.awt.GridBagLayout;
28 import java.awt.Insets;
29
30 import java.awt.event.ActionEvent;
31 import java.awt.event.ActionListener;
32
33 import java.io.File;
34
35 import javax.swing.BoxLayout;
36 import javax.swing.ButtonGroup;
37 import javax.swing.ImageIcon;
38 import javax.swing.JButton;
39 import javax.swing.JCheckBox;
40 import javax.swing.JComboBox;
41 import javax.swing.JLabel;
42 import javax.swing.JPanel;
43 import javax.swing.JRadioButton;
44 import javax.swing.JSlider;
45 import javax.swing.JSpinner;
46 import javax.swing.JTextField;
47 import javax.swing.SpinnerNumberModel;
48
49 import javax.swing.event.DocumentEvent;
50 import javax.swing.event.DocumentListener;
51
52 import net.sf.juife.Wizard;
53
54 import net.sf.juife.event.TaskEvent;
55 import net.sf.juife.event.TaskListener;
56
57 import net.sf.juife.wizard.DefaultWizardModel;
58 import net.sf.juife.wizard.UserInputPage;
59 import net.sf.juife.wizard.WizardPage;
60
61 import org.jsampler.CC;
62 import org.jsampler.OrchestraInstrument;
63 import org.jsampler.JSPrefs;
64 import org.jsampler.MidiInstrumentMap;
65 import org.jsampler.OrchestraModel;
66
67 import org.jsampler.task.Global;
68
69 import org.linuxsampler.lscp.Instrument;
70 import org.linuxsampler.lscp.MidiInstrumentEntry;
71 import org.linuxsampler.lscp.MidiInstrumentInfo;
72 import org.linuxsampler.lscp.SamplerEngine;
73
74 import static org.jsampler.view.std.StdI18n.i18n;
75 import static org.linuxsampler.lscp.Parser.*;
76
77
78 /**
79 * A wizard for mapping new MIDI instrument.
80 * @author Grigor Iliev
81 */
82 public class JSNewMidiInstrumentWizard extends Wizard {
83 /**
84 * Creates a new instance of <code>JSNewMidiInstrumentWizard</code>.
85 */
86 public
87 JSNewMidiInstrumentWizard(ImageIcon iconBrowse) {
88 this(iconBrowse, null);
89 }
90
91 /**
92 * Creates a new instance of <code>JSNewMidiInstrumentWizard</code>.
93 */
94 public
95 JSNewMidiInstrumentWizard(ImageIcon iconBrowse, MidiInstrumentMap defaultMap) {
96 super(CC.getMainFrame(), i18n.getLabel("JSNewMidiInstrumentWizard.title"));
97 setModel(new NewMidiInstrumentWizardModel(iconBrowse, defaultMap));
98 }
99
100 }
101
102 class NewMidiInstrumentWizardModel extends DefaultWizardModel {
103 private final InstrLocationMethodWizardPage instrLocationMethodWizardPage;
104 private final OrchestraSelectWizardPage orchestraSelectWizardPage;
105 private final ManualSelectWizardPage manualSelectWizardPage;
106 private final InstrumentMappingWizardPage instrumentMappingWizardPage;
107
108 private MidiInstrumentMap defaultMap;
109
110 NewMidiInstrumentWizardModel(ImageIcon iconBrowse, MidiInstrumentMap defaultMap) {
111 this.defaultMap = defaultMap;
112 instrLocationMethodWizardPage = new InstrLocationMethodWizardPage();
113 orchestraSelectWizardPage = new OrchestraSelectWizardPage();
114 manualSelectWizardPage = new ManualSelectWizardPage(iconBrowse);
115 instrumentMappingWizardPage = new InstrumentMappingWizardPage(this);
116
117 addPage(instrLocationMethodWizardPage);
118 addStep(i18n.getLabel("JSNewMidiInstrumentWizard.step1"));
119
120 addPage(manualSelectWizardPage);
121 addPage(orchestraSelectWizardPage);
122 addStep(i18n.getLabel("JSNewMidiInstrumentWizard.step2"), 2);
123
124 addPage(instrumentMappingWizardPage);
125 addStep(i18n.getLabel("JSNewMidiInstrumentWizard.step3"));
126 }
127
128 public String
129 getInstrumentName() {
130 if(!instrLocationMethodWizardPage.isOrchestraMethodSelected()) {
131 return manualSelectWizardPage.getInstrumentName();
132 }
133 OrchestraInstrument instr = orchestraSelectWizardPage.getInstrument();
134 if(instr == null) return null;
135 return instr.getName();
136 }
137
138 /**
139 * Moves to the next page in the wizard.
140 * @return The next page in the wizard.
141 */
142 @Override
143 public WizardPage
144 next() {
145 InstrLocationMethodWizardPage p1 = instrLocationMethodWizardPage;
146 WizardPage p2 = manualSelectWizardPage;
147
148 if(getCurrentPage() == p1 && p1.isOrchestraMethodSelected()) {
149 super.next();
150 } else if(getCurrentPage() == manualSelectWizardPage) {
151 super.next();
152 }
153
154 return super.next();
155 }
156
157 /**
158 * Moves to the previous page in the wizard.
159 * @return The previous page in the wizard.
160 * @see #hasPrevious
161 */
162 @Override
163 public WizardPage
164 previous() {
165 InstrLocationMethodWizardPage p1 = instrLocationMethodWizardPage;
166 WizardPage p2 = instrumentMappingWizardPage;
167
168 if(getCurrentPage() == orchestraSelectWizardPage) {
169 super.previous();
170 } else if(getCurrentPage() == p2 && !p1.isOrchestraMethodSelected()) {
171 super.previous();
172 }
173
174 return super.previous();
175 }
176
177 public void
178 mapInstrument() {
179 MidiInstrumentInfo instr = new MidiInstrumentInfo();
180 if(instrLocationMethodWizardPage.isOrchestraMethodSelected()) {
181 OrchestraInstrument i = orchestraSelectWizardPage.getInstrument();
182 instr.setFilePath(i.getFilePath());
183 instr.setInstrumentIndex(i.getInstrumentIndex());
184 instr.setEngine(i.getEngine());
185 instr.setLoadMode(orchestraSelectWizardPage.getLoadMode());
186 } else {
187 instr.setFilePath(manualSelectWizardPage.getSelectedFile());
188 instr.setInstrumentIndex(manualSelectWizardPage.getInstrumentIndex());
189 instr.setEngine(manualSelectWizardPage.getEngine());
190 instr.setLoadMode(manualSelectWizardPage.getLoadMode());
191 }
192
193 int map = instrumentMappingWizardPage.getMapId();
194 int bank = instrumentMappingWizardPage.getMidiBank();
195 int prog = instrumentMappingWizardPage.getMidiProgram();
196
197 instr.setName(instrumentMappingWizardPage.getInstrumentName());
198 instr.setVolume(instrumentMappingWizardPage.getVolume());
199
200 CC.getSamplerModel().mapBackendMidiInstrument(map, bank, prog, instr);
201 }
202
203 public MidiInstrumentMap
204 getDefaultMap() { return defaultMap; }
205
206 public void
207 setDefaultMap(MidiInstrumentMap map) { defaultMap = map; }
208 }
209
210 class InstrLocationMethodWizardPage extends UserInputPage {
211 private final static String INSTR_LOCATION_METHOD = "InstrLocationMethod";
212 private final static String NEW_MIDI_INSTR_WIZARD_SKIP1 = "NewMidiInstrumentWizard.skip1";
213
214 private final JRadioButton rbManual =
215 new JRadioButton(i18n.getLabel("InstrLocationMethodWizardPage.rbManual"));
216
217 private final JRadioButton rbOrchestra =
218 new JRadioButton(i18n.getLabel("InstrLocationMethodWizardPage.rbOrchestra"));
219
220 private final JCheckBox checkSkip =
221 new JCheckBox(i18n.getLabel("InstrLocationMethodWizardPage.checkSkip"));
222
223 InstrLocationMethodWizardPage() {
224 super(i18n.getLabel("InstrLocationMethodWizardPage.subtitle"));
225
226 String s = i18n.getLabel("InstrLocationMethodWizardPage.mainInstructions");
227 setMainInstructions(s);
228
229 JPanel p = new JPanel();
230 p.setLayout(new BoxLayout(p, BoxLayout.Y_AXIS));
231
232 ButtonGroup group = new ButtonGroup();
233 group.add(rbManual);
234 group.add(rbOrchestra);
235 rbManual.setSelected(true);
236
237 p.add(rbManual);
238 p.add(rbOrchestra);
239
240 JPanel p2 = new JPanel();
241 p2.setLayout(new BorderLayout());
242 p2.add(p, BorderLayout.NORTH);
243 p2.add(checkSkip, BorderLayout.SOUTH);
244 setMainPane(p2);
245
246 switch(preferences().getIntProperty(INSTR_LOCATION_METHOD)) {
247 case 0:
248 rbManual.setSelected(true);
249 break;
250 case 1:
251 rbOrchestra.setSelected(true);
252 }
253
254 checkSkip.setSelected(preferences().getBoolProperty(NEW_MIDI_INSTR_WIZARD_SKIP1));
255
256 rbManual.addActionListener(new ActionListener() {
257 public void
258 actionPerformed(ActionEvent e) {
259 if(rbManual.isSelected()) {
260 preferences().setIntProperty(INSTR_LOCATION_METHOD, 0);
261 }
262 }
263 });
264
265 rbOrchestra.addActionListener(new ActionListener() {
266 public void
267 actionPerformed(ActionEvent e) {
268 if(rbOrchestra.isSelected()) {
269 preferences().setIntProperty(INSTR_LOCATION_METHOD, 1);
270 }
271 }
272 });
273
274 checkSkip.addActionListener(new ActionListener() {
275 public void
276 actionPerformed(ActionEvent e) {
277 boolean b = checkSkip.isSelected();
278 preferences().setBoolProperty(NEW_MIDI_INSTR_WIZARD_SKIP1, b);
279 }
280 });
281 }
282
283 protected JSPrefs
284 preferences() { return CC.getViewConfig().preferences(); }
285
286 /**
287 * Determines whether the user selected an orchestra location method.
288 */
289 public boolean
290 isOrchestraMethodSelected() { return rbOrchestra.isSelected(); }
291 }
292
293 class OrchestraSelectWizardPage extends UserInputPage {
294 private final JLabel lOrchestras =
295 new JLabel(i18n.getLabel("OrchestraSelectWizardPage.lOrchestras"));
296
297 private final JLabel lInstruments =
298 new JLabel(i18n.getLabel("OrchestraSelectWizardPage.lInstruments"));
299
300 private final JLabel lLoadMode =
301 new JLabel(i18n.getLabel("OrchestraSelectWizardPage.lLoadMode"));
302
303 private final JComboBox cbOrchestras = new JComboBox();
304 private final JComboBox cbInstruments = new JComboBox();
305 private final JComboBox cbLoadMode = new JComboBox();
306
307 OrchestraSelectWizardPage() {
308 super(i18n.getLabel("OrchestraSelectWizardPage.subtitle"));
309 setMainInstructions(i18n.getLabel("OrchestraSelectWizardPage.mainInstructions"));
310
311 GridBagLayout gridbag = new GridBagLayout();
312 GridBagConstraints c = new GridBagConstraints();
313
314 JPanel p = new JPanel();
315
316 p.setLayout(gridbag);
317
318 c.fill = GridBagConstraints.NONE;
319
320 c.gridx = 0;
321 c.gridy = 0;
322 c.anchor = GridBagConstraints.EAST;
323 c.insets = new Insets(0, 0, 6, 16);
324 gridbag.setConstraints(lOrchestras, c);
325 p.add(lOrchestras);
326
327 c.gridx = 0;
328 c.gridy = 1;
329 gridbag.setConstraints(lInstruments, c);
330 p.add(lInstruments);
331
332 c.gridx = 0;
333 c.gridy = 2;
334 c.insets = new Insets(12, 0, 6, 16);
335 gridbag.setConstraints(lLoadMode, c);
336 p.add(lLoadMode);
337
338 c.gridx = 1;
339 c.gridy = 0;
340 c.weightx = 1.0;
341 c.insets = new Insets(0, 0, 6, 48);
342 c.fill = GridBagConstraints.HORIZONTAL;
343 c.anchor = GridBagConstraints.NORTH;
344 gridbag.setConstraints(cbOrchestras, c);
345 p.add(cbOrchestras);
346
347 c.gridx = 1;
348 c.gridy = 1;
349 gridbag.setConstraints(cbInstruments, c);
350 p.add(cbInstruments);
351
352 c.gridx = 1;
353 c.gridy = 2;
354 c.insets = new Insets(12, 0, 6, 48);
355 gridbag.setConstraints(cbLoadMode, c);
356 p.add(cbLoadMode);
357
358 JPanel p2 = new JPanel();
359 p2.setOpaque(false);
360 c.gridx = 0;
361 c.gridy = 3;
362 c.fill = GridBagConstraints.VERTICAL;
363 c.weightx = 0.0;
364 c.weighty = 1.0;
365 gridbag.setConstraints(p2, c);
366 p.add(p2);
367
368 setMainPane(p);
369
370 int orchIdx =
371 preferences().getIntProperty("OrchestraSelectWizardPage.OrchestraIndex", 0);
372
373 cbOrchestras.addActionListener(new ActionListener() {
374 public void
375 actionPerformed(ActionEvent e) { orchestraChanged(); }
376 });
377
378 for(int i = 0; i < CC.getOrchestras().getOrchestraCount(); i++) {
379 cbOrchestras.addItem(CC.getOrchestras().getOrchestra(i));
380 }
381
382 if(CC.getOrchestras().getOrchestraCount() > orchIdx) {
383 cbOrchestras.setSelectedIndex(orchIdx);
384 }
385
386 cbInstruments.addActionListener(new ActionListener() {
387 public void
388 actionPerformed(ActionEvent e) { instrumentChanged(); }
389 });
390
391 cbLoadMode.addItem(MidiInstrumentInfo.LoadMode.DEFAULT);
392 cbLoadMode.addItem(MidiInstrumentInfo.LoadMode.ON_DEMAND);
393 cbLoadMode.addItem(MidiInstrumentInfo.LoadMode.ON_DEMAND_HOLD);
394 cbLoadMode.addItem(MidiInstrumentInfo.LoadMode.PERSISTENT);
395
396 int i = preferences().getIntProperty("std.midiInstrument.loadMode", 0);
397 if(cbLoadMode.getItemCount() > i) cbLoadMode.setSelectedIndex(i);
398
399 cbLoadMode.addActionListener(new ActionListener() {
400 public void
401 actionPerformed(ActionEvent e) {
402 int j = cbLoadMode.getSelectedIndex();
403 if(j < 0) return;
404 preferences().setIntProperty("std.midiInstrument.loadMode", j);
405 }
406 });
407 }
408
409 protected JSPrefs
410 preferences() { return CC.getViewConfig().preferences(); }
411
412 private void
413 orchestraChanged() {
414 OrchestraModel om = (OrchestraModel)cbOrchestras.getSelectedItem();
415 String s = om == null ? null : om.getDescription();
416 if(s != null && s.length() == 0) s = null;
417 cbOrchestras.setToolTipText(s);
418
419 s = "OrchestraSelectWizardPage.OrchestraIndex";
420 int orchIdx = cbOrchestras.getSelectedIndex();
421 if(orchIdx >= 0) preferences().setIntProperty(s, orchIdx);
422
423 cbInstruments.removeAllItems();
424 if(om == null || om.getInstrumentCount() == 0) {
425 cbInstruments.setEnabled(false);
426 return;
427 }
428
429 cbInstruments.setEnabled(true);
430
431 for(int i = 0; i < om.getInstrumentCount(); i++) {
432 cbInstruments.addItem(om.getInstrument(i));
433 }
434 }
435
436 private void
437 instrumentChanged() {
438 OrchestraInstrument instr = (OrchestraInstrument)cbInstruments.getSelectedItem();
439 String s = instr == null ? null : instr.getDescription();
440 if(s != null && s.length() == 0) s = null;
441 cbInstruments.setToolTipText(s);
442
443 getWizard().enableNextButton(instr != null);
444 }
445
446 @Override
447 public void
448 postinitPage() {
449 getWizard().enableNextButton(cbInstruments.getSelectedItem() != null);
450 }
451
452 /**
453 * Gets the selected instrument.
454 */
455 public OrchestraInstrument
456 getInstrument() { return (OrchestraInstrument)cbInstruments.getSelectedItem(); }
457
458 /**
459 * Gets the selected load mode.
460 */
461 public MidiInstrumentInfo.LoadMode
462 getLoadMode() { return (MidiInstrumentInfo.LoadMode) cbLoadMode.getSelectedItem(); }
463 }
464
465 class ManualSelectWizardPage extends UserInputPage {
466 private final JLabel lFilename =
467 new JLabel(i18n.getLabel("ManualSelectWizardPage.lFilename"));
468
469 private final JLabel lIndex = new JLabel(i18n.getLabel("ManualSelectWizardPage.lIndex"));
470
471 private final JLabel lEngine = new JLabel(i18n.getLabel("ManualSelectWizardPage.lEngine"));
472
473 private final JLabel lLoadMode =
474 new JLabel(i18n.getLabel("ManualSelectWizardPage.lLoadMode"));
475
476 private final JComboBox cbFilename = new JComboBox();
477 private final JComboBox cbIndex = new JComboBox();
478
479 private final JButton btnBrowse;
480
481 private final JComboBox cbEngine = new JComboBox();
482 private final JComboBox cbLoadMode = new JComboBox();
483
484 ManualSelectWizardPage(ImageIcon iconBrowse) {
485 super(i18n.getLabel("ManualSelectWizardPage.subtitle"));
486 setMainInstructions(i18n.getLabel("ManualSelectWizardPage.mainInstructions"));
487
488 GridBagLayout gridbag = new GridBagLayout();
489 GridBagConstraints c = new GridBagConstraints();
490
491 JPanel mainPane = new JPanel();
492
493 mainPane.setLayout(gridbag);
494
495 c.fill = GridBagConstraints.NONE;
496
497 c.gridx = 0;
498 c.gridy = 0;
499 c.anchor = GridBagConstraints.EAST;
500 c.insets = new Insets(3, 3, 3, 3);
501 gridbag.setConstraints(lFilename, c);
502 mainPane.add(lFilename);
503
504 c.gridx = 0;
505 c.gridy = 1;
506 gridbag.setConstraints(lIndex, c);
507 mainPane.add(lIndex);
508
509 c.gridx = 0;
510 c.gridy = 2;
511 c.insets = new Insets(12, 3, 3, 3);
512 gridbag.setConstraints(lEngine, c);
513 mainPane.add(lEngine);
514
515 c.gridx = 0;
516 c.gridy = 3;
517 c.insets = new Insets(3, 3, 3, 3);
518 gridbag.setConstraints(lLoadMode, c);
519 mainPane.add(lLoadMode);
520
521 btnBrowse = new JButton(iconBrowse);
522 btnBrowse.setMargin(new Insets(0, 0, 0, 0));
523 btnBrowse.setToolTipText(i18n.getLabel("ManualSelectWizardPage.btnBrowse"));
524 c.gridx = 2;
525 c.gridy = 0;
526 gridbag.setConstraints(btnBrowse, c);
527 mainPane.add(btnBrowse);
528
529 c.fill = GridBagConstraints.HORIZONTAL;
530 c.gridx = 1;
531 c.gridy = 0;
532 c.weightx = 1.0;
533 c.anchor = GridBagConstraints.WEST;
534 gridbag.setConstraints(cbFilename, c);
535 mainPane.add(cbFilename);
536
537 for(int i = 0; i < 101; i++) cbIndex.addItem(i);
538
539 c.gridx = 1;
540 c.gridy = 1;
541 gridbag.setConstraints(cbIndex, c);
542 mainPane.add(cbIndex);
543
544 c.gridx = 1;
545 c.gridy = 2;
546 c.insets = new Insets(12, 3, 3, 64);
547 gridbag.setConstraints(cbEngine, c);
548 mainPane.add(cbEngine);
549
550 c.gridx = 1;
551 c.gridy = 3;
552 c.insets = new Insets(3, 3, 3, 64);
553 gridbag.setConstraints(cbLoadMode, c);
554 mainPane.add(cbLoadMode);
555
556 JPanel p2 = new JPanel();
557 p2.setOpaque(false);
558 c.gridx = 0;
559 c.gridy = 4;
560 c.fill = GridBagConstraints.VERTICAL;
561 c.weightx = 0.0;
562 c.weighty = 1.0;
563 gridbag.setConstraints(p2, c);
564 mainPane.add(p2);
565
566 setMainPane(mainPane);
567
568 cbFilename.setEditable(true);
569 String[] files = preferences().getStringListProperty("recentInstrumentFiles");
570 for(String s : files) cbFilename.addItem(s);
571 cbFilename.setSelectedItem(null);
572
573 cbFilename.addActionListener(new ActionListener() {
574 public void
575 actionPerformed(ActionEvent e) {
576 updateState();
577 updateFileInstruments();
578 }
579 });
580
581 btnBrowse.addActionListener(new ActionListener() {
582 public void
583 actionPerformed(ActionEvent e) { onBrowse(); }
584 });
585
586 for(SamplerEngine e : CC.getSamplerModel().getEngines()) cbEngine.addItem(e);
587
588 cbLoadMode.addItem(MidiInstrumentInfo.LoadMode.DEFAULT);
589 cbLoadMode.addItem(MidiInstrumentInfo.LoadMode.ON_DEMAND);
590 cbLoadMode.addItem(MidiInstrumentInfo.LoadMode.ON_DEMAND_HOLD);
591 cbLoadMode.addItem(MidiInstrumentInfo.LoadMode.PERSISTENT);
592
593 int i = preferences().getIntProperty("std.midiInstrument.loadMode", 0);
594 if(cbLoadMode.getItemCount() > i) cbLoadMode.setSelectedIndex(i);
595
596 cbLoadMode.addActionListener(new ActionListener() {
597 public void
598 actionPerformed(ActionEvent e) {
599 int j = cbLoadMode.getSelectedIndex();
600 if(j < 0) return;
601 preferences().setIntProperty("std.midiInstrument.loadMode", j);
602 }
603 });
604 }
605
606 protected JSPrefs
607 preferences() { return CC.getViewConfig().preferences(); }
608
609 private void
610 onBrowse() {
611 File f = StdUtils.showOpenInstrumentFileChooser(getWizardDialog());
612 if(f == null) return;
613
614 String path = f.getAbsolutePath();
615 if(java.io.File.separatorChar == '\\') {
616 path = path.replace('\\', '/');
617 }
618 cbFilename.setSelectedItem(toEscapedString(path));
619 }
620
621 private void
622 updateState() {
623 boolean b = false;
624 Object o = cbFilename.getSelectedItem();
625 if(o == null) b = false;
626 else b = o.toString().length() > 0;
627
628 o = cbIndex.getSelectedItem();
629 if(o == null || o.toString().length() == 0) b = false;
630
631 getWizard().enableNextButton(b);
632 }
633
634 private void
635 updateFileInstruments() {
636 Object o = cbFilename.getSelectedItem();
637 if(o == null) return;
638 String s = o.toString();
639 final Global.GetFileInstruments t = new Global.GetFileInstruments(s);
640
641 t.addTaskListener(new TaskListener() {
642 public void
643 taskPerformed(TaskEvent e) {
644 Instrument[] instrs = t.getResult();
645 if(instrs == null) {
646 cbIndex.removeAllItems();
647 for(int i = 0; i < 101; i++) cbIndex.addItem(i);
648 return;
649 } else {
650
651 cbIndex.removeAllItems();
652 for(int i = 0; i < instrs.length; i++) {
653 cbIndex.addItem(i + " - " + instrs[i].getName());
654 }
655 }
656 }
657 });
658
659 CC.getTaskQueue().add(t);
660 }
661
662 public String
663 getInstrumentName() {
664 if(cbIndex.getSelectedItem() == null) return null;
665 String s = cbIndex.getSelectedItem().toString();
666 int i = s.indexOf(" - ");
667 if(i == -1) return null;
668 return s.substring(i + 3);
669 }
670
671 @Override
672 public void
673 postinitPage() { updateState(); }
674
675 /**
676 * Gets the name of the instrument file.
677 * @return The name of the instrument file.
678 */
679 public String
680 getSelectedFile() { return cbFilename.getSelectedItem().toString(); }
681
682 /**
683 * Gets the index of the instrument in the instrument file.
684 * @return The index of the instrument in the instrument file.
685 */
686 public int
687 getInstrumentIndex() { return cbIndex.getSelectedIndex(); }
688
689 /**
690 * Gets the selected engine.
691 */
692 public String
693 getEngine() { return ((SamplerEngine)cbEngine.getSelectedItem()).getName(); }
694
695 /**
696 * Gets the selected load mode.
697 */
698 public MidiInstrumentInfo.LoadMode
699 getLoadMode() { return (MidiInstrumentInfo.LoadMode)cbLoadMode.getSelectedItem(); }
700
701 private final Handler eventHandler = new Handler();
702
703 private Handler
704 getHandler() { return eventHandler; }
705
706 private class Handler implements DocumentListener {
707 // DocumentListener
708 @Override
709 public void
710 insertUpdate(DocumentEvent e) { updateState(); }
711
712 @Override
713 public void
714 removeUpdate(DocumentEvent e) { updateState(); }
715
716 @Override
717 public void
718 changedUpdate(DocumentEvent e) { updateState(); }
719 }
720 }
721
722 class InstrumentMappingWizardPage extends WizardPage {
723 private final JLabel lName = new JLabel(i18n.getLabel("InstrumentMappingWizardPage.lName"));
724 private final JLabel lMap = new JLabel(i18n.getLabel("InstrumentMappingWizardPage.lMap"));
725 private final JLabel lBank = new JLabel(i18n.getLabel("InstrumentMappingWizardPage.lBank"));
726 private final JLabel lProgram =
727 new JLabel(i18n.getLabel("InstrumentMappingWizardPage.lProgram"));
728
729 private final JLabel lVolume =
730 new JLabel(i18n.getLabel("InstrumentMappingWizardPage.lVolume"));
731
732 private final JTextField tfName = new JTextField();
733 private final JComboBox cbMap = new JComboBox();
734 private final JSpinner spinnerBank;
735 private final JComboBox cbProgram = new JComboBox();
736 private final JSlider slVolume = StdUtils.createVolumeSlider();
737
738 private final NewMidiInstrumentWizardModel wizardModel;
739
740 private int mbBase;
741 private int mpBase;
742
743 InstrumentMappingWizardPage(NewMidiInstrumentWizardModel wizardModel) {
744 super(i18n.getLabel("InstrumentMappingWizardPage.subtitle"));
745 this.wizardModel = wizardModel;
746
747 setPageType(Type.CONFIRMATION_PAGE);
748
749 mbBase = CC.getViewConfig().getFirstMidiBankNumber();
750 mpBase = CC.getViewConfig().getFirstMidiProgramNumber();
751
752 spinnerBank = new JSpinner(new SpinnerNumberModel(mbBase, mbBase, 16383 + mbBase, 1));
753
754 GridBagLayout gridbag = new GridBagLayout();
755 GridBagConstraints c = new GridBagConstraints();
756
757 setLayout(gridbag);
758
759 c.fill = GridBagConstraints.NONE;
760
761 c.gridx = 0;
762 c.gridy = 0;
763 c.anchor = GridBagConstraints.EAST;
764 c.insets = new Insets(24, 3, 3, 0);
765 gridbag.setConstraints(lName, c);
766 add(lName);
767
768 c.gridx = 0;
769 c.gridy = 2;
770 c.insets = new Insets(3, 3, 3, 0);
771 gridbag.setConstraints(lMap, c);
772 add(lMap);
773
774 c.gridx = 0;
775 c.gridy = 3;
776 gridbag.setConstraints(lBank, c);
777 add(lBank);
778
779 c.gridx = 0;
780 c.gridy = 4;
781 gridbag.setConstraints(lProgram, c);
782 add(lProgram);
783
784 c.gridx = 0;
785 c.gridy = 1;
786 c.insets = new Insets(3, 3, 24, 0);
787 gridbag.setConstraints(lVolume, c);
788 add(lVolume);
789
790 c.fill = GridBagConstraints.HORIZONTAL;
791 c.gridx = 1;
792 c.gridy = 0;
793 c.weightx = 1.0;
794 c.insets = new Insets(24, 12, 3, 36);
795 c.anchor = GridBagConstraints.WEST;
796 gridbag.setConstraints(tfName, c);
797 add(tfName);
798
799 c.gridx = 1;
800 c.gridy = 2;
801 c.insets = new Insets(3, 12, 3, 36);
802 gridbag.setConstraints(cbMap, c);
803 add(cbMap);
804
805 c.gridx = 1;
806 c.gridy = 3;
807 gridbag.setConstraints(spinnerBank, c);
808 add(spinnerBank);
809
810 c.gridx = 1;
811 c.gridy = 4;
812 gridbag.setConstraints(cbProgram, c);
813 add(cbProgram);
814
815 c.gridx = 1;
816 c.gridy = 1;
817 c.insets = new Insets(3, 12, 24, 36);
818 gridbag.setConstraints(slVolume, c);
819 add(slVolume);
820
821 JPanel p2 = new JPanel();
822 p2.setOpaque(false);
823 c.gridx = 0;
824 c.gridy = 5;
825 c.insets = new Insets(0, 0, 0, 0);
826 c.fill = GridBagConstraints.VERTICAL;
827 c.weightx = 0.0;
828 c.weighty = 1.0;
829 gridbag.setConstraints(p2, c);
830 add(p2);
831
832 cbMap.addActionListener(new ActionListener() {
833 public void
834 actionPerformed(ActionEvent e) { updateState(); }
835 });
836
837 for(MidiInstrumentMap m : CC.getSamplerModel().getMidiInstrumentMaps()) {
838 cbMap.addItem(m);
839 }
840
841 tfName.getDocument().addDocumentListener(getHandler());
842
843 cbMap.setEnabled(cbMap.getItemCount() > 0);
844
845
846 for(int i = 0; i < 128; i++) cbProgram.addItem(new Integer(i) + mpBase);
847
848 cbMap.addActionListener(new ActionListener() {
849 public void
850 actionPerformed(ActionEvent e) { updateMapping(); }
851 });
852
853 updateMapping();
854 }
855
856 protected JSPrefs
857 preferences() { return CC.getViewConfig().preferences(); }
858
859 private void
860 updateState() {
861 cbMap.setEnabled(cbMap.getItemCount() > 0);
862 boolean b = cbMap.getSelectedItem() != null;
863 spinnerBank.setEnabled(b);
864 cbProgram.setEnabled(b);
865
866 b = tfName.getText().length() > 0 && cbMap.getSelectedItem() != null;
867 b = b && cbProgram.getSelectedItem() != null;
868 if(getWizard() != null) getWizard().enableFinishButton(b);
869 }
870
871 private void
872 updateMapping() {
873 if(cbMap.getSelectedItem() == null) return;
874 MidiInstrumentMap map = (MidiInstrumentMap)cbMap.getSelectedItem();
875 MidiInstrumentEntry entry = map.getAvailableEntry();
876 if(entry == null) return;
877 setMidiBank(entry.getMidiBank());
878 cbProgram.setSelectedIndex(entry.getMidiProgram());
879 }
880
881 @Override
882 public void
883 postinitPage() {
884 String s = wizardModel.getInstrumentName();
885 if(s != null) tfName.setText(s);
886 else tfName.setText("");
887 if(wizardModel.getDefaultMap() != null) {
888 cbMap.setSelectedItem(wizardModel.getDefaultMap());
889 }
890 updateState();
891 }
892
893 /**
894 * Invoked when the user clicks the 'Finish' button
895 * while this page is the current page of the wizard.
896 * @return <code>true</code>
897 */
898 @Override
899 public boolean
900 mayFinish() {
901 ((NewMidiInstrumentWizardModel)getWizardModel()).mapInstrument();
902 preferences().setIntProperty("lastUsedMidiBank", getMidiBank());
903 preferences().setIntProperty("lastUsedMidiProgram", getMidiProgram());
904 return true;
905 }
906
907 /**
908 * Gets the ID of the selected MIDI instrument map.
909 */
910 public int
911 getMapId() { return ((MidiInstrumentMap)cbMap.getSelectedItem()).getMapId(); }
912
913 /**
914 * Gets the selected MIDI bank (always zero-based).
915 */
916 public int
917 getMidiBank() { return Integer.parseInt(spinnerBank.getValue().toString()) - mbBase; }
918
919 /**
920 * Sets the selected MIDI bank (always zero-based).
921 */
922 public void
923 setMidiBank(int bank) { spinnerBank.setValue(mbBase + bank); }
924
925 /**
926 * Gets the selected MIDI program (always zero-based).
927 */
928 public int
929 getMidiProgram() { return cbProgram.getSelectedIndex(); }
930
931 /**
932 * Gets the chosen name for the new MIDI instrument.
933 * @return The chosen name for the new MIDI instrument.
934 */
935 public String
936 getInstrumentName() { return tfName.getText(); }
937
938 /**
939 * Returns the volume level of the new MIDI instrument.
940 * @return The volume level of the new MIDI instrument.
941 */
942 public float
943 getVolume() {
944 float f = slVolume.getValue();
945 f /= 100;
946 return f;
947 }
948
949 private final Handler eventHandler = new Handler();
950
951 private Handler
952 getHandler() { return eventHandler; }
953
954 private class Handler implements DocumentListener {
955 // DocumentListener
956 @Override
957 public void
958 insertUpdate(DocumentEvent e) { updateState(); }
959
960 @Override
961 public void
962 removeUpdate(DocumentEvent e) { updateState(); }
963
964 @Override
965 public void
966 changedUpdate(DocumentEvent e) { updateState(); }
967 }
968 }

  ViewVC Help
Powered by ViewVC