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 |
import static org.jvnet.substance.SubstanceLookAndFeel.SCROLL_PANE_BUTTONS_POLICY; |
50 |
import static org.jvnet.substance.utils.SubstanceConstants.ScrollPaneButtonPolicyKind; |
51 |
|
52 |
|
53 |
/** |
54 |
* |
55 |
* @author Grigor Iliev |
56 |
*/ |
57 |
public class MainPane extends JPanel { |
58 |
private final ChannelsBar channelsBar = new ChannelsBar(); |
59 |
private final ChannelsPane channelsPane; |
60 |
|
61 |
final JScrollPane scrollPane; |
62 |
|
63 |
/** Creates a new instance of <code>MainPane</code> */ |
64 |
public MainPane() { |
65 |
channelsPane = new ChannelsPane("", new ActionListener() { |
66 |
public void |
67 |
actionPerformed(ActionEvent e) { scrollToBottom(); } |
68 |
}); |
69 |
|
70 |
GridBagLayout gridbag = new GridBagLayout(); |
71 |
GridBagConstraints c = new GridBagConstraints(); |
72 |
|
73 |
setLayout(gridbag); |
74 |
|
75 |
JPanel p = new JPanel(); |
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.setAlignmentX(LEFT_ALIGNMENT); |
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(420, 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 JPanel(); |
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 |
return p; |
142 |
} |
143 |
|
144 |
private void |
145 |
onScrollBarVisibilityChanged() { |
146 |
int h = scrollPane.getPreferredSize().height; |
147 |
int scrollbarWidth = scrollPane.getVerticalScrollBar().getPreferredSize().width; |
148 |
|
149 |
if(scrollPane.getVerticalScrollBar().isVisible()) { |
150 |
scrollPane.setPreferredSize(new Dimension(420 + scrollbarWidth, h)); |
151 |
} else { |
152 |
scrollPane.setPreferredSize(new Dimension(420, h)); |
153 |
} |
154 |
|
155 |
revalidate(); |
156 |
} |
157 |
|
158 |
public void |
159 |
scrollToBottom() { |
160 |
int h = scrollPane.getViewport().getView().getHeight(); |
161 |
scrollPane.getViewport().scrollRectToVisible(new Rectangle(0, h - 2, 1, 1)); |
162 |
} |
163 |
|
164 |
public ChannelsPane |
165 |
getChannelsPane() { return channelsPane; } |
166 |
|
167 |
protected void |
168 |
paintComponent(Graphics g) { |
169 |
super.paintComponent(g); |
170 |
int h = Res.gfxChannelsBg.getIconHeight(); |
171 |
if(h < 1) return; |
172 |
int i = 0; |
173 |
while(i < getHeight()) { |
174 |
Res.gfxChannelsBg.paintIcon(this, g, 0, i); |
175 |
i += h; |
176 |
} |
177 |
} |
178 |
|
179 |
class NewChannelPane extends PixmapPane implements ActionListener { |
180 |
private final PixmapButton btnNew = new PixmapButton(Res.gfxPowerOff); |
181 |
|
182 |
NewChannelPane() { |
183 |
super(Res.gfxCreateChannel); |
184 |
setPixmapInsets(new Insets(3, 3, 3, 3)); |
185 |
|
186 |
setLayout(new BoxLayout(this, BoxLayout.X_AXIS)); |
187 |
add(Box.createRigidArea(new Dimension(4, 0))); |
188 |
add(btnNew); |
189 |
add(Box.createRigidArea(new Dimension(3, 0))); |
190 |
|
191 |
add(createVSeparator()); |
192 |
|
193 |
add(Box.createRigidArea(new Dimension(275, 0))); |
194 |
|
195 |
add(createVSeparator()); |
196 |
|
197 |
add(Box.createRigidArea(new Dimension(29, 0))); |
198 |
|
199 |
add(createVSeparator()); |
200 |
|
201 |
add(Box.createRigidArea(new Dimension(40, 0))); |
202 |
|
203 |
add(createVSeparator()); |
204 |
|
205 |
setPreferredSize(new Dimension(420, 29)); |
206 |
setMinimumSize(getPreferredSize()); |
207 |
setMaximumSize(getPreferredSize()); |
208 |
|
209 |
btnNew.setPressedIcon(Res.gfxPowerOn); |
210 |
btnNew.addActionListener(this); |
211 |
|
212 |
addMouseListener(new MouseAdapter() { |
213 |
public void |
214 |
mouseClicked(MouseEvent e) { |
215 |
if(e.getButton() != e.BUTTON1) { |
216 |
return; |
217 |
} |
218 |
|
219 |
CC.getSamplerModel().addBackendChannel(); |
220 |
} |
221 |
}); |
222 |
|
223 |
setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR)); |
224 |
|
225 |
String s = i18n.getLabel("MainPane.NewChannelPane.newChannel"); |
226 |
btnNew.setToolTipText(s); |
227 |
setToolTipText(s); |
228 |
} |
229 |
|
230 |
public void |
231 |
actionPerformed(ActionEvent e) { |
232 |
CC.getSamplerModel().addBackendChannel(); |
233 |
} |
234 |
|
235 |
private JPanel |
236 |
createVSeparator() { |
237 |
PixmapPane p = new PixmapPane(Res.gfxVLine); |
238 |
p.setOpaque(false); |
239 |
p.setPreferredSize(new Dimension(2, 29)); |
240 |
p.setMinimumSize(p.getPreferredSize()); |
241 |
p.setMaximumSize(p.getPreferredSize()); |
242 |
return p; |
243 |
} |
244 |
} |
245 |
} |