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 |
import org.jsampler.CC; |
39 |
import org.jsampler.HF; |
40 |
import org.jsampler.JSPrefs; |
41 |
|
42 |
import org.jsampler.SamplerChannelModel; |
43 |
import org.jsampler.view.JSChannelsPane; |
44 |
import org.jsampler.view.LscpFileFilter; |
45 |
|
46 |
import static org.jsampler.view.std.StdI18n.i18n; |
47 |
|
48 |
|
49 |
/** |
50 |
* This class provides an <code>Action</code> instances performing some of the common tasks. |
51 |
* @author Grigor Iliev |
52 |
*/ |
53 |
public class StdA4n { |
54 |
protected static StdA4n a4n = new StdA4n(); |
55 |
|
56 |
protected StdA4n() { } |
57 |
|
58 |
protected JSPrefs preferences() { return CC.getViewConfig().preferences(); } |
59 |
|
60 |
protected void |
61 |
exportSamplerConfig() { |
62 |
String s = preferences().getStringProperty("lastScriptLocation"); |
63 |
JFileChooser fc = new JFileChooser(s); |
64 |
fc.setFileFilter(new LscpFileFilter()); |
65 |
int result = fc.showSaveDialog(CC.getMainFrame()); |
66 |
if(result != JFileChooser.APPROVE_OPTION) return; |
67 |
|
68 |
String path = fc.getCurrentDirectory().getAbsolutePath(); |
69 |
preferences().setStringProperty("lastScriptLocation", path); |
70 |
|
71 |
try { |
72 |
FileOutputStream fos = new FileOutputStream(fc.getSelectedFile()); |
73 |
fos.write(CC.exportSessionToLscpScript().getBytes("US-ASCII")); |
74 |
fos.close(); |
75 |
} catch(Exception x) { |
76 |
CC.getLogger().log(Level.FINE, HF.getErrorMessage(x), x); |
77 |
HF.showErrorMessage(x); |
78 |
} |
79 |
} |
80 |
|
81 |
protected void |
82 |
exportMidiInstrumentMaps() { |
83 |
String s = preferences().getStringProperty("lastScriptLocation"); |
84 |
JFileChooser fc = new JFileChooser(s); |
85 |
fc.setFileFilter(new LscpFileFilter()); |
86 |
int result = fc.showSaveDialog(CC.getMainFrame()); |
87 |
if(result != JFileChooser.APPROVE_OPTION) return; |
88 |
|
89 |
String path = fc.getCurrentDirectory().getAbsolutePath(); |
90 |
preferences().setStringProperty("lastScriptLocation", path); |
91 |
|
92 |
try { |
93 |
FileOutputStream fos = new FileOutputStream(fc.getSelectedFile()); |
94 |
fos.write(CC.exportInstrMapsToLscpScript().getBytes("US-ASCII")); |
95 |
fos.close(); |
96 |
} catch(Exception x) { |
97 |
CC.getLogger().log(Level.FINE, HF.getErrorMessage(x), x); |
98 |
HF.showErrorMessage(x); |
99 |
} |
100 |
} |
101 |
|
102 |
public final Action connect = new Connect(); |
103 |
|
104 |
private class Connect extends AbstractAction { |
105 |
Connect() { |
106 |
super(i18n.getMenuLabel("actions.connect")); |
107 |
|
108 |
putValue(SHORT_DESCRIPTION, i18n.getMenuLabel("actions.connect.tt")); |
109 |
} |
110 |
|
111 |
@Override |
112 |
public void |
113 |
actionPerformed(ActionEvent e) { CC.reconnect(); } |
114 |
} |
115 |
|
116 |
public final Action refresh = new Refresh(); |
117 |
|
118 |
private class Refresh extends AbstractAction { |
119 |
Refresh() { |
120 |
super(i18n.getMenuLabel("actions.refresh")); |
121 |
|
122 |
putValue(SHORT_DESCRIPTION, i18n.getMenuLabel("actions.refresh.tt")); |
123 |
} |
124 |
|
125 |
public void |
126 |
actionPerformed(ActionEvent e) { |
127 |
if(!CC.verifyConnection()) { |
128 |
CC.changeBackend(); |
129 |
return; |
130 |
} |
131 |
CC.reconnect(); |
132 |
} |
133 |
} |
134 |
|
135 |
public final Action resetSampler = new Reset(); |
136 |
|
137 |
private class Reset extends AbstractAction { |
138 |
Reset() { |
139 |
super(i18n.getMenuLabel("actions.resetSampler")); |
140 |
|
141 |
putValue(SHORT_DESCRIPTION, i18n.getMenuLabel("actions.resetSampler.tt")); |
142 |
} |
143 |
|
144 |
public void |
145 |
actionPerformed(ActionEvent e) { |
146 |
if(!CC.verifyConnection()) return; |
147 |
|
148 |
String s = i18n.getMessage("StdA4n.resetSampler?"); |
149 |
if(!HF.showYesNoDialog(CC.getMainFrame(), s)) return; |
150 |
CC.getSamplerModel().resetBackend(); |
151 |
} |
152 |
} |
153 |
|
154 |
public final Action exportSamplerConfig = new ExportSamplerConfig(); |
155 |
|
156 |
private class ExportSamplerConfig extends AbstractAction { |
157 |
ExportSamplerConfig() { |
158 |
super(i18n.getMenuLabel("actions.export.samplerConfiguration")); |
159 |
|
160 |
String s = i18n.getMenuLabel("actions.export.samplerConfiguration.tt"); |
161 |
putValue(SHORT_DESCRIPTION, s); |
162 |
|
163 |
} |
164 |
|
165 |
public void |
166 |
actionPerformed(ActionEvent e) { |
167 |
if(!CC.verifyConnection()) return; |
168 |
exportSamplerConfig(); |
169 |
} |
170 |
} |
171 |
|
172 |
public final Action exportMidiInstrumentMaps = new ExportMidiInstrumentMaps(); |
173 |
|
174 |
private class ExportMidiInstrumentMaps extends AbstractAction { |
175 |
ExportMidiInstrumentMaps() { |
176 |
super(i18n.getMenuLabel("actions.export.MidiInstrumentMaps")); |
177 |
|
178 |
String s = i18n.getMenuLabel("actions.export.MidiInstrumentMaps.tt"); |
179 |
putValue(SHORT_DESCRIPTION, s); |
180 |
} |
181 |
|
182 |
public void |
183 |
actionPerformed(ActionEvent e) { |
184 |
if(!CC.verifyConnection()) return; |
185 |
exportMidiInstrumentMaps(); |
186 |
} |
187 |
} |
188 |
|
189 |
public final Action changeBackend = new ChangeBackend(); |
190 |
|
191 |
private class ChangeBackend extends AbstractAction { |
192 |
ChangeBackend() { |
193 |
super(i18n.getMenuLabel("actions.changeBackend")); |
194 |
|
195 |
putValue(SHORT_DESCRIPTION, i18n.getMenuLabel("actions.changeBackend.tt")); |
196 |
} |
197 |
|
198 |
public void |
199 |
actionPerformed(ActionEvent e) { CC.changeBackend(); } |
200 |
} |
201 |
|
202 |
public final Action browseOnlineTutorial = new BrowseOnlineTutorial(); |
203 |
|
204 |
private class BrowseOnlineTutorial extends AbstractAction { |
205 |
BrowseOnlineTutorial() { |
206 |
super(i18n.getMenuLabel("help.onlineTutorial")); |
207 |
|
208 |
putValue(SHORT_DESCRIPTION, i18n.getMenuLabel("help.onlineTutorial.tt")); |
209 |
} |
210 |
|
211 |
public void |
212 |
actionPerformed(ActionEvent e) { |
213 |
StdUtils.browse("http://jsampler.sourceforge.net/"); |
214 |
} |
215 |
} |
216 |
|
217 |
public static abstract class LoadInstrumentAction extends AbstractAction { |
218 |
protected final SamplerChannelModel channelModel; |
219 |
|
220 |
LoadInstrumentAction(SamplerChannelModel model) { this(model, false); } |
221 |
|
222 |
LoadInstrumentAction(SamplerChannelModel model, boolean onPanel) { |
223 |
String s = onPanel ? "instrumentsdb.actions.loadInstrument.onPanel.channel" |
224 |
: "instrumentsdb.actions.loadInstrument.onChannel"; |
225 |
int i = CC.getMainFrame().getChannelNumber(model) + 1; |
226 |
putValue(Action.NAME, i18n.getMenuLabel(s, i)); |
227 |
channelModel = model; |
228 |
} |
229 |
} |
230 |
|
231 |
public static interface LoadInstrumentActionFactory { |
232 |
public LoadInstrumentAction |
233 |
createLoadInstrumentAction(SamplerChannelModel model, boolean onPanel); |
234 |
} |
235 |
|
236 |
|
237 |
|
238 |
public static void |
239 |
updateLoadInstrumentMenu(final JMenu menu, final LoadInstrumentActionFactory factory) { |
240 |
SwingUtilities.invokeLater(new Runnable() { |
241 |
public void |
242 |
run() { updateLoadInstrumentMenu0(menu, factory); } |
243 |
}); |
244 |
} |
245 |
|
246 |
private static void |
247 |
updateLoadInstrumentMenu0(JMenu menu, LoadInstrumentActionFactory factory) { |
248 |
if(CC.getMainFrame() == null) return; |
249 |
menu.removeAll(); |
250 |
int count = 0; |
251 |
JSChannelsPane chnPane = null; |
252 |
for(int i = 0; i < CC.getMainFrame().getChannelsPaneCount(); i++) { |
253 |
if(CC.getMainFrame().getChannelsPane(i).getChannelCount() == 0) continue; |
254 |
|
255 |
chnPane = CC.getMainFrame().getChannelsPane(i); |
256 |
count++; |
257 |
String s = "instrumentsdb.actions.loadInstrument.onPanel"; |
258 |
JMenu m = new JMenu(i18n.getMenuLabel(s, i + 1)); |
259 |
for(int j = 0; j < chnPane.getChannelCount(); j++) { |
260 |
SamplerChannelModel chn = chnPane.getChannel(j).getModel(); |
261 |
m.add(new JMenuItem(factory.createLoadInstrumentAction(chn, true))); |
262 |
} |
263 |
menu.add(m); |
264 |
} |
265 |
|
266 |
if(count == 1 && CC.getMainFrame().getSelectedChannelsPane() == chnPane) { |
267 |
menu.removeAll(); |
268 |
|
269 |
for(int j = 0; j < chnPane.getChannelCount(); j++) { |
270 |
SamplerChannelModel chn = chnPane.getChannel(j).getModel(); |
271 |
menu.add(new JMenuItem(factory.createLoadInstrumentAction(chn, false))); |
272 |
} |
273 |
} |
274 |
} |
275 |
} |