1 |
/* |
2 |
* JSampler - a java front-end for LinuxSampler |
3 |
* |
4 |
* Copyright (C) 2005-2006 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.Point; |
30 |
|
31 |
import java.awt.event.ActionEvent; |
32 |
import java.awt.event.ActionListener; |
33 |
import java.awt.event.MouseAdapter; |
34 |
import java.awt.event.MouseEvent; |
35 |
|
36 |
import java.util.logging.Level; |
37 |
|
38 |
import javax.swing.Box; |
39 |
import javax.swing.BoxLayout; |
40 |
import javax.swing.JCheckBoxMenuItem; |
41 |
import javax.swing.JMenu; |
42 |
import javax.swing.JMenuItem; |
43 |
import javax.swing.JPanel; |
44 |
import javax.swing.JPopupMenu; |
45 |
import javax.swing.JToggleButton; |
46 |
import javax.swing.UIManager; |
47 |
import javax.swing.plaf.synth.SynthLookAndFeel; |
48 |
|
49 |
import net.sf.juife.TitleBar; |
50 |
|
51 |
import org.jsampler.CC; |
52 |
import org.jsampler.HF; |
53 |
|
54 |
import org.jsampler.view.JSChannel; |
55 |
import org.jsampler.view.JSChannelsPane; |
56 |
import org.jsampler.view.JSMainFrame; |
57 |
|
58 |
import static org.jsampler.view.fantasia.FantasiaI18n.i18n; |
59 |
|
60 |
|
61 |
/** |
62 |
* |
63 |
* @author Grigor Iliev |
64 |
*/ |
65 |
public class MainFrame extends JSMainFrame { |
66 |
private final static int TITLE_BAR_WIDTH = 420; |
67 |
private final static int TITLE_BAR_HEIGHT = 29; |
68 |
|
69 |
private final ChannelsPane channelsPane = new ChannelsPane(""); |
70 |
|
71 |
/** Creates a new instance of <code>MainFrame</code> */ |
72 |
public |
73 |
MainFrame() { |
74 |
try { |
75 |
SynthLookAndFeel synth = new SynthLookAndFeel(); |
76 |
synth.load(MainFrame.class.getResourceAsStream("gui.xml"), MainFrame.class); |
77 |
UIManager.setLookAndFeel(synth); |
78 |
} catch(Exception e) { |
79 |
CC.getLogger().log(Level.INFO, HF.getErrorMessage(e), e); |
80 |
} |
81 |
|
82 |
setTitle(i18n.getLabel("MainFrame.title")); |
83 |
addChannelsPane(channelsPane); |
84 |
add(channelsPane); |
85 |
setUndecorated(true); |
86 |
|
87 |
JToggleButton btn = new PixmapToggleButton(Res.iconPowerOff, Res.iconPowerOn) { |
88 |
public boolean |
89 |
contains(int x, int y) { |
90 |
return (x - 11)*(x - 11) + (y - 11)*(y - 11) < 71; |
91 |
} |
92 |
}; |
93 |
|
94 |
btn.setSelected(true); |
95 |
btn.setAlignmentX(JPanel.LEFT_ALIGNMENT); |
96 |
btn.addActionListener(new ActionListener() { |
97 |
public void |
98 |
actionPerformed(ActionEvent e) { onWindowClose(); } |
99 |
}); |
100 |
|
101 |
|
102 |
FantasiaTitleBar tb = new FantasiaTitleBar(); |
103 |
tb.setName("FantasiaTitleBar"); |
104 |
tb.setLayout(new BoxLayout(tb, BoxLayout.X_AXIS)); |
105 |
tb.setOpaque(true); |
106 |
tb.add(Box.createRigidArea(new Dimension(4, 0))); |
107 |
tb.add(btn); |
108 |
tb.add(Box.createRigidArea(new Dimension(3, 0))); |
109 |
|
110 |
|
111 |
|
112 |
tb.add(createVSeparator()); |
113 |
|
114 |
tb.add(Box.createRigidArea(new Dimension(275, 0))); |
115 |
|
116 |
tb.add(createVSeparator()); |
117 |
|
118 |
tb.add(Box.createRigidArea(new Dimension(29, 0))); |
119 |
|
120 |
tb.add(createVSeparator()); |
121 |
|
122 |
tb.add(Box.createRigidArea(new Dimension(40, 0))); |
123 |
|
124 |
tb.add(createVSeparator()); |
125 |
|
126 |
tb.setPreferredSize(new Dimension(TITLE_BAR_WIDTH, TITLE_BAR_HEIGHT)); |
127 |
tb.setMinimumSize(tb.getPreferredSize()); |
128 |
tb.setMaximumSize(tb.getPreferredSize()); |
129 |
add(tb, BorderLayout.SOUTH); |
130 |
|
131 |
getContentPane().setBackground(new java.awt.Color(0x818181)); |
132 |
getRootPane().setOpaque(false); |
133 |
getLayeredPane().setOpaque(false); |
134 |
//getContentPane().setVisible(false); |
135 |
|
136 |
setAlwaysOnTop(FantasiaPrefs.isAlwaysOnTop()); |
137 |
pack(); |
138 |
|
139 |
String s = FantasiaPrefs.getWindowLocation(); |
140 |
|
141 |
try { |
142 |
if(s == null) { |
143 |
setDefaultLocation(); |
144 |
} else { |
145 |
int i = s.indexOf(','); |
146 |
int x = Integer.parseInt(s.substring(0, i)); |
147 |
|
148 |
s = s.substring(i + 1); |
149 |
int y = Integer.parseInt(s); |
150 |
|
151 |
setLocation(x, y); |
152 |
} |
153 |
} catch(Exception x) { |
154 |
String msg = "Parsing of window size and location string failed"; |
155 |
CC.getLogger().log(Level.INFO, msg, x); |
156 |
setDefaultLocation(); |
157 |
} |
158 |
} |
159 |
|
160 |
private void |
161 |
setDefaultLocation() { |
162 |
Dimension d = java.awt.Toolkit.getDefaultToolkit().getScreenSize(); |
163 |
setLocation((d.width - TITLE_BAR_WIDTH) / 2, (d.height - TITLE_BAR_HEIGHT) / 2); |
164 |
} |
165 |
|
166 |
|
167 |
/** Invoked when this window is about to close. */ |
168 |
protected void |
169 |
onWindowClose() { |
170 |
FantasiaPrefs.setAlwaysOnTop(isAlwaysOnTop()); |
171 |
|
172 |
java.awt.Point p = getLocation(); |
173 |
Dimension d = getSize(); |
174 |
StringBuffer sb = new StringBuffer(); |
175 |
sb.append(p.x).append(',').append(p.y + getSize().height - TITLE_BAR_HEIGHT); |
176 |
FantasiaPrefs.setWindowLocation(sb.toString()); |
177 |
|
178 |
super.onWindowClose(); |
179 |
} |
180 |
|
181 |
private JPanel |
182 |
createVSeparator() { |
183 |
JPanel p = new JPanel(); |
184 |
p.setName("VSeparator"); |
185 |
p.setOpaque(false); |
186 |
p.setPreferredSize(new Dimension(2, 29)); |
187 |
p.setMinimumSize(p.getPreferredSize()); |
188 |
p.setMaximumSize(p.getPreferredSize()); |
189 |
return p; |
190 |
} |
191 |
|
192 |
/** |
193 |
* This method does nothing, because <b>Fantasia</b> has exactly |
194 |
* one pane containing sampler channels, which can not be changed. |
195 |
*/ |
196 |
public void |
197 |
insertChannelsPane(JSChannelsPane pane, int idx) { |
198 |
getChannelsPaneList().removeAllElements(); |
199 |
addChannelsPane(pane); |
200 |
} |
201 |
|
202 |
/** |
203 |
* This method always returns the <code>JSChannelsPane</code> at index 0, |
204 |
* because the <b>Fantasia</b> view has exactly one pane containing sampler channels. |
205 |
* @return The <code>JSChannelsPane</code> at index 0. |
206 |
*/ |
207 |
public JSChannelsPane |
208 |
getSelectedChannelsPane() { return getChannelsPane(0); } |
209 |
|
210 |
/** |
211 |
* This method does nothing because the <b>Fantasia</b> view has |
212 |
* exactly one pane containing sampler channels which is always shown. |
213 |
*/ |
214 |
public void |
215 |
setSelectedChannelsPane(JSChannelsPane pane) { } |
216 |
|
217 |
public static void |
218 |
repack(JSMainFrame frame) { |
219 |
int y = frame.getLocation().y; |
220 |
int height = frame.getSize().height; |
221 |
y += (height - frame.getPreferredSize().height); |
222 |
|
223 |
if((height - frame.getPreferredSize().height) > 0) { |
224 |
frame.pack(); |
225 |
frame.setLocation(frame.getLocation().x, y); |
226 |
} else { |
227 |
frame.setLocation(frame.getLocation().x, y); |
228 |
frame.pack(); |
229 |
} |
230 |
} |
231 |
|
232 |
public void |
233 |
installJSamplerHome() { } |
234 |
|
235 |
public void |
236 |
showDetailedErrorMessage(Frame owner, String err, String details) { |
237 |
// TODO: |
238 |
} |
239 |
|
240 |
public void |
241 |
showDetailedErrorMessage(Dialog owner, String err, String details) { |
242 |
// TODO: |
243 |
} |
244 |
} |
245 |
|
246 |
class FantasiaTitleBar extends TitleBar { |
247 |
FantasiaTitleBar() { this.addMouseListener(new ContextMenu()); } |
248 |
|
249 |
class ContextMenu extends MouseAdapter { |
250 |
private final JPopupMenu cmenu = new JPopupMenu(); |
251 |
|
252 |
ContextMenu() { |
253 |
JMenuItem mi; |
254 |
|
255 |
final JCheckBoxMenuItem cmi = new JCheckBoxMenuItem ( |
256 |
i18n.getMenuLabel("FantasiaTitleBar.AlwaysOnTop") |
257 |
); |
258 |
cmi.setIcon(null); |
259 |
cmi.setSelected(FantasiaPrefs.isAlwaysOnTop()); |
260 |
|
261 |
cmenu.add(cmi); |
262 |
|
263 |
cmi.addActionListener(new ActionListener() { |
264 |
public void |
265 |
actionPerformed(ActionEvent e) { |
266 |
CC.getMainFrame().setAlwaysOnTop(cmi.isSelected()); |
267 |
} |
268 |
}); |
269 |
|
270 |
/*mi = new JMenuItem(A4n.moveChannelsUp); |
271 |
mi.setIcon(null); |
272 |
cmenu.add(mi); |
273 |
|
274 |
cmenu.addSeparator(); |
275 |
|
276 |
mi = new JMenuItem(A4n.removeChannels); |
277 |
mi.setIcon(null); |
278 |
cmenu.add(mi);*/ |
279 |
} |
280 |
|
281 |
public void |
282 |
mousePressed(MouseEvent e) { |
283 |
if(e.isPopupTrigger()) show(e); |
284 |
} |
285 |
|
286 |
public void |
287 |
mouseReleased(MouseEvent e) { |
288 |
if(e.isPopupTrigger()) show(e); |
289 |
} |
290 |
|
291 |
void |
292 |
show(MouseEvent e) { |
293 |
cmenu.show(e.getComponent(), e.getX(), e.getY()); |
294 |
} |
295 |
} |
296 |
} |