1 |
/* |
2 |
* JSampler - a java front-end for LinuxSampler |
3 |
* |
4 |
* Copyright (C) 2011 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 |
package org.jsampler.view.std; |
23 |
|
24 |
import java.awt.event.ActionEvent; |
25 |
import java.awt.event.ActionListener; |
26 |
import java.awt.event.MouseAdapter; |
27 |
import java.awt.event.MouseEvent; |
28 |
|
29 |
import javax.swing.AbstractAction; |
30 |
import javax.swing.Action; |
31 |
import javax.swing.JMenuItem; |
32 |
import javax.swing.JPopupMenu; |
33 |
|
34 |
import org.jsampler.AudioDeviceModel; |
35 |
import org.jsampler.CC; |
36 |
import org.jsampler.EffectInstance; |
37 |
import org.jsampler.view.SamplerTreeModel.*; |
38 |
|
39 |
import org.linuxsampler.lscp.EffectParameter; |
40 |
|
41 |
import static org.jsampler.view.std.StdA4n.a4n; |
42 |
import static org.jsampler.view.std.StdI18n.i18n; |
43 |
import static org.jsampler.view.std.StdViewConfig.getViewConfig; |
44 |
|
45 |
/** |
46 |
* |
47 |
* @author Grigor Iliev |
48 |
*/ |
49 |
public class SamplerBrowser { |
50 |
|
51 |
|
52 |
private static class SamplerMenu extends JPopupMenu { |
53 |
public |
54 |
SamplerMenu() { |
55 |
JMenuItem mi = new JMenuItem(a4n.refresh); |
56 |
add(mi); |
57 |
|
58 |
mi = new JMenuItem(a4n.resetSampler); |
59 |
add(mi); |
60 |
} |
61 |
} |
62 |
|
63 |
private static class DestEffectMenu extends JPopupMenu { |
64 |
public |
65 |
DestEffectMenu() { |
66 |
String s = i18n.getMenuLabel("SamplerBrowser.action.setDestEffect"); |
67 |
JMenuItem mi = new JMenuItem(s); |
68 |
add(mi); |
69 |
mi.addActionListener(new ActionListener() { |
70 |
public void |
71 |
actionPerformed(ActionEvent e) { |
72 |
DestEffectTreeNode node = getDestEffectTreeNode(); |
73 |
if(node == null) return; |
74 |
chooseDestEffect(node); |
75 |
} |
76 |
}); |
77 |
|
78 |
s = i18n.getMenuLabel("SamplerBrowser.action.removeDestEffect"); |
79 |
mi = new JMenuItem(s); |
80 |
add(mi); |
81 |
mi.addActionListener(new ActionListener() { |
82 |
public void |
83 |
actionPerformed(ActionEvent e) { |
84 |
DestEffectTreeNode node = getDestEffectTreeNode(); |
85 |
if(node == null) return; |
86 |
int fxSendId = node.getFxSend().getFxSendId(); |
87 |
node.getChannel().removeBackendFxSendEffect(fxSendId); |
88 |
} |
89 |
}); |
90 |
} |
91 |
|
92 |
private void |
93 |
chooseDestEffect(DestEffectTreeNode node) { |
94 |
AudioDeviceModel audioDev = node.getAudioDevice(); |
95 |
JSDestEffectChooser dlg = getViewConfig().createDestEffectChooser(audioDev); |
96 |
|
97 |
dlg.setVisible(true); |
98 |
if(dlg.isCancelled()) return; |
99 |
|
100 |
int fxSendId = node.getFxSend().getFxSendId(); |
101 |
int chainId = dlg.getSelectedNode().getParent().getId(); |
102 |
int chainPos = dlg.getSelectedNode().getParent().getIndex(dlg.getSelectedNode()); |
103 |
node.getChannel().setBackendFxSendEffect(fxSendId, chainId, chainPos); |
104 |
} |
105 |
|
106 |
private DestEffectTreeNode |
107 |
getDestEffectTreeNode() { |
108 |
if(ContextMenu.getCurrentOwner() == null) return null; |
109 |
Object o = ContextMenu.getCurrentOwner().getSelectedItem(); |
110 |
|
111 |
DestEffectTreeNode node = null; |
112 |
if(o == null) return null; |
113 |
|
114 |
if(o instanceof DestEffectDirTreeNode) { |
115 |
node = ((DestEffectDirTreeNode)o).getChildAt(0); |
116 |
} else if(o instanceof DestEffectTreeNode) { |
117 |
node = (DestEffectTreeNode)o; |
118 |
} |
119 |
|
120 |
return node; |
121 |
} |
122 |
} |
123 |
|
124 |
private static class AudioDeviceMenu extends JPopupMenu { |
125 |
public |
126 |
AudioDeviceMenu() { |
127 |
String s = i18n.getMenuLabel("SamplerBrowser.action.removeAudioDev"); |
128 |
JMenuItem mi = new JMenuItem(s); |
129 |
add(mi); |
130 |
mi.addActionListener(new ActionListener() { |
131 |
public void |
132 |
actionPerformed(ActionEvent e) { |
133 |
AudioDeviceTreeNode node = getAudioDeviceTreeNode(); |
134 |
if(node == null) return; |
135 |
|
136 |
int id = node.getAudioDevice().getDeviceId(); |
137 |
CC.getSamplerModel().removeBackendAudioDevice(id); |
138 |
} |
139 |
}); |
140 |
} |
141 |
|
142 |
private AudioDeviceTreeNode |
143 |
getAudioDeviceTreeNode() { |
144 |
if(ContextMenu.getCurrentOwner() == null) return null; |
145 |
Object o = ContextMenu.getCurrentOwner().getSelectedItem(); |
146 |
if(o == null || !(o instanceof AudioDeviceTreeNode)) return null; |
147 |
return (AudioDeviceTreeNode)o; |
148 |
} |
149 |
} |
150 |
|
151 |
private static class AddSendEffectChainAction extends AbstractAction { |
152 |
AddSendEffectChainAction() { |
153 |
super(i18n.getMenuLabel("SamplerBrowser.action.addChain")); |
154 |
} |
155 |
|
156 |
@Override |
157 |
public void |
158 |
actionPerformed(ActionEvent e) { |
159 |
SendEffectChainsTreeNode node = getSendEffectChainsTreeNode(); |
160 |
if(node == null) return; |
161 |
node.getAudioDevice().addBackendSendEffectChain(); |
162 |
} |
163 |
|
164 |
private SendEffectChainsTreeNode |
165 |
getSendEffectChainsTreeNode() { |
166 |
Object o = getSelectedObject(); |
167 |
if(o == null || !(o instanceof SendEffectChainsTreeNode)) return null; |
168 |
return (SendEffectChainsTreeNode)o; |
169 |
} |
170 |
} |
171 |
|
172 |
private static Object |
173 |
getSelectedObject() { |
174 |
if(ContextMenu.getCurrentOwner() == null) return null; |
175 |
Object o = ContextMenu.getCurrentOwner().getSelectedItem(); |
176 |
if(o == null) o = ContextMenu.getCurrentOwner().getSelectedParent(); |
177 |
return o; |
178 |
} |
179 |
|
180 |
private static class SendEffectChainDirMenu extends JPopupMenu { |
181 |
public |
182 |
SendEffectChainDirMenu() { |
183 |
JMenuItem mi = new JMenuItem(new AddSendEffectChainAction()); |
184 |
add(mi); |
185 |
} |
186 |
} |
187 |
|
188 |
private static class SendEffectChainMenu extends JPopupMenu { |
189 |
public |
190 |
SendEffectChainMenu() { |
191 |
String s = i18n.getMenuLabel("SamplerBrowser.action.removeChain"); |
192 |
JMenuItem mi = new JMenuItem(s); |
193 |
add(mi); |
194 |
mi.addActionListener(new ActionListener() { |
195 |
public void |
196 |
actionPerformed(ActionEvent e) { |
197 |
SendEffectChainTreeNode node = getSendEffectChainTreeNode(); |
198 |
if(node == null) return; |
199 |
|
200 |
node.getAudioDevice().removeBackendSendEffectChain(node.getId()); |
201 |
} |
202 |
}); |
203 |
|
204 |
mi = new JMenuItem(new AddSendEffectInstsAction()); |
205 |
add(mi); |
206 |
} |
207 |
} |
208 |
|
209 |
private static class AddSendEffectInstsAction extends AbstractAction { |
210 |
AddSendEffectInstsAction() { |
211 |
super(i18n.getMenuLabel("SamplerBrowser.action.appendEffectInstance")); |
212 |
} |
213 |
|
214 |
@Override |
215 |
public void |
216 |
actionPerformed(ActionEvent e) { |
217 |
SendEffectChainTreeNode node = getSendEffectChainTreeNode(); |
218 |
if(node == null) return; |
219 |
|
220 |
JSAddEffectInstancesDlg dlg = new JSAddEffectInstancesDlg(); |
221 |
dlg.setVisible(true); |
222 |
if(dlg.isCancelled()) return; |
223 |
|
224 |
node.getAudioDevice().addBackendEffectInstances ( |
225 |
dlg.getSelectedEffects(), node.getId(), -1 |
226 |
); |
227 |
} |
228 |
} |
229 |
|
230 |
private static SendEffectChainTreeNode |
231 |
getSendEffectChainTreeNode() { |
232 |
Object o = getSelectedObject(); |
233 |
if(o == null || !(o instanceof SendEffectChainTreeNode)) return null; |
234 |
return (SendEffectChainTreeNode)o; |
235 |
} |
236 |
|
237 |
private static class EffectInstanceMenu extends JPopupMenu { |
238 |
public |
239 |
EffectInstanceMenu() { |
240 |
String s = i18n.getMenuLabel("SamplerBrowser.action.removeEffectInstance"); |
241 |
JMenuItem mi = new JMenuItem(s); |
242 |
add(mi); |
243 |
mi.addActionListener(new ActionListener() { |
244 |
public void |
245 |
actionPerformed(ActionEvent e) { |
246 |
EffectInstanceTreeNode node = getEffectInstanceTreeNode(); |
247 |
if(node == null) return; |
248 |
|
249 |
node.getParent().getAudioDevice().removeBackendEffectInstance ( |
250 |
node.getParent().getId(), node.getInstanceId() |
251 |
); |
252 |
} |
253 |
}); |
254 |
|
255 |
s = i18n.getMenuLabel("SamplerBrowser.action.insertEffectInstance"); |
256 |
mi = new JMenuItem(s); |
257 |
add(mi); |
258 |
mi.addActionListener(new ActionListener() { |
259 |
public void |
260 |
actionPerformed(ActionEvent e) { |
261 |
EffectInstanceTreeNode node = getEffectInstanceTreeNode(); |
262 |
if(node == null) return; |
263 |
|
264 |
JSAddEffectInstancesDlg dlg = new JSAddEffectInstancesDlg(); |
265 |
dlg.setVisible(true); |
266 |
if(dlg.isCancelled()) return; |
267 |
|
268 |
SendEffectChainTreeNode parent = node.getParent(); |
269 |
int idx = parent.getEffectChain().getIndex(node.getInstanceId()); |
270 |
|
271 |
parent.getAudioDevice().addBackendEffectInstances ( |
272 |
dlg.getSelectedEffects(), parent.getId(), idx |
273 |
); |
274 |
} |
275 |
}); |
276 |
} |
277 |
|
278 |
private EffectInstanceTreeNode |
279 |
getEffectInstanceTreeNode() { |
280 |
if(ContextMenu.getCurrentOwner() == null) return null; |
281 |
Object o = ContextMenu.getCurrentOwner().getSelectedItem(); |
282 |
if(o == null || !(o instanceof EffectInstanceTreeNode)) return null; |
283 |
return (EffectInstanceTreeNode)o; |
284 |
} |
285 |
} |
286 |
|
287 |
private static class EffectParameterMenu extends JPopupMenu { |
288 |
public |
289 |
EffectParameterMenu() { |
290 |
String s = i18n.getMenuLabel("SamplerBrowser.action.editEffectPrm"); |
291 |
JMenuItem mi = new JMenuItem(s); |
292 |
add(mi); |
293 |
mi.addActionListener(new ActionListener() { |
294 |
public void |
295 |
actionPerformed(ActionEvent e) { editParameter(); } |
296 |
}); |
297 |
} |
298 |
|
299 |
private void |
300 |
editParameter() { |
301 |
EffectParameter prm = getEffectParameter(); |
302 |
if(prm == null) return; |
303 |
|
304 |
JSSetParameterDlg dlg = new JSSetParameterDlg(prm); |
305 |
|
306 |
dlg.setVisible(true); |
307 |
if(dlg.isCancelled()) return; |
308 |
|
309 |
EffectInstance ei; |
310 |
ei = CC.getSamplerModel().getEffectInstanceById(prm.getEffectInstanceId()); |
311 |
if(ei == null) return; |
312 |
ei.setBackendParameter(prm.getIndex(), dlg.getNewValue()); |
313 |
} |
314 |
|
315 |
private EffectParameter |
316 |
getEffectParameter() { |
317 |
if(ContextMenu.getCurrentOwner() == null) return null; |
318 |
Object o = ContextMenu.getCurrentOwner().getSelectedItem(); |
319 |
if(o == null || !(o instanceof EffectParameter)) return null; |
320 |
return (EffectParameter)o; |
321 |
} |
322 |
} |
323 |
|
324 |
public static class ContextMenu extends MouseAdapter { |
325 |
private static ContextMenuOwner currentOwner; |
326 |
|
327 |
private static SamplerMenu samplerMenu = null; |
328 |
private static DestEffectMenu destEffectMenu = null; |
329 |
private static AudioDeviceMenu auDevMenu = null; |
330 |
private static SendEffectChainDirMenu chainDirMenu = null; |
331 |
private static SendEffectChainMenu chainMenu = null; |
332 |
private static EffectInstanceMenu instanceMenu = null; |
333 |
private static EffectParameterMenu effectPrmMenu = null; |
334 |
|
335 |
private final ContextMenuOwner owner; |
336 |
|
337 |
public |
338 |
ContextMenu(ContextMenuOwner owner) { |
339 |
this.owner = owner; |
340 |
} |
341 |
|
342 |
public static ContextMenuOwner |
343 |
getCurrentOwner() { return currentOwner; } |
344 |
|
345 |
@Override |
346 |
public void |
347 |
mousePressed(MouseEvent e) { |
348 |
if(e.isPopupTrigger()) show(e); |
349 |
} |
350 |
|
351 |
@Override |
352 |
public void |
353 |
mouseReleased(MouseEvent e) { |
354 |
if(e.isPopupTrigger()) show(e); |
355 |
} |
356 |
|
357 |
void |
358 |
show(MouseEvent e) { |
359 |
Object o = owner.getSelectedItem(); |
360 |
if(o == null) { |
361 |
if(owner.getSelectedParent() == null) return; |
362 |
show0(e); |
363 |
return; |
364 |
} |
365 |
|
366 |
currentOwner = owner; |
367 |
|
368 |
if(o instanceof SamplerTreeNode) { |
369 |
if(samplerMenu == null) samplerMenu = new SamplerMenu(); |
370 |
samplerMenu.show(e.getComponent(), e.getX(), e.getY()); |
371 |
} else if(o instanceof DestEffectDirTreeNode) { |
372 |
if(destEffectMenu == null) destEffectMenu = new DestEffectMenu(); |
373 |
destEffectMenu.show(e.getComponent(), e.getX(), e.getY()); |
374 |
} else if(o instanceof DestEffectTreeNode) { |
375 |
if(destEffectMenu == null) destEffectMenu = new DestEffectMenu(); |
376 |
destEffectMenu.show(e.getComponent(), e.getX(), e.getY()); |
377 |
} else if(o instanceof AudioDeviceTreeNode) { |
378 |
if(auDevMenu == null) auDevMenu = new AudioDeviceMenu(); |
379 |
auDevMenu.show(e.getComponent(), e.getX(), e.getY()); |
380 |
} else if(o instanceof SendEffectChainsTreeNode) { |
381 |
if(chainDirMenu == null) chainDirMenu = new SendEffectChainDirMenu(); |
382 |
chainDirMenu.show(e.getComponent(), e.getX(), e.getY()); |
383 |
} else if(o instanceof SendEffectChainTreeNode) { |
384 |
if(chainMenu == null) chainMenu = new SendEffectChainMenu(); |
385 |
chainMenu.show(e.getComponent(), e.getX(), e.getY()); |
386 |
} else if(o instanceof EffectInstanceTreeNode) { |
387 |
if(instanceMenu == null) instanceMenu = new EffectInstanceMenu(); |
388 |
instanceMenu.show(e.getComponent(), e.getX(), e.getY()); |
389 |
} else if(o instanceof EffectParameter) { |
390 |
if(effectPrmMenu == null) effectPrmMenu = new EffectParameterMenu(); |
391 |
effectPrmMenu.show(e.getComponent(), e.getX(), e.getY()); |
392 |
} |
393 |
|
394 |
} |
395 |
|
396 |
void |
397 |
show0(MouseEvent e) { |
398 |
Object o = owner.getSelectedParent(); |
399 |
|
400 |
if(o instanceof SendEffectChainsTreeNode) { |
401 |
if(inChainDirMenu == null) inChainDirMenu = new InSendEffectChainDirMenu(); |
402 |
inChainDirMenu.show(e.getComponent(), e.getX(), e.getY()); |
403 |
} else if(o instanceof SendEffectChainTreeNode) { |
404 |
if(inChainMenu == null) inChainMenu = new InSendEffectChainMenu(); |
405 |
inChainMenu.show(e.getComponent(), e.getX(), e.getY()); |
406 |
} |
407 |
} |
408 |
|
409 |
private static InSendEffectChainDirMenu inChainDirMenu = null; |
410 |
private static InSendEffectChainMenu inChainMenu = null; |
411 |
} |
412 |
|
413 |
private static class InSendEffectChainDirMenu extends JPopupMenu { |
414 |
public |
415 |
InSendEffectChainDirMenu() { |
416 |
JMenuItem mi = new JMenuItem(new AddSendEffectChainAction()); |
417 |
add(mi); |
418 |
} |
419 |
} |
420 |
|
421 |
private static class InSendEffectChainMenu extends JPopupMenu { |
422 |
public |
423 |
InSendEffectChainMenu() { |
424 |
JMenuItem mi = new JMenuItem(new AddSendEffectInstsAction()); |
425 |
add(mi); |
426 |
} |
427 |
} |
428 |
|
429 |
public static interface ContextMenuOwner { |
430 |
public Object getSelectedItem(); |
431 |
public Object getSelectedParent(); |
432 |
} |
433 |
} |