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