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