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 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(final 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 |
|
202 |
CC.scheduleInTaskQueue(new Runnable() { |
203 |
public void |
204 |
run() { setMidiInstrumentEntry(map.getAvailableEntry()); } |
205 |
}); |
206 |
|
207 |
} |
208 |
|
209 |
protected JSPrefs |
210 |
preferences() { return CC.getViewConfig().preferences(); } |
211 |
|
212 |
/** |
213 |
* Sets the MIDI bank and MIDI program of the dialog. |
214 |
*/ |
215 |
public void |
216 |
setMidiInstrumentEntry(MidiInstrumentEntry entry) { |
217 |
if(entry == null) return; |
218 |
setMidiBank(entry.getMidiBank()); |
219 |
setMidiProgram(entry.getMidiProgram()); |
220 |
} |
221 |
|
222 |
/** |
223 |
* Gets the selected MIDI bank. |
224 |
*/ |
225 |
public int |
226 |
getMidiBank() { return Integer.parseInt(spinnerBank.getValue().toString()); } |
227 |
|
228 |
/** |
229 |
* Sets the selected MIDI bank. |
230 |
*/ |
231 |
public void |
232 |
setMidiBank(int bank) { spinnerBank.setValue(bank); } |
233 |
|
234 |
/** |
235 |
* Gets the selected MIDI program. |
236 |
*/ |
237 |
public int |
238 |
getMidiProgram() { return cbProgram.getSelectedIndex(); } |
239 |
|
240 |
/** |
241 |
* Sets the selected MIDI program. |
242 |
*/ |
243 |
public void |
244 |
setMidiProgram(int program) { cbProgram.setSelectedIndex(program); } |
245 |
|
246 |
/** |
247 |
* Gets the chosen name for the new MIDI instrument. |
248 |
* @return The chosen name for the new MIDI instrument. |
249 |
*/ |
250 |
public String |
251 |
getInstrumentName() { return tfName.getText(); } |
252 |
|
253 |
/** |
254 |
* Sets the name for the new MIDI instrument. |
255 |
* @param name The name for the new MIDI instrument. |
256 |
*/ |
257 |
public void |
258 |
setInstrumentName(String name) { tfName.setText(name); } |
259 |
|
260 |
/** |
261 |
* Returns the volume level of the new MIDI instrument. |
262 |
* @return The volume level of the new MIDI instrument. |
263 |
*/ |
264 |
public float |
265 |
getVolume() { |
266 |
float f = slVolume.getValue(); |
267 |
f /= 100; |
268 |
return f; |
269 |
} |
270 |
|
271 |
/** Gets the selected load mode. */ |
272 |
public MidiInstrumentInfo.LoadMode |
273 |
getLoadMode() { return (MidiInstrumentInfo.LoadMode) cbLoadMode.getSelectedItem(); } |
274 |
|
275 |
protected void |
276 |
onOk() { |
277 |
if(!btnOk.isEnabled()) return; |
278 |
|
279 |
btnOk.setEnabled(false); |
280 |
preferences().setIntProperty("lastUsedMidiBank", getMidiBank()); |
281 |
preferences().setIntProperty("lastUsedMidiProgram", getMidiProgram()); |
282 |
|
283 |
MidiInstrumentInfo instrInfo = new MidiInstrumentInfo(); |
284 |
instrInfo.setName(getInstrumentName()); |
285 |
instrInfo.setFilePath(instr.getFilePath()); |
286 |
instrInfo.setInstrumentIndex(instr.getInstrumentIndex()); |
287 |
instrInfo.setEngine(instr.getEngine()); |
288 |
instrInfo.setVolume(getVolume()); |
289 |
instrInfo.setLoadMode(getLoadMode()); |
290 |
|
291 |
int id = map.getMapId(); |
292 |
int b = getMidiBank(); |
293 |
int p = getMidiProgram(); |
294 |
final Midi.MapInstrument t = new Midi.MapInstrument(id, b, p, instrInfo); |
295 |
|
296 |
t.addTaskListener(new TaskListener() { |
297 |
public void |
298 |
taskPerformed(TaskEvent e) { |
299 |
btnOk.setEnabled(true); |
300 |
if(t.doneWithErrors()) return; |
301 |
setCancelled(false); |
302 |
setVisible(false); |
303 |
} |
304 |
}); |
305 |
|
306 |
CC.getTaskQueue().add(t); |
307 |
} |
308 |
|
309 |
protected void |
310 |
onCancel() { setVisible(false); } |
311 |
|
312 |
private void |
313 |
updateState() { |
314 |
boolean b = tfName.getText().length() > 0; |
315 |
b = b && cbProgram.getSelectedItem() != null; |
316 |
btnOk.setEnabled(b); |
317 |
} |
318 |
|
319 |
private final Handler eventHandler = new Handler(); |
320 |
|
321 |
private Handler |
322 |
getHandler() { return eventHandler; } |
323 |
|
324 |
private class Handler implements DocumentListener { |
325 |
// DocumentListener |
326 |
public void |
327 |
insertUpdate(DocumentEvent e) { updateState(); } |
328 |
|
329 |
public void |
330 |
removeUpdate(DocumentEvent e) { updateState(); } |
331 |
|
332 |
public void |
333 |
changedUpdate(DocumentEvent e) { updateState(); } |
334 |
} |
335 |
} |