/[svn]/jsampler/trunk/src/org/jsampler/view/std/StdA4n.java
ViewVC logotype

Contents of /jsampler/trunk/src/org/jsampler/view/std/StdA4n.java

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1883 - (show annotations) (download)
Sun Apr 5 09:15:35 2009 UTC (15 years ago) by iliev
File size: 13538 byte(s)
* fixed the channel order when exporting sampler configuration
* don't mute channels muted by solo channel when exporting
   sampler configuration
* don't ask whether to replace a file on Mac OS when using
  native file choosers

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

  ViewVC Help
Powered by ViewVC