jdk/src/share/classes/javax/swing/plaf/basic/BasicScrollBarUI.java
author ohair
Tue, 25 May 2010 15:58:33 -0700
changeset 5506 202f599c92aa
parent 4394 92a8ec883f5d
child 6652 482a7a13ef22
permissions -rw-r--r--
6943119: Rebrand source copyright notices Reviewed-by: darcy, weijun
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4394
diff changeset
     2
 * Copyright (c) 1997, 2006, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4394
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4394
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4394
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4394
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4394
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
package javax.swing.plaf.basic;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import sun.swing.DefaultLookup;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import sun.swing.UIAction;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.awt.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.awt.event.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.beans.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import javax.swing.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import javax.swing.event.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import javax.swing.plaf.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * Implementation of ScrollBarUI for the Basic Look and Feel
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * @author Rich Schiavi
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * @author David Kloba
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * @author Hans Muller
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
public class BasicScrollBarUI
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    extends ScrollBarUI implements LayoutManager, SwingConstants
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    private static final int POSITIVE_SCROLL = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    private static final int NEGATIVE_SCROLL = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    private static final int MIN_SCROLL = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    private static final int MAX_SCROLL = 3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    // NOTE: DO NOT use this field directly, SynthScrollBarUI assumes you'll
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    // call getMinimumThumbSize to access it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    protected Dimension minimumThumbSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    protected Dimension maximumThumbSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    protected Color thumbHighlightColor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    protected Color thumbLightShadowColor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    protected Color thumbDarkShadowColor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    protected Color thumbColor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    protected Color trackColor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    protected Color trackHighlightColor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    protected JScrollBar scrollbar;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    protected JButton incrButton;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    protected JButton decrButton;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    protected boolean isDragging;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    protected TrackListener trackListener;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    protected ArrowButtonListener buttonListener;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    protected ModelListener modelListener;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    protected Rectangle thumbRect;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    protected Rectangle trackRect;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    protected int trackHighlight;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    protected static final int NO_HIGHLIGHT = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    protected static final int DECREASE_HIGHLIGHT = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    protected static final int INCREASE_HIGHLIGHT = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    protected ScrollListener scrollListener;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    protected PropertyChangeListener propertyChangeListener;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    protected Timer scrollTimer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    private final static int scrollSpeedThrottle = 60; // delay in milli seconds
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    /** True indicates a middle click will absolutely position the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     * scrollbar. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    private boolean supportsAbsolutePositioning;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
4394
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 2658
diff changeset
    96
    /**
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 2658
diff changeset
    97
     * Hint as to what width (when vertical) or height (when horizontal)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     * should be.
4394
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 2658
diff changeset
    99
     *
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 2658
diff changeset
   100
     * @since 1.7
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     */
4394
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 2658
diff changeset
   102
    protected int scrollBarWidth;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    private Handler handler;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    private boolean thumbActive;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     * Determine whether scrollbar layout should use cached value or adjusted
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     * value returned by scrollbar's <code>getValue</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    private boolean useCachedValue = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     * The scrollbar value is cached to save real value if the view is adjusted.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    private int scrollBarValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
2658
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2
diff changeset
   118
    /**
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2
diff changeset
   119
     * Distance between the increment button and the track. This may be a negative
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2
diff changeset
   120
     * number. If negative, then an overlap between the button and track will occur,
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2
diff changeset
   121
     * which is useful for shaped buttons.
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2
diff changeset
   122
     *
4394
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 2658
diff changeset
   123
     * @since 1.7
2658
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2
diff changeset
   124
     */
4394
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 2658
diff changeset
   125
    protected int incrGap;
2658
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2
diff changeset
   126
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2
diff changeset
   127
    /**
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2
diff changeset
   128
     * Distance between the decrement button and the track. This may be a negative
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2
diff changeset
   129
     * number. If negative, then an overlap between the button and track will occur,
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2
diff changeset
   130
     * which is useful for shaped buttons.
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2
diff changeset
   131
     *
4394
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 2658
diff changeset
   132
     * @since 1.7
2658
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2
diff changeset
   133
     */
4394
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 2658
diff changeset
   134
    protected int decrGap;
2658
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2
diff changeset
   135
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    static void loadActionMap(LazyActionMap map) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        map.put(new Actions(Actions.POSITIVE_UNIT_INCREMENT));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        map.put(new Actions(Actions.POSITIVE_BLOCK_INCREMENT));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        map.put(new Actions(Actions.NEGATIVE_UNIT_INCREMENT));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        map.put(new Actions(Actions.NEGATIVE_BLOCK_INCREMENT));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        map.put(new Actions(Actions.MIN_SCROLL));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        map.put(new Actions(Actions.MAX_SCROLL));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
    public static ComponentUI createUI(JComponent c)    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        return new BasicScrollBarUI();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    protected void configureScrollBarColors()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        LookAndFeel.installColors(scrollbar, "ScrollBar.background",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
                                  "ScrollBar.foreground");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        thumbHighlightColor = UIManager.getColor("ScrollBar.thumbHighlight");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        thumbLightShadowColor = UIManager.getColor("ScrollBar.thumbShadow");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        thumbDarkShadowColor = UIManager.getColor("ScrollBar.thumbDarkShadow");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        thumbColor = UIManager.getColor("ScrollBar.thumb");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        trackColor = UIManager.getColor("ScrollBar.track");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        trackHighlightColor = UIManager.getColor("ScrollBar.trackHighlight");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
    public void installUI(JComponent c)   {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        scrollbar = (JScrollBar)c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        thumbRect = new Rectangle(0, 0, 0, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        trackRect = new Rectangle(0, 0, 0, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        installDefaults();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        installComponents();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        installListeners();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        installKeyboardActions();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
    public void uninstallUI(JComponent c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        scrollbar = (JScrollBar)c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        uninstallListeners();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        uninstallDefaults();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        uninstallComponents();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        uninstallKeyboardActions();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        thumbRect = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        scrollbar = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        incrButton = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        decrButton = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
    protected void installDefaults()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        scrollBarWidth = UIManager.getInt("ScrollBar.width");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        if (scrollBarWidth <= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
            scrollBarWidth = 16;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        minimumThumbSize = (Dimension)UIManager.get("ScrollBar.minimumThumbSize");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        maximumThumbSize = (Dimension)UIManager.get("ScrollBar.maximumThumbSize");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        Boolean absB = (Boolean)UIManager.get("ScrollBar.allowsAbsolutePositioning");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        supportsAbsolutePositioning = (absB != null) ? absB.booleanValue() :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
                                      false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        trackHighlight = NO_HIGHLIGHT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        if (scrollbar.getLayout() == null ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
                     (scrollbar.getLayout() instanceof UIResource)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
            scrollbar.setLayout(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        configureScrollBarColors();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        LookAndFeel.installBorder(scrollbar, "ScrollBar.border");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
        LookAndFeel.installProperty(scrollbar, "opaque", Boolean.TRUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        scrollBarValue = scrollbar.getValue();
2658
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2
diff changeset
   210
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2
diff changeset
   211
        incrGap = UIManager.getInt("ScrollBar.incrementButtonGap");
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2
diff changeset
   212
        decrGap = UIManager.getInt("ScrollBar.decrementButtonGap");
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2
diff changeset
   213
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2
diff changeset
   214
        // TODO this can be removed when incrGap/decrGap become protected
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2
diff changeset
   215
        // handle scaling for sizeVarients for special case components. The
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2
diff changeset
   216
        // key "JComponent.sizeVariant" scales for large/small/mini
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2
diff changeset
   217
        // components are based on Apples LAF
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2
diff changeset
   218
        String scaleKey = (String)scrollbar.getClientProperty(
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2
diff changeset
   219
                "JComponent.sizeVariant");
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2
diff changeset
   220
        if (scaleKey != null){
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2
diff changeset
   221
            if ("large".equals(scaleKey)){
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2
diff changeset
   222
                scrollBarWidth *= 1.15;
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2
diff changeset
   223
                incrGap *= 1.15;
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2
diff changeset
   224
                decrGap *= 1.15;
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2
diff changeset
   225
            } else if ("small".equals(scaleKey)){
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2
diff changeset
   226
                scrollBarWidth *= 0.857;
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2
diff changeset
   227
                incrGap *= 0.857;
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2
diff changeset
   228
                decrGap *= 0.714;
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2
diff changeset
   229
            } else if ("mini".equals(scaleKey)){
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2
diff changeset
   230
                scrollBarWidth *= 0.714;
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2
diff changeset
   231
                incrGap *= 0.714;
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2
diff changeset
   232
                decrGap *= 0.714;
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2
diff changeset
   233
            }
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2
diff changeset
   234
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
    protected void installComponents(){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
        switch (scrollbar.getOrientation()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
        case JScrollBar.VERTICAL:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
            incrButton = createIncreaseButton(SOUTH);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
            decrButton = createDecreaseButton(NORTH);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
        case JScrollBar.HORIZONTAL:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
            if (scrollbar.getComponentOrientation().isLeftToRight()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
                incrButton = createIncreaseButton(EAST);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
                decrButton = createDecreaseButton(WEST);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
                incrButton = createIncreaseButton(WEST);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
                decrButton = createDecreaseButton(EAST);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
        scrollbar.add(incrButton);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
        scrollbar.add(decrButton);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
        // Force the children's enabled state to be updated.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
        scrollbar.setEnabled(scrollbar.isEnabled());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
    protected void uninstallComponents(){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
        scrollbar.remove(incrButton);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
        scrollbar.remove(decrButton);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
    protected void installListeners(){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
        trackListener = createTrackListener();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
        buttonListener = createArrowButtonListener();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
        modelListener = createModelListener();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
        propertyChangeListener = createPropertyChangeListener();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
        scrollbar.addMouseListener(trackListener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
        scrollbar.addMouseMotionListener(trackListener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
        scrollbar.getModel().addChangeListener(modelListener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
        scrollbar.addPropertyChangeListener(propertyChangeListener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
        scrollbar.addFocusListener(getHandler());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
        if (incrButton != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
            incrButton.addMouseListener(buttonListener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
        if (decrButton != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
            decrButton.addMouseListener(buttonListener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
        scrollListener = createScrollListener();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
        scrollTimer = new Timer(scrollSpeedThrottle, scrollListener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
        scrollTimer.setInitialDelay(300);  // default InitialDelay?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
    protected void installKeyboardActions(){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
        LazyActionMap.installLazyActionMap(scrollbar, BasicScrollBarUI.class,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
                                           "ScrollBar.actionMap");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
        InputMap inputMap = getInputMap(JComponent.WHEN_FOCUSED);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
        SwingUtilities.replaceUIInputMap(scrollbar, JComponent.WHEN_FOCUSED,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
                                         inputMap);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
        inputMap = getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
        SwingUtilities.replaceUIInputMap(scrollbar,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
                   JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT, inputMap);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
    protected void uninstallKeyboardActions(){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
        SwingUtilities.replaceUIInputMap(scrollbar, JComponent.WHEN_FOCUSED,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
                                         null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
        SwingUtilities.replaceUIActionMap(scrollbar, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
    private InputMap getInputMap(int condition) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
        if (condition == JComponent.WHEN_FOCUSED) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
            InputMap keyMap = (InputMap)DefaultLookup.get(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
                        scrollbar, this, "ScrollBar.focusInputMap");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
            InputMap rtlKeyMap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
            if (scrollbar.getComponentOrientation().isLeftToRight() ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
                ((rtlKeyMap = (InputMap)DefaultLookup.get(scrollbar, this, "ScrollBar.focusInputMap.RightToLeft")) == null)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
                return keyMap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
                rtlKeyMap.setParent(keyMap);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
                return rtlKeyMap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
        else if (condition == JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
            InputMap keyMap = (InputMap)DefaultLookup.get(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
                        scrollbar, this, "ScrollBar.ancestorInputMap");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
            InputMap rtlKeyMap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
            if (scrollbar.getComponentOrientation().isLeftToRight() ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
                ((rtlKeyMap = (InputMap)DefaultLookup.get(scrollbar, this, "ScrollBar.ancestorInputMap.RightToLeft")) == null)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
                return keyMap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
                rtlKeyMap.setParent(keyMap);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
                return rtlKeyMap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
    protected void uninstallListeners() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
        scrollTimer.stop();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
        scrollTimer = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
        if (decrButton != null){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
            decrButton.removeMouseListener(buttonListener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
        if (incrButton != null){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
            incrButton.removeMouseListener(buttonListener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
        scrollbar.getModel().removeChangeListener(modelListener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
        scrollbar.removeMouseListener(trackListener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
        scrollbar.removeMouseMotionListener(trackListener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
        scrollbar.removePropertyChangeListener(propertyChangeListener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
        scrollbar.removeFocusListener(getHandler());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
        handler = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
    protected void uninstallDefaults(){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
        LookAndFeel.uninstallBorder(scrollbar);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
        if (scrollbar.getLayout() == this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
            scrollbar.setLayout(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
    private Handler getHandler() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
        if (handler == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
            handler = new Handler();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
        return handler;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
    protected TrackListener createTrackListener(){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
        return new TrackListener();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
    protected ArrowButtonListener createArrowButtonListener(){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
        return new ArrowButtonListener();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
    protected ModelListener createModelListener(){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
        return new ModelListener();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
    protected ScrollListener createScrollListener(){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
        return new ScrollListener();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
    protected PropertyChangeListener createPropertyChangeListener() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
        return getHandler();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
    private void updateThumbState(int x, int y) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
        Rectangle rect = getThumbBounds();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
        setThumbRollover(rect.contains(x, y));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
     * Sets whether or not the mouse is currently over the thumb.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
     * @param active True indicates the thumb is currently active.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
    protected void setThumbRollover(boolean active) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
        if (thumbActive != active) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
            thumbActive = active;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
            scrollbar.repaint(getThumbBounds());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
     * Returns true if the mouse is currently over the thumb.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
     * @return true if the thumb is currently active
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
    public boolean isThumbRollover() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
        return thumbActive;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
    public void paint(Graphics g, JComponent c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
        paintTrack(g, c, getTrackBounds());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
        Rectangle thumbBounds = getThumbBounds();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
        if (thumbBounds.intersects(g.getClipBounds())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
            paintThumb(g, c, thumbBounds);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
     * A vertical scrollbar's preferred width is the maximum of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
     * preferred widths of the (non <code>null</code>)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
     * increment/decrement buttons,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
     * and the minimum width of the thumb. The preferred height is the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
     * sum of the preferred heights of the same parts.  The basis for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
     * the preferred size of a horizontal scrollbar is similar.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
     * The <code>preferredSize</code> is only computed once, subsequent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
     * calls to this method just return a cached size.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
     * @param c the <code>JScrollBar</code> that's delegating this method to us
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
     * @return the preferred size of a Basic JScrollBar
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
     * @see #getMaximumSize
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
     * @see #getMinimumSize
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
    public Dimension getPreferredSize(JComponent c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
        return (scrollbar.getOrientation() == JScrollBar.VERTICAL)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
            ? new Dimension(scrollBarWidth, 48)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
            : new Dimension(48, scrollBarWidth);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
     * @param c The JScrollBar that's delegating this method to us.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
     * @return new Dimension(Integer.MAX_VALUE, Integer.MAX_VALUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
     * @see #getMinimumSize
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
     * @see #getPreferredSize
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
    public Dimension getMaximumSize(JComponent c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
        return new Dimension(Integer.MAX_VALUE, Integer.MAX_VALUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
    protected JButton createDecreaseButton(int orientation)  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
        return new BasicArrowButton(orientation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
                                    UIManager.getColor("ScrollBar.thumb"),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
                                    UIManager.getColor("ScrollBar.thumbShadow"),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
                                    UIManager.getColor("ScrollBar.thumbDarkShadow"),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
                                    UIManager.getColor("ScrollBar.thumbHighlight"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
    protected JButton createIncreaseButton(int orientation)  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
        return new BasicArrowButton(orientation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
                                    UIManager.getColor("ScrollBar.thumb"),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
                                    UIManager.getColor("ScrollBar.thumbShadow"),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
                                    UIManager.getColor("ScrollBar.thumbDarkShadow"),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
                                    UIManager.getColor("ScrollBar.thumbHighlight"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
    protected void paintDecreaseHighlight(Graphics g)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
        Insets insets = scrollbar.getInsets();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
        Rectangle thumbR = getThumbBounds();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
        g.setColor(trackHighlightColor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
        if (scrollbar.getOrientation() == JScrollBar.VERTICAL) {
2658
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2
diff changeset
   491
            //paint the distance between the start of the track and top of the thumb
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
            int x = insets.left;
2658
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2
diff changeset
   493
            int y = trackRect.y;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
            int w = scrollbar.getWidth() - (insets.left + insets.right);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
            int h = thumbR.y - y;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
            g.fillRect(x, y, w, h);
2658
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2
diff changeset
   497
        } else {
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2
diff changeset
   498
            //if left-to-right, fill the area between the start of the track and
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2
diff changeset
   499
            //the left edge of the thumb. If right-to-left, fill the area between
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2
diff changeset
   500
            //the end of the thumb and end of the track.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
            int x, w;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
            if (scrollbar.getComponentOrientation().isLeftToRight()) {
2658
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2
diff changeset
   503
               x = trackRect.x;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
                w = thumbR.x - x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
                x = thumbR.x + thumbR.width;
2658
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2
diff changeset
   507
                w = trackRect.x + trackRect.width - x;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
            int y = insets.top;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
            int h = scrollbar.getHeight() - (insets.top + insets.bottom);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
            g.fillRect(x, y, w, h);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
    protected void paintIncreaseHighlight(Graphics g)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
        Insets insets = scrollbar.getInsets();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
        Rectangle thumbR = getThumbBounds();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
        g.setColor(trackHighlightColor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
        if (scrollbar.getOrientation() == JScrollBar.VERTICAL) {
2658
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2
diff changeset
   523
            //fill the area between the bottom of the thumb and the end of the track.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
            int x = insets.left;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
            int y = thumbR.y + thumbR.height;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
            int w = scrollbar.getWidth() - (insets.left + insets.right);
2658
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2
diff changeset
   527
            int h = trackRect.y + trackRect.height - y;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
            g.fillRect(x, y, w, h);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
        else {
2658
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2
diff changeset
   531
            //if left-to-right, fill the area between the right of the thumb and the
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2
diff changeset
   532
            //end of the track. If right-to-left, then fill the area to the left of
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2
diff changeset
   533
            //the thumb and the start of the track.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
            int x, w;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
            if (scrollbar.getComponentOrientation().isLeftToRight()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
                x = thumbR.x + thumbR.width;
2658
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2
diff changeset
   537
                w = trackRect.x + trackRect.width - x;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
            } else {
2658
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2
diff changeset
   539
                x = trackRect.x;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
                w = thumbR.x - x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
            int y = insets.top;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
            int h = scrollbar.getHeight() - (insets.top + insets.bottom);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
            g.fillRect(x, y, w, h);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
    protected void paintTrack(Graphics g, JComponent c, Rectangle trackBounds)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
        g.setColor(trackColor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
        g.fillRect(trackBounds.x, trackBounds.y, trackBounds.width, trackBounds.height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
        if(trackHighlight == DECREASE_HIGHLIGHT)        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
            paintDecreaseHighlight(g);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
        else if(trackHighlight == INCREASE_HIGHLIGHT)           {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
            paintIncreaseHighlight(g);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
    protected void paintThumb(Graphics g, JComponent c, Rectangle thumbBounds)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
        if(thumbBounds.isEmpty() || !scrollbar.isEnabled())     {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
        int w = thumbBounds.width;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
        int h = thumbBounds.height;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
        g.translate(thumbBounds.x, thumbBounds.y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
        g.setColor(thumbDarkShadowColor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
        g.drawRect(0, 0, w-1, h-1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
        g.setColor(thumbColor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
        g.fillRect(0, 0, w-1, h-1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
        g.setColor(thumbHighlightColor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
        g.drawLine(1, 1, 1, h-2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
        g.drawLine(2, 1, w-3, 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
        g.setColor(thumbLightShadowColor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
        g.drawLine(2, h-2, w-2, h-2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
        g.drawLine(w-2, 1, w-2, h-3);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
        g.translate(-thumbBounds.x, -thumbBounds.y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
    /**
4394
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 2658
diff changeset
   592
     * Returns the smallest acceptable size for the thumb.  If the scrollbar
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
     * becomes so small that this size isn't available, the thumb will be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
     * hidden.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
     * <b>Warning </b>: the value returned by this method should not be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
     * be modified, it's a shared static constant.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
     * @return The smallest acceptable size for the thumb.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
     * @see #getMaximumThumbSize
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
    protected Dimension getMinimumThumbSize() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
        return minimumThumbSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
    /**
4394
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 2658
diff changeset
   607
     * Returns the largest acceptable size for the thumb.  To create a fixed
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
     * size thumb one make this method and <code>getMinimumThumbSize</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
     * return the same value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
     * <b>Warning </b>: the value returned by this method should not be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
     * be modified, it's a shared static constant.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
     * @return The largest acceptable size for the thumb.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
     * @see #getMinimumThumbSize
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
    protected Dimension getMaximumThumbSize()   {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
        return maximumThumbSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
     * LayoutManager Implementation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
    public void addLayoutComponent(String name, Component child) {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
    public void removeLayoutComponent(Component child) {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
    public Dimension preferredLayoutSize(Container scrollbarContainer)  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
        return getPreferredSize((JComponent)scrollbarContainer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
    public Dimension minimumLayoutSize(Container scrollbarContainer) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
        return getMinimumSize((JComponent)scrollbarContainer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
    private int getValue(JScrollBar sb) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
        return (useCachedValue) ? scrollBarValue : sb.getValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
    protected void layoutVScrollbar(JScrollBar sb)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
        Dimension sbSize = sb.getSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
        Insets sbInsets = sb.getInsets();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
         * Width and left edge of the buttons and thumb.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
        int itemW = sbSize.width - (sbInsets.left + sbInsets.right);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
        int itemX = sbInsets.left;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
        /* Nominal locations of the buttons, assuming their preferred
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
         * size will fit.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
        boolean squareButtons = DefaultLookup.getBoolean(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
            scrollbar, this, "ScrollBar.squareButtons", false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
        int decrButtonH = squareButtons ? itemW :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
                          decrButton.getPreferredSize().height;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
        int decrButtonY = sbInsets.top;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
        int incrButtonH = squareButtons ? itemW :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
                          incrButton.getPreferredSize().height;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
        int incrButtonY = sbSize.height - (sbInsets.bottom + incrButtonH);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
        /* The thumb must fit within the height left over after we
2658
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2
diff changeset
   666
         * subtract the preferredSize of the buttons and the insets
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2
diff changeset
   667
         * and the gaps
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
        int sbInsetsH = sbInsets.top + sbInsets.bottom;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
        int sbButtonsH = decrButtonH + incrButtonH;
2658
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2
diff changeset
   671
        int gaps = decrGap + incrGap;
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2
diff changeset
   672
        float trackH = sbSize.height - (sbInsetsH + sbButtonsH) - gaps;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
        /* Compute the height and origin of the thumb.   The case
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
         * where the thumb is at the bottom edge is handled specially
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
         * to avoid numerical problems in computing thumbY.  Enforce
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
         * the thumbs min/max dimensions.  If the thumb doesn't
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
         * fit in the track (trackH) we'll hide it later.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
        float min = sb.getMinimum();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
        float extent = sb.getVisibleAmount();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
        float range = sb.getMaximum() - min;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
        float value = getValue(sb);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
        int thumbH = (range <= 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
            ? getMaximumThumbSize().height : (int)(trackH * (extent / range));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
        thumbH = Math.max(thumbH, getMinimumThumbSize().height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
        thumbH = Math.min(thumbH, getMaximumThumbSize().height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
2658
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2
diff changeset
   690
        int thumbY = incrButtonY - incrGap - thumbH;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
        if (value < (sb.getMaximum() - sb.getVisibleAmount())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
            float thumbRange = trackH - thumbH;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
            thumbY = (int)(0.5f + (thumbRange * ((value - min) / (range - extent))));
2658
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2
diff changeset
   694
            thumbY +=  decrButtonY + decrButtonH + decrGap;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
        /* If the buttons don't fit, allocate half of the available
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
         * space to each and move the lower one (incrButton) down.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
        int sbAvailButtonH = (sbSize.height - sbInsetsH);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
        if (sbAvailButtonH < sbButtonsH) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
            incrButtonH = decrButtonH = sbAvailButtonH / 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
            incrButtonY = sbSize.height - (sbInsets.bottom + incrButtonH);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
        decrButton.setBounds(itemX, decrButtonY, itemW, decrButtonH);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
        incrButton.setBounds(itemX, incrButtonY, itemW, incrButtonH);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
        /* Update the trackRect field.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
         */
2658
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2
diff changeset
   710
        int itrackY = decrButtonY + decrButtonH + decrGap;
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2
diff changeset
   711
        int itrackH = incrButtonY - incrGap - itrackY;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
        trackRect.setBounds(itemX, itrackY, itemW, itrackH);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
        /* If the thumb isn't going to fit, zero it's bounds.  Otherwise
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
         * make sure it fits between the buttons.  Note that setting the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
         * thumbs bounds will cause a repaint.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
        if(thumbH >= (int)trackH)       {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
            if (UIManager.getBoolean("ScrollBar.alwaysShowThumb")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
                // This is used primarily for GTK L&F, which expands the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
                // thumb to fit the track when it would otherwise be hidden.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
                setThumbBounds(itemX, itrackY, itemW, itrackH);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
                // Other L&F's simply hide the thumb in this case.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
                setThumbBounds(0, 0, 0, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
        else {
2658
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2
diff changeset
   729
            if ((thumbY + thumbH) > incrButtonY - incrGap) {
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2
diff changeset
   730
                thumbY = incrButtonY - incrGap - thumbH;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
            }
2658
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2
diff changeset
   732
            if (thumbY  < (decrButtonY + decrButtonH + decrGap)) {
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2
diff changeset
   733
                thumbY = decrButtonY + decrButtonH + decrGap + 1;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
            setThumbBounds(itemX, thumbY, itemW, thumbH);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
    protected void layoutHScrollbar(JScrollBar sb)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
        Dimension sbSize = sb.getSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
        Insets sbInsets = sb.getInsets();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
        /* Height and top edge of the buttons and thumb.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
        int itemH = sbSize.height - (sbInsets.top + sbInsets.bottom);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
        int itemY = sbInsets.top;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
        boolean ltr = sb.getComponentOrientation().isLeftToRight();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
        /* Nominal locations of the buttons, assuming their preferred
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
         * size will fit.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
        boolean squareButtons = DefaultLookup.getBoolean(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
            scrollbar, this, "ScrollBar.squareButtons", false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
        int leftButtonW = squareButtons ? itemH :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
                          decrButton.getPreferredSize().width;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
        int rightButtonW = squareButtons ? itemH :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
                          incrButton.getPreferredSize().width;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
        if (!ltr) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
            int temp = leftButtonW;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
            leftButtonW = rightButtonW;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
            rightButtonW = temp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
        int leftButtonX = sbInsets.left;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
        int rightButtonX = sbSize.width - (sbInsets.right + rightButtonW);
2658
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2
diff changeset
   768
        int leftGap = ltr ? decrGap : incrGap;
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2
diff changeset
   769
        int rightGap = ltr ? incrGap : decrGap;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
        /* The thumb must fit within the width left over after we
2658
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2
diff changeset
   772
         * subtract the preferredSize of the buttons and the insets
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2
diff changeset
   773
         * and the gaps
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
        int sbInsetsW = sbInsets.left + sbInsets.right;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
        int sbButtonsW = leftButtonW + rightButtonW;
2658
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2
diff changeset
   777
        float trackW = sbSize.width - (sbInsetsW + sbButtonsW) - (leftGap + rightGap);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
        /* Compute the width and origin of the thumb.  Enforce
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
         * the thumbs min/max dimensions.  The case where the thumb
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
         * is at the right edge is handled specially to avoid numerical
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
         * problems in computing thumbX.  If the thumb doesn't
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
         * fit in the track (trackH) we'll hide it later.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
        float min = sb.getMinimum();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
        float max = sb.getMaximum();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
        float extent = sb.getVisibleAmount();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
        float range = max - min;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
        float value = getValue(sb);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
        int thumbW = (range <= 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
            ? getMaximumThumbSize().width : (int)(trackW * (extent / range));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
        thumbW = Math.max(thumbW, getMinimumThumbSize().width);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
        thumbW = Math.min(thumbW, getMaximumThumbSize().width);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
2658
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2
diff changeset
   796
        int thumbX = ltr ? rightButtonX - rightGap - thumbW : leftButtonX + leftButtonW + leftGap;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
        if (value < (max - sb.getVisibleAmount())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
            float thumbRange = trackW - thumbW;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
            if( ltr ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
                thumbX = (int)(0.5f + (thumbRange * ((value - min) / (range - extent))));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
                thumbX = (int)(0.5f + (thumbRange * ((max - extent - value) / (range - extent))));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
            }
2658
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2
diff changeset
   804
            thumbX += leftButtonX + leftButtonW + leftGap;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
        /* If the buttons don't fit, allocate half of the available
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
         * space to each and move the right one over.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
        int sbAvailButtonW = (sbSize.width - sbInsetsW);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
        if (sbAvailButtonW < sbButtonsW) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
            rightButtonW = leftButtonW = sbAvailButtonW / 2;
2658
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2
diff changeset
   813
            rightButtonX = sbSize.width - (sbInsets.right + rightButtonW + rightGap);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
        (ltr ? decrButton : incrButton).setBounds(leftButtonX, itemY, leftButtonW, itemH);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
        (ltr ? incrButton : decrButton).setBounds(rightButtonX, itemY, rightButtonW, itemH);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
        /* Update the trackRect field.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
         */
2658
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2
diff changeset
   821
        int itrackX = leftButtonX + leftButtonW + leftGap;
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2
diff changeset
   822
        int itrackW = rightButtonX - rightGap - itrackX;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
        trackRect.setBounds(itrackX, itemY, itrackW, itemH);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
        /* Make sure the thumb fits between the buttons.  Note
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
         * that setting the thumbs bounds causes a repaint.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
        if (thumbW >= (int)trackW) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
            if (UIManager.getBoolean("ScrollBar.alwaysShowThumb")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
                // This is used primarily for GTK L&F, which expands the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
                // thumb to fit the track when it would otherwise be hidden.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
                setThumbBounds(itrackX, itemY, itrackW, itemH);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
                // Other L&F's simply hide the thumb in this case.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
                setThumbBounds(0, 0, 0, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
        else {
2658
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2
diff changeset
   839
            if (thumbX + thumbW > rightButtonX - rightGap) {
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2
diff changeset
   840
                thumbX = rightButtonX - rightGap - thumbW;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
            }
2658
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2
diff changeset
   842
            if (thumbX  < leftButtonX + leftButtonW + leftGap) {
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2
diff changeset
   843
                thumbX = leftButtonX + leftButtonW + leftGap + 1;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
            setThumbBounds(thumbX, itemY, thumbW, itemH);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
    public void layoutContainer(Container scrollbarContainer)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
        /* If the user is dragging the value, we'll assume that the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
         * scrollbars layout is OK modulo the thumb which is being
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
         * handled by the dragging code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
        if (isDragging) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
        JScrollBar scrollbar = (JScrollBar)scrollbarContainer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
        switch (scrollbar.getOrientation()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
        case JScrollBar.VERTICAL:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
            layoutVScrollbar(scrollbar);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   864
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
        case JScrollBar.HORIZONTAL:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
            layoutHScrollbar(scrollbar);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   867
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   868
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   869
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   870
90ce3da70b43 Initial load
duke
parents:
diff changeset
   871
90ce3da70b43 Initial load
duke
parents:
diff changeset
   872
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   873
     * Set the bounds of the thumb and force a repaint that includes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   874
     * the old thumbBounds and the new one.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   875
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   876
     * @see #getThumbBounds
90ce3da70b43 Initial load
duke
parents:
diff changeset
   877
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   878
    protected void setThumbBounds(int x, int y, int width, int height)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   879
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   880
        /* If the thumbs bounds haven't changed, we're done.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   881
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   882
        if ((thumbRect.x == x) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   883
            (thumbRect.y == y) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   884
            (thumbRect.width == width) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   885
            (thumbRect.height == height)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   886
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   887
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   888
90ce3da70b43 Initial load
duke
parents:
diff changeset
   889
        /* Update thumbRect, and repaint the union of x,y,w,h and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   890
         * the old thumbRect.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   891
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   892
        int minX = Math.min(x, thumbRect.x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   893
        int minY = Math.min(y, thumbRect.y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   894
        int maxX = Math.max(x + width, thumbRect.x + thumbRect.width);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   895
        int maxY = Math.max(y + height, thumbRect.y + thumbRect.height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   896
90ce3da70b43 Initial load
duke
parents:
diff changeset
   897
        thumbRect.setBounds(x, y, width, height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   898
        scrollbar.repaint(minX, minY, maxX - minX, maxY - minY);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   899
90ce3da70b43 Initial load
duke
parents:
diff changeset
   900
        // Once there is API to determine the mouse location this will need
90ce3da70b43 Initial load
duke
parents:
diff changeset
   901
        // to be changed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   902
        setThumbRollover(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   903
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   904
90ce3da70b43 Initial load
duke
parents:
diff changeset
   905
90ce3da70b43 Initial load
duke
parents:
diff changeset
   906
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   907
     * Return the current size/location of the thumb.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   908
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   909
     * <b>Warning </b>: the value returned by this method should not be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   910
     * be modified, it's a reference to the actual rectangle, not a copy.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   911
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   912
     * @return The current size/location of the thumb.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   913
     * @see #setThumbBounds
90ce3da70b43 Initial load
duke
parents:
diff changeset
   914
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   915
    protected Rectangle getThumbBounds() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   916
        return thumbRect;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   917
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   918
90ce3da70b43 Initial load
duke
parents:
diff changeset
   919
90ce3da70b43 Initial load
duke
parents:
diff changeset
   920
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   921
     * Returns the current bounds of the track, i.e. the space in between
90ce3da70b43 Initial load
duke
parents:
diff changeset
   922
     * the increment and decrement buttons, less the insets.  The value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   923
     * returned by this method is updated each time the scrollbar is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   924
     * laid out (validated).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   925
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   926
     * <b>Warning </b>: the value returned by this method should not be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   927
     * be modified, it's a reference to the actual rectangle, not a copy.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   928
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   929
     * @return the current bounds of the scrollbar track
90ce3da70b43 Initial load
duke
parents:
diff changeset
   930
     * @see #layoutContainer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   931
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   932
    protected Rectangle getTrackBounds() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   933
        return trackRect;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   934
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   935
90ce3da70b43 Initial load
duke
parents:
diff changeset
   936
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   937
     * Method for scrolling by a block increment.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   938
     * Added for mouse wheel scrolling support, RFE 4202656.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   939
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   940
    static void scrollByBlock(JScrollBar scrollbar, int direction) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   941
        // This method is called from BasicScrollPaneUI to implement wheel
90ce3da70b43 Initial load
duke
parents:
diff changeset
   942
        // scrolling, and also from scrollByBlock().
90ce3da70b43 Initial load
duke
parents:
diff changeset
   943
            int oldValue = scrollbar.getValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   944
            int blockIncrement = scrollbar.getBlockIncrement(direction);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   945
            int delta = blockIncrement * ((direction > 0) ? +1 : -1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   946
            int newValue = oldValue + delta;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   947
90ce3da70b43 Initial load
duke
parents:
diff changeset
   948
            // Check for overflow.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   949
            if (delta > 0 && newValue < oldValue) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   950
                newValue = scrollbar.getMaximum();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   951
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   952
            else if (delta < 0 && newValue > oldValue) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   953
                newValue = scrollbar.getMinimum();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   954
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   955
90ce3da70b43 Initial load
duke
parents:
diff changeset
   956
            scrollbar.setValue(newValue);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   957
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   958
90ce3da70b43 Initial load
duke
parents:
diff changeset
   959
    protected void scrollByBlock(int direction)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   960
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   961
        scrollByBlock(scrollbar, direction);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   962
            trackHighlight = direction > 0 ? INCREASE_HIGHLIGHT : DECREASE_HIGHLIGHT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   963
            Rectangle dirtyRect = getTrackBounds();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   964
            scrollbar.repaint(dirtyRect.x, dirtyRect.y, dirtyRect.width, dirtyRect.height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   965
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   966
90ce3da70b43 Initial load
duke
parents:
diff changeset
   967
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   968
     * Method for scrolling by a unit increment.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   969
     * Added for mouse wheel scrolling support, RFE 4202656.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   970
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   971
     * If limitByBlock is set to true, the scrollbar will scroll at least 1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   972
     * unit increment, but will not scroll farther than the block increment.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   973
     * See BasicScrollPaneUI.Handler.mouseWheelMoved().
90ce3da70b43 Initial load
duke
parents:
diff changeset
   974
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   975
    static void scrollByUnits(JScrollBar scrollbar, int direction,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   976
                              int units, boolean limitToBlock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   977
        // This method is called from BasicScrollPaneUI to implement wheel
90ce3da70b43 Initial load
duke
parents:
diff changeset
   978
        // scrolling, as well as from scrollByUnit().
90ce3da70b43 Initial load
duke
parents:
diff changeset
   979
        int delta;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   980
        int limit = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   981
90ce3da70b43 Initial load
duke
parents:
diff changeset
   982
        if (limitToBlock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   983
            if (direction < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   984
                limit = scrollbar.getValue() -
90ce3da70b43 Initial load
duke
parents:
diff changeset
   985
                                         scrollbar.getBlockIncrement(direction);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   986
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   987
            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   988
                limit = scrollbar.getValue() +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   989
                                         scrollbar.getBlockIncrement(direction);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   990
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   991
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   992
90ce3da70b43 Initial load
duke
parents:
diff changeset
   993
        for (int i=0; i<units; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   994
            if (direction > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   995
                delta = scrollbar.getUnitIncrement(direction);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   996
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   997
            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   998
                delta = -scrollbar.getUnitIncrement(direction);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   999
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1000
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1001
            int oldValue = scrollbar.getValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1002
            int newValue = oldValue + delta;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1003
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1004
            // Check for overflow.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1005
            if (delta > 0 && newValue < oldValue) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1006
                newValue = scrollbar.getMaximum();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1007
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1008
            else if (delta < 0 && newValue > oldValue) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1009
                newValue = scrollbar.getMinimum();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1010
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1011
            if (oldValue == newValue) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1012
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1013
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1014
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1015
            if (limitToBlock && i > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1016
                assert limit != -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1017
                if ((direction < 0 && newValue < limit) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1018
                    (direction > 0 && newValue > limit)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1019
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1020
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1021
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1022
            scrollbar.setValue(newValue);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1023
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1024
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1025
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1026
    protected void scrollByUnit(int direction)  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1027
        scrollByUnits(scrollbar, direction, 1, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1028
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1029
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1030
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1031
     * Indicates whether the user can absolutely position the thumb with
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1032
     * a mouse gesture (usually the middle mouse button).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1033
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1034
     * @return true if a mouse gesture can absolutely position the thumb
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1035
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1036
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1037
    public boolean getSupportsAbsolutePositioning() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1038
        return supportsAbsolutePositioning;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1039
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1040
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1041
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1042
     * A listener to listen for model changes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1043
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1044
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1045
    protected class ModelListener implements ChangeListener {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1046
        public void stateChanged(ChangeEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1047
            if (!useCachedValue) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1048
                scrollBarValue = scrollbar.getValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1049
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1050
            layoutContainer(scrollbar);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1051
            useCachedValue = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1052
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1053
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1054
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1055
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1056
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1057
     * Track mouse drags.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1058
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1059
    protected class TrackListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1060
        extends MouseAdapter implements MouseMotionListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1061
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1062
        protected transient int offset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1063
        protected transient int currentMouseX, currentMouseY;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1064
        private transient int direction = +1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1065
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1066
        public void mouseReleased(MouseEvent e)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1067
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1068
            if (isDragging) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1069
                updateThumbState(e.getX(), e.getY());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1070
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1071
            if (SwingUtilities.isRightMouseButton(e) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1072
                (!getSupportsAbsolutePositioning() &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1073
                 SwingUtilities.isMiddleMouseButton(e)))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1074
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1075
            if(!scrollbar.isEnabled())
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1076
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1077
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1078
            Rectangle r = getTrackBounds();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1079
            scrollbar.repaint(r.x, r.y, r.width, r.height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1080
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1081
            trackHighlight = NO_HIGHLIGHT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1082
            isDragging = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1083
            offset = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1084
            scrollTimer.stop();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1085
            useCachedValue = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1086
            scrollbar.setValueIsAdjusting(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1087
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1088
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1089
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1090
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1091
         * If the mouse is pressed above the "thumb" component
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1092
         * then reduce the scrollbars value by one page ("page up"),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1093
         * otherwise increase it by one page.  If there is no
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1094
         * thumb then page up if the mouse is in the upper half
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1095
         * of the track.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1096
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1097
        public void mousePressed(MouseEvent e)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1098
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1099
            if (SwingUtilities.isRightMouseButton(e) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1100
                (!getSupportsAbsolutePositioning() &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1101
                 SwingUtilities.isMiddleMouseButton(e)))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1102
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1103
            if(!scrollbar.isEnabled())
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1104
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1105
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1106
            if (!scrollbar.hasFocus() && scrollbar.isRequestFocusEnabled()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1107
                scrollbar.requestFocus();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1108
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1109
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1110
            useCachedValue = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1111
            scrollbar.setValueIsAdjusting(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1112
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1113
            currentMouseX = e.getX();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1114
            currentMouseY = e.getY();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1115
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1116
            // Clicked in the Thumb area?
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1117
            if(getThumbBounds().contains(currentMouseX, currentMouseY)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1118
                switch (scrollbar.getOrientation()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1119
                case JScrollBar.VERTICAL:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1120
                    offset = currentMouseY - getThumbBounds().y;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1121
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1122
                case JScrollBar.HORIZONTAL:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1123
                    offset = currentMouseX - getThumbBounds().x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1124
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1125
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1126
                isDragging = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1127
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1128
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1129
            else if (getSupportsAbsolutePositioning() &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1130
                     SwingUtilities.isMiddleMouseButton(e)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1131
                switch (scrollbar.getOrientation()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1132
                case JScrollBar.VERTICAL:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1133
                    offset = getThumbBounds().height / 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1134
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1135
                case JScrollBar.HORIZONTAL:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1136
                    offset = getThumbBounds().width / 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1137
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1138
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1139
                isDragging = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1140
                setValueFrom(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1141
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1142
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1143
            isDragging = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1144
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1145
            Dimension sbSize = scrollbar.getSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1146
            direction = +1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1147
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1148
            switch (scrollbar.getOrientation()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1149
            case JScrollBar.VERTICAL:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1150
                if (getThumbBounds().isEmpty()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1151
                    int scrollbarCenter = sbSize.height / 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1152
                    direction = (currentMouseY < scrollbarCenter) ? -1 : +1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1153
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1154
                    int thumbY = getThumbBounds().y;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1155
                    direction = (currentMouseY < thumbY) ? -1 : +1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1156
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1157
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1158
            case JScrollBar.HORIZONTAL:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1159
                if (getThumbBounds().isEmpty()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1160
                    int scrollbarCenter = sbSize.width / 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1161
                    direction = (currentMouseX < scrollbarCenter) ? -1 : +1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1162
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1163
                    int thumbX = getThumbBounds().x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1164
                    direction = (currentMouseX < thumbX) ? -1 : +1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1165
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1166
                if (!scrollbar.getComponentOrientation().isLeftToRight()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1167
                    direction = -direction;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1168
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1169
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1170
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1171
            scrollByBlock(direction);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1172
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1173
            scrollTimer.stop();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1174
            scrollListener.setDirection(direction);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1175
            scrollListener.setScrollByBlock(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1176
            startScrollTimerIfNecessary();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1177
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1178
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1179
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1180
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1181
         * Set the models value to the position of the thumb's top of Vertical
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1182
         * scrollbar, or the left/right of Horizontal scrollbar in
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1183
         * left-to-right/right-to-left scrollbar relative to the origin of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1184
         * track.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1185
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1186
        public void mouseDragged(MouseEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1187
            if (SwingUtilities.isRightMouseButton(e) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1188
                (!getSupportsAbsolutePositioning() &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1189
                 SwingUtilities.isMiddleMouseButton(e)))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1190
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1191
            if(!scrollbar.isEnabled() || getThumbBounds().isEmpty()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1192
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1193
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1194
            if (isDragging) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1195
                setValueFrom(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1196
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1197
                currentMouseX = e.getX();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1198
                currentMouseY = e.getY();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1199
                updateThumbState(currentMouseX, currentMouseY);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1200
                startScrollTimerIfNecessary();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1201
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1202
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1203
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1204
        private void setValueFrom(MouseEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1205
            boolean active = isThumbRollover();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1206
            BoundedRangeModel model = scrollbar.getModel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1207
            Rectangle thumbR = getThumbBounds();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1208
            float trackLength;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1209
            int thumbMin, thumbMax, thumbPos;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1210
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1211
            if (scrollbar.getOrientation() == JScrollBar.VERTICAL) {
2658
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2
diff changeset
  1212
                thumbMin = trackRect.y;
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2
diff changeset
  1213
                thumbMax = trackRect.y + trackRect.height - thumbR.height;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1214
                thumbPos = Math.min(thumbMax, Math.max(thumbMin, (e.getY() - offset)));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1215
                setThumbBounds(thumbR.x, thumbPos, thumbR.width, thumbR.height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1216
                trackLength = getTrackBounds().height;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1217
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1218
            else {
2658
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2
diff changeset
  1219
                thumbMin = trackRect.x;
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2
diff changeset
  1220
                thumbMax = trackRect.x + trackRect.width - thumbR.width;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1221
                thumbPos = Math.min(thumbMax, Math.max(thumbMin, (e.getX() - offset)));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1222
                setThumbBounds(thumbPos, thumbR.y, thumbR.width, thumbR.height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1223
                trackLength = getTrackBounds().width;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1224
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1225
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1226
            /* Set the scrollbars value.  If the thumb has reached the end of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1227
             * the scrollbar, then just set the value to its maximum.  Otherwise
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1228
             * compute the value as accurately as possible.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1229
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1230
            if (thumbPos == thumbMax) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1231
                if (scrollbar.getOrientation() == JScrollBar.VERTICAL ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1232
                    scrollbar.getComponentOrientation().isLeftToRight()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1233
                    scrollbar.setValue(model.getMaximum() - model.getExtent());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1234
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1235
                    scrollbar.setValue(model.getMinimum());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1236
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1237
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1238
            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1239
                float valueMax = model.getMaximum() - model.getExtent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1240
                float valueRange = valueMax - model.getMinimum();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1241
                float thumbValue = thumbPos - thumbMin;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1242
                float thumbRange = thumbMax - thumbMin;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1243
                int value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1244
                if (scrollbar.getOrientation() == JScrollBar.VERTICAL ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1245
                    scrollbar.getComponentOrientation().isLeftToRight()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1246
                    value = (int)(0.5 + ((thumbValue / thumbRange) * valueRange));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1247
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1248
                    value = (int)(0.5 + (((thumbMax - thumbPos) / thumbRange) * valueRange));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1249
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1250
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1251
                useCachedValue = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1252
                scrollBarValue = value + model.getMinimum();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1253
                scrollbar.setValue(adjustValueIfNecessary(scrollBarValue));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1254
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1255
            setThumbRollover(active);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1256
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1257
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1258
        private int adjustValueIfNecessary(int value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1259
            if (scrollbar.getParent() instanceof JScrollPane) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1260
                JScrollPane scrollpane = (JScrollPane)scrollbar.getParent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1261
                JViewport viewport = scrollpane.getViewport();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1262
                Component view = viewport.getView();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1263
                if (view instanceof JList) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1264
                    JList list = (JList)view;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1265
                    if (DefaultLookup.getBoolean(list, list.getUI(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1266
                                                 "List.lockToPositionOnScroll", false)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1267
                        int adjustedValue = value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1268
                        int mode = list.getLayoutOrientation();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1269
                        int orientation = scrollbar.getOrientation();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1270
                        if (orientation == JScrollBar.VERTICAL && mode == JList.VERTICAL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1271
                            int index = list.locationToIndex(new Point(0, value));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1272
                            Rectangle rect = list.getCellBounds(index, index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1273
                            if (rect != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1274
                                adjustedValue = rect.y;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1275
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1276
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1277
                        if (orientation == JScrollBar.HORIZONTAL &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1278
                            (mode == JList.VERTICAL_WRAP || mode == JList.HORIZONTAL_WRAP)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1279
                            if (scrollpane.getComponentOrientation().isLeftToRight()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1280
                                int index = list.locationToIndex(new Point(value, 0));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1281
                                Rectangle rect = list.getCellBounds(index, index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1282
                                if (rect != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1283
                                    adjustedValue = rect.x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1284
                                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1285
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1286
                            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1287
                                Point loc = new Point(value, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1288
                                int extent = viewport.getExtentSize().width;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1289
                                loc.x += extent - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1290
                                int index = list.locationToIndex(loc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1291
                                Rectangle rect = list.getCellBounds(index, index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1292
                                if (rect != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1293
                                    adjustedValue = rect.x + rect.width - extent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1294
                                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1295
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1296
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1297
                        value = adjustedValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1298
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1299
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1300
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1301
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1302
            return value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1303
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1304
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1305
        private void startScrollTimerIfNecessary() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1306
            if (scrollTimer.isRunning()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1307
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1308
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1309
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1310
            Rectangle tb = getThumbBounds();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1311
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1312
            switch (scrollbar.getOrientation()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1313
            case JScrollBar.VERTICAL:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1314
                if (direction > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1315
                    if (tb.y + tb.height < trackListener.currentMouseY) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1316
                        scrollTimer.start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1317
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1318
                } else if (tb.y > trackListener.currentMouseY) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1319
                    scrollTimer.start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1320
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1321
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1322
            case JScrollBar.HORIZONTAL:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1323
                if ((direction > 0 && isMouseAfterThumb())
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1324
                        || (direction < 0 && isMouseBeforeThumb())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1325
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1326
                    scrollTimer.start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1327
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1328
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1329
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1330
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1331
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1332
        public void mouseMoved(MouseEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1333
            if (!isDragging) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1334
                updateThumbState(e.getX(), e.getY());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1335
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1336
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1337
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1338
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1339
         * Invoked when the mouse exits the scrollbar.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1340
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1341
         * @param e MouseEvent further describing the event
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1342
         * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1343
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1344
        public void mouseExited(MouseEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1345
            if (!isDragging) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1346
                setThumbRollover(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1347
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1348
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1349
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1350
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1351
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1352
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1353
     * Listener for cursor keys.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1354
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1355
    protected class ArrowButtonListener extends MouseAdapter
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1356
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1357
        // Because we are handling both mousePressed and Actions
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1358
        // we need to make sure we don't fire under both conditions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1359
        // (keyfocus on scrollbars causes action without mousePress
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1360
        boolean handledEvent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1361
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1362
        public void mousePressed(MouseEvent e)          {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1363
            if(!scrollbar.isEnabled()) { return; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1364
            // not an unmodified left mouse button
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1365
            //if(e.getModifiers() != InputEvent.BUTTON1_MASK) {return; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1366
            if( ! SwingUtilities.isLeftMouseButton(e)) { return; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1367
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1368
            int direction = (e.getSource() == incrButton) ? 1 : -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1369
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1370
            scrollByUnit(direction);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1371
            scrollTimer.stop();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1372
            scrollListener.setDirection(direction);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1373
            scrollListener.setScrollByBlock(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1374
            scrollTimer.start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1375
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1376
            handledEvent = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1377
            if (!scrollbar.hasFocus() && scrollbar.isRequestFocusEnabled()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1378
                scrollbar.requestFocus();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1379
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1380
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1381
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1382
        public void mouseReleased(MouseEvent e)         {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1383
            scrollTimer.stop();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1384
            handledEvent = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1385
            scrollbar.setValueIsAdjusting(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1386
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1387
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1388
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1389
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1390
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1391
     * Listener for scrolling events initiated in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1392
     * <code>ScrollPane</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1393
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1394
    protected class ScrollListener implements ActionListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1395
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1396
        int direction = +1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1397
        boolean useBlockIncrement;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1398
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1399
        public ScrollListener() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1400
            direction = +1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1401
            useBlockIncrement = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1402
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1403
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1404
        public ScrollListener(int dir, boolean block)   {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1405
            direction = dir;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1406
            useBlockIncrement = block;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1407
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1408
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1409
        public void setDirection(int direction) { this.direction = direction; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1410
        public void setScrollByBlock(boolean block) { this.useBlockIncrement = block; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1411
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1412
        public void actionPerformed(ActionEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1413
            if(useBlockIncrement)       {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1414
                scrollByBlock(direction);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1415
                // Stop scrolling if the thumb catches up with the mouse
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1416
                if(scrollbar.getOrientation() == JScrollBar.VERTICAL)   {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1417
                    if(direction > 0)   {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1418
                        if(getThumbBounds().y + getThumbBounds().height
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1419
                                >= trackListener.currentMouseY)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1420
                                    ((Timer)e.getSource()).stop();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1421
                    } else if(getThumbBounds().y <= trackListener.currentMouseY)        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1422
                        ((Timer)e.getSource()).stop();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1423
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1424
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1425
                    if ((direction > 0 && !isMouseAfterThumb())
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1426
                           || (direction < 0 && !isMouseBeforeThumb())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1427
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1428
                       ((Timer)e.getSource()).stop();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1429
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1430
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1431
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1432
                scrollByUnit(direction);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1433
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1434
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1435
            if(direction > 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1436
                && scrollbar.getValue()+scrollbar.getVisibleAmount()
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1437
                        >= scrollbar.getMaximum())
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1438
                ((Timer)e.getSource()).stop();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1439
            else if(direction < 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1440
                && scrollbar.getValue() <= scrollbar.getMinimum())
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1441
                ((Timer)e.getSource()).stop();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1442
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1443
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1444
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1445
    private boolean isMouseLeftOfThumb() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1446
        return trackListener.currentMouseX < getThumbBounds().x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1447
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1448
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1449
    private boolean isMouseRightOfThumb() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1450
        Rectangle tb = getThumbBounds();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1451
        return trackListener.currentMouseX > tb.x + tb.width;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1452
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1453
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1454
    private boolean isMouseBeforeThumb() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1455
        return scrollbar.getComponentOrientation().isLeftToRight()
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1456
            ? isMouseLeftOfThumb()
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1457
            : isMouseRightOfThumb();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1458
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1459
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1460
    private boolean isMouseAfterThumb() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1461
        return scrollbar.getComponentOrientation().isLeftToRight()
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1462
            ? isMouseRightOfThumb()
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1463
            : isMouseLeftOfThumb();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1464
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1465
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1466
    private void updateButtonDirections() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1467
        int orient = scrollbar.getOrientation();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1468
        if (scrollbar.getComponentOrientation().isLeftToRight()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1469
            if (incrButton instanceof BasicArrowButton) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1470
                ((BasicArrowButton)incrButton).setDirection(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1471
                        orient == HORIZONTAL? EAST : SOUTH);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1472
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1473
            if (decrButton instanceof BasicArrowButton) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1474
                ((BasicArrowButton)decrButton).setDirection(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1475
                        orient == HORIZONTAL? WEST : NORTH);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1476
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1477
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1478
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1479
            if (incrButton instanceof BasicArrowButton) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1480
                ((BasicArrowButton)incrButton).setDirection(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1481
                        orient == HORIZONTAL? WEST : SOUTH);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1482
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1483
            if (decrButton instanceof BasicArrowButton) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1484
                ((BasicArrowButton)decrButton).setDirection(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1485
                        orient == HORIZONTAL ? EAST : NORTH);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1486
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1487
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1488
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1489
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1490
    public class PropertyChangeHandler implements PropertyChangeListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1491
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1492
        // NOTE: This class exists only for backward compatability. All
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1493
        // its functionality has been moved into Handler. If you need to add
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1494
        // new functionality add it to the Handler, but make sure this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1495
        // class calls into the Handler.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1496
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1497
        public void propertyChange(PropertyChangeEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1498
            getHandler().propertyChange(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1499
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1500
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1501
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1502
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1503
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1504
     * Used for scrolling the scrollbar.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1505
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1506
    private static class Actions extends UIAction {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1507
        private static final String POSITIVE_UNIT_INCREMENT =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1508
                                    "positiveUnitIncrement";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1509
        private static final String POSITIVE_BLOCK_INCREMENT =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1510
                                    "positiveBlockIncrement";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1511
        private static final String NEGATIVE_UNIT_INCREMENT =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1512
                                    "negativeUnitIncrement";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1513
        private static final String NEGATIVE_BLOCK_INCREMENT =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1514
                                    "negativeBlockIncrement";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1515
        private static final String MIN_SCROLL = "minScroll";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1516
        private static final String MAX_SCROLL = "maxScroll";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1517
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1518
        Actions(String name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1519
            super(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1520
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1521
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1522
        public void actionPerformed(ActionEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1523
            JScrollBar scrollBar = (JScrollBar)e.getSource();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1524
            String key = getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1525
            if (key == POSITIVE_UNIT_INCREMENT) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1526
                scroll(scrollBar, POSITIVE_SCROLL, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1527
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1528
            else if (key == POSITIVE_BLOCK_INCREMENT) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1529
                scroll(scrollBar, POSITIVE_SCROLL, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1530
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1531
            else if (key == NEGATIVE_UNIT_INCREMENT) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1532
                scroll(scrollBar, NEGATIVE_SCROLL, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1533
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1534
            else if (key == NEGATIVE_BLOCK_INCREMENT) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1535
                scroll(scrollBar, NEGATIVE_SCROLL, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1536
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1537
            else if (key == MIN_SCROLL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1538
                scroll(scrollBar, BasicScrollBarUI.MIN_SCROLL, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1539
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1540
            else if (key == MAX_SCROLL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1541
                scroll(scrollBar, BasicScrollBarUI.MAX_SCROLL, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1542
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1543
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1544
        private void scroll(JScrollBar scrollBar, int dir, boolean block) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1545
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1546
            if (dir == NEGATIVE_SCROLL || dir == POSITIVE_SCROLL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1547
                int amount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1548
                // Don't use the BasicScrollBarUI.scrollByXXX methods as we
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1549
                // don't want to use an invokeLater to reset the trackHighlight
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1550
                // via an invokeLater
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1551
                if (block) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1552
                    if (dir == NEGATIVE_SCROLL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1553
                        amount = -1 * scrollBar.getBlockIncrement(-1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1554
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1555
                    else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1556
                        amount = scrollBar.getBlockIncrement(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1557
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1558
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1559
                else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1560
                    if (dir == NEGATIVE_SCROLL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1561
                        amount = -1 * scrollBar.getUnitIncrement(-1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1562
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1563
                    else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1564
                        amount = scrollBar.getUnitIncrement(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1565
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1566
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1567
                scrollBar.setValue(scrollBar.getValue() + amount);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1568
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1569
            else if (dir == BasicScrollBarUI.MIN_SCROLL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1570
                scrollBar.setValue(scrollBar.getMinimum());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1571
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1572
            else if (dir == BasicScrollBarUI.MAX_SCROLL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1573
                scrollBar.setValue(scrollBar.getMaximum());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1574
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1575
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1576
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1577
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1578
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1579
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1580
    // EventHandler
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1581
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1582
    private class Handler implements FocusListener, PropertyChangeListener {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1583
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1584
        // FocusListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1585
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1586
        public void focusGained(FocusEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1587
            scrollbar.repaint();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1588
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1589
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1590
        public void focusLost(FocusEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1591
            scrollbar.repaint();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1592
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1593
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1594
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1595
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1596
        // PropertyChangeListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1597
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1598
        public void propertyChange(PropertyChangeEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1599
            String propertyName = e.getPropertyName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1600
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1601
            if ("model" == propertyName) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1602
                BoundedRangeModel oldModel = (BoundedRangeModel)e.getOldValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1603
                BoundedRangeModel newModel = (BoundedRangeModel)e.getNewValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1604
                oldModel.removeChangeListener(modelListener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1605
                newModel.addChangeListener(modelListener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1606
                scrollbar.repaint();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1607
                scrollbar.revalidate();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1608
            } else if ("orientation" == propertyName) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1609
                updateButtonDirections();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1610
            } else if ("componentOrientation" == propertyName) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1611
                updateButtonDirections();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1612
                InputMap inputMap = getInputMap(JComponent.WHEN_FOCUSED);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1613
                SwingUtilities.replaceUIInputMap(scrollbar, JComponent.WHEN_FOCUSED, inputMap);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1614
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1615
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1616
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1617
}