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