jdk/src/share/classes/javax/swing/plaf/synth/SynthSliderUI.java
author peterz
Thu, 10 Dec 2009 12:08:58 +0300
changeset 4394 92a8ec883f5d
parent 2658 43e06bc950ec
child 4848 ffcc849b9351
permissions -rw-r--r--
6827653: Make Synth UI classes public Reviewed-by: alexp
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
715
f16baef3a20e 6719955: Update copyright year
xdono
parents: 676
diff changeset
     2
 * Copyright 2002-2008 Sun Microsystems, Inc.  All Rights Reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
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.synth;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.awt.event.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.awt.Graphics;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.awt.Dimension;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.awt.FontMetrics;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.awt.Rectangle;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.awt.Point;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.awt.Insets;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.beans.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.util.Dictionary;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.util.Enumeration;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import javax.swing.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import javax.swing.plaf.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import javax.swing.plaf.basic.BasicSliderUI;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import sun.swing.SwingUtilities2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
/**
4394
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 2658
diff changeset
    45
 * Provides the Synth L&F UI delegate for
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 2658
diff changeset
    46
 * {@link javax.swing.JSlider}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * @author Joshua Outwater
4394
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 2658
diff changeset
    49
 * @since 1.7
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 */
4394
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 2658
diff changeset
    51
public class SynthSliderUI extends BasicSliderUI
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 2658
diff changeset
    52
                           implements PropertyChangeListener, SynthUI {
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 2658
diff changeset
    53
    private Rectangle valueRect = new Rectangle();
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 2658
diff changeset
    54
    private boolean paintValue;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
2658
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 715
diff changeset
    56
    /**
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 715
diff changeset
    57
     * When a JSlider is used as a renderer in a JTable, its layout is not
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 715
diff changeset
    58
     * being recomputed even though the size is changing. Even though there
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 715
diff changeset
    59
     * is a ComponentListener installed, it is not being notified. As such,
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 715
diff changeset
    60
     * at times when being asked to paint the layout should first be redone.
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 715
diff changeset
    61
     * At the end of the layout method we set this lastSize variable, which
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 715
diff changeset
    62
     * represents the size of the slider the last time it was layed out.
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 715
diff changeset
    63
     *
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 715
diff changeset
    64
     * In the paint method we then check to see that this is accurate, that
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 715
diff changeset
    65
     * the slider has not changed sizes since being last layed out. If necessary
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 715
diff changeset
    66
     * we recompute the layout.
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 715
diff changeset
    67
     */
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 715
diff changeset
    68
    private Dimension lastSize = null;
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 715
diff changeset
    69
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    private int trackHeight;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    private int trackBorder;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    private int thumbWidth;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    private int thumbHeight;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    private SynthStyle style;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    private SynthStyle sliderTrackStyle;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    private SynthStyle sliderThumbStyle;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    /** Used to determine the color to paint the thumb. */
2658
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 715
diff changeset
    80
    private transient boolean thumbActive; //happens on rollover, and when pressed
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 715
diff changeset
    81
    private transient boolean thumbPressed; //happens when mouse was depressed while over thumb
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    ///////////////////////////////////////////////////
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    // ComponentUI Interface Implementation methods
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    ///////////////////////////////////////////////////
4394
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 2658
diff changeset
    86
    /**
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 2658
diff changeset
    87
     * Creates a new UI object for the given component.
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 2658
diff changeset
    88
     *
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 2658
diff changeset
    89
     * @param c component to create UI object for
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 2658
diff changeset
    90
     * @return the UI object
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 2658
diff changeset
    91
     */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    public static ComponentUI createUI(JComponent c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        return new SynthSliderUI((JSlider)c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
4394
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 2658
diff changeset
    96
    protected SynthSliderUI(JSlider c) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        super(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
4394
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 2658
diff changeset
   100
    /**
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 2658
diff changeset
   101
     * @inheritDoc
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 2658
diff changeset
   102
     */
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 2658
diff changeset
   103
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    protected void installDefaults(JSlider slider) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        updateStyle(slider);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
4394
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 2658
diff changeset
   108
    /**
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 2658
diff changeset
   109
     * Uninstalls default setting. This method is called when a
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 2658
diff changeset
   110
     * {@code LookAndFeel} is uninstalled.
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 2658
diff changeset
   111
     */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    protected void uninstallDefaults() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        SynthContext context = getContext(slider, ENABLED);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        style.uninstallDefaults(context);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        context.dispose();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        style = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        context = getContext(slider, Region.SLIDER_TRACK, ENABLED);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        sliderTrackStyle.uninstallDefaults(context);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        context.dispose();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        sliderTrackStyle = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        context = getContext(slider, Region.SLIDER_THUMB, ENABLED);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        sliderThumbStyle.uninstallDefaults(context);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        context.dispose();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        sliderThumbStyle = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
4394
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 2658
diff changeset
   129
    /**
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 2658
diff changeset
   130
     * @inheritDoc
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 2658
diff changeset
   131
     */
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 2658
diff changeset
   132
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
    protected void installListeners(JSlider slider) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        super.installListeners(slider);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        slider.addPropertyChangeListener(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
4394
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 2658
diff changeset
   138
    /**
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 2658
diff changeset
   139
     * @inheritDoc
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 2658
diff changeset
   140
     */
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 2658
diff changeset
   141
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    protected void uninstallListeners(JSlider slider) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        slider.removePropertyChangeListener(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        super.uninstallListeners(slider);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
    private void updateStyle(JSlider c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        SynthContext context = getContext(c, ENABLED);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        SynthStyle oldStyle = style;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        style = SynthLookAndFeel.updateStyle(context, this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        if (style != oldStyle) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
            thumbWidth =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
                style.getInt(context, "Slider.thumbWidth", 30);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
            thumbHeight =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
                style.getInt(context, "Slider.thumbHeight", 14);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
2658
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 715
diff changeset
   159
            // handle scaling for sizeVarients for special case components. The
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 715
diff changeset
   160
            // key "JComponent.sizeVariant" scales for large/small/mini
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 715
diff changeset
   161
            // components are based on Apples LAF
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 715
diff changeset
   162
            String scaleKey = (String)slider.getClientProperty(
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 715
diff changeset
   163
                    "JComponent.sizeVariant");
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 715
diff changeset
   164
            if (scaleKey != null){
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 715
diff changeset
   165
                if ("large".equals(scaleKey)){
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 715
diff changeset
   166
                    thumbWidth *= 1.15;
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 715
diff changeset
   167
                    thumbHeight *= 1.15;
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 715
diff changeset
   168
                } else if ("small".equals(scaleKey)){
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 715
diff changeset
   169
                    thumbWidth *= 0.857;
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 715
diff changeset
   170
                    thumbHeight *= 0.857;
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 715
diff changeset
   171
                } else if ("mini".equals(scaleKey)){
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 715
diff changeset
   172
                    thumbWidth *= 0.784;
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 715
diff changeset
   173
                    thumbHeight *= 0.784;
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 715
diff changeset
   174
                }
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 715
diff changeset
   175
            }
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 715
diff changeset
   176
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
            trackBorder =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
                style.getInt(context, "Slider.trackBorder", 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
            trackHeight = thumbHeight + trackBorder * 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
            paintValue = style.getBoolean(context,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
                    "Slider.paintValue", true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
            if (oldStyle != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
                uninstallKeyboardActions(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
                installKeyboardActions(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        context.dispose();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        context = getContext(c, Region.SLIDER_TRACK, ENABLED);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        sliderTrackStyle =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
            SynthLookAndFeel.updateStyle(context, this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        context.dispose();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        context = getContext(c, Region.SLIDER_THUMB, ENABLED);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        sliderThumbStyle =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
            SynthLookAndFeel.updateStyle(context, this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        context.dispose();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
4394
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 2658
diff changeset
   202
    /**
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 2658
diff changeset
   203
     * @inheritDoc
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 2658
diff changeset
   204
     */
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 2658
diff changeset
   205
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
    protected TrackListener createTrackListener(JSlider s) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
        return new SynthTrackListener();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
    private void updateThumbState(int x, int y) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        setThumbActive(thumbRect.contains(x, y));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
2658
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 715
diff changeset
   214
    private void updateThumbState(int x, int y, boolean pressed) {
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 715
diff changeset
   215
        updateThumbState(x, y);
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 715
diff changeset
   216
        setThumbPressed(pressed);
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 715
diff changeset
   217
    }
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 715
diff changeset
   218
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
    private void setThumbActive(boolean active) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
        if (thumbActive != active) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
            thumbActive = active;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
            slider.repaint(thumbRect);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
2658
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 715
diff changeset
   226
    private void setThumbPressed(boolean pressed) {
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 715
diff changeset
   227
        if (thumbPressed != pressed) {
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 715
diff changeset
   228
            thumbPressed = pressed;
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 715
diff changeset
   229
            slider.repaint(thumbRect);
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 715
diff changeset
   230
        }
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 715
diff changeset
   231
    }
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 715
diff changeset
   232
4394
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 2658
diff changeset
   233
    /**
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 2658
diff changeset
   234
     * @inheritDoc
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 2658
diff changeset
   235
     */
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 2658
diff changeset
   236
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
    public int getBaseline(JComponent c, int width, int height) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
        if (c == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
            throw new NullPointerException("Component must be non-null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
        if (width < 0 || height < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
            throw new IllegalArgumentException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
                    "Width and height must be >= 0");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
        if (slider.getPaintLabels() && labelsHaveSameBaselines()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
            // Get the insets for the track.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
            Insets trackInsets = new Insets(0, 0, 0, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
            SynthContext trackContext = getContext(slider,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
                                                   Region.SLIDER_TRACK);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
            style.getInsets(trackContext, trackInsets);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
            trackContext.dispose();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
            if (slider.getOrientation() == JSlider.HORIZONTAL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
                int valueHeight = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
                if (paintValue) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
                    SynthContext context = getContext(slider);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
                    valueHeight = context.getStyle().getGraphicsUtils(context).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
                            getMaximumCharHeight(context);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
                    context.dispose();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
                int tickHeight = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
                if (slider.getPaintTicks()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
                    tickHeight = getTickLength();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
                int labelHeight = getHeightOfTallestLabel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
                int contentHeight = valueHeight + trackHeight +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
                        trackInsets.top + trackInsets.bottom +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
                        tickHeight + labelHeight + 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
                int centerY = height / 2 - contentHeight / 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
                centerY += valueHeight + 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
                centerY += trackHeight + trackInsets.top + trackInsets.bottom;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
                centerY += tickHeight + 2;
676
8cf833d60e87 6709530: There are unnecessary code in slider classes, such as in JSlider and SliderUIs
rupashka
parents: 2
diff changeset
   272
                JComponent label = (JComponent) slider.getLabelTable().elements().nextElement();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
                Dimension pref = label.getPreferredSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
                return centerY + label.getBaseline(pref.width, pref.height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
            else { // VERTICAL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
                Integer value = slider.getInverted() ? getLowestValue() :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
                                                       getHighestValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
                if (value != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
                    int valueY = insetCache.top;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
                    int valueHeight = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
                    if (paintValue) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
                        SynthContext context = getContext(slider);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
                        valueHeight = context.getStyle().getGraphicsUtils(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
                                context).getMaximumCharHeight(context);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
                        context.dispose();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
                    int contentHeight = height - insetCache.top -
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
                            insetCache.bottom;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
                    int trackY = valueY + valueHeight;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
                    int trackHeight = contentHeight - valueHeight;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
                    int yPosition = yPositionForValue(value.intValue(), trackY,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
                                                      trackHeight);
676
8cf833d60e87 6709530: There are unnecessary code in slider classes, such as in JSlider and SliderUIs
rupashka
parents: 2
diff changeset
   294
                    JComponent label = (JComponent) slider.getLabelTable().get(value);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
                    Dimension pref = label.getPreferredSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
                    return yPosition - pref.height / 2 +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
                            label.getBaseline(pref.width, pref.height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
        return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
4394
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 2658
diff changeset
   304
    /**
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 2658
diff changeset
   305
     * @inheritDoc
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 2658
diff changeset
   306
     */
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 2658
diff changeset
   307
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
    public Dimension getPreferredSize(JComponent c)  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
        recalculateIfInsetsChanged();
4394
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 2658
diff changeset
   310
        Dimension d = new Dimension(contentRect.width, contentRect.height);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
        if (slider.getOrientation() == JSlider.VERTICAL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
            d.height = 200;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
            d.width = 200;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
        Insets i = slider.getInsets();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
        d.width += i.left + i.right;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
        d.height += i.top + i.bottom;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
        return d;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
4394
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 2658
diff changeset
   322
    /**
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 2658
diff changeset
   323
     * @inheritDoc
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 2658
diff changeset
   324
     */
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 2658
diff changeset
   325
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
    public Dimension getMinimumSize(JComponent c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
        recalculateIfInsetsChanged();
4394
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 2658
diff changeset
   328
        Dimension d = new Dimension(contentRect.width, contentRect.height);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
        if (slider.getOrientation() == JSlider.VERTICAL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
            d.height = thumbRect.height + insetCache.top + insetCache.bottom;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
            d.width = thumbRect.width + insetCache.left + insetCache.right;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
        return d;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
4394
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 2658
diff changeset
   337
    /**
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 2658
diff changeset
   338
     * @inheritDoc
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 2658
diff changeset
   339
     */
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 2658
diff changeset
   340
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
    protected void calculateGeometry() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
        layout();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
        calculateThumbLocation();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
4394
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 2658
diff changeset
   346
    /**
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 2658
diff changeset
   347
     * Lays out the slider.
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 2658
diff changeset
   348
     */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
    protected void layout() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
        SynthContext context = getContext(slider);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
        SynthGraphicsUtils synthGraphics = style.getGraphicsUtils(context);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
        // Set the thumb size.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
        Dimension size = getThumbSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
        thumbRect.setSize(size.width, size.height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
        // Get the insets for the track.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
        Insets trackInsets = new Insets(0, 0, 0, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
        SynthContext trackContext = getContext(slider, Region.SLIDER_TRACK);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
        style.getInsets(trackContext, trackInsets);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
        trackContext.dispose();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
        if (slider.getOrientation() == JSlider.HORIZONTAL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
            // Calculate the height of all the subcomponents so we can center
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
            // them.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
            valueRect.height = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
            if (paintValue) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
                valueRect.height =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
                    synthGraphics.getMaximumCharHeight(context);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
            trackRect.height = trackHeight;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
            tickRect.height = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
            if (slider.getPaintTicks()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
                tickRect.height = getTickLength();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
            labelRect.height = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
            if (slider.getPaintLabels()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
                labelRect.height = getHeightOfTallestLabel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
4394
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 2658
diff changeset
   384
            contentRect.height = valueRect.height + trackRect.height
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
                + trackInsets.top + trackInsets.bottom
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
                + tickRect.height + labelRect.height + 4;
4394
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 2658
diff changeset
   387
            contentRect.width = slider.getWidth() - insetCache.left
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
                - insetCache.right;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
            // Check if any of the labels will paint out of bounds.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
            int pad = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
            if (slider.getPaintLabels()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
                // Calculate the track rectangle.  It is necessary for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
                // xPositionForValue to return correct values.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
                trackRect.x = insetCache.left;
4394
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 2658
diff changeset
   396
                trackRect.width = contentRect.width;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
                Dictionary dictionary = slider.getLabelTable();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
                if (dictionary != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
                    int minValue = slider.getMinimum();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
                    int maxValue = slider.getMaximum();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
                    // Iterate through the keys in the dictionary and find the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
                    // first and last labels indices that fall within the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
                    // slider range.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
                    int firstLblIdx = Integer.MAX_VALUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
                    int lastLblIdx = Integer.MIN_VALUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
                    for (Enumeration keys = dictionary.keys();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
                            keys.hasMoreElements(); ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
                        int keyInt = ((Integer)keys.nextElement()).intValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
                        if (keyInt >= minValue && keyInt < firstLblIdx) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
                            firstLblIdx = keyInt;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
                        if (keyInt <= maxValue && keyInt > lastLblIdx) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
                            lastLblIdx = keyInt;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
                    // Calculate the pad necessary for the labels at the first
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
                    // and last visible indices.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
                    pad = getPadForLabel(firstLblIdx);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
                    pad = Math.max(pad, getPadForLabel(lastLblIdx));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
            // Calculate the painting rectangles for each of the different
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
            // slider areas.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
            valueRect.x = trackRect.x = tickRect.x = labelRect.x =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
                (insetCache.left + pad);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
            valueRect.width = trackRect.width = tickRect.width =
4394
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 2658
diff changeset
   429
                labelRect.width = (contentRect.width - (pad * 2));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
4394
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 2658
diff changeset
   431
            int centerY = slider.getHeight() / 2 - contentRect.height / 2;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
            valueRect.y = centerY;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
            centerY += valueRect.height + 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
            trackRect.y = centerY + trackInsets.top;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
            centerY += trackRect.height + trackInsets.top + trackInsets.bottom;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
            tickRect.y = centerY;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
            centerY += tickRect.height + 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
            labelRect.y = centerY;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
            centerY += labelRect.height;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
            // Calculate the width of all the subcomponents so we can center
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
            // them.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
            trackRect.width = trackHeight;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
            tickRect.width = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
            if (slider.getPaintTicks()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
                tickRect.width = getTickLength();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
            labelRect.width = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
            if (slider.getPaintLabels()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
                labelRect.width = getWidthOfWidestLabel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
            valueRect.y = insetCache.top;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
            valueRect.height = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
            if (paintValue) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
                valueRect.height =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
                    synthGraphics.getMaximumCharHeight(context);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
            // Get the max width of the min or max value of the slider.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
            FontMetrics fm = slider.getFontMetrics(slider.getFont());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
            valueRect.width = Math.max(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
                synthGraphics.computeStringWidth(context, slider.getFont(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
                    fm, "" + slider.getMaximum()),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
                synthGraphics.computeStringWidth(context, slider.getFont(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
                    fm, "" + slider.getMinimum()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
            int l = valueRect.width / 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
            int w1 = trackInsets.left + trackRect.width / 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
            int w2 = trackRect.width / 2 + trackInsets.right +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
                              tickRect.width + labelRect.width;
4394
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 2658
diff changeset
   478
            contentRect.width = Math.max(w1, l) + Math.max(w2, l) +
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
                    2 + insetCache.left + insetCache.right;
4394
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 2658
diff changeset
   480
            contentRect.height = slider.getHeight() -
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
                                    insetCache.top - insetCache.bottom;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
            // Layout the components.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
            trackRect.y = tickRect.y = labelRect.y =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
                valueRect.y + valueRect.height;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
            trackRect.height = tickRect.height = labelRect.height =
4394
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 2658
diff changeset
   487
                contentRect.height - valueRect.height;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
4394
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 2658
diff changeset
   489
            int startX = slider.getWidth() / 2 - contentRect.width / 2;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
            if (SynthLookAndFeel.isLeftToRight(slider)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
                if (l > w1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
                    startX += (l - w1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
                trackRect.x = startX + trackInsets.left;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
                startX += trackInsets.left + trackRect.width + trackInsets.right;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
                tickRect.x = startX;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
                labelRect.x = startX + tickRect.width + 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
                if (l > w2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
                    startX += (l - w2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
                labelRect.x = startX;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
                startX += labelRect.width + 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
                tickRect.x = startX;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
                trackRect.x = startX + tickRect.width + trackInsets.left;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
        context.dispose();
2658
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 715
diff changeset
   511
        lastSize = slider.getSize();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
     * Calculates the pad for the label at the specified index.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
     *
676
8cf833d60e87 6709530: There are unnecessary code in slider classes, such as in JSlider and SliderUIs
rupashka
parents: 2
diff changeset
   517
     * @param i index of the label to calculate pad for.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
     * @return padding required to keep label visible.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
    private int getPadForLabel(int i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
        int pad = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
676
8cf833d60e87 6709530: There are unnecessary code in slider classes, such as in JSlider and SliderUIs
rupashka
parents: 2
diff changeset
   523
        JComponent c = (JComponent) slider.getLabelTable().get(i);
8cf833d60e87 6709530: There are unnecessary code in slider classes, such as in JSlider and SliderUIs
rupashka
parents: 2
diff changeset
   524
        if (c != null) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
            int centerX = xPositionForValue(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
            int cHalfWidth = c.getPreferredSize().width / 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
            if (centerX - cHalfWidth < insetCache.left) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
                pad = Math.max(pad, insetCache.left - (centerX - cHalfWidth));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
            if (centerX + cHalfWidth > slider.getWidth() - insetCache.right) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
                pad = Math.max(pad, (centerX + cHalfWidth) -
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
                        (slider.getWidth() - insetCache.right));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
        return pad;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
4394
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 2658
diff changeset
   539
    /**
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 2658
diff changeset
   540
     * @inheritDoc
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 2658
diff changeset
   541
     */
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 2658
diff changeset
   542
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
    protected void calculateThumbLocation() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
        super.calculateThumbLocation();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
        if (slider.getOrientation() == JSlider.HORIZONTAL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
            thumbRect.y += trackBorder;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
            thumbRect.x += trackBorder;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
        Point mousePosition = slider.getMousePosition();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
        if(mousePosition != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
        updateThumbState(mousePosition.x, mousePosition.y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
       }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
4394
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 2658
diff changeset
   556
    /**
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 2658
diff changeset
   557
     * @inheritDoc
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 2658
diff changeset
   558
     */
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 2658
diff changeset
   559
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
    protected void calculateTickRect() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
        if (slider.getOrientation() == JSlider.HORIZONTAL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
            tickRect.x = trackRect.x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
            tickRect.y = trackRect.y + trackRect.height + 2 + getTickLength();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
            tickRect.width = trackRect.width;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
            tickRect.height = getTickLength();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
            if (!slider.getPaintTicks()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
                --tickRect.y;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
                tickRect.height = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
            if (SynthLookAndFeel.isLeftToRight(slider)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
                tickRect.x = trackRect.x + trackRect.width;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
                tickRect.width = getTickLength();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
                tickRect.width = getTickLength();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
                tickRect.x = trackRect.x - tickRect.width;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
            tickRect.y = trackRect.y;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
            tickRect.height = trackRect.height;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
            if (!slider.getPaintTicks()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
                --tickRect.x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
                tickRect.width = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
4394
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 2658
diff changeset
   589
    /**
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 2658
diff changeset
   590
     * @inheritDoc
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 2658
diff changeset
   591
     */
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 2658
diff changeset
   592
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
    public void setThumbLocation(int x, int y) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
        super.setThumbLocation(x, y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
        // Value rect is tied to the thumb location.  We need to repaint when
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
        // the thumb repaints.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
        slider.repaint(valueRect.x, valueRect.y,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
                valueRect.width, valueRect.height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
        setThumbActive(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
4394
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 2658
diff changeset
   602
    /**
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 2658
diff changeset
   603
     * @inheritDoc
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 2658
diff changeset
   604
     */
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 2658
diff changeset
   605
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
    protected int xPositionForValue(int value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
        int min = slider.getMinimum();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
        int max = slider.getMaximum();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
        int trackLeft = trackRect.x + thumbRect.width / 2 + trackBorder;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
        int trackRight = trackRect.x + trackRect.width - thumbRect.width / 2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
            - trackBorder;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
        int trackLength = trackRight - trackLeft;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
        double valueRange = (double)max - (double)min;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
        double pixelsPerValue = (double)trackLength / valueRange;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
        int xPosition;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
        if (!drawInverted()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
            xPosition = trackLeft;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
            xPosition += Math.round( pixelsPerValue * ((double)value - min));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
            xPosition = trackRight;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
            xPosition -= Math.round( pixelsPerValue * ((double)value - min));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
        xPosition = Math.max(trackLeft, xPosition);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
        xPosition = Math.min(trackRight, xPosition);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
        return xPosition;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
4394
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 2658
diff changeset
   631
    /**
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 2658
diff changeset
   632
     * @inheritDoc
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 2658
diff changeset
   633
     */
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 2658
diff changeset
   634
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
    protected int yPositionForValue(int value, int trackY, int trackHeight) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
        int min = slider.getMinimum();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
        int max = slider.getMaximum();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
        int trackTop = trackY + thumbRect.height / 2 + trackBorder;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
        int trackBottom = trackY + trackHeight - thumbRect.height / 2 -
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
                trackBorder;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
        int trackLength = trackBottom - trackTop;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
        double valueRange = (double)max - (double)min;
676
8cf833d60e87 6709530: There are unnecessary code in slider classes, such as in JSlider and SliderUIs
rupashka
parents: 2
diff changeset
   643
        double pixelsPerValue = (double)trackLength / valueRange;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
        int yPosition;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
        if (!drawInverted()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
            yPosition = trackTop;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
            yPosition += Math.round(pixelsPerValue * ((double)max - value));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
            yPosition = trackTop;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
            yPosition += Math.round(pixelsPerValue * ((double)value - min));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
        yPosition = Math.max(trackTop, yPosition);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
        yPosition = Math.min(trackBottom, yPosition);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
        return yPosition;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
    /**
4394
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 2658
diff changeset
   661
     * @inheritDoc
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
     */
4394
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 2658
diff changeset
   663
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
    public int valueForYPosition(int yPos) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
        int value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
        int minValue = slider.getMinimum();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
        int maxValue = slider.getMaximum();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
        int trackTop = trackRect.y + thumbRect.height / 2 + trackBorder;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
        int trackBottom = trackRect.y + trackRect.height
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
            - thumbRect.height / 2 - trackBorder;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
        int trackLength = trackBottom - trackTop;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
        if (yPos <= trackTop) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
            value = drawInverted() ? minValue : maxValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
        } else if (yPos >= trackBottom) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
            value = drawInverted() ? maxValue : minValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
            int distanceFromTrackTop = yPos - trackTop;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
            double valueRange = (double)maxValue - (double)minValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
            double valuePerPixel = valueRange / (double)trackLength;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
            int valueFromTrackTop =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
                (int)Math.round(distanceFromTrackTop * valuePerPixel);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
            value = drawInverted() ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
                minValue + valueFromTrackTop : maxValue - valueFromTrackTop;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
        return value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
    /**
4394
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 2658
diff changeset
   690
     * @inheritDoc
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
     */
4394
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 2658
diff changeset
   692
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
    public int valueForXPosition(int xPos) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
        int value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
        int minValue = slider.getMinimum();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
        int maxValue = slider.getMaximum();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
        int trackLeft = trackRect.x + thumbRect.width / 2 + trackBorder;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
        int trackRight = trackRect.x + trackRect.width
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
            - thumbRect.width / 2 - trackBorder;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
        int trackLength = trackRight - trackLeft;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
        if (xPos <= trackLeft) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
            value = drawInverted() ? maxValue : minValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
        } else if (xPos >= trackRight) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
            value = drawInverted() ? minValue : maxValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
            int distanceFromTrackLeft = xPos - trackLeft;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
            double valueRange = (double)maxValue - (double)minValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
            double valuePerPixel = valueRange / (double)trackLength;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
            int valueFromTrackLeft =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
                (int)Math.round(distanceFromTrackLeft * valuePerPixel);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
            value = drawInverted() ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
                maxValue - valueFromTrackLeft : minValue + valueFromTrackLeft;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
        return value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
4394
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 2658
diff changeset
   718
    /**
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 2658
diff changeset
   719
     * @inheritDoc
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 2658
diff changeset
   720
     */
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 2658
diff changeset
   721
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
    protected Dimension getThumbSize() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
        Dimension size = new Dimension();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
        if (slider.getOrientation() == JSlider.VERTICAL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
            size.width = thumbHeight;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
            size.height = thumbWidth;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
            size.width = thumbWidth;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
            size.height = thumbHeight;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
        return size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
4394
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 2658
diff changeset
   735
    /**
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 2658
diff changeset
   736
     * @inheritDoc
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 2658
diff changeset
   737
     */
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 2658
diff changeset
   738
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
    protected void recalculateIfInsetsChanged() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
        SynthContext context = getContext(slider);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
        Insets newInsets = style.getInsets(context, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
        Insets compInsets = slider.getInsets();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
        newInsets.left += compInsets.left; newInsets.right += compInsets.right;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
        newInsets.top += compInsets.top; newInsets.bottom += compInsets.bottom;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
        if (!newInsets.equals(insetCache)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
            insetCache = newInsets;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
            calculateGeometry();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
        context.dispose();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
4394
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 2658
diff changeset
   752
    /**
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 2658
diff changeset
   753
     * @inheritDoc
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 2658
diff changeset
   754
     */
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 2658
diff changeset
   755
    @Override
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 2658
diff changeset
   756
    public SynthContext getContext(JComponent c) {
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 2658
diff changeset
   757
        return getContext(c, SynthLookAndFeel.getComponentState(c));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
4394
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 2658
diff changeset
   760
    private SynthContext getContext(JComponent c, int state) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
        return SynthContext.getContext(SynthContext.class, c,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
                            SynthLookAndFeel.getRegion(c), style, state);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
4394
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 2658
diff changeset
   765
    private SynthContext getContext(JComponent c, Region subregion) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
        return getContext(c, subregion, getComponentState(c, subregion));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
    private SynthContext getContext(JComponent c, Region subregion, int state) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
        SynthStyle style = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
        Class klass = SynthContext.class;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
        if (subregion == Region.SLIDER_TRACK) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
            style = sliderTrackStyle;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
        } else if (subregion == Region.SLIDER_THUMB) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
            style = sliderThumbStyle;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
        return SynthContext.getContext(klass, c, subregion, style, state);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
    private int getComponentState(JComponent c, Region region) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
        if (region == Region.SLIDER_THUMB && thumbActive &&c.isEnabled()) {
2658
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 715
diff changeset
   783
            int state = thumbPressed ? PRESSED : MOUSE_OVER;
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 715
diff changeset
   784
            if (c.isFocusOwner()) state |= FOCUSED;
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 715
diff changeset
   785
            return state;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
        return SynthLookAndFeel.getComponentState(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
4394
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 2658
diff changeset
   790
    /**
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 2658
diff changeset
   791
     * @inheritDoc
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 2658
diff changeset
   792
     */
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 2658
diff changeset
   793
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
    public void update(Graphics g, JComponent c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
        SynthContext context = getContext(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
        SynthLookAndFeel.update(context, g);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
        context.getPainter().paintSliderBackground(context,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
                          g, 0, 0, c.getWidth(), c.getHeight(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
                          slider.getOrientation());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
        paint(context, g);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
        context.dispose();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
4394
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 2658
diff changeset
   804
    /**
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 2658
diff changeset
   805
     * @inheritDoc
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 2658
diff changeset
   806
     */
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 2658
diff changeset
   807
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
    public void paint(Graphics g, JComponent c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
        SynthContext context = getContext(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
        paint(context, g);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
        context.dispose();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
4394
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 2658
diff changeset
   814
    /**
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 2658
diff changeset
   815
     * Paints the specified component.
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 2658
diff changeset
   816
     *
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 2658
diff changeset
   817
     * @param context context for the component being painted
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 2658
diff changeset
   818
     * @param g {@code Graphics} object used for painting
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 2658
diff changeset
   819
     */
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 2658
diff changeset
   820
    protected void paint(SynthContext context, Graphics g) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
        recalculateIfInsetsChanged();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
        recalculateIfOrientationChanged();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
        Rectangle clip = g.getClipBounds();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
2658
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 715
diff changeset
   825
        if (lastSize == null || !lastSize.equals(slider.getSize())) {
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 715
diff changeset
   826
            calculateGeometry();
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 715
diff changeset
   827
        }
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 715
diff changeset
   828
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
        if (paintValue) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
            FontMetrics fm = SwingUtilities2.getFontMetrics(slider, g);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
            int labelWidth = context.getStyle().getGraphicsUtils(context).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
                computeStringWidth(context, g.getFont(), fm,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
                    "" + slider.getValue());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
            valueRect.x = thumbRect.x + (thumbRect.width - labelWidth) / 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
            // For horizontal sliders, make sure value is not painted
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
            // outside slider bounds.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
            if (slider.getOrientation() == JSlider.HORIZONTAL) {
4394
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 2658
diff changeset
   839
                if (valueRect.x + labelWidth > insetCache.left + contentRect.width) {
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 2658
diff changeset
   840
                    valueRect.x =  (insetCache.left + contentRect.width) - labelWidth;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
                valueRect.x = Math.max(valueRect.x, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
            g.setColor(context.getStyle().getColor(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
                    context, ColorType.TEXT_FOREGROUND));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
            context.getStyle().getGraphicsUtils(context).paintText(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
                    context, g, "" + slider.getValue(), valueRect.x,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
                    valueRect.y, -1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
        SynthContext subcontext = getContext(slider, Region.SLIDER_TRACK);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
        paintTrack(subcontext, g, trackRect);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
        subcontext.dispose();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
        subcontext = getContext(slider, Region.SLIDER_THUMB);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
        paintThumb(subcontext, g, thumbRect);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
        subcontext.dispose();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
        if (slider.getPaintTicks() && clip.intersects(tickRect)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
            paintTicks(g);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
90ce3da70b43 Initial load
duke
parents:
diff changeset
   864
        if (slider.getPaintLabels() && clip.intersects(labelRect)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
            paintLabels(g);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   867
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   868
4394
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 2658
diff changeset
   869
    /**
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 2658
diff changeset
   870
     * @inheritDoc
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 2658
diff changeset
   871
     */
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 2658
diff changeset
   872
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   873
    public void paintBorder(SynthContext context, Graphics g, int x,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   874
                            int y, int w, int h) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   875
        context.getPainter().paintSliderBorder(context, g, x, y, w, h,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   876
                                               slider.getOrientation());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   877
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   878
4394
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 2658
diff changeset
   879
    /**
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 2658
diff changeset
   880
     * Paints the slider thumb.
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 2658
diff changeset
   881
     *
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 2658
diff changeset
   882
     * @param context context for the component being painted
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 2658
diff changeset
   883
     * @param g {@code Graphics} object used for painting
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 2658
diff changeset
   884
     * @param thumbBounds bounding box for the thumb
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 2658
diff changeset
   885
     */
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 2658
diff changeset
   886
    protected void paintThumb(SynthContext context, Graphics g,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   887
            Rectangle thumbBounds)  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   888
        int orientation = slider.getOrientation();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   889
        SynthLookAndFeel.updateSubregion(context, g, thumbBounds);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   890
        context.getPainter().paintSliderThumbBackground(context, g,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   891
                             thumbBounds.x, thumbBounds.y, thumbBounds.width,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   892
                             thumbBounds.height, orientation);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   893
        context.getPainter().paintSliderThumbBorder(context, g,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   894
                             thumbBounds.x, thumbBounds.y, thumbBounds.width,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   895
                             thumbBounds.height, orientation);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   896
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   897
4394
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 2658
diff changeset
   898
    /**
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 2658
diff changeset
   899
     * Paints the slider track.
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 2658
diff changeset
   900
     *
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 2658
diff changeset
   901
     * @param context context for the component being painted
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 2658
diff changeset
   902
     * @param g {@code Graphics} object used for painting
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 2658
diff changeset
   903
     * @param trackBounds bounding box for the track
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 2658
diff changeset
   904
     */
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 2658
diff changeset
   905
    protected void paintTrack(SynthContext context, Graphics g,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   906
            Rectangle trackBounds) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   907
        int orientation = slider.getOrientation();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   908
        SynthLookAndFeel.updateSubregion(context, g, trackBounds);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   909
        context.getPainter().paintSliderTrackBackground(context, g,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   910
                trackBounds.x, trackBounds.y, trackBounds.width,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   911
                trackBounds.height, orientation);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   912
        context.getPainter().paintSliderTrackBorder(context, g,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   913
                trackBounds.x, trackBounds.y, trackBounds.width,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   914
                trackBounds.height, orientation);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   915
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   916
4394
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 2658
diff changeset
   917
    /**
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 2658
diff changeset
   918
     * @inheritDoc
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 2658
diff changeset
   919
     */
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 2658
diff changeset
   920
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   921
    public void propertyChange(PropertyChangeEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   922
        if (SynthLookAndFeel.shouldUpdateStyle(e)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   923
            updateStyle((JSlider)e.getSource());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   924
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   925
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   926
90ce3da70b43 Initial load
duke
parents:
diff changeset
   927
    //////////////////////////////////////////////////
90ce3da70b43 Initial load
duke
parents:
diff changeset
   928
    /// Track Listener Class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   929
    //////////////////////////////////////////////////
90ce3da70b43 Initial load
duke
parents:
diff changeset
   930
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   931
     * Track mouse movements.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   932
     */
4394
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 2658
diff changeset
   933
    private class SynthTrackListener extends TrackListener {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   934
4394
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 2658
diff changeset
   935
        @Override public void mouseExited(MouseEvent e) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   936
            setThumbActive(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   937
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   938
4394
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 2658
diff changeset
   939
        @Override public void mousePressed(MouseEvent e) {
2658
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 715
diff changeset
   940
            super.mousePressed(e);
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 715
diff changeset
   941
            setThumbPressed(thumbRect.contains(e.getX(), e.getY()));
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 715
diff changeset
   942
        }
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 715
diff changeset
   943
4394
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 2658
diff changeset
   944
        @Override public void mouseReleased(MouseEvent e) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   945
            super.mouseReleased(e);
2658
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 715
diff changeset
   946
            updateThumbState(e.getX(), e.getY(), false);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   947
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   948
4394
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 2658
diff changeset
   949
        @Override public void mouseDragged(MouseEvent e) {
676
8cf833d60e87 6709530: There are unnecessary code in slider classes, such as in JSlider and SliderUIs
rupashka
parents: 2
diff changeset
   950
            int thumbMiddle;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   951
90ce3da70b43 Initial load
duke
parents:
diff changeset
   952
            if (!slider.isEnabled()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   953
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   954
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   955
90ce3da70b43 Initial load
duke
parents:
diff changeset
   956
            currentMouseX = e.getX();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   957
            currentMouseY = e.getY();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   958
90ce3da70b43 Initial load
duke
parents:
diff changeset
   959
            if (!isDragging()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   960
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   961
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   962
90ce3da70b43 Initial load
duke
parents:
diff changeset
   963
            slider.setValueIsAdjusting(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   964
90ce3da70b43 Initial load
duke
parents:
diff changeset
   965
            switch (slider.getOrientation()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   966
            case JSlider.VERTICAL:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   967
                int halfThumbHeight = thumbRect.height / 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   968
                int thumbTop = e.getY() - offset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   969
                int trackTop = trackRect.y;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   970
                int trackBottom = trackRect.y + trackRect.height
90ce3da70b43 Initial load
duke
parents:
diff changeset
   971
                    - halfThumbHeight - trackBorder;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   972
                int vMax = yPositionForValue(slider.getMaximum() -
90ce3da70b43 Initial load
duke
parents:
diff changeset
   973
                    slider.getExtent());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   974
90ce3da70b43 Initial load
duke
parents:
diff changeset
   975
                if (drawInverted()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   976
                    trackBottom = vMax;
2658
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 715
diff changeset
   977
                    trackTop = trackTop + halfThumbHeight;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   978
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   979
                    trackTop = vMax;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   980
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   981
                thumbTop = Math.max(thumbTop, trackTop - halfThumbHeight);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   982
                thumbTop = Math.min(thumbTop, trackBottom - halfThumbHeight);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   983
90ce3da70b43 Initial load
duke
parents:
diff changeset
   984
                setThumbLocation(thumbRect.x, thumbTop);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   985
90ce3da70b43 Initial load
duke
parents:
diff changeset
   986
                thumbMiddle = thumbTop + halfThumbHeight;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   987
                slider.setValue(valueForYPosition(thumbMiddle));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   988
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   989
            case JSlider.HORIZONTAL:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   990
                int halfThumbWidth = thumbRect.width / 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   991
                int thumbLeft = e.getX() - offset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   992
                int trackLeft = trackRect.x + halfThumbWidth + trackBorder;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   993
                int trackRight = trackRect.x + trackRect.width
90ce3da70b43 Initial load
duke
parents:
diff changeset
   994
                    - halfThumbWidth - trackBorder;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   995
                int hMax = xPositionForValue(slider.getMaximum() -
90ce3da70b43 Initial load
duke
parents:
diff changeset
   996
                    slider.getExtent());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   997
90ce3da70b43 Initial load
duke
parents:
diff changeset
   998
                if (drawInverted()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   999
                    trackLeft = hMax;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1000
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1001
                    trackRight = hMax;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1002
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1003
                thumbLeft = Math.max(thumbLeft, trackLeft - halfThumbWidth);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1004
                thumbLeft = Math.min(thumbLeft, trackRight - halfThumbWidth);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1005
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1006
                setThumbLocation(thumbLeft, thumbRect.y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1007
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1008
                thumbMiddle = thumbLeft + halfThumbWidth;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1009
                slider.setValue(valueForXPosition(thumbMiddle));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1010
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1011
            default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1012
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1013
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1014
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1015
            if (slider.getValueIsAdjusting()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1016
                setThumbActive(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1017
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1018
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1019
4394
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 2658
diff changeset
  1020
        @Override public void mouseMoved(MouseEvent e) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1021
            updateThumbState(e.getX(), e.getY());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1022
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1023
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1024
}