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