blob: 603810cdafbf53d52c9cd14459d02ac85155d3c3 [file] [log] [blame]
J. Duke319a3b92007-12-01 00:00:00 +00001/*
2 * Copyright 2002-2006 Sun Microsystems, Inc. All Rights Reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Sun designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Sun in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
22 * CA 95054 USA or visit www.sun.com if you need additional information or
23 * have any questions.
24 */
25
26package javax.swing.plaf.synth;
27
28import java.awt.*;
29import java.awt.event.*;
30
31import java.beans.*;
32
33import javax.swing.*;
34import javax.swing.event.*;
35import javax.swing.plaf.*;
36import javax.swing.plaf.basic.*;
37import sun.swing.plaf.synth.SynthUI;
38
39
40/**
41 * Synth's ScrollBarUI.
42 *
43 * @author Scott Violet
44 */
45class SynthScrollBarUI extends BasicScrollBarUI implements
46 PropertyChangeListener, SynthUI {
47
48 private SynthStyle style;
49 private SynthStyle thumbStyle;
50 private SynthStyle trackStyle;
51
52 private boolean validMinimumThumbSize;
53 private int scrollBarWidth;
54
55
56 public static ComponentUI createUI(JComponent c) {
57 return new SynthScrollBarUI();
58 }
59
60 protected void installDefaults() {
61 trackHighlight = NO_HIGHLIGHT;
62 if (scrollbar.getLayout() == null ||
63 (scrollbar.getLayout() instanceof UIResource)) {
64 scrollbar.setLayout(this);
65 }
66 updateStyle(scrollbar);
67 }
68
69 protected void configureScrollBarColors() {
70 }
71
72 private void updateStyle(JScrollBar c) {
73 SynthStyle oldStyle = style;
74 SynthContext context = getContext(c, ENABLED);
75 style = SynthLookAndFeel.updateStyle(context, this);
76 if (style != oldStyle) {
77 scrollBarWidth = style.getInt(context,"ScrollBar.thumbHeight", 14);
78 minimumThumbSize = (Dimension)style.get(context,
79 "ScrollBar.minimumThumbSize");
80 if (minimumThumbSize == null) {
81 minimumThumbSize = new Dimension();
82 validMinimumThumbSize = false;
83 }
84 else {
85 validMinimumThumbSize = true;
86 }
87 maximumThumbSize = (Dimension)style.get(context,
88 "ScrollBar.maximumThumbSize");
89 if (maximumThumbSize == null) {
90 maximumThumbSize = new Dimension(4096, 4097);
91 }
92 if (oldStyle != null) {
93 uninstallKeyboardActions();
94 installKeyboardActions();
95 }
96 }
97 context.dispose();
98
99 context = getContext(c, Region.SCROLL_BAR_TRACK, ENABLED);
100 trackStyle = SynthLookAndFeel.updateStyle(context, this);
101 context.dispose();
102
103 context = getContext(c, Region.SCROLL_BAR_THUMB, ENABLED);
104 thumbStyle = SynthLookAndFeel.updateStyle(context, this);
105 context.dispose();
106 }
107
108 protected void installListeners() {
109 super.installListeners();
110 scrollbar.addPropertyChangeListener(this);
111 }
112
113 protected void uninstallListeners() {
114 super.uninstallListeners();
115 scrollbar.removePropertyChangeListener(this);
116 }
117
118 protected void uninstallDefaults(){
119 SynthContext context = getContext(scrollbar, ENABLED);
120 style.uninstallDefaults(context);
121 context.dispose();
122 style = null;
123
124 context = getContext(scrollbar, Region.SCROLL_BAR_TRACK, ENABLED);
125 trackStyle.uninstallDefaults(context);
126 context.dispose();
127 trackStyle = null;
128
129 context = getContext(scrollbar, Region.SCROLL_BAR_THUMB, ENABLED);
130 thumbStyle.uninstallDefaults(context);
131 context.dispose();
132 thumbStyle = null;
133
134 super.uninstallDefaults();
135 }
136
137
138 public SynthContext getContext(JComponent c) {
139 return getContext(c, getComponentState(c));
140 }
141
142 private SynthContext getContext(JComponent c, int state) {
143 return SynthContext.getContext(SynthContext.class, c,
144 SynthLookAndFeel.getRegion(c), style, state);
145 }
146
147 private Region getRegion(JComponent c) {
148 return SynthLookAndFeel.getRegion(c);
149 }
150
151 private int getComponentState(JComponent c) {
152 return SynthLookAndFeel.getComponentState(c);
153 }
154
155 private SynthContext getContext(JComponent c, Region region) {
156 return getContext(c, region, getComponentState(c, region));
157 }
158
159 private SynthContext getContext(JComponent c, Region region, int state) {
160 SynthStyle style = trackStyle;
161
162 if (region == Region.SCROLL_BAR_THUMB) {
163 style = thumbStyle;
164 }
165 return SynthContext.getContext(SynthContext.class, c, region, style,
166 state);
167 }
168
169 private int getComponentState(JComponent c, Region region) {
170 if (region == Region.SCROLL_BAR_THUMB && isThumbRollover() &&
171 c.isEnabled()) {
172 return MOUSE_OVER;
173 }
174 return SynthLookAndFeel.getComponentState(c);
175 }
176
177 public boolean getSupportsAbsolutePositioning() {
178 SynthContext context = getContext(scrollbar);
179 boolean value = style.getBoolean(context,
180 "ScrollBar.allowsAbsolutePositioning", false);
181 context.dispose();
182 return value;
183 }
184
185 public void update(Graphics g, JComponent c) {
186 SynthContext context = getContext(c);
187
188 SynthLookAndFeel.update(context, g);
189 context.getPainter().paintScrollBarBackground(context,
190 g, 0, 0, c.getWidth(), c.getHeight(),
191 scrollbar.getOrientation());
192 paint(context, g);
193 context.dispose();
194 }
195
196 public void paint(Graphics g, JComponent c) {
197 SynthContext context = getContext(c);
198
199 paint(context, g);
200 context.dispose();
201 }
202
203 protected void paint(SynthContext context, Graphics g) {
204 SynthContext subcontext = getContext(scrollbar,
205 Region.SCROLL_BAR_TRACK);
206 paintTrack(subcontext, g, getTrackBounds());
207 subcontext.dispose();
208
209 subcontext = getContext(scrollbar, Region.SCROLL_BAR_THUMB);
210 paintThumb(subcontext, g, getThumbBounds());
211 subcontext.dispose();
212 }
213
214 public void paintBorder(SynthContext context, Graphics g, int x,
215 int y, int w, int h) {
216 context.getPainter().paintScrollBarBorder(context, g, x, y, w, h,
217 scrollbar.getOrientation());
218 }
219
220 protected void paintTrack(SynthContext ss, Graphics g,
221 Rectangle trackBounds) {
222 SynthLookAndFeel.updateSubregion(ss, g, trackBounds);
223 ss.getPainter().paintScrollBarTrackBackground(ss, g, trackBounds.x,
224 trackBounds.y, trackBounds.width, trackBounds.height,
225 scrollbar.getOrientation());
226 ss.getPainter().paintScrollBarTrackBorder(ss, g, trackBounds.x,
227 trackBounds.y, trackBounds.width, trackBounds.height,
228 scrollbar.getOrientation());
229 }
230
231 protected void paintThumb(SynthContext ss, Graphics g,
232 Rectangle thumbBounds) {
233 SynthLookAndFeel.updateSubregion(ss, g, thumbBounds);
234 int orientation = scrollbar.getOrientation();
235 ss.getPainter().paintScrollBarThumbBackground(ss, g, thumbBounds.x,
236 thumbBounds.y, thumbBounds.width, thumbBounds.height,
237 orientation);
238 ss.getPainter().paintScrollBarThumbBorder(ss, g, thumbBounds.x,
239 thumbBounds.y, thumbBounds.width, thumbBounds.height,
240 orientation);
241 }
242
243 /**
244 * A vertical scrollbar's preferred width is the maximum of
245 * preferred widths of the (non <code>null</code>)
246 * increment/decrement buttons,
247 * and the minimum width of the thumb. The preferred height is the
248 * sum of the preferred heights of the same parts. The basis for
249 * the preferred size of a horizontal scrollbar is similar.
250 * <p>
251 * The <code>preferredSize</code> is only computed once, subsequent
252 * calls to this method just return a cached size.
253 *
254 * @param c the <code>JScrollBar</code> that's delegating this method to us
255 * @return the preferred size of a Basic JScrollBar
256 * @see #getMaximumSize
257 * @see #getMinimumSize
258 */
259 public Dimension getPreferredSize(JComponent c) {
260 Insets insets = c.getInsets();
261 return (scrollbar.getOrientation() == JScrollBar.VERTICAL)
262 ? new Dimension(scrollBarWidth + insets.left + insets.right, 48)
263 : new Dimension(48, scrollBarWidth + insets.top + insets.bottom);
264 }
265
266 protected Dimension getMinimumThumbSize() {
267 if (!validMinimumThumbSize) {
268 if (scrollbar.getOrientation() == JScrollBar.VERTICAL) {
269 minimumThumbSize.width = scrollBarWidth;
270 minimumThumbSize.height = 7;
271 } else {
272 minimumThumbSize.width = 7;
273 minimumThumbSize.height = scrollBarWidth;
274 }
275 }
276 return minimumThumbSize;
277
278 }
279
280
281 protected JButton createDecreaseButton(int orientation) {
282 SynthArrowButton synthArrowButton = new SynthArrowButton(orientation);
283 synthArrowButton.setName("ScrollBar.button");
284 return synthArrowButton;
285 }
286
287 protected JButton createIncreaseButton(int orientation) {
288 SynthArrowButton synthArrowButton = new SynthArrowButton(orientation);
289 synthArrowButton.setName("ScrollBar.button");
290 return synthArrowButton;
291 }
292
293 protected void setThumbRollover(boolean active) {
294 if (isThumbRollover() != active) {
295 scrollbar.repaint(getThumbBounds());
296 super.setThumbRollover(active);
297 }
298 }
299
300 private void updateButtonDirections() {
301 int orient = scrollbar.getOrientation();
302 if (scrollbar.getComponentOrientation().isLeftToRight()) {
303 ((SynthArrowButton)incrButton).setDirection(
304 orient == HORIZONTAL? EAST : SOUTH);
305 ((SynthArrowButton)decrButton).setDirection(
306 orient == HORIZONTAL? WEST : NORTH);
307 }
308 else {
309 ((SynthArrowButton)incrButton).setDirection(
310 orient == HORIZONTAL? WEST : SOUTH);
311 ((SynthArrowButton)decrButton).setDirection(
312 orient == HORIZONTAL ? EAST : NORTH);
313 }
314 }
315
316 //
317 // PropertyChangeListener
318 //
319 public void propertyChange(PropertyChangeEvent e) {
320 String propertyName = e.getPropertyName();
321
322 if (SynthLookAndFeel.shouldUpdateStyle(e)) {
323 updateStyle((JScrollBar)e.getSource());
324 }
325
326 if ("orientation" == propertyName) {
327 updateButtonDirections();
328 }
329 else if ("componentOrientation" == propertyName) {
330 updateButtonDirections();
331 }
332 }
333}