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