jdk/src/java.desktop/share/classes/javax/swing/plaf/basic/BasicSliderUI.java
author weijun
Thu, 11 Dec 2014 15:19:12 +0800
changeset 28228 be83f404724d
parent 26353 b5e3b7fbf56d
child 28059 e576535359cc
permissions -rw-r--r--
8067207: Replace concat String to append in StringBuilder parameters (client) Reviewed-by: serb Contributed-by: Otavio Santana <otaviojava@java.net>
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
23697
e556a715949f 8034169: Fix serial lint warnings in javax.swing
darcy
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: 4963
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: 4963
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: 4963
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4963
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4963
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package javax.swing.plaf.basic;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.awt.event.*;
677
20718977427b 4987336: JSlider doesn't show label's animated icon
rupashka
parents: 676
diff changeset
    29
import java.awt.*;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.beans.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.util.Dictionary;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.util.Enumeration;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import javax.swing.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import javax.swing.event.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import javax.swing.plaf.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import sun.swing.DefaultLookup;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import sun.swing.UIAction;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
/**
20169
d7fa6d7586c9 8025085: [javadoc] some errors in javax/swing
yan
parents: 7668
diff changeset
    42
 * A Basic L&amp;F implementation of SliderUI.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * @author Tom Santos
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
public class BasicSliderUI extends SliderUI{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    // Old actions forward to an instance of this.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    private static final Actions SHARED_ACTION = new Actions();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    public static final int POSITIVE_SCROLL = +1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    public static final int NEGATIVE_SCROLL = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    public static final int MIN_SCROLL = -2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    public static final int MAX_SCROLL = +2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    protected Timer scrollTimer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    protected JSlider slider;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    protected Insets focusInsets = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    protected Insets insetCache = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    protected boolean leftToRightCache = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    protected Rectangle focusRect = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    protected Rectangle contentRect = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    protected Rectangle labelRect = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    protected Rectangle tickRect = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    protected Rectangle trackRect = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    protected Rectangle thumbRect = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    protected int trackBuffer = 0;  // The distance that the track is from the side of the control
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    private transient boolean isDragging;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    protected TrackListener trackListener;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    protected ChangeListener changeListener;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    protected ComponentListener componentListener;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    protected FocusListener focusListener;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    protected ScrollListener scrollListener;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    protected PropertyChangeListener propertyChangeListener;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    private Handler handler;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    private int lastValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    // Colors
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    private Color shadowColor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    private Color highlightColor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    private Color focusColor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     * Whther or not sameLabelBaselines is up to date.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    private boolean checkedLabelBaselines;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     * Whether or not all the entries in the labeltable have the same
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     * baseline.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    private boolean sameLabelBaselines;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    protected Color getShadowColor() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        return shadowColor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    protected Color getHighlightColor() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        return highlightColor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    protected Color getFocusColor() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        return focusColor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     * Returns true if the user is dragging the slider.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     * @return true if the user is dragging the slider
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    protected boolean isDragging() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        return isDragging;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    /////////////////////////////////////////////////////////////////////////////
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    // ComponentUI Interface Implementation methods
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    /////////////////////////////////////////////////////////////////////////////
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    public static ComponentUI createUI(JComponent b)    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        return new BasicSliderUI((JSlider)b);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    public BasicSliderUI(JSlider b)   {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
    public void installUI(JComponent c)   {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        slider = (JSlider) c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        checkedLabelBaselines = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        slider.setEnabled(slider.isEnabled());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        LookAndFeel.installProperty(slider, "opaque", Boolean.TRUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        isDragging = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        trackListener = createTrackListener( slider );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        changeListener = createChangeListener( slider );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        componentListener = createComponentListener( slider );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        focusListener = createFocusListener( slider );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        scrollListener = createScrollListener( slider );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        propertyChangeListener = createPropertyChangeListener( slider );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        installDefaults( slider );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        installListeners( slider );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        installKeyboardActions( slider );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        scrollTimer = new Timer( 100, scrollListener );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        scrollTimer.setInitialDelay( 300 );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        insetCache = slider.getInsets();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        leftToRightCache = BasicGraphicsUtils.isLeftToRight(slider);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        focusRect = new Rectangle();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        contentRect = new Rectangle();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        labelRect = new Rectangle();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        tickRect = new Rectangle();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        trackRect = new Rectangle();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        thumbRect = new Rectangle();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        lastValue = slider.getValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        calculateGeometry(); // This figures out where the labels, ticks, track, and thumb are.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
    public void uninstallUI(JComponent c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        if ( c != slider )
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
            throw new IllegalComponentStateException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
                                                    this + " was asked to deinstall() "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
                                                    + c + " when it only knows about "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
                                                    + slider + ".");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        scrollTimer.stop();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        scrollTimer = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
4963
004eaf21ba24 6918861: SynthSliderUI.uninstallDefaults() is not called when UI is uninstalled
rupashka
parents: 4957
diff changeset
   175
        uninstallDefaults(slider);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        uninstallListeners( slider );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        uninstallKeyboardActions(slider);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        insetCache = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        leftToRightCache = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        focusRect = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        contentRect = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        labelRect = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        tickRect = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        trackRect = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        thumbRect = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        trackListener = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        changeListener = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        componentListener = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        focusListener = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        scrollListener = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        propertyChangeListener = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        slider = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
    protected void installDefaults( JSlider slider ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        LookAndFeel.installBorder(slider, "Slider.border");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        LookAndFeel.installColorsAndFont(slider, "Slider.background",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
                                         "Slider.foreground", "Slider.font");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        highlightColor = UIManager.getColor("Slider.highlight");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
        shadowColor = UIManager.getColor("Slider.shadow");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        focusColor = UIManager.getColor("Slider.focus");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        focusInsets = (Insets)UIManager.get( "Slider.focusInsets" );
2658
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2488
diff changeset
   206
        // use default if missing so that BasicSliderUI can be used in other
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2488
diff changeset
   207
        // LAFs like Nimbus
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2488
diff changeset
   208
        if (focusInsets == null) focusInsets = new InsetsUIResource(2,2,2,2);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
4963
004eaf21ba24 6918861: SynthSliderUI.uninstallDefaults() is not called when UI is uninstalled
rupashka
parents: 4957
diff changeset
   211
    protected void uninstallDefaults(JSlider slider) {
004eaf21ba24 6918861: SynthSliderUI.uninstallDefaults() is not called when UI is uninstalled
rupashka
parents: 4957
diff changeset
   212
        LookAndFeel.uninstallBorder(slider);
004eaf21ba24 6918861: SynthSliderUI.uninstallDefaults() is not called when UI is uninstalled
rupashka
parents: 4957
diff changeset
   213
004eaf21ba24 6918861: SynthSliderUI.uninstallDefaults() is not called when UI is uninstalled
rupashka
parents: 4957
diff changeset
   214
        focusInsets = null;
004eaf21ba24 6918861: SynthSliderUI.uninstallDefaults() is not called when UI is uninstalled
rupashka
parents: 4957
diff changeset
   215
    }
004eaf21ba24 6918861: SynthSliderUI.uninstallDefaults() is not called when UI is uninstalled
rupashka
parents: 4957
diff changeset
   216
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
    protected TrackListener createTrackListener(JSlider slider) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
        return new TrackListener();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
    protected ChangeListener createChangeListener(JSlider slider) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
        return getHandler();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
    protected ComponentListener createComponentListener(JSlider slider) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
        return getHandler();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
    protected FocusListener createFocusListener(JSlider slider) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
        return getHandler();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
    protected ScrollListener createScrollListener( JSlider slider ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
        return new ScrollListener();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
    protected PropertyChangeListener createPropertyChangeListener(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
            JSlider slider) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
        return getHandler();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
    private Handler getHandler() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
        if (handler == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
            handler = new Handler();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
        return handler;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
    protected void installListeners( JSlider slider ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
        slider.addMouseListener(trackListener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
        slider.addMouseMotionListener(trackListener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
        slider.addFocusListener(focusListener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
        slider.addComponentListener(componentListener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
        slider.addPropertyChangeListener( propertyChangeListener );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
        slider.getModel().addChangeListener(changeListener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
    protected void uninstallListeners( JSlider slider ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
        slider.removeMouseListener(trackListener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
        slider.removeMouseMotionListener(trackListener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
        slider.removeFocusListener(focusListener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
        slider.removeComponentListener(componentListener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
        slider.removePropertyChangeListener( propertyChangeListener );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
        slider.getModel().removeChangeListener(changeListener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
        handler = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
    protected void installKeyboardActions( JSlider slider ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
        InputMap km = getInputMap(JComponent.WHEN_FOCUSED, slider);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
        SwingUtilities.replaceUIInputMap(slider, JComponent.WHEN_FOCUSED, km);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
        LazyActionMap.installLazyActionMap(slider, BasicSliderUI.class,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
                "Slider.actionMap");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
    InputMap getInputMap(int condition, JSlider slider) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
        if (condition == JComponent.WHEN_FOCUSED) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
            InputMap keyMap = (InputMap)DefaultLookup.get(slider, this,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
                  "Slider.focusInputMap");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
            InputMap rtlKeyMap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
            if (slider.getComponentOrientation().isLeftToRight() ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
                ((rtlKeyMap = (InputMap)DefaultLookup.get(slider, this,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
                          "Slider.focusInputMap.RightToLeft")) == null)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
                return keyMap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
                rtlKeyMap.setParent(keyMap);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
                return rtlKeyMap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
     * Populates ComboBox's actions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
    static void loadActionMap(LazyActionMap map) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
        map.put(new Actions(Actions.POSITIVE_UNIT_INCREMENT));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
        map.put(new Actions(Actions.POSITIVE_BLOCK_INCREMENT));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
        map.put(new Actions(Actions.NEGATIVE_UNIT_INCREMENT));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
        map.put(new Actions(Actions.NEGATIVE_BLOCK_INCREMENT));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
        map.put(new Actions(Actions.MIN_SCROLL_INCREMENT));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
        map.put(new Actions(Actions.MAX_SCROLL_INCREMENT));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
    protected void uninstallKeyboardActions( JSlider slider ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
        SwingUtilities.replaceUIActionMap(slider, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
        SwingUtilities.replaceUIInputMap(slider, JComponent.WHEN_FOCUSED,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
                                         null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
     * Returns the baseline.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
     * @throws NullPointerException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
     * @throws IllegalArgumentException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
     * @see javax.swing.JComponent#getBaseline(int, int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
    public int getBaseline(JComponent c, int width, int height) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
        super.getBaseline(c, width, height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
        if (slider.getPaintLabels() && labelsHaveSameBaselines()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
            FontMetrics metrics = slider.getFontMetrics(slider.getFont());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
            Insets insets = slider.getInsets();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
            Dimension thumbSize = getThumbSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
            if (slider.getOrientation() == JSlider.HORIZONTAL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
                int tickLength = getTickLength();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
                int contentHeight = height - insets.top - insets.bottom -
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
                    focusInsets.top - focusInsets.bottom;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
                int thumbHeight = thumbSize.height;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
                int centerSpacing = thumbHeight;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
                if (slider.getPaintTicks()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
                    centerSpacing += tickLength;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
                // Assume uniform labels.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
                centerSpacing += getHeightOfTallestLabel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
                int trackY = insets.top + focusInsets.top +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
                    (contentHeight - centerSpacing - 1) / 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
                int trackHeight = thumbHeight;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
                int tickY = trackY + trackHeight;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
                int tickHeight = tickLength;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
                if (!slider.getPaintTicks()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
                    tickHeight = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
                int labelY = tickY + tickHeight;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
                return labelY + metrics.getAscent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
            else { // vertical
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
                boolean inverted = slider.getInverted();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
                Integer value = inverted ? getLowestValue() :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
                                           getHighestValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
                if (value != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
                    int thumbHeight = thumbSize.height;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
                    int trackBuffer = Math.max(metrics.getHeight() / 2,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
                                               thumbHeight / 2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
                    int contentY = focusInsets.top + insets.top;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
                    int trackY = contentY + trackBuffer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
                    int trackHeight = height - focusInsets.top -
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
                        focusInsets.bottom - insets.top - insets.bottom -
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
                        trackBuffer - trackBuffer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
                    int yPosition = yPositionForValue(value, trackY,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
                                                      trackHeight);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
                    return yPosition - metrics.getHeight() / 2 +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
                        metrics.getAscent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
        return 0;
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
     * Returns an enum indicating how the baseline of the component
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
     * changes as the size changes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
     * @throws NullPointerException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
     * @see javax.swing.JComponent#getBaseline(int, int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
    public Component.BaselineResizeBehavior getBaselineResizeBehavior(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
            JComponent c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
        super.getBaselineResizeBehavior(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
        // NOTE: BasicSpinner really provides for CENTER_OFFSET, but
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
        // the default min/pref size is smaller than it should be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
        // so that getBaseline() doesn't implement the contract
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
        // for CENTER_OFFSET as defined in Component.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
        return Component.BaselineResizeBehavior.OTHER;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
     * Returns true if all the labels from the label table have the same
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
     * baseline.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
     * @return true if all the labels from the label table have the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
     *         same baseline
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
    protected boolean labelsHaveSameBaselines() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
        if (!checkedLabelBaselines) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
            checkedLabelBaselines = true;
26352
fa12363e21d6 8055254: Address source incompatability of JSlider generification
darcy
parents: 26351
diff changeset
   400
            @SuppressWarnings("rawtypes")
fa12363e21d6 8055254: Address source incompatability of JSlider generification
darcy
parents: 26351
diff changeset
   401
            Dictionary dictionary = slider.getLabelTable();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
            if (dictionary != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
                sameLabelBaselines = true;
26352
fa12363e21d6 8055254: Address source incompatability of JSlider generification
darcy
parents: 26351
diff changeset
   404
                Enumeration<?> elements = dictionary.elements();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
                int baseline = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
                while (elements.hasMoreElements()) {
26352
fa12363e21d6 8055254: Address source incompatability of JSlider generification
darcy
parents: 26351
diff changeset
   407
                    JComponent label = (JComponent) elements.nextElement();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
                    Dimension pref = label.getPreferredSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
                    int labelBaseline = label.getBaseline(pref.width,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
                                                          pref.height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
                    if (labelBaseline >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
                        if (baseline == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
                            baseline = labelBaseline;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
                        else if (baseline != labelBaseline) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
                            sameLabelBaselines = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
                            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
                    else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
                        sameLabelBaselines = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
                sameLabelBaselines = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
        return sameLabelBaselines;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
    public Dimension getPreferredHorizontalSize() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
        Dimension horizDim = (Dimension)DefaultLookup.get(slider,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
                this, "Slider.horizontalSize");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
        if (horizDim == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
            horizDim = new Dimension(200, 21);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
        return horizDim;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
    public Dimension getPreferredVerticalSize() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
        Dimension vertDim = (Dimension)DefaultLookup.get(slider,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
                this, "Slider.verticalSize");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
        if (vertDim == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
            vertDim = new Dimension(21, 200);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
        return vertDim;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
    public Dimension getMinimumHorizontalSize() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
        Dimension minHorizDim = (Dimension)DefaultLookup.get(slider,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
                this, "Slider.minimumHorizontalSize");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
        if (minHorizDim == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
            minHorizDim = new Dimension(36, 21);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
        return minHorizDim;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
    public Dimension getMinimumVerticalSize() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
        Dimension minVertDim = (Dimension)DefaultLookup.get(slider,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
                this, "Slider.minimumVerticalSize");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
        if (minVertDim == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
            minVertDim = new Dimension(21, 36);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
        return minVertDim;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
    public Dimension getPreferredSize(JComponent c)    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
        recalculateIfInsetsChanged();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
        Dimension d;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
        if ( slider.getOrientation() == JSlider.VERTICAL ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
            d = new Dimension(getPreferredVerticalSize());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
            d.width = insetCache.left + insetCache.right;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
            d.width += focusInsets.left + focusInsets.right;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
            d.width += trackRect.width + tickRect.width + labelRect.width;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
            d = new Dimension(getPreferredHorizontalSize());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
            d.height = insetCache.top + insetCache.bottom;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
            d.height += focusInsets.top + focusInsets.bottom;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
            d.height += trackRect.height + tickRect.height + labelRect.height;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
        return d;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
    public Dimension getMinimumSize(JComponent c)  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
        recalculateIfInsetsChanged();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
        Dimension d;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
        if ( slider.getOrientation() == JSlider.VERTICAL ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
            d = new Dimension(getMinimumVerticalSize());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
            d.width = insetCache.left + insetCache.right;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
            d.width += focusInsets.left + focusInsets.right;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
            d.width += trackRect.width + tickRect.width + labelRect.width;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
            d = new Dimension(getMinimumHorizontalSize());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
            d.height = insetCache.top + insetCache.bottom;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
            d.height += focusInsets.top + focusInsets.bottom;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
            d.height += trackRect.height + tickRect.height + labelRect.height;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
        return d;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
    public Dimension getMaximumSize(JComponent c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
        Dimension d = getPreferredSize(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
        if ( slider.getOrientation() == JSlider.VERTICAL ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
            d.height = Short.MAX_VALUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
            d.width = Short.MAX_VALUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
        return d;
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 calculateGeometry() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
        calculateFocusRect();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
        calculateContentRect();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
        calculateThumbSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
        calculateTrackBuffer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
        calculateTrackRect();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
        calculateTickRect();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
        calculateLabelRect();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
        calculateThumbLocation();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
    protected void calculateFocusRect() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
        focusRect.x = insetCache.left;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
        focusRect.y = insetCache.top;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
        focusRect.width = slider.getWidth() - (insetCache.left + insetCache.right);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
        focusRect.height = slider.getHeight() - (insetCache.top + insetCache.bottom);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
    protected void calculateThumbSize() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
        Dimension size = getThumbSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
        thumbRect.setSize( size.width, size.height );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
    protected void calculateContentRect() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
        contentRect.x = focusRect.x + focusInsets.left;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
        contentRect.y = focusRect.y + focusInsets.top;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
        contentRect.width = focusRect.width - (focusInsets.left + focusInsets.right);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
        contentRect.height = focusRect.height - (focusInsets.top + focusInsets.bottom);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
457
8682623ee3c3 6524424: JSlider Clicking In Tracks Behavior Inconsistent For Different Tick Spacings
rupashka
parents: 456
diff changeset
   550
    private int getTickSpacing() {
8682623ee3c3 6524424: JSlider Clicking In Tracks Behavior Inconsistent For Different Tick Spacings
rupashka
parents: 456
diff changeset
   551
        int majorTickSpacing = slider.getMajorTickSpacing();
8682623ee3c3 6524424: JSlider Clicking In Tracks Behavior Inconsistent For Different Tick Spacings
rupashka
parents: 456
diff changeset
   552
        int minorTickSpacing = slider.getMinorTickSpacing();
8682623ee3c3 6524424: JSlider Clicking In Tracks Behavior Inconsistent For Different Tick Spacings
rupashka
parents: 456
diff changeset
   553
8682623ee3c3 6524424: JSlider Clicking In Tracks Behavior Inconsistent For Different Tick Spacings
rupashka
parents: 456
diff changeset
   554
        int result;
8682623ee3c3 6524424: JSlider Clicking In Tracks Behavior Inconsistent For Different Tick Spacings
rupashka
parents: 456
diff changeset
   555
8682623ee3c3 6524424: JSlider Clicking In Tracks Behavior Inconsistent For Different Tick Spacings
rupashka
parents: 456
diff changeset
   556
        if (minorTickSpacing > 0) {
8682623ee3c3 6524424: JSlider Clicking In Tracks Behavior Inconsistent For Different Tick Spacings
rupashka
parents: 456
diff changeset
   557
            result = minorTickSpacing;
8682623ee3c3 6524424: JSlider Clicking In Tracks Behavior Inconsistent For Different Tick Spacings
rupashka
parents: 456
diff changeset
   558
        } else if (majorTickSpacing > 0) {
8682623ee3c3 6524424: JSlider Clicking In Tracks Behavior Inconsistent For Different Tick Spacings
rupashka
parents: 456
diff changeset
   559
            result = majorTickSpacing;
8682623ee3c3 6524424: JSlider Clicking In Tracks Behavior Inconsistent For Different Tick Spacings
rupashka
parents: 456
diff changeset
   560
        } else {
8682623ee3c3 6524424: JSlider Clicking In Tracks Behavior Inconsistent For Different Tick Spacings
rupashka
parents: 456
diff changeset
   561
            result = 0;
8682623ee3c3 6524424: JSlider Clicking In Tracks Behavior Inconsistent For Different Tick Spacings
rupashka
parents: 456
diff changeset
   562
        }
8682623ee3c3 6524424: JSlider Clicking In Tracks Behavior Inconsistent For Different Tick Spacings
rupashka
parents: 456
diff changeset
   563
8682623ee3c3 6524424: JSlider Clicking In Tracks Behavior Inconsistent For Different Tick Spacings
rupashka
parents: 456
diff changeset
   564
        return result;
8682623ee3c3 6524424: JSlider Clicking In Tracks Behavior Inconsistent For Different Tick Spacings
rupashka
parents: 456
diff changeset
   565
    }
8682623ee3c3 6524424: JSlider Clicking In Tracks Behavior Inconsistent For Different Tick Spacings
rupashka
parents: 456
diff changeset
   566
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
    protected void calculateThumbLocation() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
        if ( slider.getSnapToTicks() ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
            int sliderValue = slider.getValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
            int snappedValue = sliderValue;
457
8682623ee3c3 6524424: JSlider Clicking In Tracks Behavior Inconsistent For Different Tick Spacings
rupashka
parents: 456
diff changeset
   571
            int tickSpacing = getTickSpacing();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
            if ( tickSpacing != 0 ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
                // If it's not on a tick, change the value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
                if ( (sliderValue - slider.getMinimum()) % tickSpacing != 0 ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
                    float temp = (float)(sliderValue - slider.getMinimum()) / (float)tickSpacing;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
                    int whichTick = Math.round( temp );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
                    // This is the fix for the bug #6401380
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
                    if (temp - (int)temp == .5 && sliderValue < lastValue) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
                      whichTick --;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
                    snappedValue = slider.getMinimum() + (whichTick * tickSpacing);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
                if( snappedValue != sliderValue ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
                    slider.setValue( snappedValue );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
        if ( slider.getOrientation() == JSlider.HORIZONTAL ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
            int valuePosition = xPositionForValue(slider.getValue());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
            thumbRect.x = valuePosition - (thumbRect.width / 2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
            thumbRect.y = trackRect.y;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
            int valuePosition = yPositionForValue(slider.getValue());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
            thumbRect.x = trackRect.x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
            thumbRect.y = valuePosition - (thumbRect.height / 2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
    protected void calculateTrackBuffer() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
        if ( slider.getPaintLabels() && slider.getLabelTable()  != null ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
            Component highLabel = getHighestValueLabel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
            Component lowLabel = getLowestValueLabel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
            if ( slider.getOrientation() == JSlider.HORIZONTAL ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
                trackBuffer = Math.max( highLabel.getBounds().width, lowLabel.getBounds().width ) / 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
                trackBuffer = Math.max( trackBuffer, thumbRect.width / 2 );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
                trackBuffer = Math.max( highLabel.getBounds().height, lowLabel.getBounds().height ) / 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
                trackBuffer = Math.max( trackBuffer, thumbRect.height / 2 );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
            if ( slider.getOrientation() == JSlider.HORIZONTAL ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
                trackBuffer = thumbRect.width / 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
                trackBuffer = thumbRect.height / 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
    protected void calculateTrackRect() {
676
8cf833d60e87 6709530: There are unnecessary code in slider classes, such as in JSlider and SliderUIs
rupashka
parents: 457
diff changeset
   632
        int centerSpacing; // used to center sliders added using BorderLayout.CENTER (bug 4275631)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
        if ( slider.getOrientation() == JSlider.HORIZONTAL ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
            centerSpacing = thumbRect.height;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
            if ( slider.getPaintTicks() ) centerSpacing += getTickLength();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
            if ( slider.getPaintLabels() ) centerSpacing += getHeightOfTallestLabel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
            trackRect.x = contentRect.x + trackBuffer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
            trackRect.y = contentRect.y + (contentRect.height - centerSpacing - 1)/2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
            trackRect.width = contentRect.width - (trackBuffer * 2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
            trackRect.height = thumbRect.height;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
            centerSpacing = thumbRect.width;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
            if (BasicGraphicsUtils.isLeftToRight(slider)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
                if ( slider.getPaintTicks() ) centerSpacing += getTickLength();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
                if ( slider.getPaintLabels() ) centerSpacing += getWidthOfWidestLabel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
                if ( slider.getPaintTicks() ) centerSpacing -= getTickLength();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
                if ( slider.getPaintLabels() ) centerSpacing -= getWidthOfWidestLabel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
            trackRect.x = contentRect.x + (contentRect.width - centerSpacing - 1)/2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
            trackRect.y = contentRect.y + trackBuffer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
            trackRect.width = thumbRect.width;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
            trackRect.height = contentRect.height - (trackBuffer * 2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
    /**
26012
36ecb579dbc8 8050924: Fix doclint missing tag warnings in javax.swing.plaf.basic parts 5b,6b of 7
yan
parents: 25565
diff changeset
   660
     * Gets the height of the tick area for horizontal sliders and the width of
36ecb579dbc8 8050924: Fix doclint missing tag warnings in javax.swing.plaf.basic parts 5b,6b of 7
yan
parents: 25565
diff changeset
   661
     * the tick area for vertical sliders. BasicSliderUI uses the returned value
36ecb579dbc8 8050924: Fix doclint missing tag warnings in javax.swing.plaf.basic parts 5b,6b of 7
yan
parents: 25565
diff changeset
   662
     * to determine the tick area rectangle. If you want to give your ticks some
36ecb579dbc8 8050924: Fix doclint missing tag warnings in javax.swing.plaf.basic parts 5b,6b of 7
yan
parents: 25565
diff changeset
   663
     * room, make this larger than you need and paint your ticks away from the
36ecb579dbc8 8050924: Fix doclint missing tag warnings in javax.swing.plaf.basic parts 5b,6b of 7
yan
parents: 25565
diff changeset
   664
     * sides in paintTicks().
36ecb579dbc8 8050924: Fix doclint missing tag warnings in javax.swing.plaf.basic parts 5b,6b of 7
yan
parents: 25565
diff changeset
   665
     *
36ecb579dbc8 8050924: Fix doclint missing tag warnings in javax.swing.plaf.basic parts 5b,6b of 7
yan
parents: 25565
diff changeset
   666
     * @return an integer representing the height of the tick area for
36ecb579dbc8 8050924: Fix doclint missing tag warnings in javax.swing.plaf.basic parts 5b,6b of 7
yan
parents: 25565
diff changeset
   667
     * horizontal sliders, and the width of the tick area for the vertical
36ecb579dbc8 8050924: Fix doclint missing tag warnings in javax.swing.plaf.basic parts 5b,6b of 7
yan
parents: 25565
diff changeset
   668
     * sliders
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
    protected int getTickLength() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
        return 8;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
    protected void calculateTickRect() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
        if ( slider.getOrientation() == JSlider.HORIZONTAL ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
            tickRect.x = trackRect.x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
            tickRect.y = trackRect.y + trackRect.height;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
            tickRect.width = trackRect.width;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
            tickRect.height = (slider.getPaintTicks()) ? getTickLength() : 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
            tickRect.width = (slider.getPaintTicks()) ? getTickLength() : 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
            if(BasicGraphicsUtils.isLeftToRight(slider)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
                tickRect.x = trackRect.x + trackRect.width;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
                tickRect.x = trackRect.x - tickRect.width;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
            tickRect.y = trackRect.y;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
            tickRect.height = trackRect.height;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
    protected void calculateLabelRect() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
        if ( slider.getPaintLabels() ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
            if ( slider.getOrientation() == JSlider.HORIZONTAL ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
                labelRect.x = tickRect.x - trackBuffer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
                labelRect.y = tickRect.y + tickRect.height;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
                labelRect.width = tickRect.width + (trackBuffer * 2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
                labelRect.height = getHeightOfTallestLabel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
                if(BasicGraphicsUtils.isLeftToRight(slider)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
                    labelRect.x = tickRect.x + tickRect.width;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
                    labelRect.width = getWidthOfWidestLabel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
                else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
                    labelRect.width = getWidthOfWidestLabel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
                    labelRect.x = tickRect.x - labelRect.width;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
                labelRect.y = tickRect.y - trackBuffer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
                labelRect.height = tickRect.height + (trackBuffer * 2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
            if ( slider.getOrientation() == JSlider.HORIZONTAL ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
                labelRect.x = tickRect.x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
                labelRect.y = tickRect.y + tickRect.height;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
                labelRect.width = tickRect.width;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
                labelRect.height = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
                if(BasicGraphicsUtils.isLeftToRight(slider)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
                    labelRect.x = tickRect.x + tickRect.width;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
                else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
                    labelRect.x = tickRect.x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
                labelRect.y = tickRect.y;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
                labelRect.width = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
                labelRect.height = tickRect.height;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
    protected Dimension getThumbSize() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
        Dimension size = new Dimension();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
        if ( slider.getOrientation() == JSlider.VERTICAL ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
            size.width = 20;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
            size.height = 11;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
            size.width = 11;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
            size.height = 20;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
        return size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
    public class PropertyChangeHandler implements PropertyChangeListener {
21278
ef8a3a2a72f2 8022746: List of spelling errors in API doc
malenkov
parents: 20169
diff changeset
   752
        // NOTE: This class exists only for backward compatibility. All
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
        // its functionality has been moved into Handler. If you need to add
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
        // new functionality add it to the Handler, but make sure this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
        // class calls into the Handler.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
        public void propertyChange( PropertyChangeEvent e ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
            getHandler().propertyChange(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
    protected int getWidthOfWidestLabel() {
26352
fa12363e21d6 8055254: Address source incompatability of JSlider generification
darcy
parents: 26351
diff changeset
   762
        @SuppressWarnings("rawtypes")
fa12363e21d6 8055254: Address source incompatability of JSlider generification
darcy
parents: 26351
diff changeset
   763
        Dictionary dictionary = slider.getLabelTable();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
        int widest = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
        if ( dictionary != null ) {
25565
ce603b34c98d 8043548: Fix raw and unchecked lint warnings in javax.swing.plaf.*
darcy
parents: 23697
diff changeset
   766
            Enumeration<?> keys = dictionary.keys();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
            while ( keys.hasMoreElements() ) {
26352
fa12363e21d6 8055254: Address source incompatability of JSlider generification
darcy
parents: 26351
diff changeset
   768
                JComponent label = (JComponent) dictionary.get(keys.nextElement());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
                widest = Math.max( label.getPreferredSize().width, widest );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
        return widest;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
    protected int getHeightOfTallestLabel() {
26352
fa12363e21d6 8055254: Address source incompatability of JSlider generification
darcy
parents: 26351
diff changeset
   776
        @SuppressWarnings("rawtypes")
fa12363e21d6 8055254: Address source incompatability of JSlider generification
darcy
parents: 26351
diff changeset
   777
        Dictionary dictionary = slider.getLabelTable();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
        int tallest = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
        if ( dictionary != null ) {
25565
ce603b34c98d 8043548: Fix raw and unchecked lint warnings in javax.swing.plaf.*
darcy
parents: 23697
diff changeset
   780
            Enumeration<?> keys = dictionary.keys();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
            while ( keys.hasMoreElements() ) {
26352
fa12363e21d6 8055254: Address source incompatability of JSlider generification
darcy
parents: 26351
diff changeset
   782
                JComponent label = (JComponent) dictionary.get(keys.nextElement());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
                tallest = Math.max( label.getPreferredSize().height, tallest );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
        return tallest;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
    protected int getWidthOfHighValueLabel() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
        Component label = getHighestValueLabel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
        int width = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
        if ( label != null ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
            width = label.getPreferredSize().width;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
        return width;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
    protected int getWidthOfLowValueLabel() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
        Component label = getLowestValueLabel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
        int width = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
        if ( label != null ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
            width = label.getPreferredSize().width;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
        return width;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
    protected int getHeightOfHighValueLabel() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
        Component label = getHighestValueLabel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
        int height = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
        if ( label != null ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
            height = label.getPreferredSize().height;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
        return height;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
    protected int getHeightOfLowValueLabel() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
        Component label = getLowestValueLabel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
        int height = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
        if ( label != null ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
            height = label.getPreferredSize().height;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
        return height;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
    protected boolean drawInverted() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
        if (slider.getOrientation()==JSlider.HORIZONTAL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
            if(BasicGraphicsUtils.isLeftToRight(slider)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
                return slider.getInverted();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
                return !slider.getInverted();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
            return slider.getInverted();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
     * Returns the biggest value that has an entry in the label table.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
     * @return biggest value that has an entry in the label table, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
     *         null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
    protected Integer getHighestValue() {
26352
fa12363e21d6 8055254: Address source incompatability of JSlider generification
darcy
parents: 26351
diff changeset
   853
        @SuppressWarnings("rawtypes")
fa12363e21d6 8055254: Address source incompatability of JSlider generification
darcy
parents: 26351
diff changeset
   854
        Dictionary dictionary = slider.getLabelTable();
1852
958801d67667 6794836: BasicSliderUI throws NullPointerExc when JSlider maximum is Integer.MAX_VALUE
rupashka
parents: 715
diff changeset
   855
958801d67667 6794836: BasicSliderUI throws NullPointerExc when JSlider maximum is Integer.MAX_VALUE
rupashka
parents: 715
diff changeset
   856
        if (dictionary == null) {
958801d67667 6794836: BasicSliderUI throws NullPointerExc when JSlider maximum is Integer.MAX_VALUE
rupashka
parents: 715
diff changeset
   857
            return null;
958801d67667 6794836: BasicSliderUI throws NullPointerExc when JSlider maximum is Integer.MAX_VALUE
rupashka
parents: 715
diff changeset
   858
        }
958801d67667 6794836: BasicSliderUI throws NullPointerExc when JSlider maximum is Integer.MAX_VALUE
rupashka
parents: 715
diff changeset
   859
26352
fa12363e21d6 8055254: Address source incompatability of JSlider generification
darcy
parents: 26351
diff changeset
   860
        Enumeration<?> keys = dictionary.keys();
1852
958801d67667 6794836: BasicSliderUI throws NullPointerExc when JSlider maximum is Integer.MAX_VALUE
rupashka
parents: 715
diff changeset
   861
958801d67667 6794836: BasicSliderUI throws NullPointerExc when JSlider maximum is Integer.MAX_VALUE
rupashka
parents: 715
diff changeset
   862
        Integer max = null;
958801d67667 6794836: BasicSliderUI throws NullPointerExc when JSlider maximum is Integer.MAX_VALUE
rupashka
parents: 715
diff changeset
   863
958801d67667 6794836: BasicSliderUI throws NullPointerExc when JSlider maximum is Integer.MAX_VALUE
rupashka
parents: 715
diff changeset
   864
        while (keys.hasMoreElements()) {
26352
fa12363e21d6 8055254: Address source incompatability of JSlider generification
darcy
parents: 26351
diff changeset
   865
            Integer i = (Integer) keys.nextElement();
1852
958801d67667 6794836: BasicSliderUI throws NullPointerExc when JSlider maximum is Integer.MAX_VALUE
rupashka
parents: 715
diff changeset
   866
958801d67667 6794836: BasicSliderUI throws NullPointerExc when JSlider maximum is Integer.MAX_VALUE
rupashka
parents: 715
diff changeset
   867
            if (max == null || i > max) {
958801d67667 6794836: BasicSliderUI throws NullPointerExc when JSlider maximum is Integer.MAX_VALUE
rupashka
parents: 715
diff changeset
   868
                max = i;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   869
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   870
        }
1852
958801d67667 6794836: BasicSliderUI throws NullPointerExc when JSlider maximum is Integer.MAX_VALUE
rupashka
parents: 715
diff changeset
   871
958801d67667 6794836: BasicSliderUI throws NullPointerExc when JSlider maximum is Integer.MAX_VALUE
rupashka
parents: 715
diff changeset
   872
        return max;
2
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
     * Returns the smallest value that has an entry in the label table.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   877
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   878
     * @return smallest value that has an entry in the label table, or
26012
36ecb579dbc8 8050924: Fix doclint missing tag warnings in javax.swing.plaf.basic parts 5b,6b of 7
yan
parents: 25565
diff changeset
   879
     * null.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   880
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   881
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   882
    protected Integer getLowestValue() {
26352
fa12363e21d6 8055254: Address source incompatability of JSlider generification
darcy
parents: 26351
diff changeset
   883
        @SuppressWarnings("rawtypes")
fa12363e21d6 8055254: Address source incompatability of JSlider generification
darcy
parents: 26351
diff changeset
   884
        Dictionary dictionary = slider.getLabelTable();
1852
958801d67667 6794836: BasicSliderUI throws NullPointerExc when JSlider maximum is Integer.MAX_VALUE
rupashka
parents: 715
diff changeset
   885
958801d67667 6794836: BasicSliderUI throws NullPointerExc when JSlider maximum is Integer.MAX_VALUE
rupashka
parents: 715
diff changeset
   886
        if (dictionary == null) {
958801d67667 6794836: BasicSliderUI throws NullPointerExc when JSlider maximum is Integer.MAX_VALUE
rupashka
parents: 715
diff changeset
   887
            return null;
958801d67667 6794836: BasicSliderUI throws NullPointerExc when JSlider maximum is Integer.MAX_VALUE
rupashka
parents: 715
diff changeset
   888
        }
958801d67667 6794836: BasicSliderUI throws NullPointerExc when JSlider maximum is Integer.MAX_VALUE
rupashka
parents: 715
diff changeset
   889
26352
fa12363e21d6 8055254: Address source incompatability of JSlider generification
darcy
parents: 26351
diff changeset
   890
        Enumeration<?> keys = dictionary.keys();
1852
958801d67667 6794836: BasicSliderUI throws NullPointerExc when JSlider maximum is Integer.MAX_VALUE
rupashka
parents: 715
diff changeset
   891
958801d67667 6794836: BasicSliderUI throws NullPointerExc when JSlider maximum is Integer.MAX_VALUE
rupashka
parents: 715
diff changeset
   892
        Integer min = null;
958801d67667 6794836: BasicSliderUI throws NullPointerExc when JSlider maximum is Integer.MAX_VALUE
rupashka
parents: 715
diff changeset
   893
958801d67667 6794836: BasicSliderUI throws NullPointerExc when JSlider maximum is Integer.MAX_VALUE
rupashka
parents: 715
diff changeset
   894
        while (keys.hasMoreElements()) {
26352
fa12363e21d6 8055254: Address source incompatability of JSlider generification
darcy
parents: 26351
diff changeset
   895
            Integer i = (Integer) keys.nextElement();
1852
958801d67667 6794836: BasicSliderUI throws NullPointerExc when JSlider maximum is Integer.MAX_VALUE
rupashka
parents: 715
diff changeset
   896
958801d67667 6794836: BasicSliderUI throws NullPointerExc when JSlider maximum is Integer.MAX_VALUE
rupashka
parents: 715
diff changeset
   897
            if (min == null || i < min) {
958801d67667 6794836: BasicSliderUI throws NullPointerExc when JSlider maximum is Integer.MAX_VALUE
rupashka
parents: 715
diff changeset
   898
                min = i;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   899
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   900
        }
1852
958801d67667 6794836: BasicSliderUI throws NullPointerExc when JSlider maximum is Integer.MAX_VALUE
rupashka
parents: 715
diff changeset
   901
958801d67667 6794836: BasicSliderUI throws NullPointerExc when JSlider maximum is Integer.MAX_VALUE
rupashka
parents: 715
diff changeset
   902
        return min;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   903
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   904
90ce3da70b43 Initial load
duke
parents:
diff changeset
   905
90ce3da70b43 Initial load
duke
parents:
diff changeset
   906
    /**
26012
36ecb579dbc8 8050924: Fix doclint missing tag warnings in javax.swing.plaf.basic parts 5b,6b of 7
yan
parents: 25565
diff changeset
   907
     * Returns the label that corresponds to the highest slider value in the
36ecb579dbc8 8050924: Fix doclint missing tag warnings in javax.swing.plaf.basic parts 5b,6b of 7
yan
parents: 25565
diff changeset
   908
     * label table.
36ecb579dbc8 8050924: Fix doclint missing tag warnings in javax.swing.plaf.basic parts 5b,6b of 7
yan
parents: 25565
diff changeset
   909
     *
36ecb579dbc8 8050924: Fix doclint missing tag warnings in javax.swing.plaf.basic parts 5b,6b of 7
yan
parents: 25565
diff changeset
   910
     * @return the label that corresponds to the highest slider value in the
36ecb579dbc8 8050924: Fix doclint missing tag warnings in javax.swing.plaf.basic parts 5b,6b of 7
yan
parents: 25565
diff changeset
   911
     * label table
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   912
     * @see JSlider#setLabelTable
90ce3da70b43 Initial load
duke
parents:
diff changeset
   913
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   914
    protected Component getLowestValueLabel() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   915
        Integer min = getLowestValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   916
        if (min != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   917
            return (Component)slider.getLabelTable().get(min);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   918
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   919
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   920
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   921
90ce3da70b43 Initial load
duke
parents:
diff changeset
   922
    /**
26012
36ecb579dbc8 8050924: Fix doclint missing tag warnings in javax.swing.plaf.basic parts 5b,6b of 7
yan
parents: 25565
diff changeset
   923
     * Returns the label that corresponds to the lowest slider value in the
36ecb579dbc8 8050924: Fix doclint missing tag warnings in javax.swing.plaf.basic parts 5b,6b of 7
yan
parents: 25565
diff changeset
   924
     * label table.
36ecb579dbc8 8050924: Fix doclint missing tag warnings in javax.swing.plaf.basic parts 5b,6b of 7
yan
parents: 25565
diff changeset
   925
     *
36ecb579dbc8 8050924: Fix doclint missing tag warnings in javax.swing.plaf.basic parts 5b,6b of 7
yan
parents: 25565
diff changeset
   926
     * @return the label that corresponds to the lowest slider value in the
36ecb579dbc8 8050924: Fix doclint missing tag warnings in javax.swing.plaf.basic parts 5b,6b of 7
yan
parents: 25565
diff changeset
   927
     * label table
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   928
     * @see JSlider#setLabelTable
90ce3da70b43 Initial load
duke
parents:
diff changeset
   929
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   930
    protected Component getHighestValueLabel() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   931
        Integer max = getHighestValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   932
        if (max != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   933
            return (Component)slider.getLabelTable().get(max);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   934
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   935
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   936
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   937
90ce3da70b43 Initial load
duke
parents:
diff changeset
   938
    public void paint( Graphics g, JComponent c )   {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   939
        recalculateIfInsetsChanged();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   940
        recalculateIfOrientationChanged();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   941
        Rectangle clip = g.getClipBounds();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   942
90ce3da70b43 Initial load
duke
parents:
diff changeset
   943
        if ( !clip.intersects(trackRect) && slider.getPaintTrack())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   944
            calculateGeometry();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   945
90ce3da70b43 Initial load
duke
parents:
diff changeset
   946
        if ( slider.getPaintTrack() && clip.intersects( trackRect ) ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   947
            paintTrack( g );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   948
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   949
        if ( slider.getPaintTicks() && clip.intersects( tickRect ) ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   950
            paintTicks( g );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   951
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   952
        if ( slider.getPaintLabels() && clip.intersects( labelRect ) ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   953
            paintLabels( g );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   954
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   955
        if ( slider.hasFocus() && clip.intersects( focusRect ) ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   956
            paintFocus( g );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   957
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   958
        if ( clip.intersects( thumbRect ) ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   959
            paintThumb( g );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   960
        }
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 recalculateIfInsetsChanged() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   964
        Insets newInsets = slider.getInsets();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   965
        if ( !newInsets.equals( insetCache ) ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   966
            insetCache = newInsets;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   967
            calculateGeometry();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   968
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   969
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   970
90ce3da70b43 Initial load
duke
parents:
diff changeset
   971
    protected void recalculateIfOrientationChanged() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   972
        boolean ltr = BasicGraphicsUtils.isLeftToRight(slider);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   973
        if ( ltr!=leftToRightCache ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   974
            leftToRightCache = ltr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   975
            calculateGeometry();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   976
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   977
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   978
90ce3da70b43 Initial load
duke
parents:
diff changeset
   979
    public void paintFocus(Graphics g)  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   980
        g.setColor( getFocusColor() );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   981
90ce3da70b43 Initial load
duke
parents:
diff changeset
   982
        BasicGraphicsUtils.drawDashedRect( g, focusRect.x, focusRect.y,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   983
                                           focusRect.width, focusRect.height );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   984
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   985
90ce3da70b43 Initial load
duke
parents:
diff changeset
   986
    public void paintTrack(Graphics g)  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   987
90ce3da70b43 Initial load
duke
parents:
diff changeset
   988
        Rectangle trackBounds = trackRect;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   989
90ce3da70b43 Initial load
duke
parents:
diff changeset
   990
        if ( slider.getOrientation() == JSlider.HORIZONTAL ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   991
            int cy = (trackBounds.height / 2) - 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   992
            int cw = trackBounds.width;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   993
90ce3da70b43 Initial load
duke
parents:
diff changeset
   994
            g.translate(trackBounds.x, trackBounds.y + cy);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   995
90ce3da70b43 Initial load
duke
parents:
diff changeset
   996
            g.setColor(getShadowColor());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   997
            g.drawLine(0, 0, cw - 1, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   998
            g.drawLine(0, 1, 0, 2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   999
            g.setColor(getHighlightColor());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1000
            g.drawLine(0, 3, cw, 3);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1001
            g.drawLine(cw, 0, cw, 3);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1002
            g.setColor(Color.black);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1003
            g.drawLine(1, 1, cw-2, 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1004
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1005
            g.translate(-trackBounds.x, -(trackBounds.y + cy));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1006
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1007
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1008
            int cx = (trackBounds.width / 2) - 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1009
            int ch = trackBounds.height;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1010
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1011
            g.translate(trackBounds.x + cx, trackBounds.y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1012
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1013
            g.setColor(getShadowColor());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1014
            g.drawLine(0, 0, 0, ch - 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1015
            g.drawLine(1, 0, 2, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1016
            g.setColor(getHighlightColor());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1017
            g.drawLine(3, 0, 3, ch);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1018
            g.drawLine(0, ch, 3, ch);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1019
            g.setColor(Color.black);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1020
            g.drawLine(1, 1, 1, ch-2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1021
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1022
            g.translate(-(trackBounds.x + cx), -trackBounds.y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1023
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1024
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1025
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1026
    public void paintTicks(Graphics g)  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1027
        Rectangle tickBounds = tickRect;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1028
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1029
        g.setColor(DefaultLookup.getColor(slider, this, "Slider.tickColor", Color.black));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1030
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1031
        if ( slider.getOrientation() == JSlider.HORIZONTAL ) {
2488
4d54c9133cda 6794831: Infinite loop while painting ticks on Slider with maximum=MAX_INT
rupashka
parents: 1852
diff changeset
  1032
            g.translate(0, tickBounds.y);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1033
2488
4d54c9133cda 6794831: Infinite loop while painting ticks on Slider with maximum=MAX_INT
rupashka
parents: 1852
diff changeset
  1034
            if (slider.getMinorTickSpacing() > 0) {
4d54c9133cda 6794831: Infinite loop while painting ticks on Slider with maximum=MAX_INT
rupashka
parents: 1852
diff changeset
  1035
                int value = slider.getMinimum();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1036
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1037
                while ( value <= slider.getMaximum() ) {
2488
4d54c9133cda 6794831: Infinite loop while painting ticks on Slider with maximum=MAX_INT
rupashka
parents: 1852
diff changeset
  1038
                    int xPos = xPositionForValue(value);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1039
                    paintMinorTickForHorizSlider( g, tickBounds, xPos );
2488
4d54c9133cda 6794831: Infinite loop while painting ticks on Slider with maximum=MAX_INT
rupashka
parents: 1852
diff changeset
  1040
4d54c9133cda 6794831: Infinite loop while painting ticks on Slider with maximum=MAX_INT
rupashka
parents: 1852
diff changeset
  1041
                    // Overflow checking
4d54c9133cda 6794831: Infinite loop while painting ticks on Slider with maximum=MAX_INT
rupashka
parents: 1852
diff changeset
  1042
                    if (Integer.MAX_VALUE - slider.getMinorTickSpacing() < value) {
4d54c9133cda 6794831: Infinite loop while painting ticks on Slider with maximum=MAX_INT
rupashka
parents: 1852
diff changeset
  1043
                        break;
4d54c9133cda 6794831: Infinite loop while painting ticks on Slider with maximum=MAX_INT
rupashka
parents: 1852
diff changeset
  1044
                    }
4d54c9133cda 6794831: Infinite loop while painting ticks on Slider with maximum=MAX_INT
rupashka
parents: 1852
diff changeset
  1045
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1046
                    value += slider.getMinorTickSpacing();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1047
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1048
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1049
2488
4d54c9133cda 6794831: Infinite loop while painting ticks on Slider with maximum=MAX_INT
rupashka
parents: 1852
diff changeset
  1050
            if (slider.getMajorTickSpacing() > 0) {
4d54c9133cda 6794831: Infinite loop while painting ticks on Slider with maximum=MAX_INT
rupashka
parents: 1852
diff changeset
  1051
                int value = slider.getMinimum();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1052
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1053
                while ( value <= slider.getMaximum() ) {
2488
4d54c9133cda 6794831: Infinite loop while painting ticks on Slider with maximum=MAX_INT
rupashka
parents: 1852
diff changeset
  1054
                    int xPos = xPositionForValue(value);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1055
                    paintMajorTickForHorizSlider( g, tickBounds, xPos );
2488
4d54c9133cda 6794831: Infinite loop while painting ticks on Slider with maximum=MAX_INT
rupashka
parents: 1852
diff changeset
  1056
4d54c9133cda 6794831: Infinite loop while painting ticks on Slider with maximum=MAX_INT
rupashka
parents: 1852
diff changeset
  1057
                    // Overflow checking
4d54c9133cda 6794831: Infinite loop while painting ticks on Slider with maximum=MAX_INT
rupashka
parents: 1852
diff changeset
  1058
                    if (Integer.MAX_VALUE - slider.getMajorTickSpacing() < value) {
4d54c9133cda 6794831: Infinite loop while painting ticks on Slider with maximum=MAX_INT
rupashka
parents: 1852
diff changeset
  1059
                        break;
4d54c9133cda 6794831: Infinite loop while painting ticks on Slider with maximum=MAX_INT
rupashka
parents: 1852
diff changeset
  1060
                    }
4d54c9133cda 6794831: Infinite loop while painting ticks on Slider with maximum=MAX_INT
rupashka
parents: 1852
diff changeset
  1061
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1062
                    value += slider.getMajorTickSpacing();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1063
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1064
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1065
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1066
            g.translate( 0, -tickBounds.y);
2488
4d54c9133cda 6794831: Infinite loop while painting ticks on Slider with maximum=MAX_INT
rupashka
parents: 1852
diff changeset
  1067
        } else {
4d54c9133cda 6794831: Infinite loop while painting ticks on Slider with maximum=MAX_INT
rupashka
parents: 1852
diff changeset
  1068
            g.translate(tickBounds.x, 0);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1069
2488
4d54c9133cda 6794831: Infinite loop while painting ticks on Slider with maximum=MAX_INT
rupashka
parents: 1852
diff changeset
  1070
            if (slider.getMinorTickSpacing() > 0) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1071
                int offset = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1072
                if(!BasicGraphicsUtils.isLeftToRight(slider)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1073
                    offset = tickBounds.width - tickBounds.width / 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1074
                    g.translate(offset, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1075
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1076
2488
4d54c9133cda 6794831: Infinite loop while painting ticks on Slider with maximum=MAX_INT
rupashka
parents: 1852
diff changeset
  1077
                int value = slider.getMinimum();
4d54c9133cda 6794831: Infinite loop while painting ticks on Slider with maximum=MAX_INT
rupashka
parents: 1852
diff changeset
  1078
4d54c9133cda 6794831: Infinite loop while painting ticks on Slider with maximum=MAX_INT
rupashka
parents: 1852
diff changeset
  1079
                while (value <= slider.getMaximum()) {
4d54c9133cda 6794831: Infinite loop while painting ticks on Slider with maximum=MAX_INT
rupashka
parents: 1852
diff changeset
  1080
                    int yPos = yPositionForValue(value);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1081
                    paintMinorTickForVertSlider( g, tickBounds, yPos );
2488
4d54c9133cda 6794831: Infinite loop while painting ticks on Slider with maximum=MAX_INT
rupashka
parents: 1852
diff changeset
  1082
4d54c9133cda 6794831: Infinite loop while painting ticks on Slider with maximum=MAX_INT
rupashka
parents: 1852
diff changeset
  1083
                    // Overflow checking
4d54c9133cda 6794831: Infinite loop while painting ticks on Slider with maximum=MAX_INT
rupashka
parents: 1852
diff changeset
  1084
                    if (Integer.MAX_VALUE - slider.getMinorTickSpacing() < value) {
4d54c9133cda 6794831: Infinite loop while painting ticks on Slider with maximum=MAX_INT
rupashka
parents: 1852
diff changeset
  1085
                        break;
4d54c9133cda 6794831: Infinite loop while painting ticks on Slider with maximum=MAX_INT
rupashka
parents: 1852
diff changeset
  1086
                    }
4d54c9133cda 6794831: Infinite loop while painting ticks on Slider with maximum=MAX_INT
rupashka
parents: 1852
diff changeset
  1087
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1088
                    value += slider.getMinorTickSpacing();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1089
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1090
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1091
                if(!BasicGraphicsUtils.isLeftToRight(slider)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1092
                    g.translate(-offset, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1093
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1094
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1095
2488
4d54c9133cda 6794831: Infinite loop while painting ticks on Slider with maximum=MAX_INT
rupashka
parents: 1852
diff changeset
  1096
            if (slider.getMajorTickSpacing() > 0) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1097
                if(!BasicGraphicsUtils.isLeftToRight(slider)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1098
                    g.translate(2, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1099
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1100
2488
4d54c9133cda 6794831: Infinite loop while painting ticks on Slider with maximum=MAX_INT
rupashka
parents: 1852
diff changeset
  1101
                int value = slider.getMinimum();
4d54c9133cda 6794831: Infinite loop while painting ticks on Slider with maximum=MAX_INT
rupashka
parents: 1852
diff changeset
  1102
4d54c9133cda 6794831: Infinite loop while painting ticks on Slider with maximum=MAX_INT
rupashka
parents: 1852
diff changeset
  1103
                while (value <= slider.getMaximum()) {
4d54c9133cda 6794831: Infinite loop while painting ticks on Slider with maximum=MAX_INT
rupashka
parents: 1852
diff changeset
  1104
                    int yPos = yPositionForValue(value);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1105
                    paintMajorTickForVertSlider( g, tickBounds, yPos );
2488
4d54c9133cda 6794831: Infinite loop while painting ticks on Slider with maximum=MAX_INT
rupashka
parents: 1852
diff changeset
  1106
4d54c9133cda 6794831: Infinite loop while painting ticks on Slider with maximum=MAX_INT
rupashka
parents: 1852
diff changeset
  1107
                    // Overflow checking
4d54c9133cda 6794831: Infinite loop while painting ticks on Slider with maximum=MAX_INT
rupashka
parents: 1852
diff changeset
  1108
                    if (Integer.MAX_VALUE - slider.getMajorTickSpacing() < value) {
4d54c9133cda 6794831: Infinite loop while painting ticks on Slider with maximum=MAX_INT
rupashka
parents: 1852
diff changeset
  1109
                        break;
4d54c9133cda 6794831: Infinite loop while painting ticks on Slider with maximum=MAX_INT
rupashka
parents: 1852
diff changeset
  1110
                    }
4d54c9133cda 6794831: Infinite loop while painting ticks on Slider with maximum=MAX_INT
rupashka
parents: 1852
diff changeset
  1111
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1112
                    value += slider.getMajorTickSpacing();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1113
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1114
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1115
                if(!BasicGraphicsUtils.isLeftToRight(slider)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1116
                    g.translate(-2, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1117
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1118
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1119
            g.translate(-tickBounds.x, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1120
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1121
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1122
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1123
    protected void paintMinorTickForHorizSlider( Graphics g, Rectangle tickBounds, int x ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1124
        g.drawLine( x, 0, x, tickBounds.height / 2 - 1 );
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1125
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1126
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1127
    protected void paintMajorTickForHorizSlider( Graphics g, Rectangle tickBounds, int x ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1128
        g.drawLine( x, 0, x, tickBounds.height - 2 );
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1129
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1130
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1131
    protected void paintMinorTickForVertSlider( Graphics g, Rectangle tickBounds, int y ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1132
        g.drawLine( 0, y, tickBounds.width / 2 - 1, y );
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1133
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1134
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1135
    protected void paintMajorTickForVertSlider( Graphics g, Rectangle tickBounds, int y ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1136
        g.drawLine( 0, y,  tickBounds.width - 2, y );
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1137
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1138
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1139
    public void paintLabels( Graphics g ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1140
        Rectangle labelBounds = labelRect;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1141
26352
fa12363e21d6 8055254: Address source incompatability of JSlider generification
darcy
parents: 26351
diff changeset
  1142
        @SuppressWarnings("rawtypes")
fa12363e21d6 8055254: Address source incompatability of JSlider generification
darcy
parents: 26351
diff changeset
  1143
        Dictionary dictionary = slider.getLabelTable();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1144
        if ( dictionary != null ) {
26352
fa12363e21d6 8055254: Address source incompatability of JSlider generification
darcy
parents: 26351
diff changeset
  1145
            Enumeration<?> keys = dictionary.keys();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1146
            int minValue = slider.getMinimum();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1147
            int maxValue = slider.getMaximum();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1148
            boolean enabled = slider.isEnabled();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1149
            while ( keys.hasMoreElements() ) {
26352
fa12363e21d6 8055254: Address source incompatability of JSlider generification
darcy
parents: 26351
diff changeset
  1150
                Integer key = (Integer)keys.nextElement();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1151
                int value = key.intValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1152
                if (value >= minValue && value <= maxValue) {
26352
fa12363e21d6 8055254: Address source incompatability of JSlider generification
darcy
parents: 26351
diff changeset
  1153
                    JComponent label = (JComponent) dictionary.get(key);
676
8cf833d60e87 6709530: There are unnecessary code in slider classes, such as in JSlider and SliderUIs
rupashka
parents: 457
diff changeset
  1154
                    label.setEnabled(enabled);
8cf833d60e87 6709530: There are unnecessary code in slider classes, such as in JSlider and SliderUIs
rupashka
parents: 457
diff changeset
  1155
677
20718977427b 4987336: JSlider doesn't show label's animated icon
rupashka
parents: 676
diff changeset
  1156
                    if (label instanceof JLabel) {
20718977427b 4987336: JSlider doesn't show label's animated icon
rupashka
parents: 676
diff changeset
  1157
                        Icon icon = label.isEnabled() ? ((JLabel) label).getIcon() : ((JLabel) label).getDisabledIcon();
20718977427b 4987336: JSlider doesn't show label's animated icon
rupashka
parents: 676
diff changeset
  1158
20718977427b 4987336: JSlider doesn't show label's animated icon
rupashka
parents: 676
diff changeset
  1159
                        if (icon instanceof ImageIcon) {
20718977427b 4987336: JSlider doesn't show label's animated icon
rupashka
parents: 676
diff changeset
  1160
                            // Register Slider as an image observer. It allows to catch notifications about
20718977427b 4987336: JSlider doesn't show label's animated icon
rupashka
parents: 676
diff changeset
  1161
                            // image changes (e.g. gif animation)
20718977427b 4987336: JSlider doesn't show label's animated icon
rupashka
parents: 676
diff changeset
  1162
                            Toolkit.getDefaultToolkit().checkImage(((ImageIcon) icon).getImage(), -1, -1, slider);
20718977427b 4987336: JSlider doesn't show label's animated icon
rupashka
parents: 676
diff changeset
  1163
                        }
20718977427b 4987336: JSlider doesn't show label's animated icon
rupashka
parents: 676
diff changeset
  1164
                    }
20718977427b 4987336: JSlider doesn't show label's animated icon
rupashka
parents: 676
diff changeset
  1165
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1166
                    if ( slider.getOrientation() == JSlider.HORIZONTAL ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1167
                        g.translate( 0, labelBounds.y );
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1168
                        paintHorizontalLabel( g, value, label );
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1169
                        g.translate( 0, -labelBounds.y );
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1170
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1171
                    else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1172
                        int offset = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1173
                        if (!BasicGraphicsUtils.isLeftToRight(slider)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1174
                            offset = labelBounds.width -
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1175
                                label.getPreferredSize().width;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1176
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1177
                        g.translate( labelBounds.x + offset, 0 );
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1178
                        paintVerticalLabel( g, value, label );
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1179
                        g.translate( -labelBounds.x - offset, 0 );
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1180
                    }
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
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1186
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1187
    /**
26012
36ecb579dbc8 8050924: Fix doclint missing tag warnings in javax.swing.plaf.basic parts 5b,6b of 7
yan
parents: 25565
diff changeset
  1188
     * Called for every label in the label table. Used to draw the labels for
36ecb579dbc8 8050924: Fix doclint missing tag warnings in javax.swing.plaf.basic parts 5b,6b of 7
yan
parents: 25565
diff changeset
  1189
     * horizontal sliders. The graphics have been translated to labelRect.y
36ecb579dbc8 8050924: Fix doclint missing tag warnings in javax.swing.plaf.basic parts 5b,6b of 7
yan
parents: 25565
diff changeset
  1190
     * already.
36ecb579dbc8 8050924: Fix doclint missing tag warnings in javax.swing.plaf.basic parts 5b,6b of 7
yan
parents: 25565
diff changeset
  1191
     *
36ecb579dbc8 8050924: Fix doclint missing tag warnings in javax.swing.plaf.basic parts 5b,6b of 7
yan
parents: 25565
diff changeset
  1192
     * @param g the graphics context in which to paint
36ecb579dbc8 8050924: Fix doclint missing tag warnings in javax.swing.plaf.basic parts 5b,6b of 7
yan
parents: 25565
diff changeset
  1193
     * @param value the value of the slider
36ecb579dbc8 8050924: Fix doclint missing tag warnings in javax.swing.plaf.basic parts 5b,6b of 7
yan
parents: 25565
diff changeset
  1194
     * @param label the component label in the label table that needs to be
36ecb579dbc8 8050924: Fix doclint missing tag warnings in javax.swing.plaf.basic parts 5b,6b of 7
yan
parents: 25565
diff changeset
  1195
     * painted
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1196
     * @see JSlider#setLabelTable
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1197
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1198
    protected void paintHorizontalLabel( Graphics g, int value, Component label ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1199
        int labelCenter = xPositionForValue( value );
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1200
        int labelLeft = labelCenter - (label.getPreferredSize().width / 2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1201
        g.translate( labelLeft, 0 );
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1202
        label.paint( g );
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1203
        g.translate( -labelLeft, 0 );
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1204
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1205
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1206
    /**
26012
36ecb579dbc8 8050924: Fix doclint missing tag warnings in javax.swing.plaf.basic parts 5b,6b of 7
yan
parents: 25565
diff changeset
  1207
     * Called for every label in the label table. Used to draw the labels for
36ecb579dbc8 8050924: Fix doclint missing tag warnings in javax.swing.plaf.basic parts 5b,6b of 7
yan
parents: 25565
diff changeset
  1208
     * vertical sliders. The graphics have been translated to labelRect.x
36ecb579dbc8 8050924: Fix doclint missing tag warnings in javax.swing.plaf.basic parts 5b,6b of 7
yan
parents: 25565
diff changeset
  1209
     * already.
36ecb579dbc8 8050924: Fix doclint missing tag warnings in javax.swing.plaf.basic parts 5b,6b of 7
yan
parents: 25565
diff changeset
  1210
     *
36ecb579dbc8 8050924: Fix doclint missing tag warnings in javax.swing.plaf.basic parts 5b,6b of 7
yan
parents: 25565
diff changeset
  1211
     * @param g the graphics context in which to paint
36ecb579dbc8 8050924: Fix doclint missing tag warnings in javax.swing.plaf.basic parts 5b,6b of 7
yan
parents: 25565
diff changeset
  1212
     * @param value the value of the slider
36ecb579dbc8 8050924: Fix doclint missing tag warnings in javax.swing.plaf.basic parts 5b,6b of 7
yan
parents: 25565
diff changeset
  1213
     * @param label the component label in the label table that needs to be
36ecb579dbc8 8050924: Fix doclint missing tag warnings in javax.swing.plaf.basic parts 5b,6b of 7
yan
parents: 25565
diff changeset
  1214
     * painted
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1215
     * @see JSlider#setLabelTable
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1216
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1217
    protected void paintVerticalLabel( Graphics g, int value, Component label ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1218
        int labelCenter = yPositionForValue( value );
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1219
        int labelTop = labelCenter - (label.getPreferredSize().height / 2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1220
        g.translate( 0, labelTop );
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1221
        label.paint( g );
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1222
        g.translate( 0, -labelTop );
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1223
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1224
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1225
    public void paintThumb(Graphics g)  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1226
        Rectangle knobBounds = thumbRect;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1227
        int w = knobBounds.width;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1228
        int h = knobBounds.height;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1229
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1230
        g.translate(knobBounds.x, knobBounds.y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1231
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1232
        if ( slider.isEnabled() ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1233
            g.setColor(slider.getBackground());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1234
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1235
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1236
            g.setColor(slider.getBackground().darker());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1237
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1238
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1239
        Boolean paintThumbArrowShape =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1240
            (Boolean)slider.getClientProperty("Slider.paintThumbArrowShape");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1241
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1242
        if ((!slider.getPaintTicks() && paintThumbArrowShape == null) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1243
            paintThumbArrowShape == Boolean.FALSE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1244
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1245
            // "plain" version
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1246
            g.fillRect(0, 0, w, h);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1247
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1248
            g.setColor(Color.black);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1249
            g.drawLine(0, h-1, w-1, h-1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1250
            g.drawLine(w-1, 0, w-1, h-1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1251
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1252
            g.setColor(highlightColor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1253
            g.drawLine(0, 0, 0, h-2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1254
            g.drawLine(1, 0, w-2, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1255
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1256
            g.setColor(shadowColor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1257
            g.drawLine(1, h-2, w-2, h-2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1258
            g.drawLine(w-2, 1, w-2, h-3);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1259
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1260
        else if ( slider.getOrientation() == JSlider.HORIZONTAL ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1261
            int cw = w / 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1262
            g.fillRect(1, 1, w-3, h-1-cw);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1263
            Polygon p = new Polygon();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1264
            p.addPoint(1, h-cw);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1265
            p.addPoint(cw-1, h-1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1266
            p.addPoint(w-2, h-1-cw);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1267
            g.fillPolygon(p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1268
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1269
            g.setColor(highlightColor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1270
            g.drawLine(0, 0, w-2, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1271
            g.drawLine(0, 1, 0, h-1-cw);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1272
            g.drawLine(0, h-cw, cw-1, h-1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1273
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1274
            g.setColor(Color.black);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1275
            g.drawLine(w-1, 0, w-1, h-2-cw);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1276
            g.drawLine(w-1, h-1-cw, w-1-cw, h-1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1277
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1278
            g.setColor(shadowColor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1279
            g.drawLine(w-2, 1, w-2, h-2-cw);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1280
            g.drawLine(w-2, h-1-cw, w-1-cw, h-2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1281
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1282
        else {  // vertical
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1283
            int cw = h / 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1284
            if(BasicGraphicsUtils.isLeftToRight(slider)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1285
                  g.fillRect(1, 1, w-1-cw, h-3);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1286
                  Polygon p = new Polygon();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1287
                  p.addPoint(w-cw-1, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1288
                  p.addPoint(w-1, cw);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1289
                  p.addPoint(w-1-cw, h-2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1290
                  g.fillPolygon(p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1291
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1292
                  g.setColor(highlightColor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1293
                  g.drawLine(0, 0, 0, h - 2);                  // left
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1294
                  g.drawLine(1, 0, w-1-cw, 0);                 // top
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1295
                  g.drawLine(w-cw-1, 0, w-1, cw);              // top slant
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1296
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1297
                  g.setColor(Color.black);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1298
                  g.drawLine(0, h-1, w-2-cw, h-1);             // bottom
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1299
                  g.drawLine(w-1-cw, h-1, w-1, h-1-cw);        // bottom slant
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1300
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1301
                  g.setColor(shadowColor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1302
                  g.drawLine(1, h-2, w-2-cw,  h-2 );         // bottom
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1303
                  g.drawLine(w-1-cw, h-2, w-2, h-cw-1 );     // bottom slant
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1304
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1305
            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1306
                  g.fillRect(5, 1, w-1-cw, h-3);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1307
                  Polygon p = new Polygon();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1308
                  p.addPoint(cw, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1309
                  p.addPoint(0, cw);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1310
                  p.addPoint(cw, h-2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1311
                  g.fillPolygon(p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1312
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1313
                  g.setColor(highlightColor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1314
                  g.drawLine(cw-1, 0, w-2, 0);             // top
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1315
                  g.drawLine(0, cw, cw, 0);                // top slant
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1316
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1317
                  g.setColor(Color.black);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1318
                  g.drawLine(0, h-1-cw, cw, h-1 );         // bottom slant
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1319
                  g.drawLine(cw, h-1, w-1, h-1);           // bottom
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1320
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1321
                  g.setColor(shadowColor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1322
                  g.drawLine(cw, h-2, w-2,  h-2 );         // bottom
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1323
                  g.drawLine(w-1, 1, w-1,  h-2 );          // right
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1324
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1325
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1326
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1327
        g.translate(-knobBounds.x, -knobBounds.y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1328
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1329
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1330
    // Used exclusively by setThumbLocation()
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1331
    private static Rectangle unionRect = new Rectangle();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1332
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1333
    public void setThumbLocation(int x, int y)  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1334
        unionRect.setBounds( thumbRect );
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1335
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1336
        thumbRect.setLocation( x, y );
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1337
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1338
        SwingUtilities.computeUnion( thumbRect.x, thumbRect.y, thumbRect.width, thumbRect.height, unionRect );
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1339
        slider.repaint( unionRect.x, unionRect.y, unionRect.width, unionRect.height );
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1340
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1341
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1342
    public void scrollByBlock(int direction)    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1343
        synchronized(slider)    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1344
            int blockIncrement =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1345
                (slider.getMaximum() - slider.getMinimum()) / 10;
457
8682623ee3c3 6524424: JSlider Clicking In Tracks Behavior Inconsistent For Different Tick Spacings
rupashka
parents: 456
diff changeset
  1346
            if (blockIncrement == 0) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1347
                blockIncrement = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1348
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1349
457
8682623ee3c3 6524424: JSlider Clicking In Tracks Behavior Inconsistent For Different Tick Spacings
rupashka
parents: 456
diff changeset
  1350
            if (slider.getSnapToTicks()) {
8682623ee3c3 6524424: JSlider Clicking In Tracks Behavior Inconsistent For Different Tick Spacings
rupashka
parents: 456
diff changeset
  1351
                int tickSpacing = getTickSpacing();
8682623ee3c3 6524424: JSlider Clicking In Tracks Behavior Inconsistent For Different Tick Spacings
rupashka
parents: 456
diff changeset
  1352
8682623ee3c3 6524424: JSlider Clicking In Tracks Behavior Inconsistent For Different Tick Spacings
rupashka
parents: 456
diff changeset
  1353
                if (blockIncrement < tickSpacing) {
8682623ee3c3 6524424: JSlider Clicking In Tracks Behavior Inconsistent For Different Tick Spacings
rupashka
parents: 456
diff changeset
  1354
                    blockIncrement = tickSpacing;
8682623ee3c3 6524424: JSlider Clicking In Tracks Behavior Inconsistent For Different Tick Spacings
rupashka
parents: 456
diff changeset
  1355
                }
8682623ee3c3 6524424: JSlider Clicking In Tracks Behavior Inconsistent For Different Tick Spacings
rupashka
parents: 456
diff changeset
  1356
            }
8682623ee3c3 6524424: JSlider Clicking In Tracks Behavior Inconsistent For Different Tick Spacings
rupashka
parents: 456
diff changeset
  1357
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1358
            int delta = blockIncrement * ((direction > 0) ? POSITIVE_SCROLL : NEGATIVE_SCROLL);
457
8682623ee3c3 6524424: JSlider Clicking In Tracks Behavior Inconsistent For Different Tick Spacings
rupashka
parents: 456
diff changeset
  1359
            slider.setValue(slider.getValue() + delta);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1360
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1361
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1362
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1363
    public void scrollByUnit(int direction) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1364
        synchronized(slider)    {
457
8682623ee3c3 6524424: JSlider Clicking In Tracks Behavior Inconsistent For Different Tick Spacings
rupashka
parents: 456
diff changeset
  1365
            int delta = ((direction > 0) ? POSITIVE_SCROLL : NEGATIVE_SCROLL);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1366
457
8682623ee3c3 6524424: JSlider Clicking In Tracks Behavior Inconsistent For Different Tick Spacings
rupashka
parents: 456
diff changeset
  1367
            if (slider.getSnapToTicks()) {
8682623ee3c3 6524424: JSlider Clicking In Tracks Behavior Inconsistent For Different Tick Spacings
rupashka
parents: 456
diff changeset
  1368
                delta *= getTickSpacing();
8682623ee3c3 6524424: JSlider Clicking In Tracks Behavior Inconsistent For Different Tick Spacings
rupashka
parents: 456
diff changeset
  1369
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1370
457
8682623ee3c3 6524424: JSlider Clicking In Tracks Behavior Inconsistent For Different Tick Spacings
rupashka
parents: 456
diff changeset
  1371
            slider.setValue(slider.getValue() + delta);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1372
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1373
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1374
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1375
    /**
26012
36ecb579dbc8 8050924: Fix doclint missing tag warnings in javax.swing.plaf.basic parts 5b,6b of 7
yan
parents: 25565
diff changeset
  1376
     * This function is called when a mousePressed was detected in the track,
36ecb579dbc8 8050924: Fix doclint missing tag warnings in javax.swing.plaf.basic parts 5b,6b of 7
yan
parents: 25565
diff changeset
  1377
     * not in the thumb. The default behavior is to scroll by block. You can
36ecb579dbc8 8050924: Fix doclint missing tag warnings in javax.swing.plaf.basic parts 5b,6b of 7
yan
parents: 25565
diff changeset
  1378
     * override this method to stop it from scrolling or to add additional
36ecb579dbc8 8050924: Fix doclint missing tag warnings in javax.swing.plaf.basic parts 5b,6b of 7
yan
parents: 25565
diff changeset
  1379
     * behavior.
36ecb579dbc8 8050924: Fix doclint missing tag warnings in javax.swing.plaf.basic parts 5b,6b of 7
yan
parents: 25565
diff changeset
  1380
     *
36ecb579dbc8 8050924: Fix doclint missing tag warnings in javax.swing.plaf.basic parts 5b,6b of 7
yan
parents: 25565
diff changeset
  1381
     * @param dir the direction and number of blocks to scroll
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1382
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1383
    protected void scrollDueToClickInTrack( int dir ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1384
        scrollByBlock( dir );
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1385
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1386
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1387
    protected int xPositionForValue( int value )    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1388
        int min = slider.getMinimum();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1389
        int max = slider.getMaximum();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1390
        int trackLength = trackRect.width;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1391
        double valueRange = (double)max - (double)min;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1392
        double pixelsPerValue = (double)trackLength / valueRange;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1393
        int trackLeft = trackRect.x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1394
        int trackRight = trackRect.x + (trackRect.width - 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1395
        int xPosition;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1396
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1397
        if ( !drawInverted() ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1398
            xPosition = trackLeft;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1399
            xPosition += Math.round( pixelsPerValue * ((double)value - min) );
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1400
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1401
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1402
            xPosition = trackRight;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1403
            xPosition -= Math.round( pixelsPerValue * ((double)value - min) );
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1404
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1405
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1406
        xPosition = Math.max( trackLeft, xPosition );
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1407
        xPosition = Math.min( trackRight, xPosition );
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1408
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1409
        return xPosition;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1410
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1411
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1412
    protected int yPositionForValue( int value )  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1413
        return yPositionForValue(value, trackRect.y, trackRect.height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1414
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1415
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1416
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1417
     * Returns the y location for the specified value.  No checking is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1418
     * done on the arguments.  In particular if <code>trackHeight</code> is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1419
     * negative undefined results may occur.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1420
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1421
     * @param value the slider value to get the location for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1422
     * @param trackY y-origin of the track
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1423
     * @param trackHeight the height of the track
26012
36ecb579dbc8 8050924: Fix doclint missing tag warnings in javax.swing.plaf.basic parts 5b,6b of 7
yan
parents: 25565
diff changeset
  1424
     * @return the y location for the specified value of the slider
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1425
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1426
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1427
    protected int yPositionForValue(int value, int trackY, int trackHeight) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1428
        int min = slider.getMinimum();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1429
        int max = slider.getMaximum();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1430
        double valueRange = (double)max - (double)min;
676
8cf833d60e87 6709530: There are unnecessary code in slider classes, such as in JSlider and SliderUIs
rupashka
parents: 457
diff changeset
  1431
        double pixelsPerValue = (double)trackHeight / valueRange;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1432
        int trackBottom = trackY + (trackHeight - 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1433
        int yPosition;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1434
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1435
        if ( !drawInverted() ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1436
            yPosition = trackY;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1437
            yPosition += Math.round( pixelsPerValue * ((double)max - value ) );
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1438
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1439
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1440
            yPosition = trackY;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1441
            yPosition += Math.round( pixelsPerValue * ((double)value - min) );
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1442
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1443
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1444
        yPosition = Math.max( trackY, yPosition );
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1445
        yPosition = Math.min( trackBottom, yPosition );
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1446
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1447
        return yPosition;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1448
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1449
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1450
    /**
4394
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 3735
diff changeset
  1451
     * Returns the value at the y position. If {@code yPos} is beyond the
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 3735
diff changeset
  1452
     * track at the the bottom or the top, this method sets the value to either
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 3735
diff changeset
  1453
     * the minimum or maximum value of the slider, depending on if the slider
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 3735
diff changeset
  1454
     * is inverted or not.
26012
36ecb579dbc8 8050924: Fix doclint missing tag warnings in javax.swing.plaf.basic parts 5b,6b of 7
yan
parents: 25565
diff changeset
  1455
     *
36ecb579dbc8 8050924: Fix doclint missing tag warnings in javax.swing.plaf.basic parts 5b,6b of 7
yan
parents: 25565
diff changeset
  1456
     * @param yPos the location of the slider along the y axis
36ecb579dbc8 8050924: Fix doclint missing tag warnings in javax.swing.plaf.basic parts 5b,6b of 7
yan
parents: 25565
diff changeset
  1457
     * @return the value at the y position
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1458
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1459
    public int valueForYPosition( int yPos ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1460
        int value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1461
        final int minValue = slider.getMinimum();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1462
        final int maxValue = slider.getMaximum();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1463
        final int trackLength = trackRect.height;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1464
        final int trackTop = trackRect.y;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1465
        final int trackBottom = trackRect.y + (trackRect.height - 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1466
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1467
        if ( yPos <= trackTop ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1468
            value = drawInverted() ? minValue : maxValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1469
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1470
        else if ( yPos >= trackBottom ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1471
            value = drawInverted() ? maxValue : minValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1472
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1473
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1474
            int distanceFromTrackTop = yPos - trackTop;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1475
            double valueRange = (double)maxValue - (double)minValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1476
            double valuePerPixel = valueRange / (double)trackLength;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1477
            int valueFromTrackTop = (int)Math.round( distanceFromTrackTop * valuePerPixel );
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1478
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1479
            value = drawInverted() ? minValue + valueFromTrackTop : maxValue - valueFromTrackTop;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1480
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1481
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1482
        return value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1483
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1484
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1485
    /**
4394
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 3735
diff changeset
  1486
     * Returns the value at the x position.  If {@code xPos} is beyond the
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 3735
diff changeset
  1487
     * track at the left or the right, this method sets the value to either the
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 3735
diff changeset
  1488
     * minimum or maximum value of the slider, depending on if the slider is
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 3735
diff changeset
  1489
     * inverted or not.
26012
36ecb579dbc8 8050924: Fix doclint missing tag warnings in javax.swing.plaf.basic parts 5b,6b of 7
yan
parents: 25565
diff changeset
  1490
     *
36ecb579dbc8 8050924: Fix doclint missing tag warnings in javax.swing.plaf.basic parts 5b,6b of 7
yan
parents: 25565
diff changeset
  1491
     * @param xPos the location of the slider along the x axis
36ecb579dbc8 8050924: Fix doclint missing tag warnings in javax.swing.plaf.basic parts 5b,6b of 7
yan
parents: 25565
diff changeset
  1492
     * @return the value of the x position
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1493
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1494
    public int valueForXPosition( int xPos ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1495
        int value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1496
        final int minValue = slider.getMinimum();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1497
        final int maxValue = slider.getMaximum();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1498
        final int trackLength = trackRect.width;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1499
        final int trackLeft = trackRect.x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1500
        final int trackRight = trackRect.x + (trackRect.width - 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1501
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1502
        if ( xPos <= trackLeft ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1503
            value = drawInverted() ? maxValue : minValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1504
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1505
        else if ( xPos >= trackRight ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1506
            value = drawInverted() ? minValue : maxValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1507
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1508
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1509
            int distanceFromTrackLeft = xPos - trackLeft;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1510
            double valueRange = (double)maxValue - (double)minValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1511
            double valuePerPixel = valueRange / (double)trackLength;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1512
            int valueFromTrackLeft = (int)Math.round( distanceFromTrackLeft * valuePerPixel );
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1513
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1514
            value = drawInverted() ? maxValue - valueFromTrackLeft :
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1515
              minValue + valueFromTrackLeft;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1516
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1517
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1518
        return value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1519
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1520
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1521
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1522
    private class Handler implements ChangeListener,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1523
            ComponentListener, FocusListener, PropertyChangeListener {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1524
        // Change Handler
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1525
        public void stateChanged(ChangeEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1526
            if (!isDragging) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1527
                calculateThumbLocation();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1528
                slider.repaint();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1529
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1530
            lastValue = slider.getValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1531
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1532
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1533
        // Component Handler
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1534
        public void componentHidden(ComponentEvent e) { }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1535
        public void componentMoved(ComponentEvent e) { }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1536
        public void componentResized(ComponentEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1537
            calculateGeometry();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1538
            slider.repaint();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1539
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1540
        public void componentShown(ComponentEvent e) { }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1541
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1542
        // Focus Handler
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1543
        public void focusGained(FocusEvent e) { slider.repaint(); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1544
        public void focusLost(FocusEvent e) { slider.repaint(); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1545
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1546
        // Property Change Handler
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1547
        public void propertyChange(PropertyChangeEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1548
            String propertyName = e.getPropertyName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1549
            if (propertyName == "orientation" ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1550
                    propertyName == "inverted" ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1551
                    propertyName == "labelTable" ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1552
                    propertyName == "majorTickSpacing" ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1553
                    propertyName == "minorTickSpacing" ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1554
                    propertyName == "paintTicks" ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1555
                    propertyName == "paintTrack" ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1556
                    propertyName == "font" ||
3735
72286b35d392 6579827: vista : JSlider on JColorchooser is not properly render or can't be seen completely.
rupashka
parents: 2658
diff changeset
  1557
                    propertyName == "paintLabels" ||
72286b35d392 6579827: vista : JSlider on JColorchooser is not properly render or can't be seen completely.
rupashka
parents: 2658
diff changeset
  1558
                    propertyName == "Slider.paintThumbArrowShape") {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1559
                checkedLabelBaselines = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1560
                calculateGeometry();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1561
                slider.repaint();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1562
            } else if (propertyName == "componentOrientation") {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1563
                calculateGeometry();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1564
                slider.repaint();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1565
                InputMap km = getInputMap(JComponent.WHEN_FOCUSED, slider);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1566
                SwingUtilities.replaceUIInputMap(slider,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1567
                    JComponent.WHEN_FOCUSED, km);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1568
            } else if (propertyName == "model") {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1569
                ((BoundedRangeModel)e.getOldValue()).removeChangeListener(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1570
                    changeListener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1571
                ((BoundedRangeModel)e.getNewValue()).addChangeListener(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1572
                    changeListener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1573
                calculateThumbLocation();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1574
                slider.repaint();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1575
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1576
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1577
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1578
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1579
    /////////////////////////////////////////////////////////////////////////
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1580
    /// Model Listener Class
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1581
    /////////////////////////////////////////////////////////////////////////
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1582
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1583
     * Data model listener.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1584
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1585
     * This class should be treated as a &quot;protected&quot; inner class.
20169
d7fa6d7586c9 8025085: [javadoc] some errors in javax/swing
yan
parents: 7668
diff changeset
  1586
     * Instantiate it only within subclasses of <code>Foo</code>.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1587
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1588
    public class ChangeHandler implements ChangeListener {
21278
ef8a3a2a72f2 8022746: List of spelling errors in API doc
malenkov
parents: 20169
diff changeset
  1589
        // NOTE: This class exists only for backward compatibility. All
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1590
        // its functionality has been moved into Handler. If you need to add
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1591
        // new functionality add it to the Handler, but make sure this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1592
        // class calls into the Handler.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1593
        public void stateChanged(ChangeEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1594
            getHandler().stateChanged(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1595
        }
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
    /// Track Listener Class
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1600
    /////////////////////////////////////////////////////////////////////////
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1601
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1602
     * Track mouse movements.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1603
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1604
     * This class should be treated as a &quot;protected&quot; inner class.
20169
d7fa6d7586c9 8025085: [javadoc] some errors in javax/swing
yan
parents: 7668
diff changeset
  1605
     * Instantiate it only within subclasses of <code>Foo</code>.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1606
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1607
    public class TrackListener extends MouseInputAdapter {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1608
        protected transient int offset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1609
        protected transient int currentMouseX, currentMouseY;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1610
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1611
        public void mouseReleased(MouseEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1612
            if (!slider.isEnabled()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1613
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1614
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1615
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1616
            offset = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1617
            scrollTimer.stop();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1618
4957
e33105a2db5b 6848475: JSlider does not display the correct value of its BoundedRangeModel
rupashka
parents: 4394
diff changeset
  1619
            isDragging = false;
e33105a2db5b 6848475: JSlider does not display the correct value of its BoundedRangeModel
rupashka
parents: 4394
diff changeset
  1620
            slider.setValueIsAdjusting(false);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1621
            slider.repaint();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1622
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1623
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1624
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1625
        * If the mouse is pressed above the "thumb" component
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1626
        * then reduce the scrollbars value by one page ("page up"),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1627
        * otherwise increase it by one page.  If there is no
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1628
        * thumb then page up if the mouse is in the upper half
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1629
        * of the track.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1630
        */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1631
        public void mousePressed(MouseEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1632
            if (!slider.isEnabled()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1633
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1634
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1635
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1636
            // We should recalculate geometry just before
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1637
            // calculation of the thumb movement direction.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1638
            // It is important for the case, when JSlider
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1639
            // is a cell editor in JTable. See 6348946.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1640
            calculateGeometry();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1641
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1642
            currentMouseX = e.getX();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1643
            currentMouseY = e.getY();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1644
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1645
            if (slider.isRequestFocusEnabled()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1646
                slider.requestFocus();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1647
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1648
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1649
            // Clicked in the Thumb area?
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1650
            if (thumbRect.contains(currentMouseX, currentMouseY)) {
456
8ded0dbe4aaa 6614972: JSlider value should not change on right-click
rupashka
parents: 2
diff changeset
  1651
                if (UIManager.getBoolean("Slider.onlyLeftMouseButtonDrag")
8ded0dbe4aaa 6614972: JSlider value should not change on right-click
rupashka
parents: 2
diff changeset
  1652
                        && !SwingUtilities.isLeftMouseButton(e)) {
8ded0dbe4aaa 6614972: JSlider value should not change on right-click
rupashka
parents: 2
diff changeset
  1653
                    return;
8ded0dbe4aaa 6614972: JSlider value should not change on right-click
rupashka
parents: 2
diff changeset
  1654
                }
8ded0dbe4aaa 6614972: JSlider value should not change on right-click
rupashka
parents: 2
diff changeset
  1655
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1656
                switch (slider.getOrientation()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1657
                case JSlider.VERTICAL:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1658
                    offset = currentMouseY - thumbRect.y;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1659
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1660
                case JSlider.HORIZONTAL:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1661
                    offset = currentMouseX - thumbRect.x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1662
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1663
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1664
                isDragging = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1665
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1666
            }
456
8ded0dbe4aaa 6614972: JSlider value should not change on right-click
rupashka
parents: 2
diff changeset
  1667
8ded0dbe4aaa 6614972: JSlider value should not change on right-click
rupashka
parents: 2
diff changeset
  1668
            if (!SwingUtilities.isLeftMouseButton(e)) {
8ded0dbe4aaa 6614972: JSlider value should not change on right-click
rupashka
parents: 2
diff changeset
  1669
                return;
8ded0dbe4aaa 6614972: JSlider value should not change on right-click
rupashka
parents: 2
diff changeset
  1670
            }
8ded0dbe4aaa 6614972: JSlider value should not change on right-click
rupashka
parents: 2
diff changeset
  1671
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1672
            isDragging = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1673
            slider.setValueIsAdjusting(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1674
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1675
            Dimension sbSize = slider.getSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1676
            int direction = POSITIVE_SCROLL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1677
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1678
            switch (slider.getOrientation()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1679
            case JSlider.VERTICAL:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1680
                if ( thumbRect.isEmpty() ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1681
                    int scrollbarCenter = sbSize.height / 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1682
                    if ( !drawInverted() ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1683
                        direction = (currentMouseY < scrollbarCenter) ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1684
                            POSITIVE_SCROLL : NEGATIVE_SCROLL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1685
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1686
                    else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1687
                        direction = (currentMouseY < scrollbarCenter) ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1688
                            NEGATIVE_SCROLL : POSITIVE_SCROLL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1689
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1690
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1691
                else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1692
                    int thumbY = thumbRect.y;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1693
                    if ( !drawInverted() ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1694
                        direction = (currentMouseY < thumbY) ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1695
                            POSITIVE_SCROLL : NEGATIVE_SCROLL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1696
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1697
                    else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1698
                        direction = (currentMouseY < thumbY) ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1699
                            NEGATIVE_SCROLL : POSITIVE_SCROLL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1700
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1701
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1702
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1703
            case JSlider.HORIZONTAL:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1704
                if ( thumbRect.isEmpty() ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1705
                    int scrollbarCenter = sbSize.width / 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1706
                    if ( !drawInverted() ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1707
                        direction = (currentMouseX < scrollbarCenter) ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1708
                            NEGATIVE_SCROLL : POSITIVE_SCROLL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1709
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1710
                    else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1711
                        direction = (currentMouseX < scrollbarCenter) ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1712
                            POSITIVE_SCROLL : NEGATIVE_SCROLL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1713
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1714
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1715
                else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1716
                    int thumbX = thumbRect.x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1717
                    if ( !drawInverted() ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1718
                        direction = (currentMouseX < thumbX) ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1719
                            NEGATIVE_SCROLL : POSITIVE_SCROLL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1720
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1721
                    else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1722
                        direction = (currentMouseX < thumbX) ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1723
                            POSITIVE_SCROLL : NEGATIVE_SCROLL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1724
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1725
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1726
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1727
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1728
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1729
            if (shouldScroll(direction)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1730
                scrollDueToClickInTrack(direction);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1731
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1732
            if (shouldScroll(direction)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1733
                scrollTimer.stop();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1734
                scrollListener.setDirection(direction);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1735
                scrollTimer.start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1736
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1737
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1738
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1739
        public boolean shouldScroll(int direction) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1740
            Rectangle r = thumbRect;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1741
            if (slider.getOrientation() == JSlider.VERTICAL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1742
                if (drawInverted() ? direction < 0 : direction > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1743
                    if (r.y  <= currentMouseY) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1744
                        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1745
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1746
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1747
                else if (r.y + r.height >= currentMouseY) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1748
                    return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1749
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1750
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1751
            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1752
                if (drawInverted() ? direction < 0 : direction > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1753
                    if (r.x + r.width  >= currentMouseX) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1754
                        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1755
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1756
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1757
                else if (r.x <= currentMouseX) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1758
                    return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1759
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1760
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1761
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1762
            if (direction > 0 && slider.getValue() + slider.getExtent() >=
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1763
                    slider.getMaximum()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1764
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1765
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1766
            else if (direction < 0 && slider.getValue() <=
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1767
                    slider.getMinimum()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1768
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1769
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1770
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1771
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1772
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1773
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1774
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1775
        * Set the models value to the position of the top/left
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1776
        * of the thumb relative to the origin of the track.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1777
        */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1778
        public void mouseDragged(MouseEvent e) {
676
8cf833d60e87 6709530: There are unnecessary code in slider classes, such as in JSlider and SliderUIs
rupashka
parents: 457
diff changeset
  1779
            int thumbMiddle;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1780
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1781
            if (!slider.isEnabled()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1782
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1783
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1784
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1785
            currentMouseX = e.getX();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1786
            currentMouseY = e.getY();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1787
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1788
            if (!isDragging) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1789
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1790
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1791
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1792
            slider.setValueIsAdjusting(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1793
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1794
            switch (slider.getOrientation()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1795
            case JSlider.VERTICAL:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1796
                int halfThumbHeight = thumbRect.height / 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1797
                int thumbTop = e.getY() - offset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1798
                int trackTop = trackRect.y;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1799
                int trackBottom = trackRect.y + (trackRect.height - 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1800
                int vMax = yPositionForValue(slider.getMaximum() -
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1801
                                            slider.getExtent());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1802
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1803
                if (drawInverted()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1804
                    trackBottom = vMax;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1805
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1806
                else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1807
                    trackTop = vMax;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1808
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1809
                thumbTop = Math.max(thumbTop, trackTop - halfThumbHeight);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1810
                thumbTop = Math.min(thumbTop, trackBottom - halfThumbHeight);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1811
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1812
                setThumbLocation(thumbRect.x, thumbTop);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1813
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1814
                thumbMiddle = thumbTop + halfThumbHeight;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1815
                slider.setValue( valueForYPosition( thumbMiddle ) );
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1816
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1817
            case JSlider.HORIZONTAL:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1818
                int halfThumbWidth = thumbRect.width / 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1819
                int thumbLeft = e.getX() - offset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1820
                int trackLeft = trackRect.x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1821
                int trackRight = trackRect.x + (trackRect.width - 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1822
                int hMax = xPositionForValue(slider.getMaximum() -
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1823
                                            slider.getExtent());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1824
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1825
                if (drawInverted()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1826
                    trackLeft = hMax;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1827
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1828
                else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1829
                    trackRight = hMax;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1830
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1831
                thumbLeft = Math.max(thumbLeft, trackLeft - halfThumbWidth);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1832
                thumbLeft = Math.min(thumbLeft, trackRight - halfThumbWidth);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1833
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1834
                setThumbLocation(thumbLeft, thumbRect.y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1835
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1836
                thumbMiddle = thumbLeft + halfThumbWidth;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1837
                slider.setValue(valueForXPosition(thumbMiddle));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1838
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1839
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1840
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1841
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1842
        public void mouseMoved(MouseEvent e) { }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1843
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1844
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1845
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1846
     * Scroll-event listener.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1847
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1848
     * This class should be treated as a &quot;protected&quot; inner class.
20169
d7fa6d7586c9 8025085: [javadoc] some errors in javax/swing
yan
parents: 7668
diff changeset
  1849
     * Instantiate it only within subclasses of <code>Foo</code>.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1850
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1851
    public class ScrollListener implements ActionListener {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1852
        // changed this class to public to avoid bogus IllegalAccessException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1853
        // bug in InternetExplorer browser.  It was protected.  Work around
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1854
        // for 4109432
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1855
        int direction = POSITIVE_SCROLL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1856
        boolean useBlockIncrement;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1857
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1858
        public ScrollListener() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1859
            direction = POSITIVE_SCROLL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1860
            useBlockIncrement = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1861
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1862
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1863
        public ScrollListener(int dir, boolean block)   {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1864
            direction = dir;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1865
            useBlockIncrement = block;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1866
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1867
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1868
        public void setDirection(int direction) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1869
            this.direction = direction;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1870
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1871
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1872
        public void setScrollByBlock(boolean block) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1873
            this.useBlockIncrement = block;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1874
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1875
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1876
        public void actionPerformed(ActionEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1877
            if (useBlockIncrement) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1878
                scrollByBlock(direction);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1879
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1880
            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1881
                scrollByUnit(direction);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1882
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1883
            if (!trackListener.shouldScroll(direction)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1884
                ((Timer)e.getSource()).stop();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1885
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1886
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1887
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1888
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1889
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1890
     * Listener for resizing events.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1891
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1892
     * This class should be treated as a &quot;protected&quot; inner class.
20169
d7fa6d7586c9 8025085: [javadoc] some errors in javax/swing
yan
parents: 7668
diff changeset
  1893
     * Instantiate it only within subclasses of <code>Foo</code>.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1894
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1895
    public class ComponentHandler extends ComponentAdapter {
21278
ef8a3a2a72f2 8022746: List of spelling errors in API doc
malenkov
parents: 20169
diff changeset
  1896
        // NOTE: This class exists only for backward compatibility. All
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1897
        // its functionality has been moved into Handler. If you need to add
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1898
        // new functionality add it to the Handler, but make sure this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1899
        // class calls into the Handler.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1900
        public void componentResized(ComponentEvent e)  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1901
            getHandler().componentResized(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1902
        }
676
8cf833d60e87 6709530: There are unnecessary code in slider classes, such as in JSlider and SliderUIs
rupashka
parents: 457
diff changeset
  1903
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1904
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1905
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1906
     * Focus-change listener.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1907
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1908
     * This class should be treated as a &quot;protected&quot; inner class.
20169
d7fa6d7586c9 8025085: [javadoc] some errors in javax/swing
yan
parents: 7668
diff changeset
  1909
     * Instantiate it only within subclasses of <code>Foo</code>.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1910
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1911
    public class FocusHandler implements FocusListener {
21278
ef8a3a2a72f2 8022746: List of spelling errors in API doc
malenkov
parents: 20169
diff changeset
  1912
        // NOTE: This class exists only for backward compatibility. All
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1913
        // its functionality has been moved into Handler. If you need to add
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1914
        // new functionality add it to the Handler, but make sure this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1915
        // class calls into the Handler.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1916
        public void focusGained(FocusEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1917
            getHandler().focusGained(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1918
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1919
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1920
        public void focusLost(FocusEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1921
            getHandler().focusLost(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1922
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1923
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1924
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1925
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1926
     * As of Java 2 platform v1.3 this undocumented class is no longer used.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1927
     * The recommended approach to creating bindings is to use a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1928
     * combination of an <code>ActionMap</code>, to contain the action,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1929
     * and an <code>InputMap</code> to contain the mapping from KeyStroke
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1930
     * to action description. The InputMap is is usually described in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1931
     * LookAndFeel tables.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1932
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1933
     * Please refer to the key bindings specification for further details.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1934
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1935
     * This class should be treated as a &quot;protected&quot; inner class.
20169
d7fa6d7586c9 8025085: [javadoc] some errors in javax/swing
yan
parents: 7668
diff changeset
  1936
     * Instantiate it only within subclasses of <code>Foo</code>.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1937
     */
23697
e556a715949f 8034169: Fix serial lint warnings in javax.swing
darcy
parents: 23010
diff changeset
  1938
    @SuppressWarnings("serial") // Superclass is not serializable across versions
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1939
    public class ActionScroller extends AbstractAction {
21278
ef8a3a2a72f2 8022746: List of spelling errors in API doc
malenkov
parents: 20169
diff changeset
  1940
        // NOTE: This class exists only for backward compatibility. All
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1941
        // its functionality has been moved into Actions. If you need to add
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1942
        // new functionality add it to the Actions, but make sure this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1943
        // class calls into the Actions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1944
        int dir;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1945
        boolean block;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1946
        JSlider slider;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1947
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1948
        public ActionScroller( JSlider slider, int dir, boolean block) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1949
            this.dir = dir;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1950
            this.block = block;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1951
            this.slider = slider;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1952
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1953
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1954
        public void actionPerformed(ActionEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1955
            SHARED_ACTION.scroll(slider, BasicSliderUI.this, dir, block);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1956
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1957
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1958
        public boolean isEnabled() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1959
            boolean b = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1960
            if (slider != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1961
                b = slider.isEnabled();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1962
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1963
            return b;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1964
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1965
676
8cf833d60e87 6709530: There are unnecessary code in slider classes, such as in JSlider and SliderUIs
rupashka
parents: 457
diff changeset
  1966
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1967
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1968
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1969
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1970
     * A static version of the above.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1971
     */
23697
e556a715949f 8034169: Fix serial lint warnings in javax.swing
darcy
parents: 23010
diff changeset
  1972
    @SuppressWarnings("serial") // Superclass is not serializable across versions
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1973
    static class SharedActionScroller extends AbstractAction {
21278
ef8a3a2a72f2 8022746: List of spelling errors in API doc
malenkov
parents: 20169
diff changeset
  1974
        // NOTE: This class exists only for backward compatibility. All
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1975
        // its functionality has been moved into Actions. If you need to add
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1976
        // new functionality add it to the Actions, but make sure this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1977
        // class calls into the Actions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1978
        int dir;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1979
        boolean block;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1980
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1981
        public SharedActionScroller(int dir, boolean block) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1982
            this.dir = dir;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1983
            this.block = block;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1984
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1985
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1986
        public void actionPerformed(ActionEvent evt) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1987
            JSlider slider = (JSlider)evt.getSource();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1988
            BasicSliderUI ui = (BasicSliderUI)BasicLookAndFeel.getUIOfType(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1989
                    slider.getUI(), BasicSliderUI.class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1990
            if (ui == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1991
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1992
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1993
            SHARED_ACTION.scroll(slider, ui, dir, block);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1994
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1995
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1996
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1997
    private static class Actions extends UIAction {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1998
        public static final String POSITIVE_UNIT_INCREMENT =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1999
            "positiveUnitIncrement";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2000
        public static final String POSITIVE_BLOCK_INCREMENT =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2001
            "positiveBlockIncrement";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2002
        public static final String NEGATIVE_UNIT_INCREMENT =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2003
            "negativeUnitIncrement";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2004
        public static final String NEGATIVE_BLOCK_INCREMENT =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2005
            "negativeBlockIncrement";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2006
        public static final String MIN_SCROLL_INCREMENT = "minScroll";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2007
        public static final String MAX_SCROLL_INCREMENT = "maxScroll";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2008
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2009
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2010
        Actions() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2011
            super(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2012
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2013
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2014
        public Actions(String name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2015
            super(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2016
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2017
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2018
        public void actionPerformed(ActionEvent evt) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2019
            JSlider slider = (JSlider)evt.getSource();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2020
            BasicSliderUI ui = (BasicSliderUI)BasicLookAndFeel.getUIOfType(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2021
                     slider.getUI(), BasicSliderUI.class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2022
            String name = getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2023
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2024
            if (ui == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2025
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2026
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2027
            if (POSITIVE_UNIT_INCREMENT == name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2028
                scroll(slider, ui, POSITIVE_SCROLL, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2029
            } else if (NEGATIVE_UNIT_INCREMENT == name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2030
                scroll(slider, ui, NEGATIVE_SCROLL, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2031
            } else if (POSITIVE_BLOCK_INCREMENT == name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2032
                scroll(slider, ui, POSITIVE_SCROLL, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2033
            } else if (NEGATIVE_BLOCK_INCREMENT == name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2034
                scroll(slider, ui, NEGATIVE_SCROLL, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2035
            } else if (MIN_SCROLL_INCREMENT == name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2036
                scroll(slider, ui, MIN_SCROLL, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2037
            } else if (MAX_SCROLL_INCREMENT == name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2038
                scroll(slider, ui, MAX_SCROLL, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2039
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2040
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2041
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2042
        private void scroll(JSlider slider, BasicSliderUI ui, int direction,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2043
                boolean isBlock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2044
            boolean invert = slider.getInverted();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2045
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2046
            if (direction == NEGATIVE_SCROLL || direction == POSITIVE_SCROLL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2047
                if (invert) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2048
                    direction = (direction == POSITIVE_SCROLL) ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2049
                        NEGATIVE_SCROLL : POSITIVE_SCROLL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2050
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2051
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2052
                if (isBlock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2053
                    ui.scrollByBlock(direction);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2054
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2055
                    ui.scrollByUnit(direction);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2056
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2057
            } else {  // MIN or MAX
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2058
                if (invert) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2059
                    direction = (direction == MIN_SCROLL) ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2060
                        MAX_SCROLL : MIN_SCROLL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2061
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2062
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2063
                slider.setValue((direction == MIN_SCROLL) ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2064
                    slider.getMinimum() : slider.getMaximum());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2065
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2066
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2067
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2068
}