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