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