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.fantasia; |
24 |
|
25 |
import java.awt.Cursor; |
26 |
import java.awt.Dimension; |
27 |
import java.awt.Graphics; |
28 |
import java.awt.GridBagConstraints; |
29 |
import java.awt.GridBagLayout; |
30 |
import java.awt.Insets; |
31 |
import java.awt.Rectangle; |
32 |
|
33 |
import java.awt.event.ActionEvent; |
34 |
import java.awt.event.ActionListener; |
35 |
import java.awt.event.HierarchyEvent; |
36 |
import java.awt.event.HierarchyListener; |
37 |
import java.awt.event.MouseAdapter; |
38 |
import java.awt.event.MouseEvent; |
39 |
|
40 |
import java.util.Vector; |
41 |
|
42 |
import javax.swing.AbstractButton; |
43 |
import javax.swing.BorderFactory; |
44 |
import javax.swing.Box; |
45 |
import javax.swing.BoxLayout; |
46 |
import javax.swing.JPanel; |
47 |
import javax.swing.JScrollPane; |
48 |
import javax.swing.JToggleButton; |
49 |
|
50 |
import org.jsampler.CC; |
51 |
import org.jsampler.view.JSChannelsPane; |
52 |
import org.jsampler.view.fantasia.basic.*; |
53 |
|
54 |
import static org.jsampler.view.fantasia.FantasiaI18n.i18n; |
55 |
|
56 |
|
57 |
/** |
58 |
* |
59 |
* @author Grigor Iliev |
60 |
*/ |
61 |
public class MainPane extends FantasiaPanel { |
62 |
private final int CHANNELS_PANEL_NUMBER = 4; |
63 |
private final ChannelsBar channelsBar; |
64 |
private final ButtonsPanel buttonsPanel; |
65 |
private final Vector<ChannelsPanel> channelsPanes = new Vector<ChannelsPanel>(); |
66 |
|
67 |
final JScrollPane scrollPane = new JScrollPane(); |
68 |
|
69 |
/** Creates a new instance of <code>MainPane</code> */ |
70 |
public MainPane() { |
71 |
setOpaque(false); |
72 |
|
73 |
for(int i = 0; i < CHANNELS_PANEL_NUMBER; i++) { |
74 |
channelsPanes.add(new ChannelsPanel()); |
75 |
} |
76 |
|
77 |
buttonsPanel = new ButtonsPanel(); |
78 |
channelsBar = new ChannelsBar(buttonsPanel); |
79 |
GridBagLayout gridbag = new GridBagLayout(); |
80 |
GridBagConstraints c = new GridBagConstraints(); |
81 |
|
82 |
setLayout(gridbag); |
83 |
|
84 |
JPanel p = new FantasiaPanel(); |
85 |
p.setOpaque(false); |
86 |
p.setLayout(new BoxLayout(p, BoxLayout.X_AXIS)); |
87 |
p.add(channelsBar); |
88 |
p.add(Box.createGlue()); |
89 |
|
90 |
c.gridx = 0; |
91 |
c.gridy = 0; |
92 |
c.weightx = 1.0; |
93 |
c.insets = new Insets(0, 0, 0, 0); |
94 |
c.fill = GridBagConstraints.HORIZONTAL; |
95 |
gridbag.setConstraints(p, c); |
96 |
add(p); |
97 |
|
98 |
JScrollPane sp = scrollPane; |
99 |
sp.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER); |
100 |
//sp.putClientProperty(SCROLL_PANE_BUTTONS_POLICY, ScrollPaneButtonPolicyKind.NONE); |
101 |
sp.setBorder(BorderFactory.createEmptyBorder()); |
102 |
sp.setOpaque(false); |
103 |
javax.swing.JViewport wp = sp.getViewport(); |
104 |
wp.setMinimumSize(new Dimension(400, wp.getMinimumSize().height)); |
105 |
wp.setOpaque(false); |
106 |
sp.setMaximumSize(new Dimension(sp.getMaximumSize().width, Short.MAX_VALUE)); |
107 |
sp.getVerticalScrollBar().setBorder(BorderFactory.createEmptyBorder(7, 4, 0, 1)); |
108 |
//sp.getVerticalScrollBar().setUnitIncrement(12); |
109 |
sp.setPreferredSize(new Dimension(420, sp.getPreferredSize().height)); |
110 |
|
111 |
sp.getVerticalScrollBar().addHierarchyListener(new HierarchyListener() { |
112 |
public void |
113 |
hierarchyChanged(HierarchyEvent e) { |
114 |
if((e.getChangeFlags() & e.SHOWING_CHANGED) != e.SHOWING_CHANGED) { |
115 |
return; |
116 |
} |
117 |
|
118 |
onScrollBarVisibilityChanged(); |
119 |
} |
120 |
}); |
121 |
|
122 |
c.gridx = 0; |
123 |
c.gridy = 2; |
124 |
c.weightx = 1.0; |
125 |
c.weighty = 1.0; |
126 |
c.insets = new Insets(0, 0, 0, 0); |
127 |
c.fill = GridBagConstraints.BOTH; |
128 |
gridbag.setConstraints(sp, c); |
129 |
add(sp); |
130 |
|
131 |
setMaximumSize(new Dimension(420, Short.MAX_VALUE)); |
132 |
} |
133 |
|
134 |
private void |
135 |
onScrollBarVisibilityChanged() { |
136 |
int w = 420; |
137 |
int h = scrollPane.getPreferredSize().height; |
138 |
int scrollbarWidth = scrollPane.getVerticalScrollBar().getPreferredSize().width; |
139 |
|
140 |
if(scrollPane.getVerticalScrollBar().isVisible()) w += scrollbarWidth; |
141 |
|
142 |
scrollPane.setMinimumSize(new Dimension(w, scrollPane.getPreferredSize().height)); |
143 |
scrollPane.setPreferredSize(new Dimension(w, h)); |
144 |
scrollPane.setMaximumSize(new Dimension(w, Short.MAX_VALUE)); |
145 |
setMaximumSize(new Dimension(w, Short.MAX_VALUE)); |
146 |
|
147 |
if(CC.getMainFrame() != null && !CC.getMainFrame().isResizable()) { |
148 |
// this means that there are no visible side panes |
149 |
|
150 |
w = CC.getMainFrame().getPreferredSize().width; |
151 |
CC.getMainFrame().setSize(w, CC.getMainFrame().getHeight()); |
152 |
} |
153 |
|
154 |
revalidate(); |
155 |
} |
156 |
|
157 |
public void |
158 |
scrollToBottom() { |
159 |
int h = scrollPane.getViewport().getView().getHeight(); |
160 |
scrollPane.getViewport().scrollRectToVisible(new Rectangle(0, h - 2, 1, 1)); |
161 |
} |
162 |
|
163 |
public JSChannelsPane |
164 |
getChannelsPane(int index) { return channelsPanes.get(index).getChannelsPane(); } |
165 |
|
166 |
public int |
167 |
getChannelsPaneCount() { return channelsPanes.size(); } |
168 |
|
169 |
public void |
170 |
setSelectedChannelsPane(JSChannelsPane pane) { |
171 |
ChannelsPanel chnPanel = null; |
172 |
|
173 |
for(int i = 0; i < getChannelsPaneCount(); i++) { |
174 |
if(channelsPanes.get(i).getChannelsPane() == pane) { |
175 |
chnPanel = channelsPanes.get(i); |
176 |
|
177 |
if(!buttonsPanel.buttons.get(i).isSelected()) { |
178 |
buttonsPanel.buttons.get(i).setSelected(true); |
179 |
} |
180 |
|
181 |
break; |
182 |
} |
183 |
} |
184 |
|
185 |
if(chnPanel == null) { |
186 |
CC.getLogger().warning("Unknown channels pane"); |
187 |
return; |
188 |
} |
189 |
|
190 |
scrollPane.getViewport().setView(chnPanel); |
191 |
} |
192 |
|
193 |
public JSChannelsPane |
194 |
getSelectedChannelsPane() { |
195 |
for(int i = 0; i < getChannelsPaneCount(); i++) { |
196 |
if(buttonsPanel.buttons.get(i).isSelected()) { |
197 |
return channelsPanes.get(i).getChannelsPane(); |
198 |
} |
199 |
} |
200 |
|
201 |
return null; |
202 |
} |
203 |
|
204 |
@Override |
205 |
protected void |
206 |
paintComponent(Graphics g) { |
207 |
super.paintComponent(g); |
208 |
int h = Res.gfxChannelsBg.getIconHeight(); |
209 |
if(h < 1) return; |
210 |
int i = 0; |
211 |
while(i < getHeight()) { |
212 |
Res.gfxChannelsBg.paintIcon(this, g, 0, i); |
213 |
i += h; |
214 |
} |
215 |
} |
216 |
|
217 |
private final EventHandler eventHandler = new EventHandler(); |
218 |
|
219 |
private EventHandler |
220 |
getHandler() { return eventHandler; } |
221 |
|
222 |
private class EventHandler extends MouseAdapter { |
223 |
@Override |
224 |
public void |
225 |
mouseClicked(MouseEvent e) { |
226 |
if(e.getButton() != MouseEvent.BUTTON1) return; |
227 |
// TAG: channel selection system |
228 |
CC.getMainFrame().getSelectedChannelsPane().setSelectedChannel(null); |
229 |
/////// |
230 |
} |
231 |
} |
232 |
|
233 |
private class ChannelsPanel extends FantasiaPanel { |
234 |
private final JSChannelsPane channelsPane; |
235 |
ChannelsPanel() { |
236 |
ActionListener l = new ActionListener() { |
237 |
public void |
238 |
actionPerformed(ActionEvent e) { scrollToBottom(); } |
239 |
}; |
240 |
|
241 |
channelsPane = new ChannelsPane("", l); |
242 |
|
243 |
setLayout(new BoxLayout(this, BoxLayout.Y_AXIS)); |
244 |
channelsPane.setAlignmentX(LEFT_ALIGNMENT); |
245 |
add(channelsPane); |
246 |
JPanel p2 = new NewChannelPane(); |
247 |
p2.setAlignmentX(LEFT_ALIGNMENT); |
248 |
add(p2); |
249 |
add(Box.createGlue()); |
250 |
setOpaque(false); |
251 |
setBorder(BorderFactory.createEmptyBorder(7, 0, 0, 0)); |
252 |
setMinimumSize(new Dimension(420, getMinimumSize().height)); |
253 |
setAlignmentX(LEFT_ALIGNMENT); |
254 |
|
255 |
addMouseListener(getHandler()); |
256 |
} |
257 |
|
258 |
public JSChannelsPane |
259 |
getChannelsPane() { return channelsPane; } |
260 |
} |
261 |
|
262 |
private class ButtonsPanel extends FantasiaToggleButtonsPanel implements ActionListener { |
263 |
ButtonsPanel() { |
264 |
super(CHANNELS_PANEL_NUMBER, true); |
265 |
for(int i = 0; i < CHANNELS_PANEL_NUMBER; i++) { |
266 |
JToggleButton btn = buttons.get(i); |
267 |
btn.setText(String.valueOf(i + 1)); |
268 |
btn.setBorder(BorderFactory.createEmptyBorder(0, 8, 0, 8)); |
269 |
btn.addActionListener(this); |
270 |
|
271 |
String s = "MainPane.ButtonsPanel.tt"; |
272 |
btn.setToolTipText(i18n.getButtonLabel(s, i + 1)); |
273 |
} |
274 |
|
275 |
setMaximumSize(getPreferredSize()); |
276 |
} |
277 |
|
278 |
@Override |
279 |
public void |
280 |
actionPerformed(ActionEvent e) { |
281 |
AbstractButton btn = (AbstractButton)e.getSource(); |
282 |
if(!btn.isSelected()) return; |
283 |
int idx = buttons.indexOf(btn); |
284 |
if(idx == -1) return; |
285 |
|
286 |
ChannelsPanel chnPanel = channelsPanes.get(idx); |
287 |
CC.getMainFrame().setSelectedChannelsPane(chnPanel.getChannelsPane()); |
288 |
} |
289 |
} |
290 |
|
291 |
class NewChannelPane extends PixmapPane implements ActionListener { |
292 |
private final PixmapButton btnNew = new PixmapButton(Res.gfxPowerOff); |
293 |
|
294 |
NewChannelPane() { |
295 |
super(Res.gfxCreateChannel); |
296 |
setPixmapInsets(new Insets(3, 3, 3, 3)); |
297 |
|
298 |
setLayout(new BoxLayout(this, BoxLayout.X_AXIS)); |
299 |
add(Box.createRigidArea(new Dimension(3, 0))); |
300 |
add(btnNew); |
301 |
add(Box.createRigidArea(new Dimension(4, 0))); |
302 |
|
303 |
add(createVSeparator()); |
304 |
|
305 |
add(Box.createRigidArea(new Dimension(275, 0))); |
306 |
|
307 |
add(createVSeparator()); |
308 |
|
309 |
add(Box.createRigidArea(new Dimension(29, 0))); |
310 |
|
311 |
add(createVSeparator()); |
312 |
|
313 |
add(Box.createRigidArea(new Dimension(40, 0))); |
314 |
|
315 |
add(createVSeparator()); |
316 |
|
317 |
setPreferredSize(new Dimension(420, 29)); |
318 |
setMinimumSize(getPreferredSize()); |
319 |
setMaximumSize(getPreferredSize()); |
320 |
|
321 |
btnNew.setPressedIcon(Res.gfxPowerOn); |
322 |
btnNew.addActionListener(this); |
323 |
|
324 |
addMouseListener(new MouseAdapter() { |
325 |
public void |
326 |
mouseClicked(MouseEvent e) { |
327 |
if(e.getButton() != e.BUTTON1) { |
328 |
return; |
329 |
} |
330 |
|
331 |
CC.getSamplerModel().addBackendChannel(); |
332 |
} |
333 |
}); |
334 |
|
335 |
setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR)); |
336 |
|
337 |
String s = i18n.getLabel("MainPane.NewChannelPane.newChannel"); |
338 |
btnNew.setToolTipText(s); |
339 |
setToolTipText(s); |
340 |
} |
341 |
|
342 |
@Override |
343 |
public void |
344 |
actionPerformed(ActionEvent e) { |
345 |
CC.getSamplerModel().addBackendChannel(); |
346 |
} |
347 |
|
348 |
private JPanel |
349 |
createVSeparator() { |
350 |
PixmapPane p = new PixmapPane(Res.gfxVLine); |
351 |
p.setOpaque(false); |
352 |
p.setPreferredSize(new Dimension(2, 29)); |
353 |
p.setMinimumSize(p.getPreferredSize()); |
354 |
p.setMaximumSize(p.getPreferredSize()); |
355 |
return p; |
356 |
} |
357 |
} |
358 |
} |