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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1540 - (show annotations) (download)
Mon Dec 3 23:22:02 2007 UTC (16 years, 4 months ago) by iliev
File size: 8940 byte(s)
* Fantasia: by default the volume values are now shown in decibels
* Implemented support for retrieving instrument information
  from instrument files
* Some bugfixes and enhancements

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.Dialog;
26 import java.awt.Dimension;
27 import java.awt.Frame;
28 import java.awt.GridBagConstraints;
29 import java.awt.GridBagLayout;
30 import java.awt.Insets;
31
32 import java.awt.event.ActionEvent;
33 import java.awt.event.ActionListener;
34
35 import javax.swing.JComboBox;
36 import javax.swing.JLabel;
37 import javax.swing.JPanel;
38 import javax.swing.JSlider;
39 import javax.swing.JSpinner;
40 import javax.swing.JTextField;
41 import javax.swing.SpinnerNumberModel;
42
43 import javax.swing.event.DocumentEvent;
44 import javax.swing.event.DocumentListener;
45
46 import net.sf.juife.OkCancelDialog;
47
48 import net.sf.juife.event.TaskEvent;
49 import net.sf.juife.event.TaskListener;
50
51 import org.jsampler.CC;
52 import org.jsampler.JSPrefs;
53 import org.jsampler.MidiInstrumentMap;
54
55 import org.jsampler.task.Midi;
56
57 import org.linuxsampler.lscp.Instrument;
58 import org.linuxsampler.lscp.MidiInstrumentEntry;
59 import org.linuxsampler.lscp.MidiInstrumentInfo;
60
61 import static org.jsampler.view.std.StdI18n.i18n;
62
63
64 /**
65 *
66 * @author Grigor Iliev
67 */
68 public class JSAddMidiInstrumentDlg extends OkCancelDialog {
69 private final JLabel lName = new JLabel(i18n.getLabel("JSAddMidiInstrumentDlg.lName"));
70 private final JLabel lBank = new JLabel(i18n.getLabel("JSAddMidiInstrumentDlg.lBank"));
71 private final JLabel lProgram = new JLabel(i18n.getLabel("JSAddMidiInstrumentDlg.lProgram"));
72 private final JLabel lVolume = new JLabel(i18n.getLabel("JSAddMidiInstrumentDlg.lVolume"));
73 private final JLabel lLoadMode =
74 new JLabel(i18n.getLabel("JSAddMidiInstrumentDlg.lLoadMode"));
75
76 private final JTextField tfName = new JTextField();
77 private final JSpinner spinnerBank = new JSpinner(new SpinnerNumberModel(0, 0, 16383, 1));
78 private final JComboBox cbProgram = new JComboBox();
79 private final JSlider slVolume = new JSlider(0, 100, 100);
80 private final JComboBox cbLoadMode = new JComboBox();
81
82 private MidiInstrumentMap map;
83 private Instrument instr;
84
85 /**
86 * Creates a new instance of <code>JSAddMidiInstrumentDlg</code>
87 */
88 public
89 JSAddMidiInstrumentDlg(Frame owner, MidiInstrumentMap map, Instrument instr) {
90 super(owner, i18n.getLabel("JSAddMidiInstrumentDlg.title"));
91 initAddMidiInstrumentDlg(map, instr);
92 }
93
94 /**
95 * Creates a new instance of <code>JSAddMidiInstrumentDlg</code>
96 */
97 public
98 JSAddMidiInstrumentDlg(Dialog owner, MidiInstrumentMap map, Instrument instr) {
99 super(owner, i18n.getLabel("JSAddMidiInstrumentDlg.title"));
100 initAddMidiInstrumentDlg(map, instr);
101 }
102
103 private void
104 initAddMidiInstrumentDlg(MidiInstrumentMap map, Instrument instr) {
105 this.map = map;
106 this.instr = instr;
107
108 JPanel mainPane = new JPanel();
109 GridBagLayout gridbag = new GridBagLayout();
110 GridBagConstraints c = new GridBagConstraints();
111
112 mainPane.setLayout(gridbag);
113
114 c.fill = GridBagConstraints.NONE;
115
116 c.gridx = 0;
117 c.gridy = 0;
118 c.anchor = GridBagConstraints.EAST;
119 c.insets = new Insets(3, 3, 3, 3);
120 gridbag.setConstraints(lName, c);
121 mainPane.add(lName);
122
123 c.gridx = 0;
124 c.gridy = 2;
125 gridbag.setConstraints(lBank, c);
126 mainPane.add(lBank);
127
128 c.gridx = 0;
129 c.gridy = 3;
130 gridbag.setConstraints(lProgram, c);
131 mainPane.add(lProgram);
132
133 c.gridx = 0;
134 c.gridy = 4;
135 gridbag.setConstraints(lLoadMode, c);
136 mainPane.add(lLoadMode);
137
138 c.gridx = 0;
139 c.gridy = 1;
140 c.insets = new Insets(3, 3, 24, 3);
141 gridbag.setConstraints(lVolume, c);
142 mainPane.add(lVolume);
143
144 c.fill = GridBagConstraints.HORIZONTAL;
145 c.gridx = 1;
146 c.gridy = 0;
147 c.weightx = 1.0;
148 c.insets = new Insets(3, 3, 3, 3);
149 c.anchor = GridBagConstraints.WEST;
150 gridbag.setConstraints(tfName, c);
151 mainPane.add(tfName);
152
153 c.gridx = 1;
154 c.gridy = 2;
155 gridbag.setConstraints(spinnerBank, c);
156 mainPane.add(spinnerBank);
157
158 c.gridx = 1;
159 c.gridy = 3;
160 gridbag.setConstraints(cbProgram, c);
161 mainPane.add(cbProgram);
162
163 c.gridx = 1;
164 c.gridy = 4;
165 gridbag.setConstraints(cbLoadMode, c);
166 mainPane.add(cbLoadMode);
167
168 c.gridx = 1;
169 c.gridy = 1;
170 c.insets = new Insets(3, 3, 24, 3);
171 gridbag.setConstraints(slVolume, c);
172 mainPane.add(slVolume);
173
174 setMainPane(mainPane);
175
176 setResizable(true);
177 setMinimumSize(getPreferredSize());
178
179 for(int i = 0; i < 128; i++) cbProgram.addItem(new Integer(i));
180
181 cbLoadMode.addItem(MidiInstrumentInfo.LoadMode.DEFAULT);
182 cbLoadMode.addItem(MidiInstrumentInfo.LoadMode.ON_DEMAND);
183 cbLoadMode.addItem(MidiInstrumentInfo.LoadMode.ON_DEMAND_HOLD);
184 cbLoadMode.addItem(MidiInstrumentInfo.LoadMode.PERSISTENT);
185
186 int i = preferences().getIntProperty("std.midiInstrument.loadMode", 0);
187 if(cbLoadMode.getItemCount() > i) cbLoadMode.setSelectedIndex(i);
188
189 cbLoadMode.addActionListener(new ActionListener() {
190 public void
191 actionPerformed(ActionEvent e) {
192 int j = cbLoadMode.getSelectedIndex();
193 if(j < 0) return;
194 preferences().setIntProperty("std.midiInstrument.loadMode", j);
195 }
196 });
197
198 tfName.getDocument().addDocumentListener(getHandler());
199
200 setInstrumentName(instr.getName());
201 setMidiInstrumentEntry(map.getAvailableEntry());
202 }
203
204 protected JSPrefs
205 preferences() { return CC.getViewConfig().preferences(); }
206
207 /**
208 * Sets the MIDI bank and MIDI program of the dialog.
209 */
210 public void
211 setMidiInstrumentEntry(MidiInstrumentEntry entry) {
212 if(entry == null) return;
213 setMidiBank(entry.getMidiBank());
214 setMidiProgram(entry.getMidiProgram());
215 }
216
217 /**
218 * Gets the selected MIDI bank.
219 */
220 public int
221 getMidiBank() { return Integer.parseInt(spinnerBank.getValue().toString()); }
222
223 /**
224 * Sets the selected MIDI bank.
225 */
226 public void
227 setMidiBank(int bank) { spinnerBank.setValue(bank); }
228
229 /**
230 * Gets the selected MIDI program.
231 */
232 public int
233 getMidiProgram() { return cbProgram.getSelectedIndex(); }
234
235 /**
236 * Sets the selected MIDI program.
237 */
238 public void
239 setMidiProgram(int program) { cbProgram.setSelectedIndex(program); }
240
241 /**
242 * Gets the chosen name for the new MIDI instrument.
243 * @return The chosen name for the new MIDI instrument.
244 */
245 public String
246 getInstrumentName() { return tfName.getText(); }
247
248 /**
249 * Sets the name for the new MIDI instrument.
250 * @param name The name for the new MIDI instrument.
251 */
252 public void
253 setInstrumentName(String name) { tfName.setText(name); }
254
255 /**
256 * Returns the volume level of the new MIDI instrument.
257 * @return The volume level of the new MIDI instrument.
258 */
259 public float
260 getVolume() {
261 float f = slVolume.getValue();
262 f /= 100;
263 return f;
264 }
265
266 /** Gets the selected load mode. */
267 public MidiInstrumentInfo.LoadMode
268 getLoadMode() { return (MidiInstrumentInfo.LoadMode) cbLoadMode.getSelectedItem(); }
269
270 protected void
271 onOk() {
272 if(!btnOk.isEnabled()) return;
273
274 btnOk.setEnabled(false);
275 preferences().setIntProperty("lastUsedMidiBank", getMidiBank());
276
277 MidiInstrumentInfo instrInfo = new MidiInstrumentInfo();
278 instrInfo.setName(getInstrumentName());
279 instrInfo.setFilePath(instr.getFilePath());
280 instrInfo.setInstrumentIndex(instr.getInstrumentIndex());
281 instrInfo.setEngine(instr.getEngine());
282 instrInfo.setVolume(getVolume());
283 instrInfo.setLoadMode(getLoadMode());
284
285 int id = map.getMapId();
286 int b = getMidiBank();
287 int p = getMidiProgram();
288 final Midi.MapInstrument t = new Midi.MapInstrument(id, b, p, instrInfo);
289
290 t.addTaskListener(new TaskListener() {
291 public void
292 taskPerformed(TaskEvent e) {
293 btnOk.setEnabled(true);
294 if(t.doneWithErrors()) return;
295 setCancelled(false);
296 setVisible(false);
297 }
298 });
299
300 CC.getTaskQueue().add(t);
301 }
302
303 protected void
304 onCancel() { setVisible(false); }
305
306 private void
307 updateState() {
308 boolean b = tfName.getText().length() > 0;
309 b = b && cbProgram.getSelectedItem() != null;
310 btnOk.setEnabled(b);
311 }
312
313 private final Handler eventHandler = new Handler();
314
315 private Handler
316 getHandler() { return eventHandler; }
317
318 private class Handler implements DocumentListener {
319 // DocumentListener
320 public void
321 insertUpdate(DocumentEvent e) { updateState(); }
322
323 public void
324 removeUpdate(DocumentEvent e) { updateState(); }
325
326 public void
327 changedUpdate(DocumentEvent e) { updateState(); }
328 }
329 }

  ViewVC Help
Powered by ViewVC