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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1871 - (hide annotations) (download)
Sun Mar 22 18:11:39 2009 UTC (15 years, 1 month ago) by iliev
File size: 13216 byte(s)
* Mac OS integration, work in progress:
* Added option to use native file choosers
  (choose Edit/Preferences, then click the `View' tab)

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

  ViewVC Help
Powered by ViewVC