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.Dimension; |
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 javax.swing.JComboBox; |
34 |
import javax.swing.JLabel; |
35 |
import javax.swing.JPanel; |
36 |
import javax.swing.JSlider; |
37 |
import javax.swing.JTextField; |
38 |
|
39 |
import javax.swing.event.DocumentEvent; |
40 |
import javax.swing.event.DocumentListener; |
41 |
|
42 |
import net.sf.juife.OkCancelDialog; |
43 |
|
44 |
import net.sf.juife.event.TaskEvent; |
45 |
import net.sf.juife.event.TaskListener; |
46 |
|
47 |
import org.jsampler.CC; |
48 |
import org.jsampler.JSPrefs; |
49 |
|
50 |
import org.jsampler.task.Global; |
51 |
|
52 |
import org.linuxsampler.lscp.Instrument; |
53 |
import org.linuxsampler.lscp.MidiInstrumentInfo; |
54 |
import org.linuxsampler.lscp.SamplerEngine; |
55 |
|
56 |
import static org.jsampler.view.std.StdI18n.i18n; |
57 |
|
58 |
|
59 |
/** |
60 |
* |
61 |
* @author Grigor Iliev |
62 |
*/ |
63 |
public class JSEditMidiInstrumentDlg extends OkCancelDialog { |
64 |
private final JLabel lName = new JLabel(i18n.getLabel("JSEditMidiInstrumentDlg.lName")); |
65 |
|
66 |
private final JLabel lFilename = |
67 |
new JLabel(i18n.getLabel("JSEditMidiInstrumentDlg.lFilename")); |
68 |
|
69 |
private final JLabel lIndex = new JLabel(i18n.getLabel("JSEditMidiInstrumentDlg.lIndex")); |
70 |
|
71 |
private final JLabel lEngine = new JLabel(i18n.getLabel("JSEditMidiInstrumentDlg.lEngine")); |
72 |
|
73 |
private final JLabel lLoadMode = |
74 |
new JLabel(i18n.getLabel("JSEditMidiInstrumentDlg.lLoadMode")); |
75 |
|
76 |
private final JLabel lVolume = |
77 |
new JLabel(i18n.getLabel("JSEditMidiInstrumentDlg.lVolume")); |
78 |
|
79 |
private final JTextField tfName = new JTextField(); |
80 |
private final JComboBox cbFilename = new JComboBox(); |
81 |
private final JComboBox cbIndex = new JComboBox(); |
82 |
private final JComboBox cbEngine = new JComboBox(); |
83 |
private final JComboBox cbLoadMode = new JComboBox(); |
84 |
private final JSlider slVolume = StdUtils.createVolumeSlider(); |
85 |
|
86 |
private final MidiInstrumentInfo instrument; |
87 |
|
88 |
/** |
89 |
* Creates a new instance of <code>JSEditMidiInstrumentDlg</code>. |
90 |
* |
91 |
* @param instr The instrument whose settings should be edited. |
92 |
*/ |
93 |
public JSEditMidiInstrumentDlg(MidiInstrumentInfo instr) { |
94 |
super(CC.getMainFrame(), i18n.getLabel("JSEditMidiInstrumentDlg.title")); |
95 |
|
96 |
this.instrument = instr; |
97 |
|
98 |
GridBagLayout gridbag = new GridBagLayout(); |
99 |
GridBagConstraints c = new GridBagConstraints(); |
100 |
|
101 |
JPanel p = new JPanel(); |
102 |
p.setLayout(gridbag); |
103 |
|
104 |
c.fill = GridBagConstraints.NONE; |
105 |
|
106 |
c.gridx = 0; |
107 |
c.gridy = 0; |
108 |
c.anchor = GridBagConstraints.EAST; |
109 |
c.insets = new Insets(3, 3, 3, 3); |
110 |
gridbag.setConstraints(lName, c); |
111 |
p.add(lName); |
112 |
|
113 |
c.gridx = 0; |
114 |
c.gridy = 1; |
115 |
gridbag.setConstraints(lFilename, c); |
116 |
p.add(lFilename); |
117 |
|
118 |
c.gridx = 0; |
119 |
c.gridy = 2; |
120 |
gridbag.setConstraints(lIndex, c); |
121 |
p.add(lIndex); |
122 |
|
123 |
c.gridx = 0; |
124 |
c.gridy = 3; |
125 |
c.insets = new Insets(12, 3, 3, 3); |
126 |
gridbag.setConstraints(lEngine, c); |
127 |
p.add(lEngine); |
128 |
|
129 |
c.gridx = 0; |
130 |
c.gridy = 4; |
131 |
c.insets = new Insets(3, 3, 3, 3); |
132 |
gridbag.setConstraints(lLoadMode, c); |
133 |
p.add(lLoadMode); |
134 |
|
135 |
c.gridx = 0; |
136 |
c.gridy = 5; |
137 |
gridbag.setConstraints(lVolume, c); |
138 |
p.add(lVolume); |
139 |
|
140 |
c.fill = GridBagConstraints.HORIZONTAL; |
141 |
c.gridx = 1; |
142 |
c.gridy = 0; |
143 |
c.weightx = 1.0; |
144 |
c.insets = new Insets(3, 12, 3, 3); |
145 |
c.anchor = GridBagConstraints.WEST; |
146 |
gridbag.setConstraints(tfName, c); |
147 |
p.add(tfName); |
148 |
|
149 |
cbFilename.setEditable(true); |
150 |
String[] files = preferences().getStringListProperty("recentInstrumentFiles"); |
151 |
for(String s : files) cbFilename.addItem(s); |
152 |
cbFilename.setSelectedItem(null); |
153 |
Dimension d = cbFilename.getPreferredSize(); |
154 |
d.width = 250; |
155 |
cbFilename.setPreferredSize(d); |
156 |
|
157 |
c.gridx = 1; |
158 |
c.gridy = 1; |
159 |
c.anchor = GridBagConstraints.WEST; |
160 |
gridbag.setConstraints(cbFilename, c); |
161 |
p.add(cbFilename); |
162 |
|
163 |
for(int i = 0; i < 101; i++) cbIndex.addItem(i); |
164 |
|
165 |
c.gridx = 1; |
166 |
c.gridy = 2; |
167 |
gridbag.setConstraints(cbIndex, c); |
168 |
p.add(cbIndex); |
169 |
|
170 |
c.gridx = 1; |
171 |
c.gridy = 3; |
172 |
c.insets = new Insets(12, 12, 3, 64); |
173 |
gridbag.setConstraints(cbEngine, c); |
174 |
p.add(cbEngine); |
175 |
|
176 |
c.gridx = 1; |
177 |
c.gridy = 4; |
178 |
c.insets = new Insets(3, 12, 3, 64); |
179 |
gridbag.setConstraints(cbLoadMode, c); |
180 |
p.add(cbLoadMode); |
181 |
|
182 |
c.gridx = 1; |
183 |
c.gridy = 5; |
184 |
gridbag.setConstraints(slVolume, c); |
185 |
p.add(slVolume); |
186 |
|
187 |
setMainPane(p); |
188 |
|
189 |
cbFilename.addActionListener(new ActionListener() { |
190 |
public void |
191 |
actionPerformed(ActionEvent e) { |
192 |
updateState(); |
193 |
updateFileInstruments(); |
194 |
} |
195 |
}); |
196 |
|
197 |
tfName.setText(instr.getName()); |
198 |
cbFilename.setSelectedItem(instr.getFilePath()); |
199 |
cbIndex.setSelectedIndex(instr.getInstrumentIndex()); |
200 |
slVolume.setValue((int)(instr.getVolume() * 100)); |
201 |
|
202 |
for(SamplerEngine e : CC.getSamplerModel().getEngines()) { |
203 |
cbEngine.addItem(e); |
204 |
if(e.getName().equals(instr.getEngine())) cbEngine.setSelectedItem(e); |
205 |
} |
206 |
|
207 |
cbLoadMode.addItem(MidiInstrumentInfo.LoadMode.DEFAULT); |
208 |
cbLoadMode.addItem(MidiInstrumentInfo.LoadMode.ON_DEMAND); |
209 |
cbLoadMode.addItem(MidiInstrumentInfo.LoadMode.ON_DEMAND_HOLD); |
210 |
cbLoadMode.addItem(MidiInstrumentInfo.LoadMode.PERSISTENT); |
211 |
cbLoadMode.setSelectedItem(instr.getLoadMode()); |
212 |
|
213 |
tfName.getDocument().addDocumentListener(getHandler()); |
214 |
} |
215 |
|
216 |
protected JSPrefs |
217 |
preferences() { return CC.getViewConfig().preferences(); } |
218 |
|
219 |
public MidiInstrumentInfo |
220 |
getInstrument() { return instrument; } |
221 |
|
222 |
public void |
223 |
onOk() { |
224 |
instrument.setName(tfName.getText()); |
225 |
instrument.setFilePath(cbFilename.getSelectedItem().toString()); |
226 |
instrument.setInstrumentIndex(cbIndex.getSelectedIndex()); |
227 |
String s = ((SamplerEngine)cbEngine.getSelectedItem()).getName(); |
228 |
instrument.setEngine(s); |
229 |
instrument.setLoadMode((MidiInstrumentInfo.LoadMode)cbLoadMode.getSelectedItem()); |
230 |
float f = slVolume.getValue(); |
231 |
f /= 100; |
232 |
instrument.setVolume(f); |
233 |
|
234 |
setCancelled(false); |
235 |
setVisible(false); |
236 |
} |
237 |
|
238 |
public void |
239 |
onCancel() { setVisible(false); } |
240 |
|
241 |
private void |
242 |
updateState() { |
243 |
boolean b = tfName.getText().length() != 0; |
244 |
if(cbFilename.getSelectedItem() == null) b = false; |
245 |
Object o = cbIndex.getSelectedItem(); |
246 |
if(o == null || o.toString().length() == 0) b = false; |
247 |
btnOk.setEnabled(b); |
248 |
} |
249 |
|
250 |
private boolean init = true; |
251 |
|
252 |
private void |
253 |
updateFileInstruments() { |
254 |
Object o = cbFilename.getSelectedItem(); |
255 |
if(o == null) return; |
256 |
String s = o.toString(); |
257 |
final Global.GetFileInstruments t = new Global.GetFileInstruments(s); |
258 |
|
259 |
t.addTaskListener(new TaskListener() { |
260 |
public void |
261 |
taskPerformed(TaskEvent e) { |
262 |
Instrument[] instrs = t.getResult(); |
263 |
if(instrs == null) { |
264 |
cbIndex.removeAllItems(); |
265 |
for(int i = 0; i < 101; i++) cbIndex.addItem(i); |
266 |
return; |
267 |
} else { |
268 |
|
269 |
cbIndex.removeAllItems(); |
270 |
for(int i = 0; i < instrs.length; i++) { |
271 |
cbIndex.addItem(i + " - " + instrs[i].getName()); |
272 |
} |
273 |
} |
274 |
|
275 |
if(init) { |
276 |
int i = getInstrument().getInstrumentIndex(); |
277 |
cbIndex.setSelectedIndex(i); |
278 |
init = false; |
279 |
} |
280 |
} |
281 |
}); |
282 |
|
283 |
CC.getTaskQueue().add(t); |
284 |
} |
285 |
|
286 |
private final Handler eventHandler = new Handler(); |
287 |
|
288 |
private Handler |
289 |
getHandler() { return eventHandler; } |
290 |
|
291 |
private class Handler implements DocumentListener { |
292 |
// DocumentListener |
293 |
public void |
294 |
insertUpdate(DocumentEvent e) { updateState(); } |
295 |
|
296 |
public void |
297 |
removeUpdate(DocumentEvent e) { updateState(); } |
298 |
|
299 |
public void |
300 |
changedUpdate(DocumentEvent e) { updateState(); } |
301 |
} |
302 |
} |