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