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.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 javax.swing.BorderFactory; |
41 |
import javax.swing.Box; |
42 |
import javax.swing.BoxLayout; |
43 |
import javax.swing.JPanel; |
44 |
import javax.swing.JScrollPane; |
45 |
|
46 |
import org.jsampler.CC; |
47 |
|
48 |
import static org.jsampler.view.fantasia.FantasiaI18n.i18n; |
49 |
|
50 |
|
51 |
/** |
52 |
* |
53 |
* @author Grigor Iliev |
54 |
*/ |
55 |
public class MainPane extends FantasiaPanel { |
56 |
private final ChannelsBar channelsBar = new ChannelsBar(); |
57 |
private final ChannelsPane channelsPane; |
58 |
|
59 |
final JScrollPane scrollPane; |
60 |
|
61 |
/** Creates a new instance of <code>MainPane</code> */ |
62 |
public MainPane() { |
63 |
setOpaque(false); |
64 |
channelsPane = new ChannelsPane("", new ActionListener() { |
65 |
public void |
66 |
actionPerformed(ActionEvent e) { scrollToBottom(); } |
67 |
}); |
68 |
|
69 |
GridBagLayout gridbag = new GridBagLayout(); |
70 |
GridBagConstraints c = new GridBagConstraints(); |
71 |
|
72 |
setLayout(gridbag); |
73 |
|
74 |
JPanel p = new FantasiaPanel(); |
75 |
p.setOpaque(false); |
76 |
p.setLayout(new BoxLayout(p, BoxLayout.X_AXIS)); |
77 |
p.add(channelsBar); |
78 |
p.add(Box.createGlue()); |
79 |
|
80 |
c.gridx = 0; |
81 |
c.gridy = 0; |
82 |
c.weightx = 1.0; |
83 |
c.insets = new Insets(0, 0, 0, 0); |
84 |
c.fill = GridBagConstraints.HORIZONTAL; |
85 |
gridbag.setConstraints(p, c); |
86 |
add(p); |
87 |
|
88 |
p = createChannelsPane(); |
89 |
p.addMouseListener(getHandler()); |
90 |
|
91 |
scrollPane = new JScrollPane(p); |
92 |
JScrollPane sp = scrollPane; |
93 |
sp.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER); |
94 |
//sp.putClientProperty(SCROLL_PANE_BUTTONS_POLICY, ScrollPaneButtonPolicyKind.NONE); |
95 |
sp.setBorder(BorderFactory.createEmptyBorder()); |
96 |
sp.setOpaque(false); |
97 |
javax.swing.JViewport wp = sp.getViewport(); |
98 |
wp.setMinimumSize(new Dimension(400, wp.getMinimumSize().height)); |
99 |
wp.setOpaque(false); |
100 |
sp.setMaximumSize(new Dimension(sp.getMaximumSize().width, Short.MAX_VALUE)); |
101 |
sp.getVerticalScrollBar().setBorder(BorderFactory.createEmptyBorder(7, 4, 0, 1)); |
102 |
//sp.getVerticalScrollBar().setUnitIncrement(12); |
103 |
sp.setPreferredSize(new Dimension(420, sp.getPreferredSize().height)); |
104 |
|
105 |
sp.getVerticalScrollBar().addHierarchyListener(new HierarchyListener() { |
106 |
public void |
107 |
hierarchyChanged(HierarchyEvent e) { |
108 |
if((e.getChangeFlags() & e.SHOWING_CHANGED) != e.SHOWING_CHANGED) { |
109 |
return; |
110 |
} |
111 |
|
112 |
onScrollBarVisibilityChanged(); |
113 |
} |
114 |
}); |
115 |
|
116 |
c.gridx = 0; |
117 |
c.gridy = 2; |
118 |
c.weightx = 1.0; |
119 |
c.weighty = 1.0; |
120 |
c.insets = new Insets(0, 0, 0, 0); |
121 |
c.fill = GridBagConstraints.BOTH; |
122 |
gridbag.setConstraints(sp, c); |
123 |
add(sp); |
124 |
|
125 |
setMaximumSize(new Dimension(420, Short.MAX_VALUE)); |
126 |
} |
127 |
|
128 |
private JPanel |
129 |
createChannelsPane() { |
130 |
JPanel p = new FantasiaPanel(); |
131 |
p.setLayout(new BoxLayout(p, BoxLayout.Y_AXIS)); |
132 |
channelsPane.setAlignmentX(LEFT_ALIGNMENT); |
133 |
p.add(channelsPane); |
134 |
JPanel p2 = new NewChannelPane(); |
135 |
p2.setAlignmentX(LEFT_ALIGNMENT); |
136 |
p.add(p2); |
137 |
p.add(Box.createGlue()); |
138 |
p.setOpaque(false); |
139 |
p.setBorder(BorderFactory.createEmptyBorder(7, 0, 0, 0)); |
140 |
p.setMinimumSize(new Dimension(420, p.getMinimumSize().height)); |
141 |
p.setAlignmentX(LEFT_ALIGNMENT); |
142 |
return p; |
143 |
} |
144 |
|
145 |
private void |
146 |
onScrollBarVisibilityChanged() { |
147 |
int w = 420; |
148 |
int h = scrollPane.getPreferredSize().height; |
149 |
int scrollbarWidth = scrollPane.getVerticalScrollBar().getPreferredSize().width; |
150 |
|
151 |
if(scrollPane.getVerticalScrollBar().isVisible()) w += scrollbarWidth; |
152 |
|
153 |
scrollPane.setMinimumSize(new Dimension(w, scrollPane.getPreferredSize().height)); |
154 |
scrollPane.setPreferredSize(new Dimension(w, h)); |
155 |
scrollPane.setMaximumSize(new Dimension(w, Short.MAX_VALUE)); |
156 |
setMaximumSize(new Dimension(w, Short.MAX_VALUE)); |
157 |
|
158 |
if(CC.getMainFrame() != null && !CC.getMainFrame().isResizable()) { |
159 |
// this means that there are no visible side panes |
160 |
|
161 |
w = CC.getMainFrame().getPreferredSize().width; |
162 |
CC.getMainFrame().setSize(w, CC.getMainFrame().getHeight()); |
163 |
} |
164 |
|
165 |
revalidate(); |
166 |
} |
167 |
|
168 |
public void |
169 |
scrollToBottom() { |
170 |
int h = scrollPane.getViewport().getView().getHeight(); |
171 |
scrollPane.getViewport().scrollRectToVisible(new Rectangle(0, h - 2, 1, 1)); |
172 |
} |
173 |
|
174 |
public ChannelsPane |
175 |
getChannelsPane() { return channelsPane; } |
176 |
|
177 |
protected void |
178 |
paintComponent(Graphics g) { |
179 |
super.paintComponent(g); |
180 |
int h = Res.gfxChannelsBg.getIconHeight(); |
181 |
if(h < 1) return; |
182 |
int i = 0; |
183 |
while(i < getHeight()) { |
184 |
Res.gfxChannelsBg.paintIcon(this, g, 0, i); |
185 |
i += h; |
186 |
} |
187 |
} |
188 |
|
189 |
private final EventHandler eventHandler = new EventHandler(); |
190 |
|
191 |
private EventHandler |
192 |
getHandler() { return eventHandler; } |
193 |
|
194 |
private class EventHandler extends MouseAdapter { |
195 |
public void |
196 |
mouseClicked(MouseEvent e) { |
197 |
if(e.getButton() != e.BUTTON1) return; |
198 |
// TAG: channel selection system |
199 |
CC.getMainFrame().getChannelsPane(0).setSelectedChannel(null); |
200 |
/////// |
201 |
} |
202 |
} |
203 |
|
204 |
class NewChannelPane extends PixmapPane implements ActionListener { |
205 |
private final PixmapButton btnNew = new PixmapButton(Res.gfxPowerOff); |
206 |
|
207 |
NewChannelPane() { |
208 |
super(Res.gfxCreateChannel); |
209 |
setPixmapInsets(new Insets(3, 3, 3, 3)); |
210 |
|
211 |
setLayout(new BoxLayout(this, BoxLayout.X_AXIS)); |
212 |
add(Box.createRigidArea(new Dimension(3, 0))); |
213 |
add(btnNew); |
214 |
add(Box.createRigidArea(new Dimension(4, 0))); |
215 |
|
216 |
add(createVSeparator()); |
217 |
|
218 |
add(Box.createRigidArea(new Dimension(275, 0))); |
219 |
|
220 |
add(createVSeparator()); |
221 |
|
222 |
add(Box.createRigidArea(new Dimension(29, 0))); |
223 |
|
224 |
add(createVSeparator()); |
225 |
|
226 |
add(Box.createRigidArea(new Dimension(40, 0))); |
227 |
|
228 |
add(createVSeparator()); |
229 |
|
230 |
setPreferredSize(new Dimension(420, 29)); |
231 |
setMinimumSize(getPreferredSize()); |
232 |
setMaximumSize(getPreferredSize()); |
233 |
|
234 |
btnNew.setPressedIcon(Res.gfxPowerOn); |
235 |
btnNew.addActionListener(this); |
236 |
|
237 |
addMouseListener(new MouseAdapter() { |
238 |
public void |
239 |
mouseClicked(MouseEvent e) { |
240 |
if(e.getButton() != e.BUTTON1) { |
241 |
return; |
242 |
} |
243 |
|
244 |
CC.getSamplerModel().addBackendChannel(); |
245 |
} |
246 |
}); |
247 |
|
248 |
setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR)); |
249 |
|
250 |
String s = i18n.getLabel("MainPane.NewChannelPane.newChannel"); |
251 |
btnNew.setToolTipText(s); |
252 |
setToolTipText(s); |
253 |
} |
254 |
|
255 |
public void |
256 |
actionPerformed(ActionEvent e) { |
257 |
CC.getSamplerModel().addBackendChannel(); |
258 |
} |
259 |
|
260 |
private JPanel |
261 |
createVSeparator() { |
262 |
PixmapPane p = new PixmapPane(Res.gfxVLine); |
263 |
p.setOpaque(false); |
264 |
p.setPreferredSize(new Dimension(2, 29)); |
265 |
p.setMinimumSize(p.getPreferredSize()); |
266 |
p.setMaximumSize(p.getPreferredSize()); |
267 |
return p; |
268 |
} |
269 |
} |
270 |
} |