KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javax > swing > plaf > basic > BasicScrollBarUI


1 /*
2  * @(#)BasicScrollBarUI.java 1.84 07/04/11
3  *
4  * Copyright 2007 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7 package javax.swing.plaf.basic;
8
9
10 import sun.swing.DefaultLookup;
11 import sun.swing.UIAction;
12
13 import java.awt.*;
14 import java.awt.event.*;
15
16 import java.beans.*;
17
18 import javax.swing.*;
19 import javax.swing.event.*;
20 import javax.swing.plaf.*;
21
22
23 /**
24  * Implementation of ScrollBarUI for the Basic Look and Feel
25  *
26  * @version 1.84 04/11/07
27  * @author Rich Schiavi
28  * @author David Kloba
29  * @author Hans Muller
30  */

31 public class BasicScrollBarUI
32     extends ScrollBarUI implements LayoutManager, SwingConstants
33 {
34     private static final int POSITIVE_SCROLL = 1;
35     private static final int NEGATIVE_SCROLL = -1;
36
37     private static final int MIN_SCROLL = 2;
38     private static final int MAX_SCROLL = 3;
39
40     protected Dimension minimumThumbSize;
41     protected Dimension maximumThumbSize;
42
43     protected Color thumbHighlightColor;
44     protected Color thumbLightShadowColor;
45     protected Color thumbDarkShadowColor;
46     protected Color thumbColor;
47     protected Color trackColor;
48     protected Color trackHighlightColor;
49
50     protected JScrollBar scrollbar;
51     protected JButton incrButton;
52     protected JButton decrButton;
53     protected boolean isDragging;
54     protected TrackListener trackListener;
55     protected ArrowButtonListener buttonListener;
56     protected ModelListener modelListener;
57
58     protected Rectangle thumbRect;
59     protected Rectangle trackRect;
60
61     protected int trackHighlight;
62
63     protected static final int NO_HIGHLIGHT = 0;
64     protected static final int DECREASE_HIGHLIGHT = 1;
65     protected static final int INCREASE_HIGHLIGHT = 2;
66
67     protected ScrollListener scrollListener;
68     protected PropertyChangeListener propertyChangeListener;
69     protected Timer scrollTimer;
70
71     private final static int scrollSpeedThrottle = 60; // delay in milli seconds
72

73     /** True indicates a middle click will absolutely position the
74      * scrollbar. */

75     private boolean supportsAbsolutePositioning;
76
77     /** Hint as to what width (when vertical) or height (when horizontal)
78      * should be.
79      */

80     private int scrollBarWidth;
81
82     private Handler handler;
83
84     private boolean thumbActive;
85
86     /**
87      * Determine whether scrollbar layout should use cached value or adjusted
88      * value returned by scrollbar's <code>getValue</code>.
89      */

90     private boolean useCachedValue = false;
91     /**
92      * The scrollbar value is cached to save real value if the view is adjusted.
93      */

94     private int scrollBarValue;
95
96     static void loadActionMap(LazyActionMap JavaDoc map) {
97         map.put(new Actions(Actions.POSITIVE_UNIT_INCREMENT));
98         map.put(new Actions(Actions.POSITIVE_BLOCK_INCREMENT));
99         map.put(new Actions(Actions.NEGATIVE_UNIT_INCREMENT));
100         map.put(new Actions(Actions.NEGATIVE_BLOCK_INCREMENT));
101         map.put(new Actions(Actions.MIN_SCROLL));
102         map.put(new Actions(Actions.MAX_SCROLL));
103     }
104
105
106     public static ComponentUI createUI(JComponent c) {
107         return new BasicScrollBarUI JavaDoc();
108     }
109
110
111     protected void configureScrollBarColors()
112     {
113         LookAndFeel.installColors(scrollbar, "ScrollBar.background",
114                                   "ScrollBar.foreground");
115     thumbHighlightColor = UIManager.getColor("ScrollBar.thumbHighlight");
116     thumbLightShadowColor = UIManager.getColor("ScrollBar.thumbShadow");
117     thumbDarkShadowColor = UIManager.getColor("ScrollBar.thumbDarkShadow");
118     thumbColor = UIManager.getColor("ScrollBar.thumb");
119     trackColor = UIManager.getColor("ScrollBar.track");
120     trackHighlightColor = UIManager.getColor("ScrollBar.trackHighlight");
121     }
122
123
124     public void installUI(JComponent c) {
125     scrollbar = (JScrollBar)c;
126         thumbRect = new Rectangle(0, 0, 0, 0);
127         trackRect = new Rectangle(0, 0, 0, 0);
128     installDefaults();
129     installComponents();
130     installListeners();
131     installKeyboardActions();
132     }
133
134     public void uninstallUI(JComponent c) {
135     scrollbar = (JScrollBar)c;
136     uninstallListeners();
137     uninstallDefaults();
138     uninstallComponents();
139     uninstallKeyboardActions();
140     thumbRect = null;
141     scrollbar = null;
142     incrButton = null;
143     decrButton = null;
144     }
145     
146
147     protected void installDefaults()
148     {
149     scrollBarWidth = UIManager.getInt("ScrollBar.width");
150         if (scrollBarWidth <= 0) {
151             scrollBarWidth = 16;
152         }
153     minimumThumbSize = (Dimension)UIManager.get("ScrollBar.minimumThumbSize");
154     maximumThumbSize = (Dimension)UIManager.get("ScrollBar.maximumThumbSize");
155
156     Boolean JavaDoc absB = (Boolean JavaDoc)UIManager.get("ScrollBar.allowsAbsolutePositioning");
157     supportsAbsolutePositioning = (absB != null) ? absB.booleanValue() :
158                                   false;
159
160     trackHighlight = NO_HIGHLIGHT;
161         if (scrollbar.getLayout() == null ||
162                      (scrollbar.getLayout() instanceof UIResource)) {
163             scrollbar.setLayout(this);
164         }
165     configureScrollBarColors();
166         LookAndFeel.installBorder(scrollbar, "ScrollBar.border");
167     LookAndFeel.installProperty(scrollbar, "opaque", Boolean.TRUE);
168
169     scrollBarValue = scrollbar.getValue();
170     }
171
172
173     protected void installComponents(){
174         switch (scrollbar.getOrientation()) {
175         case JScrollBar.VERTICAL:
176             incrButton = createIncreaseButton(SOUTH);
177             decrButton = createDecreaseButton(NORTH);
178             break;
179             
180         case JScrollBar.HORIZONTAL:
181             if (scrollbar.getComponentOrientation().isLeftToRight()) {
182                 incrButton = createIncreaseButton(EAST);
183                 decrButton = createDecreaseButton(WEST);
184             } else {
185                 incrButton = createIncreaseButton(WEST);
186                 decrButton = createDecreaseButton(EAST);
187             }
188             break;
189         }
190         scrollbar.add(incrButton);
191         scrollbar.add(decrButton);
192         // Force the children's enabled state to be updated.
193
scrollbar.setEnabled(scrollbar.isEnabled());
194     }
195
196     protected void uninstallComponents(){
197     scrollbar.remove(incrButton);
198     scrollbar.remove(decrButton);
199     }
200
201   
202     protected void installListeners(){
203     trackListener = createTrackListener();
204         buttonListener = createArrowButtonListener();
205         modelListener = createModelListener();
206     propertyChangeListener = createPropertyChangeListener();
207
208     scrollbar.addMouseListener(trackListener);
209     scrollbar.addMouseMotionListener(trackListener);
210         scrollbar.getModel().addChangeListener(modelListener);
211     scrollbar.addPropertyChangeListener(propertyChangeListener);
212         scrollbar.addFocusListener(getHandler());
213
214         if (incrButton != null) {
215             incrButton.addMouseListener(buttonListener);
216     }
217         if (decrButton != null) {
218             decrButton.addMouseListener(buttonListener);
219     }
220
221     scrollListener = createScrollListener();
222     scrollTimer = new Timer(scrollSpeedThrottle, scrollListener);
223     scrollTimer.setInitialDelay(300); // default InitialDelay?
224
}
225
226
227     protected void installKeyboardActions(){
228         LazyActionMap.installLazyActionMap(scrollbar, BasicScrollBarUI JavaDoc.class,
229                                            "ScrollBar.actionMap");
230
231     InputMap inputMap = getInputMap(JComponent.WHEN_FOCUSED);
232     SwingUtilities.replaceUIInputMap(scrollbar, JComponent.WHEN_FOCUSED,
233                      inputMap);
234     inputMap = getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
235     SwingUtilities.replaceUIInputMap(scrollbar,
236                    JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT, inputMap);
237     }
238
239     protected void uninstallKeyboardActions(){
240     SwingUtilities.replaceUIInputMap(scrollbar, JComponent.WHEN_FOCUSED,
241                      null);
242     SwingUtilities.replaceUIActionMap(scrollbar, null);
243     }
244
245     private InputMap getInputMap(int condition) {
246     if (condition == JComponent.WHEN_FOCUSED) {
247             InputMap keyMap = (InputMap)DefaultLookup.get(
248                         scrollbar, this, "ScrollBar.focusInputMap");
249             InputMap rtlKeyMap;
250
251             if (scrollbar.getComponentOrientation().isLeftToRight() ||
252                 ((rtlKeyMap = (InputMap)DefaultLookup.get(scrollbar, this, "ScrollBar.focusInputMap.RightToLeft")) == null)) {
253                 return keyMap;
254             } else {
255                 rtlKeyMap.setParent(keyMap);
256                 return rtlKeyMap;
257             }
258     }
259     else if (condition == JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT) {
260             InputMap keyMap = (InputMap)DefaultLookup.get(
261                         scrollbar, this, "ScrollBar.ancestorInputMap");
262             InputMap rtlKeyMap;
263
264             if (scrollbar.getComponentOrientation().isLeftToRight() ||
265                 ((rtlKeyMap = (InputMap)DefaultLookup.get(scrollbar, this, "ScrollBar.ancestorInputMap.RightToLeft")) == null)) {
266                 return keyMap;
267             } else {
268                 rtlKeyMap.setParent(keyMap);
269                 return rtlKeyMap;
270             }
271     }
272     return null;
273     }
274
275
276     protected void uninstallListeners() {
277     scrollTimer.stop();
278     scrollTimer = null;
279
280     if (decrButton != null){
281         decrButton.removeMouseListener(buttonListener);
282     }
283     if (incrButton != null){
284         incrButton.removeMouseListener(buttonListener);
285     }
286     
287     scrollbar.getModel().removeChangeListener(modelListener);
288     scrollbar.removeMouseListener(trackListener);
289     scrollbar.removeMouseMotionListener(trackListener);
290     scrollbar.removePropertyChangeListener(propertyChangeListener);
291         scrollbar.removeFocusListener(getHandler());
292         handler = null;
293     }
294
295
296     protected void uninstallDefaults(){
297         LookAndFeel.uninstallBorder(scrollbar);
298         if (scrollbar.getLayout() == this) {
299             scrollbar.setLayout(null);
300         }
301     }
302
303
304     private Handler getHandler() {
305         if (handler == null) {
306             handler = new Handler();
307         }
308         return handler;
309     }
310
311     protected TrackListener createTrackListener(){
312     return new TrackListener();
313     }
314
315     protected ArrowButtonListener createArrowButtonListener(){
316     return new ArrowButtonListener();
317     }
318     
319     protected ModelListener createModelListener(){
320     return new ModelListener();
321     }
322
323     protected ScrollListener createScrollListener(){
324     return new ScrollListener();
325     }
326     
327     protected PropertyChangeListener createPropertyChangeListener() {
328     return getHandler();
329     }
330
331     private void updateThumbState(int x, int y) {
332         Rectangle rect = getThumbBounds();
333
334         setThumbRollover(rect.contains(x, y));
335     }
336
337     /**
338      * Sets whether or not the mouse is currently over the thumb.
339      *
340      * @param active True indicates the thumb is currently active.
341      * @since 1.5
342      */

343     protected void setThumbRollover(boolean active) {
344         if (thumbActive != active) {
345             thumbActive = active;
346             scrollbar.repaint(getThumbBounds());
347         }
348     }
349
350     /**
351      * Returns true if the mouse is currently over the thumb.
352      *
353      * @return true if the thumb is currently active
354      * @since 1.5
355      */

356     public boolean isThumbRollover() {
357         return thumbActive;
358     }
359
360     public void paint(Graphics g, JComponent c) {
361     paintTrack(g, c, getTrackBounds());
362     Rectangle thumbBounds = getThumbBounds();
363     if (thumbBounds.intersects(g.getClipBounds())) {
364         paintThumb(g, c, thumbBounds);
365     }
366     }
367
368         
369     /**
370      * A vertical scrollbar's preferred width is the maximum of
371      * preferred widths of the (non <code>null</code>)
372      * increment/decrement buttons,
373      * and the minimum width of the thumb. The preferred height is the
374      * sum of the preferred heights of the same parts. The basis for
375      * the preferred size of a horizontal scrollbar is similar.
376      * <p>
377      * The <code>preferredSize</code> is only computed once, subsequent
378      * calls to this method just return a cached size.
379      *
380      * @param c the <code>JScrollBar</code> that's delegating this method to us
381      * @return the preferred size of a Basic JScrollBar
382      * @see #getMaximumSize
383      * @see #getMinimumSize
384      */

385     public Dimension getPreferredSize(JComponent c) {
386     return (scrollbar.getOrientation() == JScrollBar.VERTICAL)
387         ? new Dimension(scrollBarWidth, 48)
388         : new Dimension(48, scrollBarWidth);
389     }
390
391
392     /**
393      * @param c The JScrollBar that's delegating this method to us.
394      * @return new Dimension(Integer.MAX_VALUE, Integer.MAX_VALUE);
395      * @see #getMinimumSize
396      * @see #getPreferredSize
397      */

398     public Dimension getMaximumSize(JComponent c) {
399         return new Dimension(Integer.MAX_VALUE, Integer.MAX_VALUE);
400     }
401             
402     protected JButton createDecreaseButton(int orientation) {
403         return new BasicArrowButton JavaDoc(orientation,
404                     UIManager.getColor("ScrollBar.thumb"),
405                     UIManager.getColor("ScrollBar.thumbShadow"),
406                     UIManager.getColor("ScrollBar.thumbDarkShadow"),
407                     UIManager.getColor("ScrollBar.thumbHighlight"));
408     }
409
410     protected JButton createIncreaseButton(int orientation) {
411         return new BasicArrowButton JavaDoc(orientation,
412                     UIManager.getColor("ScrollBar.thumb"),
413                     UIManager.getColor("ScrollBar.thumbShadow"),
414                     UIManager.getColor("ScrollBar.thumbDarkShadow"),
415                     UIManager.getColor("ScrollBar.thumbHighlight"));
416     }
417                   
418
419     protected void paintDecreaseHighlight(Graphics g)
420     {
421     Insets insets = scrollbar.getInsets();
422     Rectangle thumbR = getThumbBounds();
423     g.setColor(trackHighlightColor);
424
425     if (scrollbar.getOrientation() == JScrollBar.VERTICAL) {
426         int x = insets.left;
427         int y = decrButton.getY() + decrButton.getHeight();
428         int w = scrollbar.getWidth() - (insets.left + insets.right);
429         int h = thumbR.y - y;
430         g.fillRect(x, y, w, h);
431     }
432     else {
433         int x, w;
434         if (scrollbar.getComponentOrientation().isLeftToRight()) {
435         x = decrButton.getX() + decrButton.getWidth();
436         w = thumbR.x - x;
437         } else {
438         x = thumbR.x + thumbR.width;
439         w = decrButton.getX() - x;
440         }
441         int y = insets.top;
442         int h = scrollbar.getHeight() - (insets.top + insets.bottom);
443         g.fillRect(x, y, w, h);
444     }
445     }
446     
447
448     protected void paintIncreaseHighlight(Graphics g)
449     {
450     Insets insets = scrollbar.getInsets();
451     Rectangle thumbR = getThumbBounds();
452     g.setColor(trackHighlightColor);
453
454     if (scrollbar.getOrientation() == JScrollBar.VERTICAL) {
455         int x = insets.left;
456         int y = thumbR.y + thumbR.height;
457         int w = scrollbar.getWidth() - (insets.left + insets.right);
458         int h = incrButton.getY() - y;
459         g.fillRect(x, y, w, h);
460     }
461     else {
462         int x, w;
463         if (scrollbar.getComponentOrientation().isLeftToRight()) {
464         x = thumbR.x + thumbR.width;
465         w = incrButton.getX() - x;
466         } else {
467         x = incrButton.getX() + incrButton.getWidth();
468         w = thumbR.x - x;
469         }
470         int y = insets.top;
471         int h = scrollbar.getHeight() - (insets.top + insets.bottom);
472         g.fillRect(x, y, w, h);
473     }
474     }
475
476
477     protected void paintTrack(Graphics g, JComponent c, Rectangle trackBounds)
478     {
479         g.setColor(trackColor);
480         g.fillRect(trackBounds.x, trackBounds.y, trackBounds.width, trackBounds.height);
481
482     if(trackHighlight == DECREASE_HIGHLIGHT) {
483         paintDecreaseHighlight(g);
484     }
485     else if(trackHighlight == INCREASE_HIGHLIGHT) {
486         paintIncreaseHighlight(g);
487     }
488     }
489
490     
491     protected void paintThumb(Graphics g, JComponent c, Rectangle thumbBounds)
492     {
493     if(thumbBounds.isEmpty() || !scrollbar.isEnabled()) {
494         return;
495     }
496
497         int w = thumbBounds.width;
498         int h = thumbBounds.height;
499
500     g.translate(thumbBounds.x, thumbBounds.y);
501
502     g.setColor(thumbDarkShadowColor);
503     g.drawRect(0, 0, w-1, h-1);
504     g.setColor(thumbColor);
505     g.fillRect(0, 0, w-1, h-1);
506         
507         g.setColor(thumbHighlightColor);
508         g.drawLine(1, 1, 1, h-2);
509         g.drawLine(2, 1, w-3, 1);
510         
511         g.setColor(thumbLightShadowColor);
512         g.drawLine(2, h-2, w-2, h-2);
513         g.drawLine(w-2, 1, w-2, h-3);
514
515     g.translate(-thumbBounds.x, -thumbBounds.y);
516     }
517
518
519     /**
520      * Return the smallest acceptable size for the thumb. If the scrollbar
521      * becomes so small that this size isn't available, the thumb will be
522      * hidden.
523      * <p>
524      * <b>Warning </b>: the value returned by this method should not be
525      * be modified, it's a shared static constant.
526      *
527      * @return The smallest acceptable size for the thumb.
528      * @see #getMaximumThumbSize
529      */

530     protected Dimension getMinimumThumbSize() {
531     return minimumThumbSize;
532     }
533
534     /**
535      * Return the largest acceptable size for the thumb. To create a fixed
536      * size thumb one make this method and <code>getMinimumThumbSize</code>
537      * return the same value.
538      * <p>
539      * <b>Warning </b>: the value returned by this method should not be
540      * be modified, it's a shared static constant.
541      *
542      * @return The largest acceptable size for the thumb.
543      * @see #getMinimumThumbSize
544      */

545     protected Dimension getMaximumThumbSize() {
546     return maximumThumbSize;
547     }
548
549
550     /*
551      * LayoutManager Implementation
552      */

553
554     public void addLayoutComponent(String JavaDoc name, Component child) {}
555     public void removeLayoutComponent(Component child) {}
556     
557     public Dimension preferredLayoutSize(Container scrollbarContainer) {
558         return getPreferredSize((JComponent)scrollbarContainer);
559     }
560     
561     public Dimension minimumLayoutSize(Container scrollbarContainer) {
562         return getMinimumSize((JComponent)scrollbarContainer);
563     }
564     
565     private int getValue(JScrollBar sb) {
566     return (useCachedValue) ? scrollBarValue : sb.getValue();
567     }
568
569     protected void layoutVScrollbar(JScrollBar sb)
570     {
571         Dimension sbSize = sb.getSize();
572         Insets sbInsets = sb.getInsets();
573
574     /*
575      * Width and left edge of the buttons and thumb.
576      */

577     int itemW = sbSize.width - (sbInsets.left + sbInsets.right);
578     int itemX = sbInsets.left;
579         
580         /* Nominal locations of the buttons, assuming their preferred
581      * size will fit.
582      */

583         boolean squareButtons = DefaultLookup.getBoolean(
584             scrollbar, this, "ScrollBar.squareButtons", false);
585         int decrButtonH = squareButtons ? itemW :
586                           decrButton.getPreferredSize().height;
587         int decrButtonY = sbInsets.top;
588         
589         int incrButtonH = squareButtons ? itemW :
590                           incrButton.getPreferredSize().height;
591         int incrButtonY = sbSize.height - (sbInsets.bottom + incrButtonH);
592         
593         /* The thumb must fit within the height left over after we
594      * subtract the preferredSize of the buttons and the insets.
595      */

596         int sbInsetsH = sbInsets.top + sbInsets.bottom;
597         int sbButtonsH = decrButtonH + incrButtonH;
598         float trackH = sbSize.height - (sbInsetsH + sbButtonsH);
599         
600         /* Compute the height and origin of the thumb. The case
601      * where the thumb is at the bottom edge is handled specially
602      * to avoid numerical problems in computing thumbY. Enforce
603      * the thumbs min/max dimensions. If the thumb doesn't
604      * fit in the track (trackH) we'll hide it later.
605      */

606     float min = sb.getMinimum();
607     float extent = sb.getVisibleAmount();
608     float range = sb.getMaximum() - min;
609     float value = getValue(sb);
610
611         int thumbH = (range <= 0)
612         ? getMaximumThumbSize().height : (int)(trackH * (extent / range));
613     thumbH = Math.max(thumbH, getMinimumThumbSize().height);
614     thumbH = Math.min(thumbH, getMaximumThumbSize().height);
615         
616     int thumbY = incrButtonY - thumbH;
617     if (value < (sb.getMaximum() - sb.getVisibleAmount())) {
618         float thumbRange = trackH - thumbH;
619         thumbY = (int)(0.5f + (thumbRange * ((value - min) / (range - extent))));
620         thumbY += decrButtonY + decrButtonH;
621     }
622
623         /* If the buttons don't fit, allocate half of the available
624      * space to each and move the lower one (incrButton) down.
625      */

626         int sbAvailButtonH = (sbSize.height - sbInsetsH);
627         if (sbAvailButtonH < sbButtonsH) {
628             incrButtonH = decrButtonH = sbAvailButtonH / 2;
629             incrButtonY = sbSize.height - (sbInsets.bottom + incrButtonH);
630         }
631         decrButton.setBounds(itemX, decrButtonY, itemW, decrButtonH);
632         incrButton.setBounds(itemX, incrButtonY, itemW, incrButtonH);
633
634     /* Update the trackRect field.
635      */

636     int itrackY = decrButtonY + decrButtonH;
637     int itrackH = incrButtonY - itrackY;
638     trackRect.setBounds(itemX, itrackY, itemW, itrackH);
639     
640     /* If the thumb isn't going to fit, zero it's bounds. Otherwise
641      * make sure it fits between the buttons. Note that setting the
642      * thumbs bounds will cause a repaint.
643      */

644     if(thumbH >= (int)trackH) {
645         setThumbBounds(0, 0, 0, 0);
646     }
647     else {
648         if ((thumbY + thumbH) > incrButtonY) {
649         thumbY = incrButtonY - thumbH;
650         }
651         if (thumbY < (decrButtonY + decrButtonH)) {
652         thumbY = decrButtonY + decrButtonH + 1;
653         }
654         setThumbBounds(itemX, thumbY, itemW, thumbH);
655     }
656     }
657     
658
659     protected void layoutHScrollbar(JScrollBar sb)
660     {
661         Dimension sbSize = sb.getSize();
662         Insets sbInsets = sb.getInsets();
663         
664     /* Height and top edge of the buttons and thumb.
665      */

666     int itemH = sbSize.height - (sbInsets.top + sbInsets.bottom);
667     int itemY = sbInsets.top;
668
669         boolean ltr = sb.getComponentOrientation().isLeftToRight();
670
671         /* Nominal locations of the buttons, assuming their preferred
672      * size will fit.
673      */

674         boolean squareButtons = DefaultLookup.getBoolean(
675             scrollbar, this, "ScrollBar.squareButtons", false);
676         int leftButtonW = squareButtons ? itemH :
677                           decrButton.getPreferredSize().width;
678         int rightButtonW = squareButtons ? itemH :
679                           incrButton.getPreferredSize().width;
680         if (!ltr) {
681             int temp = leftButtonW;
682             leftButtonW = rightButtonW;
683             rightButtonW = temp;
684         }
685         int leftButtonX = sbInsets.left;
686         int rightButtonX = sbSize.width - (sbInsets.right + rightButtonW);
687
688         /* The thumb must fit within the width left over after we
689      * subtract the preferredSize of the buttons and the insets.
690      */

691         int sbInsetsW = sbInsets.left + sbInsets.right;
692         int sbButtonsW = leftButtonW + rightButtonW;
693         float trackW = sbSize.width - (sbInsetsW + sbButtonsW);
694         
695         /* Compute the width and origin of the thumb. Enforce
696      * the thumbs min/max dimensions. The case where the thumb
697      * is at the right edge is handled specially to avoid numerical
698      * problems in computing thumbX. If the thumb doesn't
699      * fit in the track (trackH) we'll hide it later.
700      */

701         float min = sb.getMinimum();
702         float max = sb.getMaximum();
703         float extent = sb.getVisibleAmount();
704         float range = max - min;
705         float value = getValue(sb);
706
707         int thumbW = (range <= 0)
708         ? getMaximumThumbSize().width : (int)(trackW * (extent / range));
709         thumbW = Math.max(thumbW, getMinimumThumbSize().width);
710         thumbW = Math.min(thumbW, getMaximumThumbSize().width);
711         
712     int thumbX = ltr ? rightButtonX - thumbW : leftButtonX + leftButtonW;
713     if (value < (max - sb.getVisibleAmount())) {
714         float thumbRange = trackW - thumbW;
715             if( ltr ) {
716                 thumbX = (int)(0.5f + (thumbRange * ((value - min) / (range - extent))));
717             } else {
718                 thumbX = (int)(0.5f + (thumbRange * ((max - extent - value) / (range - extent))));
719             }
720         thumbX += leftButtonX + leftButtonW;
721     }
722
723         /* If the buttons don't fit, allocate half of the available
724          * space to each and move the right one over.
725          */

726         int sbAvailButtonW = (sbSize.width - sbInsetsW);
727         if (sbAvailButtonW < sbButtonsW) {
728             rightButtonW = leftButtonW = sbAvailButtonW / 2;
729             rightButtonX = sbSize.width - (sbInsets.right + rightButtonW);
730         }
731         
732         (ltr ? decrButton : incrButton).setBounds(leftButtonX, itemY, leftButtonW, itemH);
733         (ltr ? incrButton : decrButton).setBounds(rightButtonX, itemY, rightButtonW, itemH);
734
735     /* Update the trackRect field.
736      */

737     int itrackX = leftButtonX + leftButtonW;
738     int itrackW = rightButtonX - itrackX;
739     trackRect.setBounds(itrackX, itemY, itrackW, itemH);
740
741     /* Make sure the thumb fits between the buttons. Note
742      * that setting the thumbs bounds causes a repaint.
743      */

744     if (thumbW >= (int)trackW) {
745         setThumbBounds(0, 0, 0, 0);
746     }
747     else {
748         if (thumbX + thumbW > rightButtonX) {
749         thumbX = rightButtonX - thumbW;
750         }
751         if (thumbX < leftButtonX + leftButtonW) {
752         thumbX = leftButtonX + leftButtonW + 1;
753         }
754         setThumbBounds(thumbX, itemY, thumbW, itemH);
755     }
756     }
757
758     public void layoutContainer(Container scrollbarContainer)
759     {
760     /* If the user is dragging the value, we'll assume that the
761      * scrollbars layout is OK modulo the thumb which is being
762      * handled by the dragging code.
763      */

764     if (isDragging) {
765         return;
766     }
767
768         JScrollBar scrollbar = (JScrollBar)scrollbarContainer;
769         switch (scrollbar.getOrientation()) {
770         case JScrollBar.VERTICAL:
771             layoutVScrollbar(scrollbar);
772             break;
773             
774         case JScrollBar.HORIZONTAL:
775             layoutHScrollbar(scrollbar);
776             break;
777         }
778     }
779
780
781     /**
782      * Set the bounds of the thumb and force a repaint that includes
783      * the old thumbBounds and the new one.
784      *
785      * @see #getThumbBounds
786      */

787     protected void setThumbBounds(int x, int y, int width, int height)
788     {
789     /* If the thumbs bounds haven't changed, we're done.
790      */

791     if ((thumbRect.x == x) &&
792         (thumbRect.y == y) &&
793         (thumbRect.width == width) &&
794         (thumbRect.height == height)) {
795         return;
796     }
797
798     /* Update thumbRect, and repaint the union of x,y,w,h and
799      * the old thumbRect.
800      */

801     int minX = Math.min(x, thumbRect.x);
802     int minY = Math.min(y, thumbRect.y);
803     int maxX = Math.max(x + width, thumbRect.x + thumbRect.width);
804     int maxY = Math.max(y + height, thumbRect.y + thumbRect.height);
805
806     thumbRect.setBounds(x, y, width, height);
807     scrollbar.repaint(minX, minY, maxX - minX, maxY - minY);
808
809         // Once there is API to determine the mouse location this will need
810
// to be changed.
811
setThumbRollover(false);
812     }
813
814
815     /**
816      * Return the current size/location of the thumb.
817      * <p>
818      * <b>Warning </b>: the value returned by this method should not be
819      * be modified, it's a reference to the actual rectangle, not a copy.
820      *
821      * @return The current size/location of the thumb.
822      * @see #setThumbBounds
823      */

824     protected Rectangle getThumbBounds() {
825     return thumbRect;
826     }
827
828
829     /**
830      * Returns the current bounds of the track, i.e. the space in between
831      * the increment and decrement buttons, less the insets. The value
832      * returned by this method is updated each time the scrollbar is
833      * laid out (validated).
834      * <p>
835      * <b>Warning </b>: the value returned by this method should not be
836      * be modified, it's a reference to the actual rectangle, not a copy.
837      *
838      * @return the current bounds of the scrollbar track
839      * @see #layoutContainer
840      */

841     protected Rectangle getTrackBounds() {
842     return trackRect;
843     }
844
845     /*
846      * Method for scrolling by a block increment.
847      * Added for mouse wheel scrolling support, RFE 4202656.
848      */

849     static void scrollByBlock(JScrollBar scrollbar, int direction) {
850         // This method is called from BasicScrollPaneUI to implement wheel
851
// scrolling, and also from scrollByBlock().
852
int oldValue = scrollbar.getValue();
853         int blockIncrement = scrollbar.getBlockIncrement(direction);
854         int delta = blockIncrement * ((direction > 0) ? +1 : -1);
855         int newValue = oldValue + delta;
856         
857         // Check for overflow.
858
if (delta > 0 && newValue < oldValue) {
859         newValue = scrollbar.getMaximum();
860         }
861         else if (delta < 0 && newValue > oldValue) {
862         newValue = scrollbar.getMinimum();
863         }
864
865         scrollbar.setValue(newValue);
866     }
867
868     protected void scrollByBlock(int direction)
869     {
870         scrollByBlock(scrollbar, direction);
871         trackHighlight = direction > 0 ? INCREASE_HIGHLIGHT : DECREASE_HIGHLIGHT;
872         Rectangle dirtyRect = getTrackBounds();
873         scrollbar.repaint(dirtyRect.x, dirtyRect.y, dirtyRect.width, dirtyRect.height);
874     }
875     
876     /*
877      * Method for scrolling by a unit increment.
878      * Added for mouse wheel scrolling support, RFE 4202656.
879      */

880     static void scrollByUnits(JScrollBar scrollbar, int direction,
881                                    int units) {
882         // This method is called from BasicScrollPaneUI to implement wheel
883
// scrolling, as well as from scrollByUnit().
884
int delta;
885
886     for (int i=0; i<units; i++) {
887         if (direction > 0) {
888         delta = scrollbar.getUnitIncrement(direction);
889         }
890         else {
891         delta = -scrollbar.getUnitIncrement(direction);
892         }
893
894         int oldValue = scrollbar.getValue();
895         int newValue = oldValue + delta;
896         
897         // Check for overflow.
898
if (delta > 0 && newValue < oldValue) {
899         newValue = scrollbar.getMaximum();
900         }
901         else if (delta < 0 && newValue > oldValue) {
902         newValue = scrollbar.getMinimum();
903         }
904         if (oldValue == newValue) {
905         break;
906         }
907         scrollbar.setValue(newValue);
908     }
909     }
910
911     protected void scrollByUnit(int direction) {
912         scrollByUnits(scrollbar, direction, 1);
913     }
914
915     /**
916      * Indicates whether the user can absolutely position the thumb with
917      * a mouse gesture (usually the middle mouse button).
918      *
919      * @return true if a mouse gesture can absolutely position the thumb
920      * @since 1.5
921      */

922     public boolean getSupportsAbsolutePositioning() {
923     return supportsAbsolutePositioning;
924     }
925
926     /**
927      * A listener to listen for model changes.
928      *
929      */

930     protected class ModelListener implements ChangeListener {
931         public void stateChanged(ChangeEvent e) {
932         if (!useCachedValue) {
933         scrollBarValue = scrollbar.getValue();
934         }
935         layoutContainer(scrollbar);
936         useCachedValue = false;
937         }
938     }
939
940
941     /**
942      * Track mouse drags.
943      */

944     protected class TrackListener
945         extends MouseAdapter implements MouseMotionListener
946     {
947     protected transient int offset;
948     protected transient int currentMouseX, currentMouseY;
949     private transient int direction = +1;
950         
951         public void mouseReleased(MouseEvent e)
952         {
953             if (isDragging) {
954                 updateThumbState(e.getX(), e.getY());
955             }
956         if (SwingUtilities.isRightMouseButton(e) ||
957         (!getSupportsAbsolutePositioning() &&
958          SwingUtilities.isMiddleMouseButton(e)))
959         return;
960         if(!scrollbar.isEnabled())
961         return;
962
963         Rectangle r = getTrackBounds();
964         scrollbar.repaint(r.x, r.y, r.width, r.height);
965
966         trackHighlight = NO_HIGHLIGHT;
967         isDragging = false;
968         offset = 0;
969         scrollTimer.stop();
970         useCachedValue = true;
971         scrollbar.setValueIsAdjusting(false);
972     }
973         
974
975         /**
976      * If the mouse is pressed above the "thumb" component
977      * then reduce the scrollbars value by one page ("page up"),
978      * otherwise increase it by one page. If there is no
979      * thumb then page up if the mouse is in the upper half
980      * of the track.
981      */

982         public void mousePressed(MouseEvent e)
983     {
984         if (SwingUtilities.isRightMouseButton(e) ||
985         (!getSupportsAbsolutePositioning() &&
986          SwingUtilities.isMiddleMouseButton(e)))
987         return;
988         if(!scrollbar.isEnabled())
989         return;
990
991         if (!scrollbar.hasFocus() && scrollbar.isRequestFocusEnabled()) {
992         scrollbar.requestFocus();
993         }
994
995         useCachedValue = true;
996         scrollbar.setValueIsAdjusting(true);
997
998             currentMouseX = e.getX();
999             currentMouseY = e.getY();
1000    
1001        // Clicked in the Thumb area?
1002
if(getThumbBounds().contains(currentMouseX, currentMouseY)) {
1003                switch (scrollbar.getOrientation()) {
1004                case JScrollBar.VERTICAL:
1005            offset = currentMouseY - getThumbBounds().y;
1006                    break;
1007                case JScrollBar.HORIZONTAL:
1008            offset = currentMouseX - getThumbBounds().x;
1009                    break;
1010                }
1011        isDragging = true;
1012   &n