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.event.ActionEvent; |
26 |
|
27 |
import java.io.FileOutputStream; |
28 |
|
29 |
import java.util.logging.Level; |
30 |
|
31 |
import javax.swing.AbstractAction; |
32 |
import javax.swing.Action; |
33 |
import javax.swing.JFileChooser; |
34 |
|
35 |
import javax.swing.JMenu; |
36 |
import javax.swing.JMenuItem; |
37 |
import javax.swing.SwingUtilities; |
38 |
|
39 |
import javax.swing.event.ListSelectionEvent; |
40 |
import javax.swing.event.ListSelectionListener; |
41 |
|
42 |
import org.jsampler.CC; |
43 |
import org.jsampler.HF; |
44 |
import org.jsampler.JSPrefs; |
45 |
|
46 |
import org.jsampler.SamplerChannelModel; |
47 |
|
48 |
import org.jsampler.view.JSChannel; |
49 |
import org.jsampler.view.JSChannelsPane; |
50 |
import org.jsampler.view.LscpFileFilter; |
51 |
|
52 |
import static org.jsampler.view.std.StdI18n.i18n; |
53 |
|
54 |
|
55 |
/** |
56 |
* This class provides an <code>Action</code> instances performing some of the common tasks. |
57 |
* @author Grigor Iliev |
58 |
*/ |
59 |
public class StdA4n { |
60 |
protected static StdA4n a4n = new StdA4n(); |
61 |
|
62 |
protected StdA4n() { } |
63 |
|
64 |
protected JSPrefs preferences() { return CC.getViewConfig().preferences(); } |
65 |
|
66 |
protected void |
67 |
exportSamplerConfig() { |
68 |
String s = preferences().getStringProperty("lastScriptLocation"); |
69 |
JFileChooser fc = new JFileChooser(s); |
70 |
fc.setFileFilter(new LscpFileFilter()); |
71 |
int result = fc.showSaveDialog(CC.getMainFrame()); |
72 |
if(result != JFileChooser.APPROVE_OPTION) return; |
73 |
|
74 |
String path = fc.getCurrentDirectory().getAbsolutePath(); |
75 |
preferences().setStringProperty("lastScriptLocation", path); |
76 |
|
77 |
try { |
78 |
FileOutputStream fos = new FileOutputStream(fc.getSelectedFile()); |
79 |
fos.write(CC.exportSessionToLscpScript().getBytes("US-ASCII")); |
80 |
fos.close(); |
81 |
} catch(Exception x) { |
82 |
CC.getLogger().log(Level.FINE, HF.getErrorMessage(x), x); |
83 |
HF.showErrorMessage(x); |
84 |
} |
85 |
} |
86 |
|
87 |
protected void |
88 |
exportMidiInstrumentMaps() { |
89 |
String s = preferences().getStringProperty("lastScriptLocation"); |
90 |
JFileChooser fc = new JFileChooser(s); |
91 |
fc.setFileFilter(new LscpFileFilter()); |
92 |
int result = fc.showSaveDialog(CC.getMainFrame()); |
93 |
if(result != JFileChooser.APPROVE_OPTION) return; |
94 |
|
95 |
String path = fc.getCurrentDirectory().getAbsolutePath(); |
96 |
preferences().setStringProperty("lastScriptLocation", path); |
97 |
|
98 |
try { |
99 |
FileOutputStream fos = new FileOutputStream(fc.getSelectedFile()); |
100 |
fos.write(CC.exportInstrMapsToLscpScript().getBytes("US-ASCII")); |
101 |
fos.close(); |
102 |
} catch(Exception x) { |
103 |
CC.getLogger().log(Level.FINE, HF.getErrorMessage(x), x); |
104 |
HF.showErrorMessage(x); |
105 |
} |
106 |
} |
107 |
|
108 |
public final Action connect = new Connect(); |
109 |
|
110 |
private class Connect extends AbstractAction { |
111 |
Connect() { |
112 |
super(i18n.getMenuLabel("actions.connect")); |
113 |
|
114 |
putValue(SHORT_DESCRIPTION, i18n.getMenuLabel("actions.connect.tt")); |
115 |
} |
116 |
|
117 |
@Override |
118 |
public void |
119 |
actionPerformed(ActionEvent e) { CC.reconnect(); } |
120 |
} |
121 |
|
122 |
public final Action refresh = new Refresh(); |
123 |
|
124 |
private class Refresh extends AbstractAction { |
125 |
Refresh() { |
126 |
super(i18n.getMenuLabel("actions.refresh")); |
127 |
|
128 |
putValue(SHORT_DESCRIPTION, i18n.getMenuLabel("actions.refresh.tt")); |
129 |
} |
130 |
|
131 |
@Override |
132 |
public void |
133 |
actionPerformed(ActionEvent e) { |
134 |
if(!CC.verifyConnection()) { |
135 |
CC.changeBackend(); |
136 |
return; |
137 |
} |
138 |
CC.reconnect(); |
139 |
} |
140 |
} |
141 |
|
142 |
public final Action resetSampler = new Reset(); |
143 |
|
144 |
private class Reset extends AbstractAction { |
145 |
Reset() { |
146 |
super(i18n.getMenuLabel("actions.resetSampler")); |
147 |
|
148 |
putValue(SHORT_DESCRIPTION, i18n.getMenuLabel("actions.resetSampler.tt")); |
149 |
} |
150 |
|
151 |
@Override |
152 |
public void |
153 |
actionPerformed(ActionEvent e) { |
154 |
if(!CC.verifyConnection()) return; |
155 |
|
156 |
String s = i18n.getMessage("StdA4n.resetSampler?"); |
157 |
if(!HF.showYesNoDialog(CC.getMainFrame(), s)) return; |
158 |
CC.getSamplerModel().resetBackend(); |
159 |
} |
160 |
} |
161 |
|
162 |
public final Action exportSamplerConfig = new ExportSamplerConfig(); |
163 |
|
164 |
private class ExportSamplerConfig extends AbstractAction { |
165 |
ExportSamplerConfig() { |
166 |
super(i18n.getMenuLabel("actions.export.samplerConfiguration")); |
167 |
|
168 |
String s = i18n.getMenuLabel("actions.export.samplerConfiguration.tt"); |
169 |
putValue(SHORT_DESCRIPTION, s); |
170 |
|
171 |
} |
172 |
|
173 |
@Override |
174 |
public void |
175 |
actionPerformed(ActionEvent e) { |
176 |
if(!CC.verifyConnection()) return; |
177 |
exportSamplerConfig(); |
178 |
} |
179 |
} |
180 |
|
181 |
public final Action exportMidiInstrumentMaps = new ExportMidiInstrumentMaps(); |
182 |
|
183 |
private class ExportMidiInstrumentMaps extends AbstractAction { |
184 |
ExportMidiInstrumentMaps() { |
185 |
super(i18n.getMenuLabel("actions.export.MidiInstrumentMaps")); |
186 |
|
187 |
String s = i18n.getMenuLabel("actions.export.MidiInstrumentMaps.tt"); |
188 |
putValue(SHORT_DESCRIPTION, s); |
189 |
} |
190 |
|
191 |
@Override |
192 |
public void |
193 |
actionPerformed(ActionEvent e) { |
194 |
if(!CC.verifyConnection()) return; |
195 |
exportMidiInstrumentMaps(); |
196 |
} |
197 |
} |
198 |
|
199 |
public final Action changeBackend = new ChangeBackend(); |
200 |
|
201 |
private class ChangeBackend extends AbstractAction { |
202 |
ChangeBackend() { |
203 |
super(i18n.getMenuLabel("actions.changeBackend")); |
204 |
|
205 |
putValue(SHORT_DESCRIPTION, i18n.getMenuLabel("actions.changeBackend.tt")); |
206 |
} |
207 |
|
208 |
@Override |
209 |
public void |
210 |
actionPerformed(ActionEvent e) { CC.changeBackend(); } |
211 |
} |
212 |
|
213 |
|
214 |
public final Action moveChannelsOnTop = new MoveChannelsOnTop(); |
215 |
|
216 |
private class MoveChannelsOnTop extends AbstractAction { |
217 |
MoveChannelsOnTop() { |
218 |
super(i18n.getMenuLabel("channels.moveOnTop")); |
219 |
|
220 |
putValue(SHORT_DESCRIPTION, i18n.getMenuLabel("channels.moveOnTop.tt")); |
221 |
setEnabled(false); |
222 |
} |
223 |
|
224 |
@Override |
225 |
public void |
226 |
actionPerformed(ActionEvent e) { |
227 |
JSChannelsPane p = CC.getMainFrame().getSelectedChannelsPane(); |
228 |
p.moveSelectedChannelsOnTop(); |
229 |
} |
230 |
} |
231 |
|
232 |
public final Action moveChannelsUp = new MoveChannelsUp(); |
233 |
|
234 |
private class MoveChannelsUp extends AbstractAction { |
235 |
MoveChannelsUp() { |
236 |
super(i18n.getMenuLabel("channels.moveUp")); |
237 |
|
238 |
putValue(SHORT_DESCRIPTION, i18n.getMenuLabel("channels.moveUp.tt")); |
239 |
setEnabled(false); |
240 |
} |
241 |
|
242 |
@Override |
243 |
public void |
244 |
actionPerformed(ActionEvent e) { |
245 |
JSChannelsPane p = CC.getMainFrame().getSelectedChannelsPane(); |
246 |
p.moveSelectedChannelsUp(); |
247 |
} |
248 |
} |
249 |
|
250 |
public final Action moveChannelsDown = new MoveChannelsDown(); |
251 |
|
252 |
private class MoveChannelsDown extends AbstractAction { |
253 |
MoveChannelsDown() { |
254 |
super(i18n.getMenuLabel("channels.moveDown")); |
255 |
|
256 |
putValue(SHORT_DESCRIPTION, i18n.getMenuLabel("channels.moveDown.tt")); |
257 |
setEnabled(false); |
258 |
} |
259 |
|
260 |
@Override |
261 |
public void |
262 |
actionPerformed(ActionEvent e) { |
263 |
JSChannelsPane p = CC.getMainFrame().getSelectedChannelsPane(); |
264 |
p.moveSelectedChannelsDown(); |
265 |
} |
266 |
} |
267 |
|
268 |
public final Action moveChannelsAtBottom = new MoveChannelsAtBottom(); |
269 |
|
270 |
private class MoveChannelsAtBottom extends AbstractAction { |
271 |
MoveChannelsAtBottom() { |
272 |
super(i18n.getMenuLabel("channels.moveAtBottom")); |
273 |
|
274 |
putValue(SHORT_DESCRIPTION, i18n.getMenuLabel("channels.moveAtBottom.tt")); |
275 |
setEnabled(false); |
276 |
} |
277 |
|
278 |
@Override |
279 |
public void |
280 |
actionPerformed(ActionEvent e) { |
281 |
JSChannelsPane p = CC.getMainFrame().getSelectedChannelsPane(); |
282 |
p.moveSelectedChannelsAtBottom(); |
283 |
} |
284 |
} |
285 |
|
286 |
public final Action duplicateChannels = new DuplicateChannels(); |
287 |
|
288 |
private static class DuplicateChannels extends AbstractAction { |
289 |
DuplicateChannels() { |
290 |
super(i18n.getMenuLabel("channels.duplicate")); |
291 |
|
292 |
putValue(SHORT_DESCRIPTION, i18n.getMenuLabel("channels.duplicateChannels.tt")); |
293 |
|
294 |
setEnabled(false); |
295 |
} |
296 |
|
297 |
public void |
298 |
actionPerformed(ActionEvent e) { |
299 |
JSChannel[] channels = |
300 |
CC.getMainFrame().getSelectedChannelsPane().getSelectedChannels(); |
301 |
|
302 |
if(channels.length > 2) { |
303 |
if(!HF.showYesNoDialog ( |
304 |
CC.getMainFrame(), |
305 |
i18n.getMessage("StdA4n.duplicateChannels?") |
306 |
)) return; |
307 |
} |
308 |
|
309 |
CC.getTaskQueue().add ( |
310 |
new org.jsampler.task.DuplicateChannels(channels) |
311 |
); |
312 |
} |
313 |
} |
314 |
|
315 |
public final Action removeChannels = new RemoveChannels(); |
316 |
|
317 |
private static class RemoveChannels extends AbstractAction { |
318 |
RemoveChannels() { |
319 |
super(i18n.getMenuLabel("channels.removeChannel")); |
320 |
|
321 |
putValue(SHORT_DESCRIPTION, i18n.getMenuLabel("channels.removeChannels.tt")); |
322 |
|
323 |
setEnabled(false); |
324 |
} |
325 |
|
326 |
public void |
327 |
actionPerformed(ActionEvent e) { |
328 |
JSChannelsPane p = CC.getMainFrame().getSelectedChannelsPane(); |
329 |
if(p.getSelectedChannelCount() > 1) |
330 |
if(!HF.showYesNoDialog ( |
331 |
CC.getMainFrame(), i18n.getMessage("StdA4n.removeChannels?") |
332 |
)) return; |
333 |
|
334 |
JSChannel[] chnS = p.getSelectedChannels(); |
335 |
|
336 |
for(JSChannel c : chnS) removeChannel(c); |
337 |
} |
338 |
|
339 |
private void |
340 |
removeChannel(final JSChannel c) { |
341 |
final JSChannelsPane p = CC.getMainFrame().getSelectedChannelsPane(); |
342 |
int id = c.getChannelInfo().getChannelId(); |
343 |
|
344 |
CC.getSamplerModel().removeBackendChannel(id); |
345 |
} |
346 |
} |
347 |
|
348 |
public static class |
349 |
MoveChannelsToPanel extends AbstractAction implements ListSelectionListener { |
350 |
private final JSChannelsPane pane; |
351 |
|
352 |
public |
353 |
MoveChannelsToPanel(JSChannelsPane pane) { |
354 |
super(pane.getTitle()); |
355 |
this.pane = pane; |
356 |
CC.getMainFrame().addChannelsPaneSelectionListener(this); |
357 |
valueChanged(null); |
358 |
} |
359 |
|
360 |
@Override |
361 |
public void |
362 |
actionPerformed(ActionEvent e) { |
363 |
JSChannelsPane acp = CC.getMainFrame().getSelectedChannelsPane(); |
364 |
JSChannel[] chns = acp.getSelectedChannels(); |
365 |
|
366 |
for(JSChannel c : chns) acp.removeChannel(c); |
367 |
|
368 |
pane.addChannels(chns); |
369 |
|
370 |
//CC.getMainFrame().setSelectedChannelsPane(pane); |
371 |
|
372 |
} |
373 |
|
374 |
@Override |
375 |
public void |
376 |
valueChanged(ListSelectionEvent e) { |
377 |
setEnabled(CC.getMainFrame().getSelectedChannelsPane() != pane); |
378 |
} |
379 |
|
380 |
public JSChannelsPane |
381 |
getChannelsPane() { return pane; } |
382 |
} |
383 |
|
384 |
public final Action selectAllChannels = new SelectAllChannels(); |
385 |
|
386 |
private static class SelectAllChannels extends AbstractAction { |
387 |
SelectAllChannels() { |
388 |
super(i18n.getMenuLabel("channels.selectAll")); |
389 |
|
390 |
putValue(SHORT_DESCRIPTION, i18n.getMenuLabel("channels.selectAll.tt")); |
391 |
} |
392 |
|
393 |
@Override |
394 |
public void |
395 |
actionPerformed(ActionEvent e) { |
396 |
CC.getMainFrame().getSelectedChannelsPane().selectAll(); |
397 |
} |
398 |
} |
399 |
|
400 |
public final Action deselectChannels = new DeselectChannels(); |
401 |
|
402 |
private static class DeselectChannels extends AbstractAction { |
403 |
DeselectChannels() { |
404 |
super(i18n.getMenuLabel("channels.selectNone")); |
405 |
|
406 |
putValue(SHORT_DESCRIPTION, i18n.getMenuLabel("channels.selectNone.tt")); |
407 |
} |
408 |
|
409 |
@Override |
410 |
public void |
411 |
actionPerformed(ActionEvent e) { |
412 |
CC.getMainFrame().getSelectedChannelsPane().clearSelection(); |
413 |
} |
414 |
} |
415 |
|
416 |
public final Action browseOnlineTutorial = new BrowseOnlineTutorial(); |
417 |
|
418 |
private class BrowseOnlineTutorial extends AbstractAction { |
419 |
BrowseOnlineTutorial() { |
420 |
super(i18n.getMenuLabel("help.onlineTutorial")); |
421 |
|
422 |
putValue(SHORT_DESCRIPTION, i18n.getMenuLabel("help.onlineTutorial.tt")); |
423 |
} |
424 |
|
425 |
@Override |
426 |
public void |
427 |
actionPerformed(ActionEvent e) { |
428 |
StdUtils.browse("http://jsampler.sourceforge.net/"); |
429 |
} |
430 |
} |
431 |
|
432 |
public static abstract class LoadInstrumentAction extends AbstractAction { |
433 |
protected final SamplerChannelModel channelModel; |
434 |
|
435 |
LoadInstrumentAction(SamplerChannelModel model) { this(model, false); } |
436 |
|
437 |
LoadInstrumentAction(SamplerChannelModel model, boolean onPanel) { |
438 |
String s = onPanel ? "instrumentsdb.actions.loadInstrument.onPanel.channel" |
439 |
: "instrumentsdb.actions.loadInstrument.onChannel"; |
440 |
int i = CC.getMainFrame().getChannelNumber(model) + 1; |
441 |
putValue(Action.NAME, i18n.getMenuLabel(s, i)); |
442 |
channelModel = model; |
443 |
} |
444 |
} |
445 |
|
446 |
public static interface LoadInstrumentActionFactory { |
447 |
public LoadInstrumentAction |
448 |
createLoadInstrumentAction(SamplerChannelModel model, boolean onPanel); |
449 |
} |
450 |
|
451 |
|
452 |
|
453 |
public static void |
454 |
updateLoadInstrumentMenu(final JMenu menu, final LoadInstrumentActionFactory factory) { |
455 |
SwingUtilities.invokeLater(new Runnable() { |
456 |
public void |
457 |
run() { updateLoadInstrumentMenu0(menu, factory); } |
458 |
}); |
459 |
} |
460 |
|
461 |
private static void |
462 |
updateLoadInstrumentMenu0(JMenu menu, LoadInstrumentActionFactory factory) { |
463 |
if(CC.getMainFrame() == null) return; |
464 |
menu.removeAll(); |
465 |
int count = 0; |
466 |
JSChannelsPane chnPane = null; |
467 |
for(int i = 0; i < CC.getMainFrame().getChannelsPaneCount(); i++) { |
468 |
if(CC.getMainFrame().getChannelsPane(i).getChannelCount() == 0) continue; |
469 |
|
470 |
chnPane = CC.getMainFrame().getChannelsPane(i); |
471 |
count++; |
472 |
String s = "instrumentsdb.actions.loadInstrument.onPanel"; |
473 |
JMenu m = new JMenu(i18n.getMenuLabel(s, i + 1)); |
474 |
for(int j = 0; j < chnPane.getChannelCount(); j++) { |
475 |
SamplerChannelModel chn = chnPane.getChannel(j).getModel(); |
476 |
m.add(new JMenuItem(factory.createLoadInstrumentAction(chn, true))); |
477 |
} |
478 |
menu.add(m); |
479 |
} |
480 |
|
481 |
if(count == 1 && CC.getMainFrame().getSelectedChannelsPane() == chnPane) { |
482 |
menu.removeAll(); |
483 |
|
484 |
for(int j = 0; j < chnPane.getChannelCount(); j++) { |
485 |
SamplerChannelModel chn = chnPane.getChannel(j).getModel(); |
486 |
menu.add(new JMenuItem(factory.createLoadInstrumentAction(chn, false))); |
487 |
} |
488 |
} |
489 |
} |
490 |
} |