1 |
/* |
2 |
* JSampler - a java front-end for LinuxSampler |
3 |
* |
4 |
* Copyright (C) 2005-2008 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.KeyEvent; |
33 |
import java.awt.event.MouseAdapter; |
34 |
import java.awt.event.MouseEvent; |
35 |
|
36 |
import javax.swing.AbstractAction; |
37 |
import javax.swing.Action; |
38 |
import javax.swing.JComponent; |
39 |
import javax.swing.JMenu; |
40 |
import javax.swing.JMenuItem; |
41 |
import javax.swing.JPanel; |
42 |
import javax.swing.JPopupMenu; |
43 |
import javax.swing.JScrollPane; |
44 |
import javax.swing.KeyStroke; |
45 |
|
46 |
import javax.swing.event.ListSelectionEvent; |
47 |
import javax.swing.event.ListSelectionListener; |
48 |
|
49 |
import net.sf.juife.JuifeUtils; |
50 |
|
51 |
import org.jsampler.CC; |
52 |
import org.jsampler.DefaultOrchestraModel; |
53 |
import org.jsampler.OrchestraInstrument; |
54 |
import org.jsampler.MidiInstrumentMap; |
55 |
import org.jsampler.OrchestraModel; |
56 |
import org.jsampler.SamplerChannelModel; |
57 |
|
58 |
import org.jsampler.event.ListEvent; |
59 |
import org.jsampler.event.ListListener; |
60 |
import org.jsampler.event.SamplerChannelListEvent; |
61 |
import org.jsampler.event.SamplerChannelListListener; |
62 |
|
63 |
import org.jsampler.view.InstrumentTable; |
64 |
import org.jsampler.view.InstrumentTableModel; |
65 |
|
66 |
import org.linuxsampler.lscp.MidiInstrumentEntry; |
67 |
import org.linuxsampler.lscp.MidiInstrumentInfo; |
68 |
|
69 |
import static org.jsampler.view.std.StdI18n.i18n; |
70 |
|
71 |
/** |
72 |
* |
73 |
* @author Grigor Iliev |
74 |
*/ |
75 |
public class JSOrchestraPane extends JPanel { |
76 |
protected final InstrumentTable instrumentTable; |
77 |
|
78 |
protected final Action actionAddInstrument = new AddInstrumentAction(); |
79 |
protected final Action actionEditInstrument = new EditInstrumentAction(); |
80 |
protected final Action actionDeleteInstrument = new DeleteInstrumentAction(); |
81 |
protected final Action actionInstrumentUp = new InstrumentUpAction(); |
82 |
protected final Action actionInstrumentDown = new InstrumentDownAction(); |
83 |
|
84 |
private OrchestraModel orchestra; |
85 |
|
86 |
/** Creates a new instance of <code>JSOrchestraPane</code> */ |
87 |
public |
88 |
JSOrchestraPane() { |
89 |
this(null); |
90 |
} |
91 |
|
92 |
/** Creates a new instance of <code>JSOrchestraPane</code> */ |
93 |
public |
94 |
JSOrchestraPane(OrchestraModel orchestra) { |
95 |
instrumentTable = new InstrumentTable(); |
96 |
instrumentTable.setFillsViewportHeight(true); |
97 |
setOrchestra(orchestra); |
98 |
|
99 |
setLayout(new BorderLayout()); |
100 |
JScrollPane sp = new JScrollPane(instrumentTable); |
101 |
add(sp); |
102 |
|
103 |
installListeneres(); |
104 |
} |
105 |
|
106 |
private void |
107 |
installListeneres() { |
108 |
InstrumentSelectionHandler l = new InstrumentSelectionHandler(); |
109 |
instrumentTable.getSelectionModel().addListSelectionListener(l); |
110 |
|
111 |
instrumentTable.addMouseListener(new MouseAdapter() { |
112 |
public void |
113 |
mousePressed(MouseEvent e) { |
114 |
if(e.getButton() != e.BUTTON3) return; |
115 |
|
116 |
int i = instrumentTable.rowAtPoint(e.getPoint()); |
117 |
if(i == -1) return; |
118 |
|
119 |
instrumentTable.getSelectionModel().setSelectionInterval(i, i); |
120 |
|
121 |
} |
122 |
|
123 |
public void |
124 |
mouseClicked(MouseEvent e) { |
125 |
if(e.getClickCount() < 2) return; |
126 |
|
127 |
if(instrumentTable.getSelectedInstrument() == null) return; |
128 |
editInstrument(instrumentTable.getSelectedInstrument()); |
129 |
} |
130 |
}); |
131 |
|
132 |
ContextMenu contextMenu = new ContextMenu(); |
133 |
instrumentTable.addMouseListener(contextMenu); |
134 |
|
135 |
KeyStroke k = KeyStroke.getKeyStroke(KeyEvent.VK_DELETE, 0); |
136 |
instrumentTable.getInputMap(JComponent.WHEN_FOCUSED).put(k, "deleteInstrument"); |
137 |
instrumentTable.getActionMap().put("deleteInstrument", actionDeleteInstrument); |
138 |
} |
139 |
|
140 |
public void |
141 |
setOrchestra(OrchestraModel orchestra) { |
142 |
this.orchestra = orchestra; |
143 |
if(orchestra == null) { |
144 |
orchestra = new DefaultOrchestraModel(); |
145 |
actionAddInstrument.setEnabled(false); |
146 |
} else { |
147 |
actionAddInstrument.setEnabled(true); |
148 |
} |
149 |
instrumentTable.getModel().setOrchestraModel(orchestra); |
150 |
} |
151 |
|
152 |
public OrchestraInstrument |
153 |
getSelectedInstrument() { return instrumentTable.getSelectedInstrument(); } |
154 |
|
155 |
/** |
156 |
* Invoked when the user initiates the creation of new instrument. |
157 |
* @return The instrument to add |
158 |
* or <code>null</code> if the user cancelled the task. |
159 |
*/ |
160 |
public OrchestraInstrument |
161 |
createInstrument() { |
162 |
JSAddOrEditInstrumentDlg dlg = new JSAddOrEditInstrumentDlg(); |
163 |
dlg.setVisible(true); |
164 |
|
165 |
if(dlg.isCancelled()) return null; |
166 |
return dlg.getInstrument(); |
167 |
} |
168 |
|
169 |
public void |
170 |
editInstrument(OrchestraInstrument instr) { |
171 |
JSAddOrEditInstrumentDlg dlg = new JSAddOrEditInstrumentDlg(instr); |
172 |
dlg.setVisible(true); |
173 |
} |
174 |
|
175 |
private class InstrumentSelectionHandler implements ListSelectionListener { |
176 |
public void |
177 |
valueChanged(ListSelectionEvent e) { |
178 |
if(e.getValueIsAdjusting()) return; |
179 |
|
180 |
if(instrumentTable.getSelectedInstrument() == null) { |
181 |
actionEditInstrument.setEnabled(false); |
182 |
actionDeleteInstrument.setEnabled(false); |
183 |
actionInstrumentUp.setEnabled(false); |
184 |
actionInstrumentDown.setEnabled(false); |
185 |
return; |
186 |
} |
187 |
|
188 |
actionEditInstrument.setEnabled(true); |
189 |
actionDeleteInstrument.setEnabled(true); |
190 |
|
191 |
int idx = instrumentTable.getSelectedRow(); |
192 |
actionInstrumentUp.setEnabled(idx != 0); |
193 |
actionInstrumentDown.setEnabled(idx != instrumentTable.getRowCount() - 1); |
194 |
} |
195 |
} |
196 |
|
197 |
private class AddInstrumentAction extends AbstractAction { |
198 |
AddInstrumentAction() { |
199 |
super(""); |
200 |
|
201 |
String s = i18n.getLabel("JSOrchestraPane.ttAddInstrument"); |
202 |
putValue(SHORT_DESCRIPTION, s); |
203 |
} |
204 |
|
205 |
public void |
206 |
actionPerformed(ActionEvent e) { |
207 |
OrchestraInstrument instr = createInstrument(); |
208 |
if(instr == null) return; |
209 |
orchestra.addInstrument(instr); |
210 |
} |
211 |
} |
212 |
|
213 |
private class EditInstrumentAction extends AbstractAction { |
214 |
EditInstrumentAction() { |
215 |
super(""); |
216 |
|
217 |
String s = i18n.getLabel("JSOrchestraPane.ttEditInstrument"); |
218 |
putValue(SHORT_DESCRIPTION, s); |
219 |
|
220 |
setEnabled(false); |
221 |
} |
222 |
|
223 |
public void |
224 |
actionPerformed(ActionEvent e) { |
225 |
editInstrument(instrumentTable.getSelectedInstrument()); |
226 |
} |
227 |
} |
228 |
|
229 |
private class DeleteInstrumentAction extends AbstractAction { |
230 |
DeleteInstrumentAction() { |
231 |
super(""); |
232 |
|
233 |
String s = i18n.getLabel("JSOrchestraPane.ttDeleteInstrument"); |
234 |
putValue(SHORT_DESCRIPTION, s); |
235 |
|
236 |
setEnabled(false); |
237 |
} |
238 |
|
239 |
public void |
240 |
actionPerformed(ActionEvent e) { |
241 |
OrchestraInstrument instr = instrumentTable.getSelectedInstrument(); |
242 |
if(instr == null) return; |
243 |
int i = instrumentTable.getSelectedRow(); |
244 |
orchestra.removeInstrument(instr); |
245 |
|
246 |
if(instrumentTable.getRowCount() > i) { |
247 |
instrumentTable.getSelectionModel().setSelectionInterval(i, i); |
248 |
} |
249 |
} |
250 |
} |
251 |
|
252 |
private class InstrumentUpAction extends AbstractAction { |
253 |
InstrumentUpAction() { |
254 |
super(""); |
255 |
|
256 |
String s = i18n.getLabel("JSOrchestraPane.ttInstrumentUp"); |
257 |
putValue(SHORT_DESCRIPTION, s); |
258 |
|
259 |
setEnabled(false); |
260 |
} |
261 |
|
262 |
public void |
263 |
actionPerformed(ActionEvent e) { |
264 |
OrchestraInstrument instr = instrumentTable.getSelectedInstrument(); |
265 |
instrumentTable.getModel().getOrchestraModel().moveInstrumentUp(instr); |
266 |
instrumentTable.setSelectedInstrument(instr); |
267 |
} |
268 |
} |
269 |
|
270 |
private class InstrumentDownAction extends AbstractAction { |
271 |
InstrumentDownAction() { |
272 |
super(""); |
273 |
|
274 |
String s = i18n.getLabel("JSOrchestraPane.ttInstrumentDown"); |
275 |
putValue(SHORT_DESCRIPTION, s); |
276 |
|
277 |
setEnabled(false); |
278 |
} |
279 |
|
280 |
public void |
281 |
actionPerformed(ActionEvent e) { |
282 |
OrchestraInstrument instr = instrumentTable.getSelectedInstrument(); |
283 |
instrumentTable.getModel().getOrchestraModel().moveInstrumentDown(instr); |
284 |
instrumentTable.setSelectedInstrument(instr); |
285 |
} |
286 |
} |
287 |
|
288 |
|
289 |
|
290 |
private class LoadInstrumentAction extends AbstractAction { |
291 |
private final SamplerChannelModel channelModel; |
292 |
|
293 |
LoadInstrumentAction(SamplerChannelModel model) { |
294 |
String s = "instrumentsdb.actions.loadInstrument.onChannel"; |
295 |
int i = CC.getSamplerModel().getChannelIndex(model) + 1; |
296 |
putValue(Action.NAME, i18n.getMenuLabel(s, i)); |
297 |
channelModel = model; |
298 |
} |
299 |
|
300 |
public void |
301 |
actionPerformed(ActionEvent e) { |
302 |
OrchestraInstrument instr = instrumentTable.getSelectedInstrument(); |
303 |
if(instr == null) return; |
304 |
|
305 |
int idx = instr.getInstrumentIndex(); |
306 |
channelModel.setBackendEngineType(instr.getEngine()); |
307 |
channelModel.loadBackendInstrument(instr.getFilePath(), idx); |
308 |
} |
309 |
} |
310 |
|
311 |
class AddToMidiMapAction extends AbstractAction { |
312 |
private final MidiInstrumentMap midiMap; |
313 |
|
314 |
AddToMidiMapAction(MidiInstrumentMap map) { |
315 |
super(map.getName()); |
316 |
midiMap = map; |
317 |
} |
318 |
|
319 |
public void |
320 |
actionPerformed(ActionEvent e) { |
321 |
OrchestraInstrument instr = instrumentTable.getSelectedInstrument(); |
322 |
if(instr == null) return; |
323 |
|
324 |
JSAddMidiInstrumentDlg dlg; |
325 |
Window w = JuifeUtils.getWindow(JSOrchestraPane.this); |
326 |
if(w instanceof Dialog) { |
327 |
dlg = new JSAddMidiInstrumentDlg((Dialog)w, midiMap, instr); |
328 |
} else if(w instanceof Frame) { |
329 |
dlg = new JSAddMidiInstrumentDlg((Frame)w, midiMap, instr); |
330 |
} else { |
331 |
dlg = new JSAddMidiInstrumentDlg((Frame)null, midiMap, instr); |
332 |
} |
333 |
|
334 |
dlg.setVisible(true); |
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 |
OrchestraInstrument 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 |
OrchestraInstrument 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 |
if(CC.getSamplerModel().getChannelListIsAdjusting()) return; |
438 |
updateLoadInstrumentMenu(mLoadInstrument); |
439 |
} |
440 |
|
441 |
public void |
442 |
channelRemoved(SamplerChannelListEvent e) { |
443 |
updateLoadInstrumentMenu(mLoadInstrument); |
444 |
} |
445 |
|
446 |
public void |
447 |
mousePressed(MouseEvent e) { |
448 |
if(e.isPopupTrigger()) show(e); |
449 |
} |
450 |
|
451 |
public void |
452 |
mouseReleased(MouseEvent e) { |
453 |
if(e.isPopupTrigger()) show(e); |
454 |
} |
455 |
|
456 |
void |
457 |
show(MouseEvent e) { |
458 |
cmenu.show(e.getComponent(), e.getX(), e.getY()); |
459 |
} |
460 |
} |
461 |
} |