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.Dialog; |
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 javax.swing.JCheckBox; |
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 JSpinner spinnerBank; |
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 final JCheckBox checkApplyToAll = |
83 |
new JCheckBox(i18n.getLabel("JSAddMidiInstrumentDlg.checkApplyToAll")); |
84 |
|
85 |
private MidiInstrumentMap map; |
86 |
private Instrument instr; |
87 |
|
88 |
private int mbBase; |
89 |
private int mpBase; |
90 |
|
91 |
private boolean bShowApplyToAll; |
92 |
|
93 |
/** |
94 |
* Creates a new instance of <code>JSAddMidiInstrumentDlg</code> |
95 |
*/ |
96 |
public |
97 |
JSAddMidiInstrumentDlg(Frame owner, MidiInstrumentMap map, Instrument instr) { |
98 |
this(owner, map, instr, false); |
99 |
} |
100 |
|
101 |
/** |
102 |
* Creates a new instance of <code>JSAddMidiInstrumentDlg</code> |
103 |
*/ |
104 |
public |
105 |
JSAddMidiInstrumentDlg(Dialog owner, MidiInstrumentMap map, Instrument instr) { |
106 |
this(owner, map, instr, false); |
107 |
} |
108 |
|
109 |
/** |
110 |
* Creates a new instance of <code>JSAddMidiInstrumentDlg</code> |
111 |
* @param showApplyToAll Determines whether to show additional check box |
112 |
* for remembering the user decision and applying to all selected instruments |
113 |
*/ |
114 |
public |
115 |
JSAddMidiInstrumentDlg(Frame owner, MidiInstrumentMap map, Instrument instr, boolean showApplyToAll) { |
116 |
super(owner, i18n.getLabel("JSAddMidiInstrumentDlg.title")); |
117 |
bShowApplyToAll = showApplyToAll; |
118 |
initAddMidiInstrumentDlg(map, instr); |
119 |
} |
120 |
|
121 |
/** |
122 |
* Creates a new instance of <code>JSAddMidiInstrumentDlg</code> |
123 |
* @param showApplyToAll Determines whether to show additional check box |
124 |
* for remembering the user decision and applying to all selected instruments |
125 |
*/ |
126 |
public |
127 |
JSAddMidiInstrumentDlg(Dialog owner, MidiInstrumentMap map, Instrument instr, boolean showApplyToAll) { |
128 |
super(owner, i18n.getLabel("JSAddMidiInstrumentDlg.title")); |
129 |
bShowApplyToAll = showApplyToAll; |
130 |
initAddMidiInstrumentDlg(map, instr); |
131 |
} |
132 |
|
133 |
private void |
134 |
initAddMidiInstrumentDlg(final MidiInstrumentMap map, Instrument instr) { |
135 |
mbBase = CC.getViewConfig().getFirstMidiBankNumber(); |
136 |
mpBase = CC.getViewConfig().getFirstMidiProgramNumber(); |
137 |
|
138 |
spinnerBank = new JSpinner(new SpinnerNumberModel(mbBase, mbBase, 16383 + mbBase, 1)); |
139 |
|
140 |
this.map = map; |
141 |
this.instr = instr; |
142 |
|
143 |
JPanel mainPane = new JPanel(); |
144 |
GridBagLayout gridbag = new GridBagLayout(); |
145 |
GridBagConstraints c = new GridBagConstraints(); |
146 |
|
147 |
mainPane.setLayout(gridbag); |
148 |
|
149 |
c.fill = GridBagConstraints.NONE; |
150 |
|
151 |
c.gridx = 0; |
152 |
c.gridy = 0; |
153 |
c.anchor = GridBagConstraints.EAST; |
154 |
c.insets = new Insets(3, 3, 3, 3); |
155 |
gridbag.setConstraints(lName, c); |
156 |
mainPane.add(lName); |
157 |
|
158 |
c.gridx = 0; |
159 |
c.gridy = 2; |
160 |
gridbag.setConstraints(lBank, c); |
161 |
mainPane.add(lBank); |
162 |
|
163 |
c.gridx = 0; |
164 |
c.gridy = 3; |
165 |
gridbag.setConstraints(lProgram, c); |
166 |
mainPane.add(lProgram); |
167 |
|
168 |
c.gridx = 0; |
169 |
c.gridy = 4; |
170 |
gridbag.setConstraints(lLoadMode, c); |
171 |
mainPane.add(lLoadMode); |
172 |
|
173 |
c.gridx = 0; |
174 |
c.gridy = 1; |
175 |
c.insets = new Insets(3, 3, 24, 3); |
176 |
gridbag.setConstraints(lVolume, c); |
177 |
mainPane.add(lVolume); |
178 |
|
179 |
c.fill = GridBagConstraints.HORIZONTAL; |
180 |
c.gridx = 1; |
181 |
c.gridy = 0; |
182 |
c.weightx = 1.0; |
183 |
c.insets = new Insets(3, 3, 3, 3); |
184 |
c.anchor = GridBagConstraints.WEST; |
185 |
gridbag.setConstraints(tfName, c); |
186 |
mainPane.add(tfName); |
187 |
|
188 |
c.gridx = 1; |
189 |
c.gridy = 2; |
190 |
gridbag.setConstraints(spinnerBank, c); |
191 |
mainPane.add(spinnerBank); |
192 |
|
193 |
c.gridx = 1; |
194 |
c.gridy = 3; |
195 |
gridbag.setConstraints(cbProgram, c); |
196 |
mainPane.add(cbProgram); |
197 |
|
198 |
c.gridx = 1; |
199 |
c.gridy = 4; |
200 |
gridbag.setConstraints(cbLoadMode, c); |
201 |
mainPane.add(cbLoadMode); |
202 |
|
203 |
if(bShowApplyToAll) { |
204 |
c.gridx = 0; |
205 |
c.gridy = 5; |
206 |
c.gridwidth = 2; |
207 |
gridbag.setConstraints(checkApplyToAll, c); |
208 |
mainPane.add(checkApplyToAll); |
209 |
} |
210 |
|
211 |
c.gridx = 1; |
212 |
c.gridy = 1; |
213 |
c.gridwidth = 1; |
214 |
c.insets = new Insets(3, 3, 24, 3); |
215 |
gridbag.setConstraints(slVolume, c); |
216 |
mainPane.add(slVolume); |
217 |
|
218 |
setMainPane(mainPane); |
219 |
|
220 |
setResizable(true); |
221 |
setMinimumSize(getPreferredSize()); |
222 |
|
223 |
for(int i = 0; i < 128; i++) cbProgram.addItem(new Integer(i + mpBase)); |
224 |
|
225 |
cbLoadMode.addItem(MidiInstrumentInfo.LoadMode.DEFAULT); |
226 |
cbLoadMode.addItem(MidiInstrumentInfo.LoadMode.ON_DEMAND); |
227 |
cbLoadMode.addItem(MidiInstrumentInfo.LoadMode.ON_DEMAND_HOLD); |
228 |
cbLoadMode.addItem(MidiInstrumentInfo.LoadMode.PERSISTENT); |
229 |
|
230 |
int i = preferences().getIntProperty("std.midiInstrument.loadMode", 0); |
231 |
if(cbLoadMode.getItemCount() > i) cbLoadMode.setSelectedIndex(i); |
232 |
|
233 |
cbLoadMode.addActionListener(new ActionListener() { |
234 |
public void |
235 |
actionPerformed(ActionEvent e) { |
236 |
int j = cbLoadMode.getSelectedIndex(); |
237 |
if(j < 0) return; |
238 |
preferences().setIntProperty("std.midiInstrument.loadMode", j); |
239 |
} |
240 |
}); |
241 |
|
242 |
tfName.getDocument().addDocumentListener(getHandler()); |
243 |
|
244 |
setInstrumentName(instr.getName()); |
245 |
|
246 |
CC.scheduleInTaskQueue(new Runnable() { |
247 |
public void |
248 |
run() { setMidiInstrumentEntry(map.getAvailableEntry()); } |
249 |
}); |
250 |
|
251 |
} |
252 |
|
253 |
protected JSPrefs |
254 |
preferences() { return CC.getViewConfig().preferences(); } |
255 |
|
256 |
/** |
257 |
* Sets the MIDI bank and MIDI program of the dialog. |
258 |
*/ |
259 |
public void |
260 |
setMidiInstrumentEntry(MidiInstrumentEntry entry) { |
261 |
if(entry == null) return; |
262 |
setMidiBank(entry.getMidiBank()); |
263 |
setMidiProgram(entry.getMidiProgram()); |
264 |
} |
265 |
|
266 |
/** |
267 |
* Gets the selected MIDI bank (always zero-based). |
268 |
*/ |
269 |
public int |
270 |
getMidiBank() { return Integer.parseInt(spinnerBank.getValue().toString()) - mbBase; } |
271 |
|
272 |
/** |
273 |
* Sets the selected MIDI bank (always zero-based). |
274 |
*/ |
275 |
public void |
276 |
setMidiBank(int bank) { spinnerBank.setValue(mbBase + bank); } |
277 |
|
278 |
/** |
279 |
* Gets the selected MIDI program (always zero-based). |
280 |
*/ |
281 |
public int |
282 |
getMidiProgram() { return cbProgram.getSelectedIndex(); } |
283 |
|
284 |
/** |
285 |
* Sets the selected MIDI program (always zero-based). |
286 |
*/ |
287 |
public void |
288 |
setMidiProgram(int program) { cbProgram.setSelectedIndex(program); } |
289 |
|
290 |
/** |
291 |
* Gets the chosen name for the new MIDI instrument. |
292 |
* @return The chosen name for the new MIDI instrument. |
293 |
*/ |
294 |
public String |
295 |
getInstrumentName() { return tfName.getText(); } |
296 |
|
297 |
/** |
298 |
* Sets the name for the new MIDI instrument. |
299 |
* @param name The name for the new MIDI instrument. |
300 |
*/ |
301 |
public void |
302 |
setInstrumentName(String name) { tfName.setText(name); } |
303 |
|
304 |
/** |
305 |
* Returns the volume level of the new MIDI instrument. |
306 |
* @return The volume level of the new MIDI instrument. |
307 |
*/ |
308 |
public float |
309 |
getVolume() { |
310 |
float f = slVolume.getValue(); |
311 |
f /= 100; |
312 |
return f; |
313 |
} |
314 |
|
315 |
/** Gets the selected load mode. */ |
316 |
public MidiInstrumentInfo.LoadMode |
317 |
getLoadMode() { return (MidiInstrumentInfo.LoadMode) cbLoadMode.getSelectedItem(); } |
318 |
|
319 |
public MidiInstrumentInfo |
320 |
getMidiInstrumentInfo() { |
321 |
MidiInstrumentInfo instrInfo = new MidiInstrumentInfo(); |
322 |
instrInfo.setName(getInstrumentName()); |
323 |
instrInfo.setFilePath(instr.getFilePath()); |
324 |
instrInfo.setInstrumentIndex(instr.getInstrumentIndex()); |
325 |
instrInfo.setEngine(instr.getEngine()); |
326 |
instrInfo.setVolume(getVolume()); |
327 |
instrInfo.setLoadMode(getLoadMode()); |
328 |
|
329 |
return instrInfo; |
330 |
} |
331 |
|
332 |
public boolean |
333 |
isApplyToAllSelected() { return checkApplyToAll.isSelected(); } |
334 |
|
335 |
protected void |
336 |
onOk() { |
337 |
if(!btnOk.isEnabled()) return; |
338 |
|
339 |
btnOk.setEnabled(false); |
340 |
preferences().setIntProperty("lastUsedMidiBank", getMidiBank()); |
341 |
preferences().setIntProperty("lastUsedMidiProgram", getMidiProgram()); |
342 |
|
343 |
MidiInstrumentInfo instrInfo = getMidiInstrumentInfo(); |
344 |
|
345 |
int id = map.getMapId(); |
346 |
int b = getMidiBank(); |
347 |
int p = getMidiProgram(); |
348 |
final Midi.MapInstrument t = new Midi.MapInstrument(id, b, p, instrInfo); |
349 |
|
350 |
t.addTaskListener(new TaskListener() { |
351 |
public void |
352 |
taskPerformed(TaskEvent e) { |
353 |
btnOk.setEnabled(true); |
354 |
if(t.doneWithErrors()) return; |
355 |
setCancelled(false); |
356 |
setVisible(false); |
357 |
} |
358 |
}); |
359 |
|
360 |
CC.getTaskQueue().add(t); |
361 |
} |
362 |
|
363 |
protected void |
364 |
onCancel() { setVisible(false); } |
365 |
|
366 |
private void |
367 |
updateState() { |
368 |
boolean b = tfName.getText().length() > 0; |
369 |
b = b && cbProgram.getSelectedItem() != null; |
370 |
btnOk.setEnabled(b); |
371 |
} |
372 |
|
373 |
private final Handler eventHandler = new Handler(); |
374 |
|
375 |
private Handler |
376 |
getHandler() { return eventHandler; } |
377 |
|
378 |
private class Handler implements DocumentListener { |
379 |
// DocumentListener |
380 |
public void |
381 |
insertUpdate(DocumentEvent e) { updateState(); } |
382 |
|
383 |
public void |
384 |
removeUpdate(DocumentEvent e) { updateState(); } |
385 |
|
386 |
public void |
387 |
changedUpdate(DocumentEvent e) { updateState(); } |
388 |
} |
389 |
} |