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