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