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