1 |
iliev |
1286 |
/* |
2 |
|
|
* JSampler - a java front-end for LinuxSampler |
3 |
|
|
* |
4 |
iliev |
2288 |
* Copyright (C) 2005-2011 Grigor Iliev <grigor@grigoriliev.com> |
5 |
iliev |
1286 |
* |
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 |
iliev |
1871 |
import java.io.File; |
34 |
|
|
|
35 |
iliev |
2288 |
import java.util.Locale; |
36 |
iliev |
1286 |
import javax.swing.JButton; |
37 |
iliev |
1308 |
import javax.swing.JComboBox; |
38 |
iliev |
1286 |
import javax.swing.JLabel; |
39 |
|
|
import javax.swing.JPanel; |
40 |
|
|
import javax.swing.JTextField; |
41 |
|
|
|
42 |
|
|
import javax.swing.event.DocumentEvent; |
43 |
|
|
import javax.swing.event.DocumentListener; |
44 |
|
|
|
45 |
iliev |
2288 |
import net.sf.juife.swing.OkCancelDialog; |
46 |
iliev |
1286 |
|
47 |
iliev |
1540 |
import net.sf.juife.event.TaskEvent; |
48 |
|
|
import net.sf.juife.event.TaskListener; |
49 |
|
|
|
50 |
iliev |
1286 |
import org.jsampler.CC; |
51 |
iliev |
1540 |
import org.jsampler.OrchestraInstrument; |
52 |
iliev |
1304 |
import org.jsampler.JSPrefs; |
53 |
iliev |
1540 |
import org.jsampler.task.Global; |
54 |
iliev |
2288 |
import org.jsampler.view.swing.SHF; |
55 |
iliev |
1540 |
|
56 |
|
|
import org.linuxsampler.lscp.Instrument; |
57 |
iliev |
2288 |
import org.linuxsampler.lscp.SamplerEngine; |
58 |
iliev |
1540 |
|
59 |
iliev |
1286 |
import static org.jsampler.view.std.StdI18n.i18n; |
60 |
iliev |
1352 |
import static org.linuxsampler.lscp.Parser.*; |
61 |
iliev |
1286 |
|
62 |
|
|
|
63 |
|
|
/** |
64 |
|
|
* |
65 |
|
|
* @author Grigor Iliev |
66 |
|
|
*/ |
67 |
|
|
public class JSAddOrEditInstrumentDlg extends OkCancelDialog { |
68 |
|
|
private final JLabel lName = new JLabel(i18n.getLabel("JSAddOrEditInstrumentDlg.lName")); |
69 |
|
|
private final JLabel lDesc = new JLabel(i18n.getLabel("JSAddOrEditInstrumentDlg.lDesc")); |
70 |
|
|
private final JLabel lPath = new JLabel(i18n.getLabel("JSAddOrEditInstrumentDlg.lPath")); |
71 |
|
|
private final JLabel lIndex = new JLabel(i18n.getLabel("JSAddOrEditInstrumentDlg.lIndex")); |
72 |
iliev |
2288 |
private final JLabel lEngine = new JLabel(i18n.getLabel("JSAddOrEditInstrumentDlg.lEngine")); |
73 |
iliev |
1286 |
|
74 |
|
|
private final JButton btnBrowse = |
75 |
|
|
new JButton(i18n.getButtonLabel("browse")); |
76 |
|
|
|
77 |
|
|
private final JTextField tfName = new JTextField(); |
78 |
|
|
private final JTextField tfDesc = new JTextField(); |
79 |
iliev |
1308 |
private final JComboBox cbPath = new JComboBox(); |
80 |
iliev |
1540 |
private final JComboBox cbIndex = new JComboBox(); |
81 |
iliev |
2288 |
private final JComboBox cbEngine = new JComboBox(); |
82 |
iliev |
1286 |
|
83 |
iliev |
1540 |
private OrchestraInstrument instrument; |
84 |
iliev |
1286 |
|
85 |
|
|
|
86 |
|
|
/** |
87 |
|
|
* Creates a new instance of <code>JSAddOrEditInstrumentDlg</code>. |
88 |
|
|
*/ |
89 |
iliev |
1540 |
public JSAddOrEditInstrumentDlg() { this(new OrchestraInstrument()); } |
90 |
iliev |
1286 |
|
91 |
|
|
/** |
92 |
|
|
* Creates a new instance of <code>JSAddOrEditInstrumentDlg</code>. |
93 |
|
|
* |
94 |
|
|
* @param instr The instrument to modify. |
95 |
|
|
*/ |
96 |
iliev |
1540 |
public JSAddOrEditInstrumentDlg(OrchestraInstrument instr) { |
97 |
iliev |
2288 |
super(SHF.getMainFrame(), i18n.getLabel("JSAddOrEditInstrumentDlg.title")); |
98 |
iliev |
2148 |
setResizable(true); |
99 |
iliev |
1286 |
|
100 |
|
|
instrument = instr; |
101 |
|
|
|
102 |
iliev |
1308 |
cbPath.setEditable(true); |
103 |
|
|
String[] files = preferences().getStringListProperty("recentInstrumentFiles"); |
104 |
|
|
for(String s : files) cbPath.addItem(s); |
105 |
|
|
cbPath.setSelectedItem(null); |
106 |
iliev |
2148 |
|
107 |
|
|
cbPath.setMinimumSize ( |
108 |
|
|
new Dimension(200, cbPath.getMinimumSize().height) |
109 |
|
|
); |
110 |
|
|
|
111 |
iliev |
1308 |
cbPath.setPreferredSize ( |
112 |
|
|
new Dimension(200, cbPath.getPreferredSize().height) |
113 |
|
|
); |
114 |
|
|
|
115 |
iliev |
2288 |
for(SamplerEngine engine : CC.getSamplerModel().getEngines()) { |
116 |
|
|
cbEngine.addItem(engine); |
117 |
|
|
} |
118 |
|
|
|
119 |
iliev |
1286 |
JPanel p = new JPanel(); |
120 |
|
|
|
121 |
|
|
GridBagLayout gridbag = new GridBagLayout(); |
122 |
|
|
GridBagConstraints c = new GridBagConstraints(); |
123 |
|
|
|
124 |
|
|
p.setLayout(gridbag); |
125 |
|
|
|
126 |
|
|
c.fill = GridBagConstraints.NONE; |
127 |
|
|
|
128 |
|
|
c.gridx = 0; |
129 |
|
|
c.gridy = 0; |
130 |
|
|
c.anchor = GridBagConstraints.EAST; |
131 |
|
|
c.insets = new Insets(3, 3, 3, 3); |
132 |
|
|
gridbag.setConstraints(lName, c); |
133 |
|
|
p.add(lName); |
134 |
|
|
|
135 |
|
|
c.gridx = 0; |
136 |
|
|
c.gridy = 1; |
137 |
|
|
gridbag.setConstraints(lDesc, c); |
138 |
|
|
p.add(lDesc); |
139 |
|
|
|
140 |
|
|
c.gridx = 0; |
141 |
|
|
c.gridy = 2; |
142 |
|
|
gridbag.setConstraints(lPath, c); |
143 |
|
|
p.add(lPath); |
144 |
|
|
|
145 |
|
|
c.gridx = 2; |
146 |
|
|
c.gridy = 2; |
147 |
|
|
gridbag.setConstraints(btnBrowse, c); |
148 |
|
|
p.add(btnBrowse); |
149 |
|
|
|
150 |
|
|
c.gridx = 0; |
151 |
|
|
c.gridy = 3; |
152 |
|
|
gridbag.setConstraints(lIndex, c); |
153 |
|
|
p.add(lIndex); |
154 |
|
|
|
155 |
iliev |
2288 |
c.gridx = 0; |
156 |
|
|
c.gridy = 4; |
157 |
|
|
gridbag.setConstraints(lEngine, c); |
158 |
|
|
p.add(lEngine); |
159 |
|
|
|
160 |
iliev |
1286 |
c.fill = GridBagConstraints.HORIZONTAL; |
161 |
|
|
c.gridx = 1; |
162 |
|
|
c.gridy = 0; |
163 |
|
|
c.weightx = 1.0; |
164 |
|
|
c.gridwidth = 2; |
165 |
|
|
c.anchor = GridBagConstraints.WEST; |
166 |
|
|
gridbag.setConstraints(tfName, c); |
167 |
|
|
p.add(tfName); |
168 |
|
|
|
169 |
|
|
c.gridx = 1; |
170 |
|
|
c.gridy = 1; |
171 |
|
|
gridbag.setConstraints(tfDesc, c); |
172 |
|
|
p.add(tfDesc); |
173 |
|
|
|
174 |
|
|
c.gridx = 1; |
175 |
|
|
c.gridy = 2; |
176 |
|
|
c.gridwidth = 1; |
177 |
iliev |
1308 |
gridbag.setConstraints(cbPath, c); |
178 |
|
|
p.add(cbPath); |
179 |
iliev |
1286 |
|
180 |
iliev |
1540 |
for(int i = 0; i < 101; i++) cbIndex.addItem(i); |
181 |
|
|
|
182 |
iliev |
1286 |
c.gridx = 1; |
183 |
|
|
c.gridy = 3; |
184 |
|
|
c.gridwidth = 2; |
185 |
iliev |
1540 |
gridbag.setConstraints(cbIndex, c); |
186 |
|
|
p.add(cbIndex); |
187 |
iliev |
1286 |
|
188 |
iliev |
2288 |
c.gridx = 1; |
189 |
|
|
c.gridy = 4; |
190 |
|
|
gridbag.setConstraints(cbEngine, c); |
191 |
|
|
p.add(cbEngine); |
192 |
|
|
|
193 |
iliev |
1286 |
setMainPane(p); |
194 |
|
|
|
195 |
|
|
int w = getPreferredSize().width; |
196 |
|
|
Dimension d = new Dimension(w > 500 ? w : 500, getPreferredSize().height); |
197 |
|
|
setPreferredSize(d); |
198 |
|
|
pack(); |
199 |
|
|
|
200 |
|
|
setLocationRelativeTo(getOwner()); |
201 |
|
|
|
202 |
|
|
tfName.getDocument().addDocumentListener(getHandler()); |
203 |
|
|
btnBrowse.addActionListener(getHandler()); |
204 |
iliev |
1308 |
cbPath.addActionListener(new ActionListener() { |
205 |
|
|
public void |
206 |
iliev |
1540 |
actionPerformed(ActionEvent e) { |
207 |
|
|
updateState(); |
208 |
|
|
updateFileInstruments(); |
209 |
|
|
} |
210 |
iliev |
1308 |
}); |
211 |
iliev |
1871 |
|
212 |
|
|
cbIndex.addActionListener(new ActionListener() { |
213 |
|
|
public void |
214 |
|
|
actionPerformed(ActionEvent e) { |
215 |
|
|
Object o = cbIndex.getSelectedItem(); |
216 |
|
|
if(o == null) return; |
217 |
|
|
if(o.toString().length() > 5) tfName.setText(o.toString()); |
218 |
|
|
} |
219 |
|
|
}); |
220 |
iliev |
1286 |
|
221 |
|
|
updateInfo(); |
222 |
|
|
updateState(); |
223 |
iliev |
2148 |
|
224 |
|
|
setMinimumSize(getPreferredSize()); |
225 |
iliev |
1286 |
} |
226 |
|
|
|
227 |
iliev |
1304 |
protected JSPrefs |
228 |
|
|
preferences() { return CC.getViewConfig().preferences(); } |
229 |
|
|
|
230 |
iliev |
1286 |
private void |
231 |
|
|
updateInfo() { |
232 |
|
|
tfName.setText(getInstrument().getName()); |
233 |
|
|
tfDesc.setText(getInstrument().getDescription()); |
234 |
iliev |
1540 |
cbPath.setSelectedItem(getInstrument().getFilePath()); |
235 |
|
|
cbIndex.setSelectedIndex(getInstrument().getInstrumentIndex()); |
236 |
iliev |
2288 |
|
237 |
|
|
setSelectedEngine(getInstrument().getEngine()); |
238 |
iliev |
1286 |
} |
239 |
|
|
|
240 |
|
|
private void |
241 |
|
|
updateState() { |
242 |
|
|
boolean b = true; |
243 |
|
|
if(tfName.getText().length() == 0) b = false; |
244 |
iliev |
1308 |
Object o = cbPath.getSelectedItem(); |
245 |
|
|
if(o == null || o.toString().length() == 0) b = false; |
246 |
iliev |
1540 |
o = cbIndex.getSelectedItem(); |
247 |
|
|
if(o == null || o.toString().length() == 0) b = false; |
248 |
iliev |
1286 |
|
249 |
|
|
btnOk.setEnabled(b); |
250 |
|
|
} |
251 |
|
|
|
252 |
iliev |
1540 |
private boolean init = true; |
253 |
|
|
|
254 |
|
|
private void |
255 |
|
|
updateFileInstruments() { |
256 |
|
|
Object o = cbPath.getSelectedItem(); |
257 |
|
|
if(o == null) return; |
258 |
|
|
String s = o.toString(); |
259 |
|
|
final Global.GetFileInstruments t = new Global.GetFileInstruments(s); |
260 |
|
|
|
261 |
|
|
t.addTaskListener(new TaskListener() { |
262 |
|
|
public void |
263 |
|
|
taskPerformed(TaskEvent e) { |
264 |
|
|
Instrument[] instrs = t.getResult(); |
265 |
|
|
if(instrs == null) { |
266 |
|
|
cbIndex.removeAllItems(); |
267 |
|
|
for(int i = 0; i < 101; i++) cbIndex.addItem(i); |
268 |
|
|
return; |
269 |
|
|
} else { |
270 |
|
|
|
271 |
|
|
cbIndex.removeAllItems(); |
272 |
|
|
for(int i = 0; i < instrs.length; i++) { |
273 |
|
|
cbIndex.addItem(i + " - " + instrs[i].getName()); |
274 |
|
|
} |
275 |
|
|
} |
276 |
|
|
|
277 |
|
|
if(init) { |
278 |
|
|
int i = getInstrument().getInstrumentIndex(); |
279 |
|
|
cbIndex.setSelectedIndex(i); |
280 |
|
|
init = false; |
281 |
|
|
} |
282 |
|
|
} |
283 |
|
|
}); |
284 |
|
|
|
285 |
|
|
CC.getTaskQueue().add(t); |
286 |
|
|
} |
287 |
|
|
|
288 |
iliev |
1871 |
@Override |
289 |
iliev |
1286 |
protected void |
290 |
|
|
onOk() { |
291 |
|
|
if(!btnOk.isEnabled()) return; |
292 |
|
|
|
293 |
|
|
instrument.setName(tfName.getText()); |
294 |
|
|
instrument.setDescription(tfDesc.getText()); |
295 |
iliev |
1540 |
instrument.setFilePath(cbPath.getSelectedItem().toString()); |
296 |
|
|
int idx = cbIndex.getSelectedIndex(); |
297 |
iliev |
1286 |
instrument.setInstrumentIndex(idx); |
298 |
iliev |
2288 |
instrument.setEngine(((SamplerEngine)cbEngine.getSelectedItem()).getName()); |
299 |
iliev |
1286 |
|
300 |
iliev |
1540 |
StdUtils.updateRecentElements("recentInstrumentFiles", instrument.getFilePath()); |
301 |
iliev |
1308 |
|
302 |
iliev |
1286 |
setVisible(false); |
303 |
|
|
setCancelled(false); |
304 |
|
|
} |
305 |
|
|
|
306 |
iliev |
1871 |
@Override |
307 |
iliev |
1286 |
protected void |
308 |
iliev |
1871 |
onCancel() { setVisible(false); } |
309 |
iliev |
1286 |
|
310 |
|
|
/** |
311 |
|
|
* Gets the created/modified orchestra. |
312 |
|
|
* @return The created/modified orchestra. |
313 |
|
|
*/ |
314 |
iliev |
1540 |
public OrchestraInstrument |
315 |
iliev |
1286 |
getInstrument() { return instrument; } |
316 |
|
|
|
317 |
iliev |
2288 |
private void |
318 |
|
|
guessEngine(String file) { |
319 |
|
|
if(file == null || file.length() < 4) return; |
320 |
|
|
String engineName = null; |
321 |
|
|
file = file.substring(file.length() - 4).toLowerCase(); |
322 |
|
|
if (file.equals(".gig")) { |
323 |
|
|
engineName = "GIG"; |
324 |
|
|
} else if(file.equals(".sfz")) { |
325 |
|
|
engineName = "SFZ"; |
326 |
|
|
} else if(file.equals(".sf2")) { |
327 |
|
|
engineName = "SF2"; |
328 |
|
|
} |
329 |
|
|
|
330 |
|
|
setSelectedEngine(engineName); |
331 |
|
|
|
332 |
|
|
|
333 |
|
|
} |
334 |
iliev |
1286 |
|
335 |
iliev |
2288 |
private void |
336 |
|
|
setSelectedEngine(String engineName) { |
337 |
|
|
if(engineName == null) return; |
338 |
|
|
for(int i = 0; i < cbEngine.getItemCount(); i ++) { |
339 |
|
|
SamplerEngine engine = (SamplerEngine)cbEngine.getItemAt(i); |
340 |
|
|
if(engine.getName().equals(engineName)) { |
341 |
|
|
cbEngine.setSelectedIndex(i); |
342 |
|
|
break; |
343 |
|
|
} |
344 |
|
|
} |
345 |
|
|
} |
346 |
|
|
|
347 |
|
|
|
348 |
iliev |
1286 |
private final Handler eventHandler = new Handler(); |
349 |
|
|
|
350 |
|
|
private Handler |
351 |
|
|
getHandler() { return eventHandler; } |
352 |
|
|
|
353 |
|
|
private class Handler implements DocumentListener, ActionListener { |
354 |
|
|
// DocumentListener |
355 |
iliev |
1871 |
@Override |
356 |
iliev |
1286 |
public void |
357 |
|
|
insertUpdate(DocumentEvent e) { updateState(); } |
358 |
|
|
|
359 |
iliev |
1871 |
@Override |
360 |
iliev |
1286 |
public void |
361 |
|
|
removeUpdate(DocumentEvent e) { updateState(); } |
362 |
|
|
|
363 |
iliev |
1871 |
@Override |
364 |
iliev |
1286 |
public void |
365 |
|
|
changedUpdate(DocumentEvent e) { updateState(); } |
366 |
|
|
|
367 |
|
|
// ActionListener |
368 |
iliev |
1871 |
@Override |
369 |
iliev |
1286 |
public void |
370 |
|
|
actionPerformed(ActionEvent e) { |
371 |
iliev |
1871 |
File f = StdUtils.showOpenInstrumentFileChooser(JSAddOrEditInstrumentDlg.this); |
372 |
|
|
if(f == null) return; |
373 |
iliev |
1352 |
|
374 |
iliev |
1871 |
String path = f.getAbsolutePath(); |
375 |
iliev |
1480 |
if(java.io.File.separatorChar == '\\') { |
376 |
iliev |
1483 |
path = path.replace('\\', '/'); |
377 |
iliev |
1480 |
} |
378 |
|
|
path = toEscapedString(path); |
379 |
|
|
cbPath.setSelectedItem(path); |
380 |
iliev |
2288 |
guessEngine(path); |
381 |
iliev |
1286 |
} |
382 |
|
|
} |
383 |
|
|
} |