jdk/src/share/classes/javax/swing/plaf/basic/BasicSliderUI.java
author malenkov
Tue, 29 Oct 2013 17:01:06 +0400
changeset 21278 ef8a3a2a72f2
parent 20169 d7fa6d7586c9
child 23010 6dadb192ad81
permissions -rw-r--r--
8022746: List of spelling errors in API doc Reviewed-by: alexsch, smarks
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
7668
d4a77089c587 6962318: Update copyright year
ohair
parents: 5506
diff changeset
     2
 * Copyright (c) 1997, 2010, 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&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;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
            Dictionary dictionary = slider.getLabelTable();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
            if (dictionary != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
                sameLabelBaselines = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
                Enumeration elements = dictionary.elements();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
                int baseline = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
                while (elements.hasMoreElements()) {
676
8cf833d60e87 6709530: There are unnecessary code in slider classes, such as in JSlider and SliderUIs
rupashka
parents: 457
diff changeset
   406
                    JComponent label = (JComponent) elements.nextElement();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
                    Dimension pref = label.getPreferredSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
                    int labelBaseline = label.getBaseline(pref.width,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
                                                          pref.height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
                    if (labelBaseline >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
                        if (baseline == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
                            baseline = labelBaseline;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
                        else if (baseline != labelBaseline) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
                            sameLabelBaselines = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
                            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
                    else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
                        sameLabelBaselines = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
                sameLabelBaselines = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
        return sameLabelBaselines;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
    public Dimension getPreferredHorizontalSize() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
        Dimension horizDim = (Dimension)DefaultLookup.get(slider,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
                this, "Slider.horizontalSize");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
        if (horizDim == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
            horizDim = new Dimension(200, 21);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
        return horizDim;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
    public Dimension getPreferredVerticalSize() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
        Dimension vertDim = (Dimension)DefaultLookup.get(slider,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
                this, "Slider.verticalSize");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
        if (vertDim == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
            vertDim = new Dimension(21, 200);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
        return vertDim;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
    public Dimension getMinimumHorizontalSize() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
        Dimension minHorizDim = (Dimension)DefaultLookup.get(slider,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
                this, "Slider.minimumHorizontalSize");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
        if (minHorizDim == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
            minHorizDim = new Dimension(36, 21);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
        return minHorizDim;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
    public Dimension getMinimumVerticalSize() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
        Dimension minVertDim = (Dimension)DefaultLookup.get(slider,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
                this, "Slider.minimumVerticalSize");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
        if (minVertDim == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
            minVertDim = new Dimension(21, 36);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
        return minVertDim;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
    public Dimension getPreferredSize(JComponent c)    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
        recalculateIfInsetsChanged();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
        Dimension d;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
        if ( slider.getOrientation() == JSlider.VERTICAL ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
            d = new Dimension(getPreferredVerticalSize());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
            d.width = insetCache.left + insetCache.right;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
            d.width += focusInsets.left + focusInsets.right;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
            d.width += trackRect.width + tickRect.width + labelRect.width;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
            d = new Dimension(getPreferredHorizontalSize());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
            d.height = insetCache.top + insetCache.bottom;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
            d.height += focusInsets.top + focusInsets.bottom;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
            d.height += trackRect.height + tickRect.height + labelRect.height;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
        return d;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
    public Dimension getMinimumSize(JComponent c)  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
        recalculateIfInsetsChanged();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
        Dimension d;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
        if ( slider.getOrientation() == JSlider.VERTICAL ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
            d = new Dimension(getMinimumVerticalSize());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
            d.width = insetCache.left + insetCache.right;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
            d.width += focusInsets.left + focusInsets.right;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
            d.width += trackRect.width + tickRect.width + labelRect.width;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
            d = new Dimension(getMinimumHorizontalSize());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
            d.height = insetCache.top + insetCache.bottom;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
            d.height += focusInsets.top + focusInsets.bottom;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
            d.height += trackRect.height + tickRect.height + labelRect.height;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
        return d;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
    public Dimension getMaximumSize(JComponent c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
        Dimension d = getPreferredSize(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
        if ( slider.getOrientation() == JSlider.VERTICAL ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
            d.height = Short.MAX_VALUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
            d.width = Short.MAX_VALUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
        return d;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
    protected void calculateGeometry() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
        calculateFocusRect();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
        calculateContentRect();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
        calculateThumbSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
        calculateTrackBuffer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
        calculateTrackRect();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
        calculateTickRect();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
        calculateLabelRect();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
        calculateThumbLocation();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
    protected void calculateFocusRect() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
        focusRect.x = insetCache.left;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
        focusRect.y = insetCache.top;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
        focusRect.width = slider.getWidth() - (insetCache.left + insetCache.right);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
        focusRect.height = slider.getHeight() - (insetCache.top + insetCache.bottom);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
    protected void calculateThumbSize() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
        Dimension size = getThumbSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
        thumbRect.setSize( size.width, size.height );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
    protected void calculateContentRect() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
        contentRect.x = focusRect.x + focusInsets.left;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
        contentRect.y = focusRect.y + focusInsets.top;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
        contentRect.width = focusRect.width - (focusInsets.left + focusInsets.right);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
        contentRect.height = focusRect.height - (focusInsets.top + focusInsets.bottom);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
457
8682623ee3c3 6524424: JSlider Clicking In Tracks Behavior Inconsistent For Different Tick Spacings
rupashka
parents: 456
diff changeset
   549
    private int getTickSpacing() {
8682623ee3c3 6524424: JSlider Clicking In Tracks Behavior Inconsistent For Different Tick Spacings
rupashka
parents: 456
diff changeset
   550
        int majorTickSpacing = slider.getMajorTickSpacing();
8682623ee3c3 6524424: JSlider Clicking In Tracks Behavior Inconsistent For Different Tick Spacings
rupashka
parents: 456
diff changeset
   551
        int minorTickSpacing = slider.getMinorTickSpacing();
8682623ee3c3 6524424: JSlider Clicking In Tracks Behavior Inconsistent For Different Tick Spacings
rupashka
parents: 456
diff changeset
   552
8682623ee3c3 6524424: JSlider Clicking In Tracks Behavior Inconsistent For Different Tick Spacings
rupashka
parents: 456
diff changeset
   553
        int result;
8682623ee3c3 6524424: JSlider Clicking In Tracks Behavior Inconsistent For Different Tick Spacings
rupashka
parents: 456
diff changeset
   554
8682623ee3c3 6524424: JSlider Clicking In Tracks Behavior Inconsistent For Different Tick Spacings
rupashka
parents: 456
diff changeset
   555
        if (minorTickSpacing > 0) {
8682623ee3c3 6524424: JSlider Clicking In Tracks Behavior Inconsistent For Different Tick Spacings
rupashka
parents: 456
diff changeset
   556
            result = minorTickSpacing;
8682623ee3c3 6524424: JSlider Clicking In Tracks Behavior Inconsistent For Different Tick Spacings
rupashka
parents: 456
diff changeset
   557
        } else if (majorTickSpacing > 0) {
8682623ee3c3 6524424: JSlider Clicking In Tracks Behavior Inconsistent For Different Tick Spacings
rupashka
parents: 456
diff changeset
   558
            result = majorTickSpacing;
8682623ee3c3 6524424: JSlider Clicking In Tracks Behavior Inconsistent For Different Tick Spacings
rupashka
parents: 456
diff changeset
   559
        } else {
8682623ee3c3 6524424: JSlider Clicking In Tracks Behavior Inconsistent For Different Tick Spacings
rupashka
parents: 456
diff changeset
   560
            result = 0;
8682623ee3c3 6524424: JSlider Clicking In Tracks Behavior Inconsistent For Different Tick Spacings
rupashka
parents: 456
diff changeset
   561
        }
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
        return result;
8682623ee3c3 6524424: JSlider Clicking In Tracks Behavior Inconsistent For Different Tick Spacings
rupashka
parents: 456
diff changeset
   564
    }
8682623ee3c3 6524424: JSlider Clicking In Tracks Behavior Inconsistent For Different Tick Spacings
rupashka
parents: 456
diff changeset
   565
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
    protected void calculateThumbLocation() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
        if ( slider.getSnapToTicks() ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
            int sliderValue = slider.getValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
            int snappedValue = sliderValue;
457
8682623ee3c3 6524424: JSlider Clicking In Tracks Behavior Inconsistent For Different Tick Spacings
rupashka
parents: 456
diff changeset
   570
            int tickSpacing = getTickSpacing();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
            if ( tickSpacing != 0 ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
                // If it's not on a tick, change the value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
                if ( (sliderValue - slider.getMinimum()) % tickSpacing != 0 ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
                    float temp = (float)(sliderValue - slider.getMinimum()) / (float)tickSpacing;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
                    int whichTick = Math.round( temp );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
                    // This is the fix for the bug #6401380
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
                    if (temp - (int)temp == .5 && sliderValue < lastValue) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
                      whichTick --;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
                    snappedValue = slider.getMinimum() + (whichTick * tickSpacing);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
                if( snappedValue != sliderValue ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
                    slider.setValue( snappedValue );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
                }
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
        if ( slider.getOrientation() == JSlider.HORIZONTAL ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
            int valuePosition = xPositionForValue(slider.getValue());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
            thumbRect.x = valuePosition - (thumbRect.width / 2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
            thumbRect.y = trackRect.y;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
            int valuePosition = yPositionForValue(slider.getValue());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
            thumbRect.x = trackRect.x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
            thumbRect.y = valuePosition - (thumbRect.height / 2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
    protected void calculateTrackBuffer() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
        if ( slider.getPaintLabels() && slider.getLabelTable()  != null ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
            Component highLabel = getHighestValueLabel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
            Component lowLabel = getLowestValueLabel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
            if ( slider.getOrientation() == JSlider.HORIZONTAL ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
                trackBuffer = Math.max( highLabel.getBounds().width, lowLabel.getBounds().width ) / 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
                trackBuffer = Math.max( trackBuffer, thumbRect.width / 2 );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
                trackBuffer = Math.max( highLabel.getBounds().height, lowLabel.getBounds().height ) / 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
                trackBuffer = Math.max( trackBuffer, thumbRect.height / 2 );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
            if ( slider.getOrientation() == JSlider.HORIZONTAL ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
                trackBuffer = thumbRect.width / 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
                trackBuffer = thumbRect.height / 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
            }
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
    protected void calculateTrackRect() {
676
8cf833d60e87 6709530: There are unnecessary code in slider classes, such as in JSlider and SliderUIs
rupashka
parents: 457
diff changeset
   631
        int centerSpacing; // used to center sliders added using BorderLayout.CENTER (bug 4275631)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
        if ( slider.getOrientation() == JSlider.HORIZONTAL ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
            centerSpacing = thumbRect.height;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
            if ( slider.getPaintTicks() ) centerSpacing += getTickLength();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
            if ( slider.getPaintLabels() ) centerSpacing += getHeightOfTallestLabel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
            trackRect.x = contentRect.x + trackBuffer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
            trackRect.y = contentRect.y + (contentRect.height - centerSpacing - 1)/2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
            trackRect.width = contentRect.width - (trackBuffer * 2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
            trackRect.height = thumbRect.height;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
            centerSpacing = thumbRect.width;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
            if (BasicGraphicsUtils.isLeftToRight(slider)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
                if ( slider.getPaintTicks() ) centerSpacing += getTickLength();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
                if ( slider.getPaintLabels() ) centerSpacing += getWidthOfWidestLabel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
                if ( slider.getPaintTicks() ) centerSpacing -= getTickLength();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
                if ( slider.getPaintLabels() ) centerSpacing -= getWidthOfWidestLabel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
            trackRect.x = contentRect.x + (contentRect.width - centerSpacing - 1)/2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
            trackRect.y = contentRect.y + trackBuffer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
            trackRect.width = thumbRect.width;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
            trackRect.height = contentRect.height - (trackBuffer * 2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
        }
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
     * Gets the height of the tick area for horizontal sliders and the width of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
     * tick area for vertical sliders.  BasicSliderUI uses the returned value to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
     * determine the tick area rectangle.  If you want to give your ticks some room,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
     * make this larger than you need and paint your ticks away from the sides in paintTicks().
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
    protected int getTickLength() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
        return 8;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
    protected void calculateTickRect() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
        if ( slider.getOrientation() == JSlider.HORIZONTAL ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
            tickRect.x = trackRect.x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
            tickRect.y = trackRect.y + trackRect.height;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
            tickRect.width = trackRect.width;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
            tickRect.height = (slider.getPaintTicks()) ? getTickLength() : 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
            tickRect.width = (slider.getPaintTicks()) ? getTickLength() : 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
            if(BasicGraphicsUtils.isLeftToRight(slider)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
                tickRect.x = trackRect.x + trackRect.width;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
                tickRect.x = trackRect.x - tickRect.width;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
            tickRect.y = trackRect.y;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
            tickRect.height = trackRect.height;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
    protected void calculateLabelRect() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
        if ( slider.getPaintLabels() ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
            if ( slider.getOrientation() == JSlider.HORIZONTAL ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
                labelRect.x = tickRect.x - trackBuffer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
                labelRect.y = tickRect.y + tickRect.height;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
                labelRect.width = tickRect.width + (trackBuffer * 2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
                labelRect.height = getHeightOfTallestLabel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
                if(BasicGraphicsUtils.isLeftToRight(slider)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
                    labelRect.x = tickRect.x + tickRect.width;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
                    labelRect.width = getWidthOfWidestLabel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
                else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
                    labelRect.width = getWidthOfWidestLabel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
                    labelRect.x = tickRect.x - labelRect.width;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
                labelRect.y = tickRect.y - trackBuffer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
                labelRect.height = tickRect.height + (trackBuffer * 2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
            if ( slider.getOrientation() == JSlider.HORIZONTAL ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
                labelRect.x = tickRect.x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
                labelRect.y = tickRect.y + tickRect.height;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
                labelRect.width = tickRect.width;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
                labelRect.height = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
                if(BasicGraphicsUtils.isLeftToRight(slider)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
                    labelRect.x = tickRect.x + tickRect.width;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
                else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
                    labelRect.x = tickRect.x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
                labelRect.y = tickRect.y;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
                labelRect.width = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
                labelRect.height = tickRect.height;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
    protected Dimension getThumbSize() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
        Dimension size = new Dimension();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
        if ( slider.getOrientation() == JSlider.VERTICAL ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
            size.width = 20;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
            size.height = 11;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
            size.width = 11;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
            size.height = 20;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
        return size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
    public class PropertyChangeHandler implements PropertyChangeListener {
21278
ef8a3a2a72f2 8022746: List of spelling errors in API doc
malenkov
parents: 20169
diff changeset
   746
        // NOTE: This class exists only for backward compatibility. All
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
        // its functionality has been moved into Handler. If you need to add
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
        // new functionality add it to the Handler, but make sure this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
        // class calls into the Handler.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
        public void propertyChange( PropertyChangeEvent e ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
            getHandler().propertyChange(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
    protected int getWidthOfWidestLabel() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
        Dictionary dictionary = slider.getLabelTable();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
        int widest = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
        if ( dictionary != null ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
            Enumeration keys = dictionary.keys();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
            while ( keys.hasMoreElements() ) {
676
8cf833d60e87 6709530: There are unnecessary code in slider classes, such as in JSlider and SliderUIs
rupashka
parents: 457
diff changeset
   761
                JComponent label = (JComponent) dictionary.get(keys.nextElement());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
                widest = Math.max( label.getPreferredSize().width, widest );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
        return widest;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
    protected int getHeightOfTallestLabel() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
        Dictionary dictionary = slider.getLabelTable();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
        int tallest = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
        if ( dictionary != null ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
            Enumeration keys = dictionary.keys();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
            while ( keys.hasMoreElements() ) {
676
8cf833d60e87 6709530: There are unnecessary code in slider classes, such as in JSlider and SliderUIs
rupashka
parents: 457
diff changeset
   774
                JComponent label = (JComponent) dictionary.get(keys.nextElement());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
                tallest = Math.max( label.getPreferredSize().height, tallest );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
        return tallest;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
    protected int getWidthOfHighValueLabel() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
        Component label = getHighestValueLabel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
        int width = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
        if ( label != null ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
            width = label.getPreferredSize().width;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
        return width;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
    protected int getWidthOfLowValueLabel() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
        Component label = getLowestValueLabel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
        int width = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
        if ( label != null ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
            width = label.getPreferredSize().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
        return width;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
    protected int getHeightOfHighValueLabel() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
        Component label = getHighestValueLabel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
        int height = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
        if ( label != null ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
            height = label.getPreferredSize().height;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
        return height;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
    protected int getHeightOfLowValueLabel() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
        Component label = getLowestValueLabel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
        int height = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
        if ( label != null ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
            height = label.getPreferredSize().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
        return height;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
    protected boolean drawInverted() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
        if (slider.getOrientation()==JSlider.HORIZONTAL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
            if(BasicGraphicsUtils.isLeftToRight(slider)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
                return slider.getInverted();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
                return !slider.getInverted();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
            return slider.getInverted();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
     * Returns the biggest value that has an entry in the label table.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
     * @return biggest value that has an entry in the label table, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
     *         null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
    protected Integer getHighestValue() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
        Dictionary dictionary = slider.getLabelTable();
1852
958801d67667 6794836: BasicSliderUI throws NullPointerExc when JSlider maximum is Integer.MAX_VALUE
rupashka
parents: 715
diff changeset
   846
958801d67667 6794836: BasicSliderUI throws NullPointerExc when JSlider maximum is Integer.MAX_VALUE
rupashka
parents: 715
diff changeset
   847
        if (dictionary == null) {
958801d67667 6794836: BasicSliderUI throws NullPointerExc when JSlider maximum is Integer.MAX_VALUE
rupashka
parents: 715
diff changeset
   848
            return null;
958801d67667 6794836: BasicSliderUI throws NullPointerExc when JSlider maximum is Integer.MAX_VALUE
rupashka
parents: 715
diff changeset
   849
        }
958801d67667 6794836: BasicSliderUI throws NullPointerExc when JSlider maximum is Integer.MAX_VALUE
rupashka
parents: 715
diff changeset
   850
958801d67667 6794836: BasicSliderUI throws NullPointerExc when JSlider maximum is Integer.MAX_VALUE
rupashka
parents: 715
diff changeset
   851
        Enumeration keys = dictionary.keys();
958801d67667 6794836: BasicSliderUI throws NullPointerExc when JSlider maximum is Integer.MAX_VALUE
rupashka
parents: 715
diff changeset
   852
958801d67667 6794836: BasicSliderUI throws NullPointerExc when JSlider maximum is Integer.MAX_VALUE
rupashka
parents: 715
diff changeset
   853
        Integer max = null;
958801d67667 6794836: BasicSliderUI throws NullPointerExc when JSlider maximum is Integer.MAX_VALUE
rupashka
parents: 715
diff changeset
   854
958801d67667 6794836: BasicSliderUI throws NullPointerExc when JSlider maximum is Integer.MAX_VALUE
rupashka
parents: 715
diff changeset
   855
        while (keys.hasMoreElements()) {
958801d67667 6794836: BasicSliderUI throws NullPointerExc when JSlider maximum is Integer.MAX_VALUE
rupashka
parents: 715
diff changeset
   856
            Integer i = (Integer) keys.nextElement();
958801d67667 6794836: BasicSliderUI throws NullPointerExc when JSlider maximum is Integer.MAX_VALUE
rupashka
parents: 715
diff changeset
   857
958801d67667 6794836: BasicSliderUI throws NullPointerExc when JSlider maximum is Integer.MAX_VALUE
rupashka
parents: 715
diff changeset
   858
            if (max == null || i > max) {
958801d67667 6794836: BasicSliderUI throws NullPointerExc when JSlider maximum is Integer.MAX_VALUE
rupashka
parents: 715
diff changeset
   859
                max = i;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
        }
1852
958801d67667 6794836: BasicSliderUI throws NullPointerExc when JSlider maximum is Integer.MAX_VALUE
rupashka
parents: 715
diff changeset
   862
958801d67667 6794836: BasicSliderUI throws NullPointerExc when JSlider maximum is Integer.MAX_VALUE
rupashka
parents: 715
diff changeset
   863
        return max;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   864
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   867
     * Returns the smallest value that has an entry in the label table.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   868
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   869
     * @return smallest value that has an entry in the label table, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   870
     *         null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   871
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   872
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   873
    protected Integer getLowestValue() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   874
        Dictionary dictionary = slider.getLabelTable();
1852
958801d67667 6794836: BasicSliderUI throws NullPointerExc when JSlider maximum is Integer.MAX_VALUE
rupashka
parents: 715
diff changeset
   875
958801d67667 6794836: BasicSliderUI throws NullPointerExc when JSlider maximum is Integer.MAX_VALUE
rupashka
parents: 715
diff changeset
   876
        if (dictionary == null) {
958801d67667 6794836: BasicSliderUI throws NullPointerExc when JSlider maximum is Integer.MAX_VALUE
rupashka
parents: 715
diff changeset
   877
            return null;
958801d67667 6794836: BasicSliderUI throws NullPointerExc when JSlider maximum is Integer.MAX_VALUE
rupashka
parents: 715
diff changeset
   878
        }
958801d67667 6794836: BasicSliderUI throws NullPointerExc when JSlider maximum is Integer.MAX_VALUE
rupashka
parents: 715
diff changeset
   879
958801d67667 6794836: BasicSliderUI throws NullPointerExc when JSlider maximum is Integer.MAX_VALUE
rupashka
parents: 715
diff changeset
   880
        Enumeration keys = dictionary.keys();
958801d67667 6794836: BasicSliderUI throws NullPointerExc when JSlider maximum is Integer.MAX_VALUE
rupashka
parents: 715
diff changeset
   881
958801d67667 6794836: BasicSliderUI throws NullPointerExc when JSlider maximum is Integer.MAX_VALUE
rupashka
parents: 715
diff changeset
   882
        Integer min = null;
958801d67667 6794836: BasicSliderUI throws NullPointerExc when JSlider maximum is Integer.MAX_VALUE
rupashka
parents: 715
diff changeset
   883
958801d67667 6794836: BasicSliderUI throws NullPointerExc when JSlider maximum is Integer.MAX_VALUE
rupashka
parents: 715
diff changeset
   884
        while (keys.hasMoreElements()) {
958801d67667 6794836: BasicSliderUI throws NullPointerExc when JSlider maximum is Integer.MAX_VALUE
rupashka
parents: 715
diff changeset
   885
            Integer i = (Integer) keys.nextElement();
958801d67667 6794836: BasicSliderUI throws NullPointerExc when JSlider maximum is Integer.MAX_VALUE
rupashka
parents: 715
diff changeset
   886
958801d67667 6794836: BasicSliderUI throws NullPointerExc when JSlider maximum is Integer.MAX_VALUE
rupashka
parents: 715
diff changeset
   887
            if (min == null || i < min) {
958801d67667 6794836: BasicSliderUI throws NullPointerExc when JSlider maximum is Integer.MAX_VALUE
rupashka
parents: 715
diff changeset
   888
                min = i;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   889
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   890
        }
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
        return min;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   893
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   894
90ce3da70b43 Initial load
duke
parents:
diff changeset
   895
90ce3da70b43 Initial load
duke
parents:
diff changeset
   896
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   897
     * Returns the label that corresponds to the highest slider value in the label table.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   898
     * @see JSlider#setLabelTable
90ce3da70b43 Initial load
duke
parents:
diff changeset
   899
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   900
    protected Component getLowestValueLabel() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   901
        Integer min = getLowestValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   902
        if (min != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   903
            return (Component)slider.getLabelTable().get(min);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   904
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   905
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   906
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   907
90ce3da70b43 Initial load
duke
parents:
diff changeset
   908
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   909
     * Returns the label that corresponds to the lowest slider value in the label table.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   910
     * @see JSlider#setLabelTable
90ce3da70b43 Initial load
duke
parents:
diff changeset
   911
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   912
    protected Component getHighestValueLabel() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   913
        Integer max = getHighestValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   914
        if (max != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   915
            return (Component)slider.getLabelTable().get(max);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   916
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   917
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   918
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   919
90ce3da70b43 Initial load
duke
parents:
diff changeset
   920
    public void paint( Graphics g, JComponent c )   {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   921
        recalculateIfInsetsChanged();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   922
        recalculateIfOrientationChanged();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   923
        Rectangle clip = g.getClipBounds();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   924
90ce3da70b43 Initial load
duke
parents:
diff changeset
   925
        if ( !clip.intersects(trackRect) && slider.getPaintTrack())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   926
            calculateGeometry();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   927
90ce3da70b43 Initial load
duke
parents:
diff changeset
   928
        if ( slider.getPaintTrack() && clip.intersects( trackRect ) ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   929
            paintTrack( g );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   930
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   931
        if ( slider.getPaintTicks() && clip.intersects( tickRect ) ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   932
            paintTicks( g );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   933
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   934
        if ( slider.getPaintLabels() && clip.intersects( labelRect ) ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   935
            paintLabels( g );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   936
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   937
        if ( slider.hasFocus() && clip.intersects( focusRect ) ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   938
            paintFocus( g );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   939
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   940
        if ( clip.intersects( thumbRect ) ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   941
            paintThumb( g );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   942
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   943
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   944
90ce3da70b43 Initial load
duke
parents:
diff changeset
   945
    protected void recalculateIfInsetsChanged() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   946
        Insets newInsets = slider.getInsets();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   947
        if ( !newInsets.equals( insetCache ) ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   948
            insetCache = newInsets;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   949
            calculateGeometry();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   950
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   951
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   952
90ce3da70b43 Initial load
duke
parents:
diff changeset
   953
    protected void recalculateIfOrientationChanged() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   954
        boolean ltr = BasicGraphicsUtils.isLeftToRight(slider);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   955
        if ( ltr!=leftToRightCache ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   956
            leftToRightCache = ltr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   957
            calculateGeometry();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   958
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   959
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   960
90ce3da70b43 Initial load
duke
parents:
diff changeset
   961
    public void paintFocus(Graphics g)  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   962
        g.setColor( getFocusColor() );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   963
90ce3da70b43 Initial load
duke
parents:
diff changeset
   964
        BasicGraphicsUtils.drawDashedRect( g, focusRect.x, focusRect.y,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   965
                                           focusRect.width, focusRect.height );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   966
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   967
90ce3da70b43 Initial load
duke
parents:
diff changeset
   968
    public void paintTrack(Graphics g)  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   969
90ce3da70b43 Initial load
duke
parents:
diff changeset
   970
        Rectangle trackBounds = trackRect;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   971
90ce3da70b43 Initial load
duke
parents:
diff changeset
   972
        if ( slider.getOrientation() == JSlider.HORIZONTAL ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   973
            int cy = (trackBounds.height / 2) - 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   974
            int cw = trackBounds.width;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   975
90ce3da70b43 Initial load
duke
parents:
diff changeset
   976
            g.translate(trackBounds.x, trackBounds.y + cy);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   977
90ce3da70b43 Initial load
duke
parents:
diff changeset
   978
            g.setColor(getShadowColor());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   979
            g.drawLine(0, 0, cw - 1, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   980
            g.drawLine(0, 1, 0, 2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   981
            g.setColor(getHighlightColor());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   982
            g.drawLine(0, 3, cw, 3);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   983
            g.drawLine(cw, 0, cw, 3);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   984
            g.setColor(Color.black);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   985
            g.drawLine(1, 1, cw-2, 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   986
90ce3da70b43 Initial load
duke
parents:
diff changeset
   987
            g.translate(-trackBounds.x, -(trackBounds.y + cy));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   988
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   989
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   990
            int cx = (trackBounds.width / 2) - 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   991
            int ch = trackBounds.height;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   992
90ce3da70b43 Initial load
duke
parents:
diff changeset
   993
            g.translate(trackBounds.x + cx, trackBounds.y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   994
90ce3da70b43 Initial load
duke
parents:
diff changeset
   995
            g.setColor(getShadowColor());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   996
            g.drawLine(0, 0, 0, ch - 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   997
            g.drawLine(1, 0, 2, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   998
            g.setColor(getHighlightColor());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   999
            g.drawLine(3, 0, 3, ch);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1000
            g.drawLine(0, ch, 3, ch);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1001
            g.setColor(Color.black);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1002
            g.drawLine(1, 1, 1, ch-2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1003
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1004
            g.translate(-(trackBounds.x + cx), -trackBounds.y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1005
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1006
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1007
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1008
    public void paintTicks(Graphics g)  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1009
        Rectangle tickBounds = tickRect;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1010
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1011
        g.setColor(DefaultLookup.getColor(slider, this, "Slider.tickColor", Color.black));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1012
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1013
        if ( slider.getOrientation() == JSlider.HORIZONTAL ) {
2488
4d54c9133cda 6794831: Infinite loop while painting ticks on Slider with maximum=MAX_INT
rupashka
parents: 1852
diff changeset
  1014
            g.translate(0, tickBounds.y);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1015
2488
4d54c9133cda 6794831: Infinite loop while painting ticks on Slider with maximum=MAX_INT
rupashka
parents: 1852
diff changeset
  1016
            if (slider.getMinorTickSpacing() > 0) {
4d54c9133cda 6794831: Infinite loop while painting ticks on Slider with maximum=MAX_INT
rupashka
parents: 1852
diff changeset
  1017
                int value = slider.getMinimum();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1018
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1019
                while ( value <= slider.getMaximum() ) {
2488
4d54c9133cda 6794831: Infinite loop while painting ticks on Slider with maximum=MAX_INT
rupashka
parents: 1852
diff changeset
  1020
                    int xPos = xPositionForValue(value);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1021
                    paintMinorTickForHorizSlider( g, tickBounds, xPos );
2488
4d54c9133cda 6794831: Infinite loop while painting ticks on Slider with maximum=MAX_INT
rupashka
parents: 1852
diff changeset
  1022
4d54c9133cda 6794831: Infinite loop while painting ticks on Slider with maximum=MAX_INT
rupashka
parents: 1852
diff changeset
  1023
                    // Overflow checking
4d54c9133cda 6794831: Infinite loop while painting ticks on Slider with maximum=MAX_INT
rupashka
parents: 1852
diff changeset
  1024
                    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
  1025
                        break;
4d54c9133cda 6794831: Infinite loop while painting ticks on Slider with maximum=MAX_INT
rupashka
parents: 1852
diff changeset
  1026
                    }
4d54c9133cda 6794831: Infinite loop while painting ticks on Slider with maximum=MAX_INT
rupashka
parents: 1852
diff changeset
  1027
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1028
                    value += slider.getMinorTickSpacing();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1029
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1030
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1031
2488
4d54c9133cda 6794831: Infinite loop while painting ticks on Slider with maximum=MAX_INT
rupashka
parents: 1852
diff changeset
  1032
            if (slider.getMajorTickSpacing() > 0) {
4d54c9133cda 6794831: Infinite loop while painting ticks on Slider with maximum=MAX_INT
rupashka
parents: 1852
diff changeset
  1033
                int value = slider.getMinimum();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1034
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1035
                while ( value <= slider.getMaximum() ) {
2488
4d54c9133cda 6794831: Infinite loop while painting ticks on Slider with maximum=MAX_INT
rupashka
parents: 1852
diff changeset
  1036
                    int xPos = xPositionForValue(value);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1037
                    paintMajorTickForHorizSlider( g, tickBounds, xPos );
2488
4d54c9133cda 6794831: Infinite loop while painting ticks on Slider with maximum=MAX_INT
rupashka
parents: 1852
diff changeset
  1038
4d54c9133cda 6794831: Infinite loop while painting ticks on Slider with maximum=MAX_INT
rupashka
parents: 1852
diff changeset
  1039
                    // Overflow checking
4d54c9133cda 6794831: Infinite loop while painting ticks on Slider with maximum=MAX_INT
rupashka
parents: 1852
diff changeset
  1040
                    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
  1041
                        break;
4d54c9133cda 6794831: Infinite loop while painting ticks on Slider with maximum=MAX_INT
rupashka
parents: 1852
diff changeset
  1042
                    }
4d54c9133cda 6794831: Infinite loop while painting ticks on Slider with maximum=MAX_INT
rupashka
parents: 1852
diff changeset
  1043
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1044
                    value += slider.getMajorTickSpacing();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1045
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1046
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1047
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1048
            g.translate( 0, -tickBounds.y);
2488
4d54c9133cda 6794831: Infinite loop while painting ticks on Slider with maximum=MAX_INT
rupashka
parents: 1852
diff changeset
  1049
        } else {
4d54c9133cda 6794831: Infinite loop while painting ticks on Slider with maximum=MAX_INT
rupashka
parents: 1852
diff changeset
  1050
            g.translate(tickBounds.x, 0);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1051
2488
4d54c9133cda 6794831: Infinite loop while painting ticks on Slider with maximum=MAX_INT
rupashka
parents: 1852
diff changeset
  1052
            if (slider.getMinorTickSpacing() > 0) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1053
                int offset = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1054
                if(!BasicGraphicsUtils.isLeftToRight(slider)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1055
                    offset = tickBounds.width - tickBounds.width / 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1056
                    g.translate(offset, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1057
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1058
2488
4d54c9133cda 6794831: Infinite loop while painting ticks on Slider with maximum=MAX_INT
rupashka
parents: 1852
diff changeset
  1059
                int value = slider.getMinimum();
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
                while (value <= slider.getMaximum()) {
4d54c9133cda 6794831: Infinite loop while painting ticks on Slider with maximum=MAX_INT
rupashka
parents: 1852
diff changeset
  1062
                    int yPos = yPositionForValue(value);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1063
                    paintMinorTickForVertSlider( g, tickBounds, yPos );
2488
4d54c9133cda 6794831: Infinite loop while painting ticks on Slider with maximum=MAX_INT
rupashka
parents: 1852
diff changeset
  1064
4d54c9133cda 6794831: Infinite loop while painting ticks on Slider with maximum=MAX_INT
rupashka
parents: 1852
diff changeset
  1065
                    // Overflow checking
4d54c9133cda 6794831: Infinite loop while painting ticks on Slider with maximum=MAX_INT
rupashka
parents: 1852
diff changeset
  1066
                    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
  1067
                        break;
4d54c9133cda 6794831: Infinite loop while painting ticks on Slider with maximum=MAX_INT
rupashka
parents: 1852
diff changeset
  1068
                    }
4d54c9133cda 6794831: Infinite loop while painting ticks on Slider with maximum=MAX_INT
rupashka
parents: 1852
diff changeset
  1069
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1070
                    value += slider.getMinorTickSpacing();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1071
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1072
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1073
                if(!BasicGraphicsUtils.isLeftToRight(slider)) {
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
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1077
2488
4d54c9133cda 6794831: Infinite loop while painting ticks on Slider with maximum=MAX_INT
rupashka
parents: 1852
diff changeset
  1078
            if (slider.getMajorTickSpacing() > 0) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1079
                if(!BasicGraphicsUtils.isLeftToRight(slider)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1080
                    g.translate(2, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1081
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1082
2488
4d54c9133cda 6794831: Infinite loop while painting ticks on Slider with maximum=MAX_INT
rupashka
parents: 1852
diff changeset
  1083
                int value = slider.getMinimum();
4d54c9133cda 6794831: Infinite loop while painting ticks on Slider with maximum=MAX_INT
rupashka
parents: 1852
diff changeset
  1084
4d54c9133cda 6794831: Infinite loop while painting ticks on Slider with maximum=MAX_INT
rupashka
parents: 1852
diff changeset
  1085
                while (value <= slider.getMaximum()) {
4d54c9133cda 6794831: Infinite loop while painting ticks on Slider with maximum=MAX_INT
rupashka
parents: 1852
diff changeset
  1086
                    int yPos = yPositionForValue(value);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1087
                    paintMajorTickForVertSlider( g, tickBounds, yPos );
2488
4d54c9133cda 6794831: Infinite loop while painting ticks on Slider with maximum=MAX_INT
rupashka
parents: 1852
diff changeset
  1088
4d54c9133cda 6794831: Infinite loop while painting ticks on Slider with maximum=MAX_INT
rupashka
parents: 1852
diff changeset
  1089
                    // Overflow checking
4d54c9133cda 6794831: Infinite loop while painting ticks on Slider with maximum=MAX_INT
rupashka
parents: 1852
diff changeset
  1090
                    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
  1091
                        break;
4d54c9133cda 6794831: Infinite loop while painting ticks on Slider with maximum=MAX_INT
rupashka
parents: 1852
diff changeset
  1092
                    }
4d54c9133cda 6794831: Infinite loop while painting ticks on Slider with maximum=MAX_INT
rupashka
parents: 1852
diff changeset
  1093
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1094
                    value += slider.getMajorTickSpacing();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1095
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1096
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
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1101
            g.translate(-tickBounds.x, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1102
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1103
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1104
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1105
    protected void paintMinorTickForHorizSlider( Graphics g, Rectangle tickBounds, int x ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1106
        g.drawLine( x, 0, x, tickBounds.height / 2 - 1 );
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1107
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1108
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1109
    protected void paintMajorTickForHorizSlider( Graphics g, Rectangle tickBounds, int x ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1110
        g.drawLine( x, 0, x, tickBounds.height - 2 );
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1111
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1112
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1113
    protected void paintMinorTickForVertSlider( Graphics g, Rectangle tickBounds, int y ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1114
        g.drawLine( 0, y, tickBounds.width / 2 - 1, y );
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1115
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1116
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1117
    protected void paintMajorTickForVertSlider( Graphics g, Rectangle tickBounds, int y ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1118
        g.drawLine( 0, y,  tickBounds.width - 2, y );
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1119
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1120
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1121
    public void paintLabels( Graphics g ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1122
        Rectangle labelBounds = labelRect;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1123
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1124
        Dictionary dictionary = slider.getLabelTable();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1125
        if ( dictionary != null ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1126
            Enumeration keys = dictionary.keys();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1127
            int minValue = slider.getMinimum();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1128
            int maxValue = slider.getMaximum();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1129
            boolean enabled = slider.isEnabled();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1130
            while ( keys.hasMoreElements() ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1131
                Integer key = (Integer)keys.nextElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1132
                int value = key.intValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1133
                if (value >= minValue && value <= maxValue) {
676
8cf833d60e87 6709530: There are unnecessary code in slider classes, such as in JSlider and SliderUIs
rupashka
parents: 457
diff changeset
  1134
                    JComponent label = (JComponent) dictionary.get(key);
8cf833d60e87 6709530: There are unnecessary code in slider classes, such as in JSlider and SliderUIs
rupashka
parents: 457
diff changeset
  1135
                    label.setEnabled(enabled);
8cf833d60e87 6709530: There are unnecessary code in slider classes, such as in JSlider and SliderUIs
rupashka
parents: 457
diff changeset
  1136
677
20718977427b 4987336: JSlider doesn't show label's animated icon
rupashka
parents: 676
diff changeset
  1137
                    if (label instanceof JLabel) {
20718977427b 4987336: JSlider doesn't show label's animated icon
rupashka
parents: 676
diff changeset
  1138
                        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
  1139
20718977427b 4987336: JSlider doesn't show label's animated icon
rupashka
parents: 676
diff changeset
  1140
                        if (icon instanceof ImageIcon) {
20718977427b 4987336: JSlider doesn't show label's animated icon
rupashka
parents: 676
diff changeset
  1141
                            // 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
  1142
                            // image changes (e.g. gif animation)
20718977427b 4987336: JSlider doesn't show label's animated icon
rupashka
parents: 676
diff changeset
  1143
                            Toolkit.getDefaultToolkit().checkImage(((ImageIcon) icon).getImage(), -1, -1, slider);
20718977427b 4987336: JSlider doesn't show label's animated icon
rupashka
parents: 676
diff changeset
  1144
                        }
20718977427b 4987336: JSlider doesn't show label's animated icon
rupashka
parents: 676
diff changeset
  1145
                    }
20718977427b 4987336: JSlider doesn't show label's animated icon
rupashka
parents: 676
diff changeset
  1146
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1147
                    if ( slider.getOrientation() == JSlider.HORIZONTAL ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1148
                        g.translate( 0, labelBounds.y );
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1149
                        paintHorizontalLabel( g, value, label );
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1150
                        g.translate( 0, -labelBounds.y );
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1151
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1152
                    else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1153
                        int offset = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1154
                        if (!BasicGraphicsUtils.isLeftToRight(slider)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1155
                            offset = labelBounds.width -
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1156
                                label.getPreferredSize().width;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1157
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1158
                        g.translate( labelBounds.x + offset, 0 );
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1159
                        paintVerticalLabel( g, value, label );
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1160
                        g.translate( -labelBounds.x - offset, 0 );
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1161
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1162
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1163
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1164
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1165
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1166
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1167
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1168
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1169
     * Called for every label in the label table.  Used to draw the labels for horizontal sliders.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1170
     * The graphics have been translated to labelRect.y already.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1171
     * @see JSlider#setLabelTable
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1172
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1173
    protected void paintHorizontalLabel( Graphics g, int value, Component label ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1174
        int labelCenter = xPositionForValue( value );
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1175
        int labelLeft = labelCenter - (label.getPreferredSize().width / 2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1176
        g.translate( labelLeft, 0 );
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1177
        label.paint( g );
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1178
        g.translate( -labelLeft, 0 );
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1179
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1180
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1181
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1182
     * Called for every label in the label table.  Used to draw the labels for vertical sliders.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1183
     * The graphics have been translated to labelRect.x already.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1184
     * @see JSlider#setLabelTable
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1185
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1186
    protected void paintVerticalLabel( Graphics g, int value, Component label ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1187
        int labelCenter = yPositionForValue( value );
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1188
        int labelTop = labelCenter - (label.getPreferredSize().height / 2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1189
        g.translate( 0, labelTop );
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1190
        label.paint( g );
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1191
        g.translate( 0, -labelTop );
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1192
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1193
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1194
    public void paintThumb(Graphics g)  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1195
        Rectangle knobBounds = thumbRect;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1196
        int w = knobBounds.width;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1197
        int h = knobBounds.height;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1198
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1199
        g.translate(knobBounds.x, knobBounds.y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1200
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1201
        if ( slider.isEnabled() ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1202
            g.setColor(slider.getBackground());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1203
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1204
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1205
            g.setColor(slider.getBackground().darker());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1206
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1207
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1208
        Boolean paintThumbArrowShape =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1209
            (Boolean)slider.getClientProperty("Slider.paintThumbArrowShape");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1210
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1211
        if ((!slider.getPaintTicks() && paintThumbArrowShape == null) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1212
            paintThumbArrowShape == Boolean.FALSE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1213
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1214
            // "plain" version
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1215
            g.fillRect(0, 0, w, h);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1216
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1217
            g.setColor(Color.black);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1218
            g.drawLine(0, h-1, w-1, h-1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1219
            g.drawLine(w-1, 0, w-1, h-1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1220
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1221
            g.setColor(highlightColor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1222
            g.drawLine(0, 0, 0, h-2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1223
            g.drawLine(1, 0, w-2, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1224
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1225
            g.setColor(shadowColor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1226
            g.drawLine(1, h-2, w-2, h-2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1227
            g.drawLine(w-2, 1, w-2, h-3);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1228
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1229
        else if ( slider.getOrientation() == JSlider.HORIZONTAL ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1230
            int cw = w / 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1231
            g.fillRect(1, 1, w-3, h-1-cw);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1232
            Polygon p = new Polygon();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1233
            p.addPoint(1, h-cw);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1234
            p.addPoint(cw-1, h-1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1235
            p.addPoint(w-2, h-1-cw);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1236
            g.fillPolygon(p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1237
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1238
            g.setColor(highlightColor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1239
            g.drawLine(0, 0, w-2, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1240
            g.drawLine(0, 1, 0, h-1-cw);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1241
            g.drawLine(0, h-cw, cw-1, h-1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1242
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1243
            g.setColor(Color.black);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1244
            g.drawLine(w-1, 0, w-1, h-2-cw);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1245
            g.drawLine(w-1, h-1-cw, w-1-cw, h-1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1246
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1247
            g.setColor(shadowColor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1248
            g.drawLine(w-2, 1, w-2, h-2-cw);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1249
            g.drawLine(w-2, h-1-cw, w-1-cw, h-2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1250
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1251
        else {  // vertical
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1252
            int cw = h / 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1253
            if(BasicGraphicsUtils.isLeftToRight(slider)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1254
                  g.fillRect(1, 1, w-1-cw, h-3);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1255
                  Polygon p = new Polygon();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1256
                  p.addPoint(w-cw-1, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1257
                  p.addPoint(w-1, cw);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1258
                  p.addPoint(w-1-cw, h-2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1259
                  g.fillPolygon(p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1260
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1261
                  g.setColor(highlightColor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1262
                  g.drawLine(0, 0, 0, h - 2);                  // left
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1263
                  g.drawLine(1, 0, w-1-cw, 0);                 // top
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1264
                  g.drawLine(w-cw-1, 0, w-1, cw);              // top slant
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1265
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1266
                  g.setColor(Color.black);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1267
                  g.drawLine(0, h-1, w-2-cw, h-1);             // bottom
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1268
                  g.drawLine(w-1-cw, h-1, w-1, h-1-cw);        // bottom slant
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1269
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1270
                  g.setColor(shadowColor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1271
                  g.drawLine(1, h-2, w-2-cw,  h-2 );         // bottom
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1272
                  g.drawLine(w-1-cw, h-2, w-2, h-cw-1 );     // bottom slant
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1273
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1274
            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1275
                  g.fillRect(5, 1, w-1-cw, h-3);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1276
                  Polygon p = new Polygon();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1277
                  p.addPoint(cw, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1278
                  p.addPoint(0, cw);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1279
                  p.addPoint(cw, h-2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1280
                  g.fillPolygon(p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1281
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1282
                  g.setColor(highlightColor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1283
                  g.drawLine(cw-1, 0, w-2, 0);             // top
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1284
                  g.drawLine(0, cw, cw, 0);                // top slant
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1285
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1286
                  g.setColor(Color.black);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1287
                  g.drawLine(0, h-1-cw, cw, h-1 );         // bottom slant
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1288
                  g.drawLine(cw, h-1, w-1, h-1);           // bottom
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1289
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1290
                  g.setColor(shadowColor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1291
                  g.drawLine(cw, h-2, w-2,  h-2 );         // bottom
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1292
                  g.drawLine(w-1, 1, w-1,  h-2 );          // right
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1293
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1294
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1295
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1296
        g.translate(-knobBounds.x, -knobBounds.y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1297
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1298
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1299
    // Used exclusively by setThumbLocation()
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1300
    private static Rectangle unionRect = new Rectangle();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1301
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1302
    public void setThumbLocation(int x, int y)  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1303
        unionRect.setBounds( thumbRect );
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1304
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1305
        thumbRect.setLocation( x, y );
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1306
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1307
        SwingUtilities.computeUnion( thumbRect.x, thumbRect.y, thumbRect.width, thumbRect.height, unionRect );
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1308
        slider.repaint( unionRect.x, unionRect.y, unionRect.width, unionRect.height );
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1309
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1310
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1311
    public void scrollByBlock(int direction)    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1312
        synchronized(slider)    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1313
            int blockIncrement =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1314
                (slider.getMaximum() - slider.getMinimum()) / 10;
457
8682623ee3c3 6524424: JSlider Clicking In Tracks Behavior Inconsistent For Different Tick Spacings
rupashka
parents: 456
diff changeset
  1315
            if (blockIncrement == 0) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1316
                blockIncrement = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1317
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1318
457
8682623ee3c3 6524424: JSlider Clicking In Tracks Behavior Inconsistent For Different Tick Spacings
rupashka
parents: 456
diff changeset
  1319
            if (slider.getSnapToTicks()) {
8682623ee3c3 6524424: JSlider Clicking In Tracks Behavior Inconsistent For Different Tick Spacings
rupashka
parents: 456
diff changeset
  1320
                int tickSpacing = getTickSpacing();
8682623ee3c3 6524424: JSlider Clicking In Tracks Behavior Inconsistent For Different Tick Spacings
rupashka
parents: 456
diff changeset
  1321
8682623ee3c3 6524424: JSlider Clicking In Tracks Behavior Inconsistent For Different Tick Spacings
rupashka
parents: 456
diff changeset
  1322
                if (blockIncrement < tickSpacing) {
8682623ee3c3 6524424: JSlider Clicking In Tracks Behavior Inconsistent For Different Tick Spacings
rupashka
parents: 456
diff changeset
  1323
                    blockIncrement = tickSpacing;
8682623ee3c3 6524424: JSlider Clicking In Tracks Behavior Inconsistent For Different Tick Spacings
rupashka
parents: 456
diff changeset
  1324
                }
8682623ee3c3 6524424: JSlider Clicking In Tracks Behavior Inconsistent For Different Tick Spacings
rupashka
parents: 456
diff changeset
  1325
            }
8682623ee3c3 6524424: JSlider Clicking In Tracks Behavior Inconsistent For Different Tick Spacings
rupashka
parents: 456
diff changeset
  1326
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1327
            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
  1328
            slider.setValue(slider.getValue() + delta);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1329
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1330
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1331
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1332
    public void scrollByUnit(int direction) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1333
        synchronized(slider)    {
457
8682623ee3c3 6524424: JSlider Clicking In Tracks Behavior Inconsistent For Different Tick Spacings
rupashka
parents: 456
diff changeset
  1334
            int delta = ((direction > 0) ? POSITIVE_SCROLL : NEGATIVE_SCROLL);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1335
457
8682623ee3c3 6524424: JSlider Clicking In Tracks Behavior Inconsistent For Different Tick Spacings
rupashka
parents: 456
diff changeset
  1336
            if (slider.getSnapToTicks()) {
8682623ee3c3 6524424: JSlider Clicking In Tracks Behavior Inconsistent For Different Tick Spacings
rupashka
parents: 456
diff changeset
  1337
                delta *= getTickSpacing();
8682623ee3c3 6524424: JSlider Clicking In Tracks Behavior Inconsistent For Different Tick Spacings
rupashka
parents: 456
diff changeset
  1338
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1339
457
8682623ee3c3 6524424: JSlider Clicking In Tracks Behavior Inconsistent For Different Tick Spacings
rupashka
parents: 456
diff changeset
  1340
            slider.setValue(slider.getValue() + delta);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1341
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1342
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1343
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1344
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1345
     * This function is called when a mousePressed was detected in the track, not
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1346
     * in the thumb.  The default behavior is to scroll by block.  You can
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1347
     *  override this method to stop it from scrolling or to add additional behavior.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1348
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1349
    protected void scrollDueToClickInTrack( int dir ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1350
        scrollByBlock( dir );
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1351
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1352
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1353
    protected int xPositionForValue( int value )    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1354
        int min = slider.getMinimum();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1355
        int max = slider.getMaximum();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1356
        int trackLength = trackRect.width;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1357
        double valueRange = (double)max - (double)min;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1358
        double pixelsPerValue = (double)trackLength / valueRange;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1359
        int trackLeft = trackRect.x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1360
        int trackRight = trackRect.x + (trackRect.width - 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1361
        int xPosition;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1362
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1363
        if ( !drawInverted() ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1364
            xPosition = trackLeft;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1365
            xPosition += Math.round( pixelsPerValue * ((double)value - min) );
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1366
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1367
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1368
            xPosition = trackRight;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1369
            xPosition -= Math.round( pixelsPerValue * ((double)value - min) );
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1370
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1371
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1372
        xPosition = Math.max( trackLeft, xPosition );
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1373
        xPosition = Math.min( trackRight, xPosition );
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1374
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1375
        return xPosition;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1376
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1377
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1378
    protected int yPositionForValue( int value )  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1379
        return yPositionForValue(value, trackRect.y, trackRect.height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1380
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1381
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1382
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1383
     * Returns the y location for the specified value.  No checking is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1384
     * done on the arguments.  In particular if <code>trackHeight</code> is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1385
     * negative undefined results may occur.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1386
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1387
     * @param value the slider value to get the location for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1388
     * @param trackY y-origin of the track
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1389
     * @param trackHeight the height of the track
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1390
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1391
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1392
    protected int yPositionForValue(int value, int trackY, int trackHeight) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1393
        int min = slider.getMinimum();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1394
        int max = slider.getMaximum();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1395
        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
  1396
        double pixelsPerValue = (double)trackHeight / valueRange;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1397
        int trackBottom = trackY + (trackHeight - 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1398
        int yPosition;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1399
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1400
        if ( !drawInverted() ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1401
            yPosition = trackY;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1402
            yPosition += Math.round( pixelsPerValue * ((double)max - value ) );
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1403
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1404
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1405
            yPosition = trackY;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1406
            yPosition += Math.round( pixelsPerValue * ((double)value - min) );
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1407
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1408
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1409
        yPosition = Math.max( trackY, yPosition );
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1410
        yPosition = Math.min( trackBottom, yPosition );
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1411
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1412
        return yPosition;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1413
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1414
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1415
    /**
4394
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 3735
diff changeset
  1416
     * 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
  1417
     * 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
  1418
     * 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
  1419
     * is inverted or not.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1420
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1421
    public int valueForYPosition( int yPos ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1422
        int value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1423
        final int minValue = slider.getMinimum();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1424
        final int maxValue = slider.getMaximum();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1425
        final int trackLength = trackRect.height;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1426
        final int trackTop = trackRect.y;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1427
        final int trackBottom = trackRect.y + (trackRect.height - 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1428
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1429
        if ( yPos <= trackTop ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1430
            value = drawInverted() ? minValue : maxValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1431
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1432
        else if ( yPos >= trackBottom ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1433
            value = drawInverted() ? maxValue : minValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1434
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1435
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1436
            int distanceFromTrackTop = yPos - trackTop;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1437
            double valueRange = (double)maxValue - (double)minValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1438
            double valuePerPixel = valueRange / (double)trackLength;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1439
            int valueFromTrackTop = (int)Math.round( distanceFromTrackTop * valuePerPixel );
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1440
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1441
            value = drawInverted() ? minValue + valueFromTrackTop : maxValue - valueFromTrackTop;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1442
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1443
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1444
        return value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1445
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1446
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1447
    /**
4394
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 3735
diff changeset
  1448
     * 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
  1449
     * 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
  1450
     * 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
  1451
     * inverted or not.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1452
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1453
    public int valueForXPosition( int xPos ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1454
        int value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1455
        final int minValue = slider.getMinimum();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1456
        final int maxValue = slider.getMaximum();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1457
        final int trackLength = trackRect.width;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1458
        final int trackLeft = trackRect.x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1459
        final int trackRight = trackRect.x + (trackRect.width - 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1460
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1461
        if ( xPos <= trackLeft ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1462
            value = drawInverted() ? maxValue : minValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1463
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1464
        else if ( xPos >= trackRight ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1465
            value = drawInverted() ? minValue : maxValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1466
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1467
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1468
            int distanceFromTrackLeft = xPos - trackLeft;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1469
            double valueRange = (double)maxValue - (double)minValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1470
            double valuePerPixel = valueRange / (double)trackLength;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1471
            int valueFromTrackLeft = (int)Math.round( distanceFromTrackLeft * valuePerPixel );
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1472
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1473
            value = drawInverted() ? maxValue - valueFromTrackLeft :
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1474
              minValue + valueFromTrackLeft;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1475
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1476
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1477
        return value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1478
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1479
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1480
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1481
    private class Handler implements ChangeListener,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1482
            ComponentListener, FocusListener, PropertyChangeListener {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1483
        // Change Handler
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1484
        public void stateChanged(ChangeEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1485
            if (!isDragging) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1486
                calculateThumbLocation();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1487
                slider.repaint();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1488
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1489
            lastValue = slider.getValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1490
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1491
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1492
        // Component Handler
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1493
        public void componentHidden(ComponentEvent e) { }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1494
        public void componentMoved(ComponentEvent e) { }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1495
        public void componentResized(ComponentEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1496
            calculateGeometry();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1497
            slider.repaint();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1498
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1499
        public void componentShown(ComponentEvent e) { }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1500
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1501
        // Focus Handler
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1502
        public void focusGained(FocusEvent e) { slider.repaint(); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1503
        public void focusLost(FocusEvent e) { slider.repaint(); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1504
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1505
        // Property Change Handler
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1506
        public void propertyChange(PropertyChangeEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1507
            String propertyName = e.getPropertyName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1508
            if (propertyName == "orientation" ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1509
                    propertyName == "inverted" ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1510
                    propertyName == "labelTable" ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1511
                    propertyName == "majorTickSpacing" ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1512
                    propertyName == "minorTickSpacing" ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1513
                    propertyName == "paintTicks" ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1514
                    propertyName == "paintTrack" ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1515
                    propertyName == "font" ||
3735
72286b35d392 6579827: vista : JSlider on JColorchooser is not properly render or can't be seen completely.
rupashka
parents: 2658
diff changeset
  1516
                    propertyName == "paintLabels" ||
72286b35d392 6579827: vista : JSlider on JColorchooser is not properly render or can't be seen completely.
rupashka
parents: 2658
diff changeset
  1517
                    propertyName == "Slider.paintThumbArrowShape") {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1518
                checkedLabelBaselines = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1519
                calculateGeometry();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1520
                slider.repaint();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1521
            } else if (propertyName == "componentOrientation") {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1522
                calculateGeometry();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1523
                slider.repaint();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1524
                InputMap km = getInputMap(JComponent.WHEN_FOCUSED, slider);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1525
                SwingUtilities.replaceUIInputMap(slider,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1526
                    JComponent.WHEN_FOCUSED, km);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1527
            } else if (propertyName == "model") {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1528
                ((BoundedRangeModel)e.getOldValue()).removeChangeListener(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1529
                    changeListener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1530
                ((BoundedRangeModel)e.getNewValue()).addChangeListener(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1531
                    changeListener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1532
                calculateThumbLocation();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1533
                slider.repaint();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1534
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1535
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1536
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1537
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1538
    /////////////////////////////////////////////////////////////////////////
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1539
    /// Model Listener Class
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1540
    /////////////////////////////////////////////////////////////////////////
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1541
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1542
     * Data model listener.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1543
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1544
     * 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
  1545
     * Instantiate it only within subclasses of <code>Foo</code>.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1546
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1547
    public class ChangeHandler implements ChangeListener {
21278
ef8a3a2a72f2 8022746: List of spelling errors in API doc
malenkov
parents: 20169
diff changeset
  1548
        // NOTE: This class exists only for backward compatibility. All
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1549
        // its functionality has been moved into Handler. If you need to add
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1550
        // new functionality add it to the Handler, but make sure this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1551
        // class calls into the Handler.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1552
        public void stateChanged(ChangeEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1553
            getHandler().stateChanged(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1554
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1555
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1556
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1557
    /////////////////////////////////////////////////////////////////////////
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1558
    /// Track Listener Class
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1559
    /////////////////////////////////////////////////////////////////////////
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1560
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1561
     * Track mouse movements.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1562
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1563
     * 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
  1564
     * Instantiate it only within subclasses of <code>Foo</code>.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1565
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1566
    public class TrackListener extends MouseInputAdapter {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1567
        protected transient int offset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1568
        protected transient int currentMouseX, currentMouseY;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1569
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1570
        public void mouseReleased(MouseEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1571
            if (!slider.isEnabled()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1572
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1573
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1574
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1575
            offset = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1576
            scrollTimer.stop();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1577
4957
e33105a2db5b 6848475: JSlider does not display the correct value of its BoundedRangeModel
rupashka
parents: 4394
diff changeset
  1578
            isDragging = false;
e33105a2db5b 6848475: JSlider does not display the correct value of its BoundedRangeModel
rupashka
parents: 4394
diff changeset
  1579
            slider.setValueIsAdjusting(false);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1580
            slider.repaint();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1581
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1582
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1583
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1584
        * If the mouse is pressed above the "thumb" component
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1585
        * then reduce the scrollbars value by one page ("page up"),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1586
        * otherwise increase it by one page.  If there is no
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1587
        * thumb then page up if the mouse is in the upper half
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1588
        * of the track.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1589
        */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1590
        public void mousePressed(MouseEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1591
            if (!slider.isEnabled()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1592
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1593
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1594
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1595
            // We should recalculate geometry just before
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1596
            // calculation of the thumb movement direction.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1597
            // It is important for the case, when JSlider
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1598
            // is a cell editor in JTable. See 6348946.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1599
            calculateGeometry();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1600
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1601
            currentMouseX = e.getX();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1602
            currentMouseY = e.getY();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1603
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1604
            if (slider.isRequestFocusEnabled()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1605
                slider.requestFocus();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1606
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1607
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1608
            // Clicked in the Thumb area?
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1609
            if (thumbRect.contains(currentMouseX, currentMouseY)) {
456
8ded0dbe4aaa 6614972: JSlider value should not change on right-click
rupashka
parents: 2
diff changeset
  1610
                if (UIManager.getBoolean("Slider.onlyLeftMouseButtonDrag")
8ded0dbe4aaa 6614972: JSlider value should not change on right-click
rupashka
parents: 2
diff changeset
  1611
                        && !SwingUtilities.isLeftMouseButton(e)) {
8ded0dbe4aaa 6614972: JSlider value should not change on right-click
rupashka
parents: 2
diff changeset
  1612
                    return;
8ded0dbe4aaa 6614972: JSlider value should not change on right-click
rupashka
parents: 2
diff changeset
  1613
                }
8ded0dbe4aaa 6614972: JSlider value should not change on right-click
rupashka
parents: 2
diff changeset
  1614
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1615
                switch (slider.getOrientation()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1616
                case JSlider.VERTICAL:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1617
                    offset = currentMouseY - thumbRect.y;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1618
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1619
                case JSlider.HORIZONTAL:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1620
                    offset = currentMouseX - thumbRect.x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1621
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1622
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1623
                isDragging = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1624
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1625
            }
456
8ded0dbe4aaa 6614972: JSlider value should not change on right-click
rupashka
parents: 2
diff changeset
  1626
8ded0dbe4aaa 6614972: JSlider value should not change on right-click
rupashka
parents: 2
diff changeset
  1627
            if (!SwingUtilities.isLeftMouseButton(e)) {
8ded0dbe4aaa 6614972: JSlider value should not change on right-click
rupashka
parents: 2
diff changeset
  1628
                return;
8ded0dbe4aaa 6614972: JSlider value should not change on right-click
rupashka
parents: 2
diff changeset
  1629
            }
8ded0dbe4aaa 6614972: JSlider value should not change on right-click
rupashka
parents: 2
diff changeset
  1630
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1631
            isDragging = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1632
            slider.setValueIsAdjusting(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1633
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1634
            Dimension sbSize = slider.getSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1635
            int direction = POSITIVE_SCROLL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1636
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1637
            switch (slider.getOrientation()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1638
            case JSlider.VERTICAL:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1639
                if ( thumbRect.isEmpty() ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1640
                    int scrollbarCenter = sbSize.height / 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1641
                    if ( !drawInverted() ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1642
                        direction = (currentMouseY < scrollbarCenter) ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1643
                            POSITIVE_SCROLL : NEGATIVE_SCROLL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1644
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1645
                    else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1646
                        direction = (currentMouseY < scrollbarCenter) ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1647
                            NEGATIVE_SCROLL : POSITIVE_SCROLL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1648
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1649
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1650
                else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1651
                    int thumbY = thumbRect.y;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1652
                    if ( !drawInverted() ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1653
                        direction = (currentMouseY < thumbY) ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1654
                            POSITIVE_SCROLL : NEGATIVE_SCROLL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1655
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1656
                    else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1657
                        direction = (currentMouseY < thumbY) ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1658
                            NEGATIVE_SCROLL : POSITIVE_SCROLL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1659
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1660
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1661
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1662
            case JSlider.HORIZONTAL:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1663
                if ( thumbRect.isEmpty() ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1664
                    int scrollbarCenter = sbSize.width / 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1665
                    if ( !drawInverted() ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1666
                        direction = (currentMouseX < scrollbarCenter) ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1667
                            NEGATIVE_SCROLL : POSITIVE_SCROLL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1668
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1669
                    else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1670
                        direction = (currentMouseX < scrollbarCenter) ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1671
                            POSITIVE_SCROLL : NEGATIVE_SCROLL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1672
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1673
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1674
                else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1675
                    int thumbX = thumbRect.x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1676
                    if ( !drawInverted() ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1677
                        direction = (currentMouseX < thumbX) ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1678
                            NEGATIVE_SCROLL : POSITIVE_SCROLL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1679
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1680
                    else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1681
                        direction = (currentMouseX < thumbX) ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1682
                            POSITIVE_SCROLL : NEGATIVE_SCROLL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1683
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1684
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1685
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1686
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1687
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1688
            if (shouldScroll(direction)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1689
                scrollDueToClickInTrack(direction);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1690
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1691
            if (shouldScroll(direction)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1692
                scrollTimer.stop();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1693
                scrollListener.setDirection(direction);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1694
                scrollTimer.start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1695
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1696
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1697
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1698
        public boolean shouldScroll(int direction) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1699
            Rectangle r = thumbRect;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1700
            if (slider.getOrientation() == JSlider.VERTICAL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1701
                if (drawInverted() ? direction < 0 : direction > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1702
                    if (r.y  <= currentMouseY) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1703
                        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1704
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1705
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1706
                else if (r.y + r.height >= currentMouseY) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1707
                    return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1708
                }
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
                if (drawInverted() ? direction < 0 : direction > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1712
                    if (r.x + r.width  >= currentMouseX) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1713
                        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1714
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1715
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1716
                else if (r.x <= currentMouseX) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1717
                    return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1718
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1719
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1720
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1721
            if (direction > 0 && slider.getValue() + slider.getExtent() >=
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1722
                    slider.getMaximum()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1723
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1724
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1725
            else if (direction < 0 && slider.getValue() <=
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1726
                    slider.getMinimum()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1727
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1728
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1729
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1730
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1731
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1732
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1733
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1734
        * Set the models value to the position of the top/left
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1735
        * of the thumb relative to the origin of the track.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1736
        */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1737
        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
  1738
            int thumbMiddle;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1739
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1740
            if (!slider.isEnabled()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1741
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1742
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1743
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1744
            currentMouseX = e.getX();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1745
            currentMouseY = e.getY();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1746
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1747
            if (!isDragging) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1748
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1749
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1750
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1751
            slider.setValueIsAdjusting(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1752
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1753
            switch (slider.getOrientation()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1754
            case JSlider.VERTICAL:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1755
                int halfThumbHeight = thumbRect.height / 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1756
                int thumbTop = e.getY() - offset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1757
                int trackTop = trackRect.y;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1758
                int trackBottom = trackRect.y + (trackRect.height - 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1759
                int vMax = yPositionForValue(slider.getMaximum() -
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1760
                                            slider.getExtent());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1761
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1762
                if (drawInverted()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1763
                    trackBottom = vMax;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1764
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1765
                else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1766
                    trackTop = vMax;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1767
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1768
                thumbTop = Math.max(thumbTop, trackTop - halfThumbHeight);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1769
                thumbTop = Math.min(thumbTop, trackBottom - halfThumbHeight);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1770
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1771
                setThumbLocation(thumbRect.x, thumbTop);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1772
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1773
                thumbMiddle = thumbTop + halfThumbHeight;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1774
                slider.setValue( valueForYPosition( thumbMiddle ) );
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1775
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1776
            case JSlider.HORIZONTAL:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1777
                int halfThumbWidth = thumbRect.width / 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1778
                int thumbLeft = e.getX() - offset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1779
                int trackLeft = trackRect.x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1780
                int trackRight = trackRect.x + (trackRect.width - 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1781
                int hMax = xPositionForValue(slider.getMaximum() -
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1782
                                            slider.getExtent());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1783
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1784
                if (drawInverted()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1785
                    trackLeft = hMax;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1786
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1787
                else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1788
                    trackRight = hMax;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1789
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1790
                thumbLeft = Math.max(thumbLeft, trackLeft - halfThumbWidth);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1791
                thumbLeft = Math.min(thumbLeft, trackRight - halfThumbWidth);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1792
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1793
                setThumbLocation(thumbLeft, thumbRect.y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1794
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1795
                thumbMiddle = thumbLeft + halfThumbWidth;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1796
                slider.setValue(valueForXPosition(thumbMiddle));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1797
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1798
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1799
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1800
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1801
        public void mouseMoved(MouseEvent e) { }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1802
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1803
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1804
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1805
     * Scroll-event listener.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1806
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1807
     * 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
  1808
     * Instantiate it only within subclasses of <code>Foo</code>.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1809
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1810
    public class ScrollListener implements ActionListener {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1811
        // changed this class to public to avoid bogus IllegalAccessException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1812
        // bug in InternetExplorer browser.  It was protected.  Work around
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1813
        // for 4109432
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1814
        int direction = POSITIVE_SCROLL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1815
        boolean useBlockIncrement;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1816
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1817
        public ScrollListener() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1818
            direction = POSITIVE_SCROLL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1819
            useBlockIncrement = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1820
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1821
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1822
        public ScrollListener(int dir, boolean block)   {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1823
            direction = dir;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1824
            useBlockIncrement = block;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1825
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1826
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1827
        public void setDirection(int direction) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1828
            this.direction = direction;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1829
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1830
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1831
        public void setScrollByBlock(boolean block) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1832
            this.useBlockIncrement = block;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1833
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1834
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1835
        public void actionPerformed(ActionEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1836
            if (useBlockIncrement) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1837
                scrollByBlock(direction);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1838
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1839
            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1840
                scrollByUnit(direction);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1841
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1842
            if (!trackListener.shouldScroll(direction)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1843
                ((Timer)e.getSource()).stop();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1844
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1845
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1846
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1847
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1848
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1849
     * Listener for resizing events.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1850
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1851
     * 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
  1852
     * Instantiate it only within subclasses of <code>Foo</code>.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1853
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1854
    public class ComponentHandler extends ComponentAdapter {
21278
ef8a3a2a72f2 8022746: List of spelling errors in API doc
malenkov
parents: 20169
diff changeset
  1855
        // NOTE: This class exists only for backward compatibility. All
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1856
        // its functionality has been moved into Handler. If you need to add
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1857
        // new functionality add it to the Handler, but make sure this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1858
        // class calls into the Handler.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1859
        public void componentResized(ComponentEvent e)  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1860
            getHandler().componentResized(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1861
        }
676
8cf833d60e87 6709530: There are unnecessary code in slider classes, such as in JSlider and SliderUIs
rupashka
parents: 457
diff changeset
  1862
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1863
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1864
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1865
     * Focus-change listener.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1866
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1867
     * 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
  1868
     * Instantiate it only within subclasses of <code>Foo</code>.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1869
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1870
    public class FocusHandler implements FocusListener {
21278
ef8a3a2a72f2 8022746: List of spelling errors in API doc
malenkov
parents: 20169
diff changeset
  1871
        // NOTE: This class exists only for backward compatibility. All
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1872
        // its functionality has been moved into Handler. If you need to add
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1873
        // new functionality add it to the Handler, but make sure this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1874
        // class calls into the Handler.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1875
        public void focusGained(FocusEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1876
            getHandler().focusGained(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1877
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1878
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1879
        public void focusLost(FocusEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1880
            getHandler().focusLost(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1881
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1882
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1883
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1884
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1885
     * As of Java 2 platform v1.3 this undocumented class is no longer used.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1886
     * The recommended approach to creating bindings is to use a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1887
     * combination of an <code>ActionMap</code>, to contain the action,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1888
     * and an <code>InputMap</code> to contain the mapping from KeyStroke
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1889
     * to action description. The InputMap is is usually described in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1890
     * LookAndFeel tables.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1891
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1892
     * Please refer to the key bindings specification for further details.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1893
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1894
     * 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
  1895
     * Instantiate it only within subclasses of <code>Foo</code>.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1896
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1897
    public class ActionScroller extends AbstractAction {
21278
ef8a3a2a72f2 8022746: List of spelling errors in API doc
malenkov
parents: 20169
diff changeset
  1898
        // NOTE: This class exists only for backward compatibility. All
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1899
        // its functionality has been moved into Actions. If you need to add
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1900
        // new functionality add it to the Actions, but make sure this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1901
        // class calls into the Actions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1902
        int dir;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1903
        boolean block;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1904
        JSlider slider;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1905
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1906
        public ActionScroller( JSlider slider, int dir, boolean block) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1907
            this.dir = dir;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1908
            this.block = block;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1909
            this.slider = slider;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1910
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1911
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1912
        public void actionPerformed(ActionEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1913
            SHARED_ACTION.scroll(slider, BasicSliderUI.this, dir, block);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1914
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1915
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1916
        public boolean isEnabled() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1917
            boolean b = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1918
            if (slider != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1919
                b = slider.isEnabled();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1920
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1921
            return b;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1922
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1923
676
8cf833d60e87 6709530: There are unnecessary code in slider classes, such as in JSlider and SliderUIs
rupashka
parents: 457
diff changeset
  1924
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1925
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1926
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1927
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1928
     * A static version of the above.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1929
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1930
    static class SharedActionScroller extends AbstractAction {
21278
ef8a3a2a72f2 8022746: List of spelling errors in API doc
malenkov
parents: 20169
diff changeset
  1931
        // NOTE: This class exists only for backward compatibility. All
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1932
        // its functionality has been moved into Actions. If you need to add
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1933
        // new functionality add it to the Actions, but make sure this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1934
        // class calls into the Actions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1935
        int dir;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1936
        boolean block;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1937
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1938
        public SharedActionScroller(int dir, boolean block) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1939
            this.dir = dir;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1940
            this.block = block;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1941
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1942
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1943
        public void actionPerformed(ActionEvent evt) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1944
            JSlider slider = (JSlider)evt.getSource();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1945
            BasicSliderUI ui = (BasicSliderUI)BasicLookAndFeel.getUIOfType(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1946
                    slider.getUI(), BasicSliderUI.class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1947
            if (ui == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1948
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1949
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1950
            SHARED_ACTION.scroll(slider, ui, dir, block);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1951
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1952
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1953
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1954
    private static class Actions extends UIAction {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1955
        public static final String POSITIVE_UNIT_INCREMENT =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1956
            "positiveUnitIncrement";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1957
        public static final String POSITIVE_BLOCK_INCREMENT =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1958
            "positiveBlockIncrement";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1959
        public static final String NEGATIVE_UNIT_INCREMENT =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1960
            "negativeUnitIncrement";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1961
        public static final String NEGATIVE_BLOCK_INCREMENT =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1962
            "negativeBlockIncrement";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1963
        public static final String MIN_SCROLL_INCREMENT = "minScroll";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1964
        public static final String MAX_SCROLL_INCREMENT = "maxScroll";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1965
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1966
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1967
        Actions() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1968
            super(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1969
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1970
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1971
        public Actions(String name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1972
            super(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1973
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1974
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1975
        public void actionPerformed(ActionEvent evt) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1976
            JSlider slider = (JSlider)evt.getSource();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1977
            BasicSliderUI ui = (BasicSliderUI)BasicLookAndFeel.getUIOfType(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1978
                     slider.getUI(), BasicSliderUI.class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1979
            String name = getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1980
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1981
            if (ui == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1982
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1983
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1984
            if (POSITIVE_UNIT_INCREMENT == name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1985
                scroll(slider, ui, POSITIVE_SCROLL, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1986
            } else if (NEGATIVE_UNIT_INCREMENT == name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1987
                scroll(slider, ui, NEGATIVE_SCROLL, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1988
            } else if (POSITIVE_BLOCK_INCREMENT == name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1989
                scroll(slider, ui, POSITIVE_SCROLL, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1990
            } else if (NEGATIVE_BLOCK_INCREMENT == name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1991
                scroll(slider, ui, NEGATIVE_SCROLL, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1992
            } else if (MIN_SCROLL_INCREMENT == name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1993
                scroll(slider, ui, MIN_SCROLL, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1994
            } else if (MAX_SCROLL_INCREMENT == name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1995
                scroll(slider, ui, MAX_SCROLL, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1996
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1997
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1998
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1999
        private void scroll(JSlider slider, BasicSliderUI ui, int direction,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2000
                boolean isBlock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2001
            boolean invert = slider.getInverted();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2002
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2003
            if (direction == NEGATIVE_SCROLL || direction == POSITIVE_SCROLL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2004
                if (invert) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2005
                    direction = (direction == POSITIVE_SCROLL) ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2006
                        NEGATIVE_SCROLL : POSITIVE_SCROLL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2007
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2008
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2009
                if (isBlock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2010
                    ui.scrollByBlock(direction);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2011
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2012
                    ui.scrollByUnit(direction);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2013
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2014
            } else {  // MIN or MAX
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2015
                if (invert) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2016
                    direction = (direction == MIN_SCROLL) ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2017
                        MAX_SCROLL : MIN_SCROLL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2018
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2019
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2020
                slider.setValue((direction == MIN_SCROLL) ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2021
                    slider.getMinimum() : slider.getMaximum());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2022
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2023
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2024
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2025
}