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.BorderLayout; |
26 |
import java.awt.Dialog; |
27 |
import java.awt.Frame; |
28 |
import java.awt.Window; |
29 |
|
30 |
import java.awt.event.ActionEvent; |
31 |
import java.awt.event.ActionListener; |
32 |
import java.awt.event.MouseAdapter; |
33 |
import java.awt.event.MouseEvent; |
34 |
|
35 |
import javax.swing.AbstractAction; |
36 |
import javax.swing.Action; |
37 |
import javax.swing.JMenu; |
38 |
import javax.swing.JMenuItem; |
39 |
import javax.swing.JPanel; |
40 |
import javax.swing.JPopupMenu; |
41 |
import javax.swing.JScrollPane; |
42 |
|
43 |
import javax.swing.event.ListSelectionEvent; |
44 |
import javax.swing.event.ListSelectionListener; |
45 |
|
46 |
import net.sf.juife.JuifeUtils; |
47 |
|
48 |
import org.jsampler.CC; |
49 |
import org.jsampler.DefaultOrchestraModel; |
50 |
import org.jsampler.Instrument; |
51 |
import org.jsampler.MidiInstrumentMap; |
52 |
import org.jsampler.OrchestraModel; |
53 |
import org.jsampler.SamplerChannelModel; |
54 |
|
55 |
import org.jsampler.event.ListEvent; |
56 |
import org.jsampler.event.ListListener; |
57 |
import org.jsampler.event.SamplerChannelListEvent; |
58 |
import org.jsampler.event.SamplerChannelListListener; |
59 |
|
60 |
import org.jsampler.view.InstrumentTable; |
61 |
import org.jsampler.view.InstrumentTableModel; |
62 |
|
63 |
import org.linuxsampler.lscp.MidiInstrumentEntry; |
64 |
import org.linuxsampler.lscp.MidiInstrumentInfo; |
65 |
|
66 |
import static org.jsampler.view.std.StdI18n.i18n; |
67 |
|
68 |
/** |
69 |
* |
70 |
* @author Grigor Iliev |
71 |
*/ |
72 |
public class JSOrchestraPane extends JPanel { |
73 |
protected final InstrumentTable instrumentTable; |
74 |
|
75 |
protected final Action actionAddInstrument = new AddInstrumentAction(); |
76 |
protected final Action actionEditInstrument = new EditInstrumentAction(); |
77 |
protected final Action actionDeleteInstrument = new DeleteInstrumentAction(); |
78 |
protected final Action actionInstrumentUp = new InstrumentUpAction(); |
79 |
protected final Action actionInstrumentDown = new InstrumentDownAction(); |
80 |
|
81 |
private OrchestraModel orchestra; |
82 |
|
83 |
/** Creates a new instance of <code>JSOrchestraPane</code> */ |
84 |
public |
85 |
JSOrchestraPane() { |
86 |
this(null); |
87 |
} |
88 |
|
89 |
/** Creates a new instance of <code>JSOrchestraPane</code> */ |
90 |
public |
91 |
JSOrchestraPane(OrchestraModel orchestra) { |
92 |
instrumentTable = new InstrumentTable(); |
93 |
setOrchestra(orchestra); |
94 |
|
95 |
setLayout(new BorderLayout()); |
96 |
JScrollPane sp = new JScrollPane(instrumentTable); |
97 |
add(sp); |
98 |
|
99 |
installListeneres(); |
100 |
} |
101 |
|
102 |
private void |
103 |
installListeneres() { |
104 |
InstrumentSelectionHandler l = new InstrumentSelectionHandler(); |
105 |
instrumentTable.getSelectionModel().addListSelectionListener(l); |
106 |
|
107 |
instrumentTable.addMouseListener(new MouseAdapter() { |
108 |
public void |
109 |
mousePressed(MouseEvent e) { |
110 |
if(e.getButton() != e.BUTTON3) return; |
111 |
|
112 |
int i = instrumentTable.rowAtPoint(e.getPoint()); |
113 |
if(i == -1) return; |
114 |
|
115 |
instrumentTable.getSelectionModel().setSelectionInterval(i, i); |
116 |
|
117 |
} |
118 |
|
119 |
public void |
120 |
mouseClicked(MouseEvent e) { |
121 |
if(e.getClickCount() < 2) return; |
122 |
|
123 |
if(instrumentTable.getSelectedInstrument() == null) return; |
124 |
editInstrument(instrumentTable.getSelectedInstrument()); |
125 |
} |
126 |
}); |
127 |
|
128 |
ContextMenu contextMenu = new ContextMenu(); |
129 |
instrumentTable.addMouseListener(contextMenu); |
130 |
} |
131 |
|
132 |
public void |
133 |
setOrchestra(OrchestraModel orchestra) { |
134 |
this.orchestra = orchestra; |
135 |
if(orchestra == null) { |
136 |
orchestra = new DefaultOrchestraModel(); |
137 |
actionAddInstrument.setEnabled(false); |
138 |
} else { |
139 |
actionAddInstrument.setEnabled(true); |
140 |
} |
141 |
instrumentTable.getModel().setOrchestraModel(orchestra); |
142 |
} |
143 |
|
144 |
public Instrument |
145 |
getSelectedInstrument() { return instrumentTable.getSelectedInstrument(); } |
146 |
|
147 |
/** |
148 |
* Invoked when the user initiates the creation of new instrument. |
149 |
* @return The instrument to add |
150 |
* or <code>null</code> if the user cancelled the task. |
151 |
*/ |
152 |
public Instrument |
153 |
createInstrument() { |
154 |
JSAddOrEditInstrumentDlg dlg = new JSAddOrEditInstrumentDlg(); |
155 |
dlg.setVisible(true); |
156 |
|
157 |
if(dlg.isCancelled()) return null; |
158 |
return dlg.getInstrument(); |
159 |
} |
160 |
|
161 |
public void |
162 |
editInstrument(Instrument instr) { |
163 |
JSAddOrEditInstrumentDlg dlg = new JSAddOrEditInstrumentDlg(instr); |
164 |
dlg.setVisible(true); |
165 |
} |
166 |
|
167 |
private class InstrumentSelectionHandler implements ListSelectionListener { |
168 |
public void |
169 |
valueChanged(ListSelectionEvent e) { |
170 |
if(e.getValueIsAdjusting()) return; |
171 |
|
172 |
if(instrumentTable.getSelectedInstrument() == null) { |
173 |
actionEditInstrument.setEnabled(false); |
174 |
actionDeleteInstrument.setEnabled(false); |
175 |
actionInstrumentUp.setEnabled(false); |
176 |
actionInstrumentDown.setEnabled(false); |
177 |
return; |
178 |
} |
179 |
|
180 |
actionEditInstrument.setEnabled(true); |
181 |
actionDeleteInstrument.setEnabled(true); |
182 |
|
183 |
int idx = instrumentTable.getSelectedRow(); |
184 |
actionInstrumentUp.setEnabled(idx != 0); |
185 |
actionInstrumentDown.setEnabled(idx != instrumentTable.getRowCount() - 1); |
186 |
} |
187 |
} |
188 |
|
189 |
private class AddInstrumentAction extends AbstractAction { |
190 |
AddInstrumentAction() { |
191 |
super(""); |
192 |
|
193 |
String s = i18n.getLabel("JSOrchestraPane.ttAddInstrument"); |
194 |
putValue(SHORT_DESCRIPTION, s); |
195 |
} |
196 |
|
197 |
public void |
198 |
actionPerformed(ActionEvent e) { |
199 |
Instrument instr = createInstrument(); |
200 |
if(instr == null) return; |
201 |
orchestra.addInstrument(instr); |
202 |
} |
203 |
} |
204 |
|
205 |
private class EditInstrumentAction extends AbstractAction { |
206 |
EditInstrumentAction() { |
207 |
super(""); |
208 |
|
209 |
String s = i18n.getLabel("JSOrchestraPane.ttEditInstrument"); |
210 |
putValue(SHORT_DESCRIPTION, s); |
211 |
|
212 |
setEnabled(false); |
213 |
} |
214 |
|
215 |
public void |
216 |
actionPerformed(ActionEvent e) { |
217 |
editInstrument(instrumentTable.getSelectedInstrument()); |
218 |
} |
219 |
} |
220 |
|
221 |
private class DeleteInstrumentAction extends AbstractAction { |
222 |
DeleteInstrumentAction() { |
223 |
super(""); |
224 |
|
225 |
String s = i18n.getLabel("JSOrchestraPane.ttDeleteInstrument"); |
226 |
putValue(SHORT_DESCRIPTION, s); |
227 |
|
228 |
setEnabled(false); |
229 |
} |
230 |
|
231 |
public void |
232 |
actionPerformed(ActionEvent e) { |
233 |
Instrument instr = instrumentTable.getSelectedInstrument(); |
234 |
if(instr == null) return; |
235 |
orchestra.removeInstrument(instr); |
236 |
} |
237 |
} |
238 |
|
239 |
private class InstrumentUpAction extends AbstractAction { |
240 |
InstrumentUpAction() { |
241 |
super(""); |
242 |
|
243 |
String s = i18n.getLabel("JSOrchestraPane.ttInstrumentUp"); |
244 |
putValue(SHORT_DESCRIPTION, s); |
245 |
|
246 |
setEnabled(false); |
247 |
} |
248 |
|
249 |
public void |
250 |
actionPerformed(ActionEvent e) { |
251 |
Instrument instr = instrumentTable.getSelectedInstrument(); |
252 |
instrumentTable.getModel().getOrchestraModel().moveInstrumentUp(instr); |
253 |
instrumentTable.setSelectedInstrument(instr); |
254 |
} |
255 |
} |
256 |
|
257 |
private class InstrumentDownAction extends AbstractAction { |
258 |
InstrumentDownAction() { |
259 |
super(""); |
260 |
|
261 |
String s = i18n.getLabel("JSOrchestraPane.ttInstrumentDown"); |
262 |
putValue(SHORT_DESCRIPTION, s); |
263 |
|
264 |
setEnabled(false); |
265 |
} |
266 |
|
267 |
public void |
268 |
actionPerformed(ActionEvent e) { |
269 |
Instrument instr = instrumentTable.getSelectedInstrument(); |
270 |
instrumentTable.getModel().getOrchestraModel().moveInstrumentDown(instr); |
271 |
instrumentTable.setSelectedInstrument(instr); |
272 |
} |
273 |
} |
274 |
|
275 |
|
276 |
|
277 |
private class LoadInstrumentAction extends AbstractAction { |
278 |
private final SamplerChannelModel channelModel; |
279 |
|
280 |
LoadInstrumentAction(SamplerChannelModel model) { |
281 |
String s = "instrumentsdb.actions.loadInstrument.onChannel"; |
282 |
putValue(Action.NAME, i18n.getMenuLabel(s, model.getChannelId())); |
283 |
channelModel = model; |
284 |
} |
285 |
|
286 |
public void |
287 |
actionPerformed(ActionEvent e) { |
288 |
Instrument instr = instrumentTable.getSelectedInstrument(); |
289 |
if(instr == null) return; |
290 |
|
291 |
int idx = instr.getInstrumentIndex(); |
292 |
channelModel.setBackendEngineType(instr.getEngine()); |
293 |
channelModel.loadBackendInstrument(instr.getPath(), idx); |
294 |
} |
295 |
} |
296 |
|
297 |
class AddToMidiMapAction extends AbstractAction { |
298 |
private final MidiInstrumentMap midiMap; |
299 |
|
300 |
AddToMidiMapAction(MidiInstrumentMap map) { |
301 |
super(map.getName()); |
302 |
midiMap = map; |
303 |
} |
304 |
|
305 |
public void |
306 |
actionPerformed(ActionEvent e) { |
307 |
Instrument instr = instrumentTable.getSelectedInstrument(); |
308 |
if(instr == null) return; |
309 |
|
310 |
JSAddMidiInstrumentDlg dlg; |
311 |
Window w = JuifeUtils.getWindow(JSOrchestraPane.this); |
312 |
if(w instanceof Dialog) { |
313 |
dlg = new JSAddMidiInstrumentDlg((Dialog)w); |
314 |
} else if(w instanceof Frame) { |
315 |
dlg = new JSAddMidiInstrumentDlg((Frame)w); |
316 |
} else { |
317 |
dlg = new JSAddMidiInstrumentDlg((Frame)null); |
318 |
} |
319 |
|
320 |
dlg.setInstrumentName(instr.getName()); |
321 |
MidiInstrumentEntry entry = midiMap.getAvailableEntry(); |
322 |
if(entry != null) { |
323 |
dlg.setMidiBank(entry.getMidiBank()); |
324 |
dlg.setMidiProgram(entry.getMidiProgram()); |
325 |
} |
326 |
dlg.setVisible(true); |
327 |
if(dlg.isCancelled()) return; |
328 |
|
329 |
MidiInstrumentInfo instrInfo = new MidiInstrumentInfo(); |
330 |
instrInfo.setName(dlg.getInstrumentName()); |
331 |
instrInfo.setFilePath(instr.getPath()); |
332 |
instrInfo.setInstrumentIndex(instr.getInstrumentIndex()); |
333 |
instrInfo.setEngine(instr.getEngine()); |
334 |
instrInfo.setVolume(dlg.getVolume()); |
335 |
instrInfo.setLoadMode(dlg.getLoadMode()); |
336 |
|
337 |
int id = midiMap.getMapId(); |
338 |
int b = dlg.getMidiBank(); |
339 |
int p = dlg.getMidiProgram(); |
340 |
CC.getSamplerModel().mapBackendMidiInstrument(id, b, p, instrInfo); |
341 |
} |
342 |
} |
343 |
|
344 |
|
345 |
class ContextMenu extends MouseAdapter |
346 |
implements SamplerChannelListListener, ListSelectionListener { |
347 |
|
348 |
private final JPopupMenu cmenu = new JPopupMenu(); |
349 |
JMenuItem miEdit = new JMenuItem(i18n.getMenuLabel("ContextMenu.edit")); |
350 |
|
351 |
JMenu mLoadInstrument = |
352 |
new JMenu(i18n.getMenuLabel("JSOrchestraPane.loadInstrument")); |
353 |
|
354 |
JMenu mMapInstrument = |
355 |
new JMenu(i18n.getMenuLabel("JSOrchestraPane.mapInstrument")); |
356 |
|
357 |
ContextMenu() { |
358 |
cmenu.add(miEdit); |
359 |
miEdit.addActionListener(new ActionListener() { |
360 |
public void |
361 |
actionPerformed(ActionEvent e) { |
362 |
actionEditInstrument.actionPerformed(null); |
363 |
} |
364 |
}); |
365 |
|
366 |
JMenuItem mi = new JMenuItem(i18n.getMenuLabel("ContextMenu.delete")); |
367 |
cmenu.add(mi); |
368 |
mi.addActionListener(new ActionListener() { |
369 |
public void |
370 |
actionPerformed(ActionEvent e) { |
371 |
actionDeleteInstrument.actionPerformed(null); |
372 |
} |
373 |
}); |
374 |
|
375 |
cmenu.addSeparator(); |
376 |
|
377 |
cmenu.add(mLoadInstrument); |
378 |
cmenu.add(mMapInstrument); |
379 |
|
380 |
CC.getSamplerModel().addSamplerChannelListListener(this); |
381 |
instrumentTable.getSelectionModel().addListSelectionListener(this); |
382 |
|
383 |
ListListener<MidiInstrumentMap> l = new ListListener<MidiInstrumentMap>() { |
384 |
public void |
385 |
entryAdded(ListEvent<MidiInstrumentMap> e) { |
386 |
updateAddToMidiMapMenu(mMapInstrument); |
387 |
} |
388 |
|
389 |
public void |
390 |
entryRemoved(ListEvent<MidiInstrumentMap> e) { |
391 |
updateAddToMidiMapMenu(mMapInstrument); |
392 |
} |
393 |
}; |
394 |
|
395 |
CC.getSamplerModel().addMidiInstrumentMapListListener(l); |
396 |
} |
397 |
|
398 |
private void |
399 |
updateLoadInstrumentMenu(JMenu menu) { |
400 |
menu.removeAll(); |
401 |
for(SamplerChannelModel m : CC.getSamplerModel().getChannels()) { |
402 |
menu.add(new JMenuItem(new LoadInstrumentAction(m))); |
403 |
} |
404 |
|
405 |
updateLoadInstrumentMenuState(menu); |
406 |
} |
407 |
|
408 |
private void |
409 |
updateLoadInstrumentMenuState(JMenu menu) { |
410 |
Instrument instr = instrumentTable.getSelectedInstrument(); |
411 |
boolean b = instr == null; |
412 |
b = b || CC.getSamplerModel().getChannelCount() == 0; |
413 |
menu.setEnabled(!b); |
414 |
} |
415 |
|
416 |
private void |
417 |
updateAddToMidiMapMenu(JMenu menu) { |
418 |
menu.removeAll(); |
419 |
for(int i = 0; i < CC.getSamplerModel().getMidiInstrumentMapCount(); i++) { |
420 |
MidiInstrumentMap m = CC.getSamplerModel().getMidiInstrumentMap(i); |
421 |
menu.add(new JMenuItem(new AddToMidiMapAction(m))); |
422 |
} |
423 |
|
424 |
updateAddToMidiMapMenuState(menu); |
425 |
} |
426 |
|
427 |
private void |
428 |
updateAddToMidiMapMenuState(JMenu menu) { |
429 |
Instrument instr = instrumentTable.getSelectedInstrument(); |
430 |
boolean b = instr == null; |
431 |
b = b || CC.getSamplerModel().getMidiInstrumentMapCount() == 0; |
432 |
menu.setEnabled(!b); |
433 |
} |
434 |
|
435 |
public void |
436 |
valueChanged(ListSelectionEvent e) { |
437 |
updateLoadInstrumentMenuState(mLoadInstrument); |
438 |
updateAddToMidiMapMenuState(mMapInstrument); |
439 |
} |
440 |
|
441 |
public void |
442 |
channelAdded(SamplerChannelListEvent e) { |
443 |
updateLoadInstrumentMenu(mLoadInstrument); |
444 |
} |
445 |
|
446 |
public void |
447 |
channelRemoved(SamplerChannelListEvent e) { |
448 |
updateLoadInstrumentMenu(mLoadInstrument); |
449 |
} |
450 |
|
451 |
public void |
452 |
mousePressed(MouseEvent e) { |
453 |
if(e.isPopupTrigger()) show(e); |
454 |
} |
455 |
|
456 |
public void |
457 |
mouseReleased(MouseEvent e) { |
458 |
if(e.isPopupTrigger()) show(e); |
459 |
} |
460 |
|
461 |
void |
462 |
show(MouseEvent e) { |
463 |
cmenu.show(e.getComponent(), e.getX(), e.getY()); |
464 |
} |
465 |
} |
466 |
} |