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