1 |
/* |
2 |
* JSampler - a java front-end for LinuxSampler |
3 |
* |
4 |
* Copyright (C) 2005-2007 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.fantasia; |
24 |
|
25 |
import java.awt.BorderLayout; |
26 |
import java.awt.Dialog; |
27 |
import java.awt.Dimension; |
28 |
import java.awt.Frame; |
29 |
import java.awt.Graphics; |
30 |
import java.awt.GridBagConstraints; |
31 |
import java.awt.GridBagLayout; |
32 |
import java.awt.Insets; |
33 |
import java.awt.Point; |
34 |
|
35 |
import java.awt.event.ActionEvent; |
36 |
import java.awt.event.ActionListener; |
37 |
import java.awt.event.KeyEvent; |
38 |
import java.awt.event.MouseAdapter; |
39 |
import java.awt.event.MouseEvent; |
40 |
|
41 |
import java.io.BufferedReader; |
42 |
import java.io.File; |
43 |
import java.io.FileNotFoundException; |
44 |
import java.io.FileReader; |
45 |
|
46 |
import java.util.Vector; |
47 |
import java.util.logging.Level; |
48 |
|
49 |
import javax.swing.BorderFactory; |
50 |
import javax.swing.Box; |
51 |
import javax.swing.BoxLayout; |
52 |
import javax.swing.JCheckBoxMenuItem; |
53 |
import javax.swing.JDialog; |
54 |
import javax.swing.JFileChooser; |
55 |
import javax.swing.JFrame; |
56 |
import javax.swing.JMenu; |
57 |
import javax.swing.JMenuBar; |
58 |
import javax.swing.JMenuItem; |
59 |
import javax.swing.JPanel; |
60 |
import javax.swing.JPopupMenu; |
61 |
import javax.swing.JScrollPane; |
62 |
import javax.swing.JSplitPane; |
63 |
import javax.swing.JToggleButton; |
64 |
import javax.swing.KeyStroke; |
65 |
import javax.swing.SwingUtilities; |
66 |
import javax.swing.UIManager; |
67 |
|
68 |
import net.sf.juife.TitleBar; |
69 |
|
70 |
import org.jsampler.CC; |
71 |
import org.jsampler.HF; |
72 |
import org.jsampler.LSConsoleModel; |
73 |
|
74 |
import org.jsampler.view.JSChannel; |
75 |
import org.jsampler.view.JSChannelsPane; |
76 |
import org.jsampler.view.JSMainFrame; |
77 |
import org.jsampler.view.LscpFileFilter; |
78 |
|
79 |
import org.jsampler.view.std.JSamplerHomeChooser; |
80 |
|
81 |
import static org.jsampler.view.fantasia.A4n.a4n; |
82 |
import static org.jsampler.view.fantasia.FantasiaI18n.i18n; |
83 |
import static org.jsampler.view.fantasia.FantasiaPrefs.preferences; |
84 |
import static org.jsampler.view.std.StdPrefs.*; |
85 |
|
86 |
|
87 |
/** |
88 |
* |
89 |
* @author Grigor Iliev |
90 |
*/ |
91 |
public class MainFrame extends JSMainFrame { |
92 |
private final FantasiaMenuBar menuBar = new FantasiaMenuBar(); |
93 |
private final StandardBar standardBar = new StandardBar(); |
94 |
private final MainPane mainPane = new MainPane(); |
95 |
private final DevicesPane devicesPane = new DevicesPane(); |
96 |
|
97 |
private final JMenu recentScriptsMenu = |
98 |
new JMenu(i18n.getMenuLabel("actions.recentScripts")); |
99 |
|
100 |
private final JSplitPane hSplitPane; |
101 |
|
102 |
private final SidePane sidePane = new SidePane(); |
103 |
|
104 |
private final LSConsoleFrame lsConsoleFrame = new LSConsoleFrame(); |
105 |
private final Vector<String> recentScripts = new Vector<String>(); |
106 |
|
107 |
|
108 |
/** Creates a new instance of <code>MainFrame</code> */ |
109 |
public |
110 |
MainFrame() { |
111 |
setTitle(i18n.getLabel("MainFrame.title")); |
112 |
|
113 |
if(Res.iconAppIcon != null) setIconImage(Res.iconAppIcon.getImage()); |
114 |
|
115 |
getContentPane().add(standardBar, BorderLayout.NORTH); |
116 |
|
117 |
addMenu(); |
118 |
|
119 |
addChannelsPane(mainPane.getChannelsPane()); |
120 |
|
121 |
JScrollPane sp = new JScrollPane(createRightPane()); |
122 |
sp.setBorder(BorderFactory.createEmptyBorder()); |
123 |
|
124 |
hSplitPane = new JSplitPane ( |
125 |
JSplitPane.HORIZONTAL_SPLIT, |
126 |
true, // continuousLayout |
127 |
sidePane, sp |
128 |
); |
129 |
|
130 |
getContentPane().add(hSplitPane); |
131 |
|
132 |
int i = preferences().getIntProperty("MainFrame.hSplitDividerLocation", 220); |
133 |
hSplitPane.setDividerLocation(i); |
134 |
|
135 |
setSavedSize(); |
136 |
} |
137 |
|
138 |
private JPanel |
139 |
createRightPane() { |
140 |
JPanel p = new JPanel(); |
141 |
GridBagLayout gridbag = new GridBagLayout(); |
142 |
GridBagConstraints c = new GridBagConstraints(); |
143 |
|
144 |
p.setLayout(gridbag); |
145 |
|
146 |
c.fill = GridBagConstraints.BOTH; |
147 |
|
148 |
c.gridx = 1; |
149 |
c.gridy = 0; |
150 |
c.weightx = 1.0; |
151 |
c.weighty = 1.0; |
152 |
c.insets = new Insets(0, 3, 3, 0); |
153 |
gridbag.setConstraints(devicesPane, c); |
154 |
p.add(devicesPane); |
155 |
|
156 |
c.gridx = 0; |
157 |
c.gridy = 0; |
158 |
c.weightx = 0.0; |
159 |
c.weighty = 1.0; |
160 |
c.insets = new Insets(0, 3, 3, 3); |
161 |
c.fill = GridBagConstraints.VERTICAL; |
162 |
gridbag.setConstraints(mainPane, c); |
163 |
p.add(mainPane); |
164 |
|
165 |
return p; |
166 |
} |
167 |
|
168 |
private void |
169 |
setSavedSize() { |
170 |
String s = preferences().getStringProperty("MainFrame.sizeAndLocation"); |
171 |
if(s == null) { |
172 |
setDefaultSizeAndLocation(); |
173 |
return; |
174 |
} |
175 |
pack(); |
176 |
try { |
177 |
int i = s.indexOf(','); |
178 |
int x = Integer.parseInt(s.substring(0, i)); |
179 |
|
180 |
s = s.substring(i + 1); |
181 |
i = s.indexOf(','); |
182 |
int y = Integer.parseInt(s.substring(0, i)); |
183 |
|
184 |
s = s.substring(i + 1); |
185 |
i = s.indexOf(','); |
186 |
int width = Integer.parseInt(s.substring(0, i)); |
187 |
|
188 |
s = s.substring(i + 1); |
189 |
int height = Integer.parseInt(s); |
190 |
|
191 |
setBounds(x, y, width, height); |
192 |
} catch(Exception x) { |
193 |
String msg = "Parsing of window size and location string failed"; |
194 |
CC.getLogger().log(Level.INFO, msg, x); |
195 |
setDefaultSizeAndLocation(); |
196 |
} |
197 |
|
198 |
if(preferences().getBoolProperty("MainFrame.windowMaximized")) { |
199 |
setExtendedState(getExtendedState() | MAXIMIZED_BOTH); |
200 |
} |
201 |
} |
202 |
|
203 |
private void |
204 |
setDefaultSizeAndLocation() { |
205 |
setPreferredSize(new Dimension(900, 600)); |
206 |
pack(); |
207 |
setLocationRelativeTo(null); |
208 |
} |
209 |
|
210 |
|
211 |
/** Invoked when this window is about to close. */ |
212 |
protected void |
213 |
onWindowClose() { |
214 |
sidePane.savePreferences(); |
215 |
|
216 |
int i = hSplitPane.getDividerLocation(); |
217 |
preferences().setIntProperty("MainFrame.hSplitDividerLocation", i); |
218 |
|
219 |
preferences().setBoolProperty ( |
220 |
"MainFrame.windowMaximized", |
221 |
(getExtendedState() & MAXIMIZED_BOTH) == MAXIMIZED_BOTH |
222 |
); |
223 |
|
224 |
if(preferences().getBoolProperty("MainFrame.windowMaximized")) { |
225 |
super.onWindowClose(); |
226 |
return; |
227 |
} |
228 |
|
229 |
java.awt.Point p = getLocation(); |
230 |
Dimension d = getSize(); |
231 |
StringBuffer sb = new StringBuffer(); |
232 |
sb.append(p.x).append(',').append(p.y).append(','); |
233 |
sb.append(d.width).append(',').append(d.height); |
234 |
preferences().setStringProperty("MainFrame.sizeAndLocation", sb.toString()); |
235 |
|
236 |
String[] list = recentScripts.toArray(new String[recentScripts.size()]); |
237 |
preferences().setStringListProperty(RECENT_LSCP_SCRIPTS, list); |
238 |
|
239 |
if(preferences().getBoolProperty(SAVE_LS_CONSOLE_HISTORY)) { |
240 |
if(lsConsoleFrame != null) getLSConsolePane().saveConsoleHistory(); |
241 |
} |
242 |
|
243 |
super.onWindowClose(); |
244 |
} |
245 |
|
246 |
private void |
247 |
addMenu() { |
248 |
JMenu m; |
249 |
JMenuItem mi; |
250 |
|
251 |
setJMenuBar(menuBar); |
252 |
|
253 |
// Actions |
254 |
m = new FantasiaMenu(i18n.getMenuLabel("actions")); |
255 |
|
256 |
mi = new JMenuItem(a4n.connect); |
257 |
mi.setIcon(null); |
258 |
//mi.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N, KeyEvent.CTRL_MASK)); |
259 |
m.add(mi); |
260 |
|
261 |
mi = new JMenuItem(a4n.samplerInfo); |
262 |
mi.setIcon(null); |
263 |
m.add(mi); |
264 |
|
265 |
m.addSeparator(); |
266 |
|
267 |
JMenu exportMenu = new JMenu(i18n.getMenuLabel("actions.export")); |
268 |
m.add(exportMenu); |
269 |
|
270 |
mi = new JMenuItem(a4n.exportSamplerConfig); |
271 |
mi.setIcon(null); |
272 |
mi.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S, KeyEvent.CTRL_MASK)); |
273 |
exportMenu.add(mi); |
274 |
|
275 |
mi = new JMenuItem(a4n.exportMidiInstrumentMaps); |
276 |
mi.setIcon(null); |
277 |
exportMenu.add(mi); |
278 |
|
279 |
m.addSeparator(); |
280 |
|
281 |
mi = new JMenuItem(a4n.loadScript); |
282 |
mi.setIcon(null); |
283 |
m.add(mi); |
284 |
|
285 |
String[] list = preferences().getStringListProperty(RECENT_LSCP_SCRIPTS); |
286 |
for(String s : list) recentScripts.add(s); |
287 |
|
288 |
updateRecentScriptsMenu(); |
289 |
|
290 |
m.add(recentScriptsMenu); |
291 |
|
292 |
m.addSeparator(); |
293 |
|
294 |
mi = new JMenuItem(i18n.getMenuLabel("actions.exit")); |
295 |
m.add(mi); |
296 |
mi.addActionListener(new ActionListener() { |
297 |
public void |
298 |
actionPerformed(ActionEvent e) { onWindowClose(); } |
299 |
}); |
300 |
|
301 |
menuBar.add(m); |
302 |
|
303 |
|
304 |
// Edit |
305 |
m = new FantasiaMenu(i18n.getMenuLabel("edit")); |
306 |
menuBar.add(m); |
307 |
|
308 |
mi = new JMenuItem(a4n.editPreferences); |
309 |
mi.setIcon(null); |
310 |
mi.setAccelerator(KeyStroke.getKeyStroke ( |
311 |
KeyEvent.VK_P, KeyEvent.CTRL_MASK | KeyEvent.SHIFT_MASK |
312 |
)); |
313 |
m.add(mi); |
314 |
|
315 |
|
316 |
// Window |
317 |
m = new FantasiaMenu(i18n.getMenuLabel("window")); |
318 |
menuBar.add(m); |
319 |
|
320 |
mi = new JMenuItem(a4n.windowLSConsole); |
321 |
mi.setIcon(null); |
322 |
m.add(mi); |
323 |
|
324 |
mi = new JMenuItem(a4n.windowInstrumentsDb); |
325 |
mi.setIcon(null); |
326 |
m.add(mi); |
327 |
|
328 |
|
329 |
// Help |
330 |
m = new FantasiaMenu(i18n.getMenuLabel("help")); |
331 |
|
332 |
mi = new JMenuItem(a4n.helpAbout); |
333 |
mi.setIcon(null); |
334 |
m.add(mi); |
335 |
|
336 |
menuBar.add(m); |
337 |
} |
338 |
|
339 |
/** |
340 |
* This method does nothing, because <b>Fantasia</b> has exactly |
341 |
* one pane containing sampler channels, which can not be changed. |
342 |
*/ |
343 |
public void |
344 |
insertChannelsPane(JSChannelsPane pane, int idx) { |
345 |
|
346 |
} |
347 |
|
348 |
/** |
349 |
* This method always returns the <code>JSChannelsPane</code> at index 0, |
350 |
* because the <b>Fantasia</b> view has exactly one pane containing sampler channels. |
351 |
* @return The <code>JSChannelsPane</code> at index 0. |
352 |
*/ |
353 |
public JSChannelsPane |
354 |
getSelectedChannelsPane() { return getChannelsPane(0); } |
355 |
|
356 |
/** |
357 |
* This method does nothing because the <b>Fantasia</b> view has |
358 |
* exactly one pane containing sampler channels which is always shown. |
359 |
*/ |
360 |
public void |
361 |
setSelectedChannelsPane(JSChannelsPane pane) { } |
362 |
|
363 |
public void |
364 |
installJSamplerHome() { |
365 |
JSamplerHomeChooser chooser = new JSamplerHomeChooser(this); |
366 |
chooser.setVisible(true); |
367 |
if(chooser.isCancelled()) return; |
368 |
|
369 |
CC.changeJSamplerHome(chooser.getJSamplerHome()); |
370 |
} |
371 |
|
372 |
public void |
373 |
showDetailedErrorMessage(Frame owner, String err, String details) { |
374 |
// TODO: |
375 |
} |
376 |
|
377 |
public void |
378 |
showDetailedErrorMessage(Dialog owner, String err, String details) { |
379 |
// TODO: |
380 |
} |
381 |
|
382 |
protected LSConsoleModel |
383 |
getLSConsoleModel() { return getLSConsolePane().getModel(); } |
384 |
|
385 |
protected LSConsolePane |
386 |
getLSConsolePane() { |
387 |
return getLSConsoleFrame().getLSConsolePane(); |
388 |
} |
389 |
|
390 |
protected LSConsoleFrame |
391 |
getLSConsoleFrame() { return lsConsoleFrame; } |
392 |
|
393 |
protected void |
394 |
runScript() { |
395 |
String s = preferences().getStringProperty("lastScriptLocation"); |
396 |
JFileChooser fc = new JFileChooser(s); |
397 |
fc.setFileFilter(new LscpFileFilter()); |
398 |
int result = fc.showOpenDialog(this); |
399 |
if(result != JFileChooser.APPROVE_OPTION) return; |
400 |
|
401 |
String path = fc.getCurrentDirectory().getAbsolutePath(); |
402 |
preferences().setStringProperty("lastScriptLocation", path); |
403 |
|
404 |
runScript(fc.getSelectedFile()); |
405 |
} |
406 |
|
407 |
private void |
408 |
runScript(String script) { runScript(new File(script)); } |
409 |
|
410 |
private void |
411 |
runScript(File script) { |
412 |
FileReader fr; |
413 |
try { fr = new FileReader(script); } |
414 |
catch(FileNotFoundException e) { |
415 |
HF.showErrorMessage(i18n.getMessage("FileNotFound!")); |
416 |
return; |
417 |
} |
418 |
|
419 |
BufferedReader br = new BufferedReader(fr); |
420 |
|
421 |
try { |
422 |
String s = br.readLine(); |
423 |
while(s != null) { |
424 |
getLSConsoleModel().setCommandLineText(s); |
425 |
getLSConsoleModel().execCommand(); |
426 |
s = br.readLine(); |
427 |
} |
428 |
} catch(Exception e) { |
429 |
HF.showErrorMessage(e); |
430 |
return; |
431 |
} |
432 |
|
433 |
String s = script.getAbsolutePath(); |
434 |
recentScripts.remove(s); |
435 |
recentScripts.insertElementAt(s, 0); |
436 |
|
437 |
updateRecentScriptsMenu(); |
438 |
} |
439 |
|
440 |
protected void |
441 |
clearRecentScripts() { |
442 |
recentScripts.removeAllElements(); |
443 |
updateRecentScriptsMenu(); |
444 |
} |
445 |
|
446 |
protected void |
447 |
updateRecentScriptsMenu() { |
448 |
int size = preferences().getIntProperty(RECENT_LSCP_SCRIPTS_SIZE); |
449 |
while(recentScripts.size() > size) { |
450 |
recentScripts.removeElementAt(recentScripts.size() - 1); |
451 |
} |
452 |
|
453 |
recentScriptsMenu.removeAll(); |
454 |
|
455 |
for(String script : recentScripts) { |
456 |
JMenuItem mi = new JMenuItem(script); |
457 |
recentScriptsMenu.add(mi); |
458 |
mi.addActionListener(new RecentScriptHandler(script)); |
459 |
} |
460 |
|
461 |
recentScriptsMenu.setEnabled(recentScripts.size() != 0); |
462 |
} |
463 |
|
464 |
private class RecentScriptHandler implements ActionListener { |
465 |
private String script; |
466 |
|
467 |
RecentScriptHandler(String script) { this.script = script; } |
468 |
|
469 |
public void |
470 |
actionPerformed(ActionEvent e) { |
471 |
runScript(script); |
472 |
if(preferences().getBoolProperty(SHOW_LS_CONSOLE_WHEN_RUN_SCRIPT)) { |
473 |
a4n.windowLSConsole.actionPerformed(null); |
474 |
} |
475 |
} |
476 |
} |
477 |
|
478 |
private static class FantasiaMenu extends JMenu { |
479 |
FantasiaMenu(String s) { |
480 |
super(s); |
481 |
setFont(getFont().deriveFont(java.awt.Font.BOLD)); |
482 |
setOpaque(false); |
483 |
} |
484 |
} |
485 |
|
486 |
private static class FantasiaMenuBar extends JMenuBar { |
487 |
private static Insets pixmapInsets = new Insets(6, 6, 0, 6); |
488 |
|
489 |
FantasiaMenuBar() { |
490 |
setOpaque(false); |
491 |
} |
492 |
|
493 |
protected void |
494 |
paintComponent(Graphics g) { |
495 |
super.paintComponent(g); |
496 |
|
497 |
PixmapPane.paintComponent(this, g, Res.gfxMenuBarBg, pixmapInsets); |
498 |
} |
499 |
} |
500 |
} |