1 /* MetalScrollBarUI.java
2 Copyright (C) 2005 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
39 package javax
.swing
.plaf
.metal
;
41 import java
.awt
.Color
;
42 import java
.awt
.Dimension
;
43 import java
.awt
.Graphics
;
44 import java
.awt
.Insets
;
45 import java
.awt
.Rectangle
;
46 import java
.beans
.PropertyChangeEvent
;
47 import java
.beans
.PropertyChangeListener
;
49 import javax
.swing
.JButton
;
50 import javax
.swing
.JComponent
;
51 import javax
.swing
.JScrollBar
;
52 import javax
.swing
.SwingConstants
;
53 import javax
.swing
.UIManager
;
54 import javax
.swing
.plaf
.ComponentUI
;
55 import javax
.swing
.plaf
.basic
.BasicScrollBarUI
;
58 * A UI delegate for the {@link JScrollBar} component.
60 public class MetalScrollBarUI
extends BasicScrollBarUI
64 * A property change handler for the UI delegate that monitors for
65 * changes to the "JScrollBar.isFreeStanding" property, and updates
66 * the buttons and track rendering as appropriate.
68 class MetalScrollBarPropertyChangeHandler
69 extends BasicScrollBarUI
.PropertyChangeHandler
72 * Creates a new handler.
74 * @see #createPropertyChangeListener()
76 public MetalScrollBarPropertyChangeHandler()
78 // Nothing to do here.
82 * Handles a property change event. If the event name is
83 * <code>JSlider.isFreeStanding</code>, this method updates the
84 * delegate, otherwise the event is passed up to the super class.
86 * @param e the property change event.
88 public void propertyChange(PropertyChangeEvent e
)
90 if (e
.getPropertyName().equals(FREE_STANDING_PROP
))
92 Boolean prop
= (Boolean
) e
.getNewValue();
93 isFreeStanding
= (prop
== null ?
true : prop
.booleanValue());
94 if (increaseButton
!= null)
95 increaseButton
.setFreeStanding(isFreeStanding
);
96 if (decreaseButton
!= null)
97 decreaseButton
.setFreeStanding(isFreeStanding
);
100 super.propertyChange(e
);
104 /** The name for the 'free standing' property. */
105 public static final String FREE_STANDING_PROP
= "JScrollBar.isFreeStanding";
107 /** The minimum thumb size for a scroll bar that is not free standing. */
108 private static final Dimension MIN_THUMB_SIZE
= new Dimension(15, 15);
110 /** The minimum thumb size for a scroll bar that is free standing. */
111 private static final Dimension MIN_THUMB_SIZE_FREE_STANDING
112 = new Dimension(17, 17);
114 /** The button that increases the value in the scroll bar. */
115 protected MetalScrollButton increaseButton
;
117 /** The button that decreases the value in the scroll bar. */
118 protected MetalScrollButton decreaseButton
;
121 * The scroll bar width.
123 protected int scrollBarWidth
;
126 * A flag that indicates whether the scroll bar is "free standing", which
127 * means it has complete borders and can be used anywhere in the UI. A
128 * scroll bar which is not free standing has borders missing from one
129 * side, and relies on being part of another container with its own borders
130 * to look right visually. */
131 protected boolean isFreeStanding
= true;
134 * The color for the scroll bar shadow (this is read from the UIDefaults in
135 * the installDefaults() method).
137 Color scrollBarShadowColor
;
140 * Constructs a new instance of <code>MetalScrollBarUI</code>, with no
141 * specific initialisation.
143 public MetalScrollBarUI()
149 * Returns a new instance of <code>MetalScrollBarUI</code>.
151 * @param component the component for which we return an UI instance
153 * @return An instance of MetalScrollBarUI
155 public static ComponentUI
createUI(JComponent component
)
157 return new MetalScrollBarUI();
161 * Installs the defaults.
163 protected void installDefaults()
165 // need to initialise isFreeStanding before calling the super class,
166 // so that the value is set when createIncreaseButton() and
167 // createDecreaseButton() are called (unless there is somewhere earlier
168 // that we can do this).
169 Boolean prop
= (Boolean
) scrollbar
.getClientProperty(FREE_STANDING_PROP
);
170 isFreeStanding
= (prop
== null ?
true : prop
.booleanValue());
171 scrollBarShadowColor
= UIManager
.getColor("ScrollBar.shadow");
172 super.installDefaults();
176 * Creates a property change listener for the delegate to use. This
177 * overrides the method to provide a custom listener for the
178 * {@link MetalLookAndFeel} that can handle the
179 * <code>JScrollBar.isFreeStanding</code> property.
181 * @return A property change listener.
183 protected PropertyChangeListener
createPropertyChangeListener()
185 return new MetalScrollBarPropertyChangeHandler();
189 * Creates a new button to use as the control at the lower end of the
190 * {@link JScrollBar}.
192 * @param orientation the orientation of the button ({@link #NORTH},
193 * {@link #SOUTH}, {@link #EAST} or {@link #WEST}).
195 * @return The button.
197 protected JButton
createDecreaseButton(int orientation
)
199 scrollBarWidth
= UIManager
.getInt("ScrollBar.width");
200 decreaseButton
= new MetalScrollButton(orientation
, scrollBarWidth
,
202 return decreaseButton
;
206 * Creates a new button to use as the control at the upper end of the
207 * {@link JScrollBar}.
209 * @param orientation the orientation of the button ({@link #NORTH},
210 * {@link #SOUTH}, {@link #EAST} or {@link #WEST}).
212 * @return The button.
214 protected JButton
createIncreaseButton(int orientation
)
216 scrollBarWidth
= UIManager
.getInt("ScrollBar.width");
217 increaseButton
= new MetalScrollButton(orientation
, scrollBarWidth
,
219 return increaseButton
;
223 * Paints the track for the scrollbar.
225 * @param g the graphics device.
226 * @param c the component.
227 * @param trackBounds the track bounds.
229 protected void paintTrack(Graphics g
, JComponent c
, Rectangle trackBounds
)
231 g
.setColor(MetalLookAndFeel
.getControl());
232 g
.fillRect(trackBounds
.x
, trackBounds
.y
, trackBounds
.width
,
234 if (scrollbar
.getOrientation() == HORIZONTAL
)
235 paintTrackHorizontal(g
, c
, trackBounds
.x
, trackBounds
.y
,
236 trackBounds
.width
, trackBounds
.height
);
238 paintTrackVertical(g
, c
, trackBounds
.x
, trackBounds
.y
,
239 trackBounds
.width
, trackBounds
.height
);
244 * Paints the track for a horizontal scrollbar.
246 * @param g the graphics device.
247 * @param c the component.
248 * @param x the x-coordinate for the track bounds.
249 * @param y the y-coordinate for the track bounds.
250 * @param w the width for the track bounds.
251 * @param h the height for the track bounds.
253 private void paintTrackHorizontal(Graphics g
, JComponent c
,
254 int x
, int y
, int w
, int h
)
258 g
.setColor(MetalLookAndFeel
.getControlDarkShadow());
259 g
.drawLine(x
, y
, x
, y
+ h
- 1);
260 g
.drawLine(x
, y
, x
+ w
- 1, y
);
261 g
.drawLine(x
+ w
- 1, y
, x
+ w
- 1, y
+ h
- 1);
263 g
.setColor(scrollBarShadowColor
);
264 g
.drawLine(x
+ 1, y
+ 1, x
+ 1, y
+ h
- 1);
265 g
.drawLine(x
+ 1, y
+ 1, x
+ w
- 2, y
+ 1);
269 g
.setColor(MetalLookAndFeel
.getControlDarkShadow());
270 g
.drawLine(x
, y
+ h
- 2, x
+ w
- 1, y
+ h
- 2);
271 g
.setColor(scrollBarShadowColor
);
272 g
.drawLine(x
, y
+ h
- 1, x
+ w
- 1, y
+ h
- 1);
277 g
.setColor(MetalLookAndFeel
.getControlDisabled());
279 g
.drawRect(x
, y
, w
- 1, h
- 1);
282 g
.drawLine(x
, y
, x
+ w
- 1, y
);
283 g
.drawLine(x
, y
, x
, y
+ h
- 1);
284 g
.drawLine(x
+ w
- 1, y
, x
+ w
- 1, y
+ h
- 1);
290 * Paints the track for a vertical scrollbar.
292 * @param g the graphics device.
293 * @param c the component.
294 * @param x the x-coordinate for the track bounds.
295 * @param y the y-coordinate for the track bounds.
296 * @param w the width for the track bounds.
297 * @param h the height for the track bounds.
299 private void paintTrackVertical(Graphics g
, JComponent c
,
300 int x
, int y
, int w
, int h
)
304 g
.setColor(MetalLookAndFeel
.getControlDarkShadow());
305 g
.drawLine(x
, y
, x
, y
+ h
- 1);
306 g
.drawLine(x
, y
, x
+ w
- 1, y
);
307 g
.drawLine(x
, y
+ h
- 1, x
+ w
- 1, y
+ h
- 1);
309 g
.setColor(scrollBarShadowColor
);
310 g
.drawLine(x
+ 1, y
+ 1, x
+ w
- 1, y
+ 1);
311 g
.drawLine(x
+ 1, y
+ 1, x
+ 1, y
+ h
- 2);
315 g
.setColor(MetalLookAndFeel
.getControlDarkShadow());
316 g
.drawLine(x
+ w
- 2, y
, x
+ w
- 2, y
+ h
- 1);
317 g
.setColor(MetalLookAndFeel
.getControlHighlight());
318 g
.drawLine(x
+ w
- 1, y
, x
+ w
- 1, y
+ h
- 1);
323 g
.setColor(MetalLookAndFeel
.getControlDisabled());
325 g
.drawRect(x
, y
, w
- 1, h
- 1);
328 g
.drawLine(x
, y
, x
+ w
- 1, y
);
329 g
.drawLine(x
, y
, x
, y
+ h
- 1);
330 g
.drawLine(x
, y
+ h
- 1, x
+ w
- 1, y
+ h
- 1);
336 * Paints the slider button of the ScrollBar.
338 * @param g the Graphics context to use
339 * @param c the JComponent on which we paint
340 * @param thumbBounds the rectangle that is the slider button
342 protected void paintThumb(Graphics g
, JComponent c
, Rectangle thumbBounds
)
344 // a disabled scrollbar has no thumb in the metal look and feel
347 if (scrollbar
.getOrientation() == HORIZONTAL
)
348 paintThumbHorizontal(g
, c
, thumbBounds
);
350 paintThumbVertical(g
, c
, thumbBounds
);
353 MetalUtils
.fillMetalPattern(c
, g
, thumbBounds
.x
+ 3, thumbBounds
.y
+ 3,
354 thumbBounds
.width
- 6, thumbBounds
.height
- 6,
355 thumbHighlightColor
, thumbLightShadowColor
);
359 * Paints the thumb for a horizontal scroll bar.
361 * @param g the graphics device.
362 * @param c the scroll bar component.
363 * @param thumbBounds the thumb bounds.
365 private void paintThumbHorizontal(Graphics g
, JComponent c
,
366 Rectangle thumbBounds
)
368 int x
= thumbBounds
.x
;
369 int y
= thumbBounds
.y
;
370 int w
= thumbBounds
.width
;
371 int h
= thumbBounds
.height
;
373 // first we fill the background
374 g
.setColor(thumbColor
);
376 g
.fillRect(x
, y
, w
, h
- 1);
378 g
.fillRect(x
, y
, w
, h
);
380 // then draw the dark box
381 g
.setColor(thumbLightShadowColor
);
383 g
.drawRect(x
, y
, w
- 1, h
- 2);
386 g
.drawLine(x
, y
, x
+ w
- 1, y
);
387 g
.drawLine(x
, y
, x
, y
+ h
- 1);
388 g
.drawLine(x
+ w
- 1, y
, x
+ w
- 1, y
+ h
-1);
391 // then the highlight
392 g
.setColor(thumbHighlightColor
);
395 g
.drawLine(x
+ 1, y
+ 1, x
+ w
- 3, y
+ 1);
396 g
.drawLine(x
+ 1, y
+ 1, x
+ 1, y
+ h
- 3);
400 g
.drawLine(x
+ 1, y
+ 1, x
+ w
- 3, y
+ 1);
401 g
.drawLine(x
+ 1, y
+ 1, x
+ 1, y
+ h
- 1);
404 // draw the shadow line
405 g
.setColor(UIManager
.getColor("ScrollBar.shadow"));
406 g
.drawLine(x
+ w
, y
+ 1, x
+ w
, y
+ h
- 1);
411 * Paints the thumb for a vertical scroll bar.
413 * @param g the graphics device.
414 * @param c the scroll bar component.
415 * @param thumbBounds the thumb bounds.
417 private void paintThumbVertical(Graphics g
, JComponent c
,
418 Rectangle thumbBounds
)
420 int x
= thumbBounds
.x
;
421 int y
= thumbBounds
.y
;
422 int w
= thumbBounds
.width
;
423 int h
= thumbBounds
.height
;
425 // first we fill the background
426 g
.setColor(thumbColor
);
428 g
.fillRect(x
, y
, w
- 1, h
);
430 g
.fillRect(x
, y
, w
, h
);
432 // then draw the dark box
433 g
.setColor(thumbLightShadowColor
);
435 g
.drawRect(x
, y
, w
- 2, h
- 1);
438 g
.drawLine(x
, y
, x
+ w
- 1, y
);
439 g
.drawLine(x
, y
, x
, y
+ h
- 1);
440 g
.drawLine(x
, y
+ h
- 1, x
+ w
- 1, y
+ h
- 1);
443 // then the highlight
444 g
.setColor(thumbHighlightColor
);
447 g
.drawLine(x
+ 1, y
+ 1, x
+ w
- 3, y
+ 1);
448 g
.drawLine(x
+ 1, y
+ 1, x
+ 1, y
+ h
- 3);
452 g
.drawLine(x
+ 1, y
+ 1, x
+ w
- 1, y
+ 1);
453 g
.drawLine(x
+ 1, y
+ 1, x
+ 1, y
+ h
- 3);
456 // draw the shadow line
457 g
.setColor(UIManager
.getColor("ScrollBar.shadow"));
458 g
.drawLine(x
+ 1, y
+ h
, x
+ w
- 2, y
+ h
);
462 * Returns the minimum thumb size. For a free standing scroll bar the
463 * minimum size is <code>17 x 17</code> pixels, whereas for a non free
464 * standing scroll bar the minimum size is <code>15 x 15</code> pixels.
466 * @return The minimum thumb size.
468 protected Dimension
getMinimumThumbSize()
471 if (scrollbar
!= null)
474 retVal
= MIN_THUMB_SIZE_FREE_STANDING
;
476 retVal
= MIN_THUMB_SIZE
;
479 retVal
= new Dimension(0, 0);
484 * Returns the <code>preferredSize</code> for the specified scroll bar.
485 * For a vertical scrollbar the height is the sum of the preferred heights
486 * of the buttons plus <code>30</code>. The width is fetched from the
487 * <code>UIManager</code> property <code>ScrollBar.width</code>.
489 * For horizontal scrollbars the width is the sum of the preferred widths
490 * of the buttons plus <code>30</code>. The height is fetched from the
491 * <code>UIManager</code> property <code>ScrollBar.height</code>.
493 * @param c the scrollbar for which to calculate the preferred size
495 * @return the <code>preferredSize</code> for the specified scroll bar
497 public Dimension
getPreferredSize(JComponent c
)
503 if (scrollbar
.getOrientation() == SwingConstants
.HORIZONTAL
)
505 width
+= incrButton
.getPreferredSize().getWidth();
506 width
+= decrButton
.getPreferredSize().getWidth();
508 height
= UIManager
.getInt("ScrollBar.width");
512 height
+= incrButton
.getPreferredSize().getHeight();
513 height
+= decrButton
.getPreferredSize().getHeight();
515 width
= UIManager
.getInt("ScrollBar.width");
518 Insets insets
= scrollbar
.getInsets();
520 height
+= insets
.top
+ insets
.bottom
;
521 width
+= insets
.left
+ insets
.right
;
523 return new Dimension(width
, height
);