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