Warning: Can't use blame annotator:
svn blame failed on trunk/src/scriptbuilder/gui/drawers/RangeSlider.java: ("Can't find a temporary directory: Internal error", 20014)

source: tmcsimulator-scriptbuilder/trunk/src/scriptbuilder/gui/drawers/RangeSlider.java @ 84

Revision 84, 4.2 KB checked in by jdalbey, 9 years ago (diff)

RangeSlider?: Fix defect that was allowing slider thumb to be dragged too far right.

RevLine 
1package scriptbuilder.gui.drawers;
2
3import javax.swing.JSlider;
4
5/*
6The MIT License
7
8Copyright (c) 2010 Ernest Yu. All rights reserved.
9
10Permission is hereby granted, free of charge, to any person obtaining a copy
11of this software and associated documentation files (the "Software"), to deal
12in the Software without restriction, including without limitation the rights
13to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14copies of the Software, and to permit persons to whom the Software is
15furnished to do so, subject to the following conditions:
16
17The above copyright notice and this permission notice shall be included in
18all copies or substantial portions of the Software.
19
20THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
26THE SOFTWARE.
27 */
28/**
29 * An extension of JSlider to select a range of values using two thumb controls.
30 * The thumb controls are used to select the lower and upper value of a range
31 * with predetermined minimum and maximum values.
32 *
33 * <p>
34 * Note that RangeSlider makes use of the default BoundedRangeModel, which
35 * supports an inner range defined by a value and an extent. The upper value
36 * returned by RangeSlider is simply the lower value plus the extent.</p>
37 */
38public class RangeSlider extends JSlider
39{
40
41    /**
42     * Constructs a RangeSlider with default minimum and maximum values of 0 and
43     * 100.
44     */
45    public RangeSlider()
46    {
47        initSlider();
48    }
49
50    /**
51     * Constructs a RangeSlider with the specified default minimum and maximum
52     * values.
53     */
54    public RangeSlider(int min, int max)
55    {
56        super(min, max);
57        initSlider();
58    }
59
60    /**
61     * Initializes the slider by setting default properties.
62     */
63    private void initSlider()
64    {
65        setOrientation(HORIZONTAL);
66    }
67
68    /**
69     * Overrides the superclass method to install the UI delegate to draw two
70     * thumbs.
71     */
72    @Override
73    public void updateUI()
74    {
75        setUI(new RangeSliderUI(this));
76        // Update UI for slider labels.  This must be called after updating the
77        // UI of the slider.  Refer to JSlider.updateUI().
78        updateLabelUIs();
79    }
80
81    /**
82     * Returns the lower value in the range.
83     */
84    @Override
85    public int getValue()
86    {
87        return super.getValue();
88    }
89
90    /**
91     * Sets the lower value in the range.
92     */
93    @Override
94    public void setValue(int value)
95    {
96        int oldValue = getValue();
97        if (oldValue == value)
98        {
99            return;
100        }
101
102        // Compute new value and extent to maintain upper value.
103        int oldExtent = getExtent();
104        // Ensure value is greater than minimum allowed
105        int lower = Math.max(getMinimum(), value);
106        // Ensure value is less than maximum allowed
107        // Fix defect that was letting lower thumb go to far right.
108        // Note this still allows the mouse to drag it beyond limit,
109        // but when released it returns to proper place.
110//        int newValue = Math.min(lower, oldValue + oldExtent);
111        int newValue = Math.min(lower, getMaximum() - oldExtent);
112        // Hacked to not change the extent (move the entire slider)
113        //int newExtent = oldExtent + oldValue - newValue;
114
115        // Set new value and fire a single change event.
116        getModel().setRangeProperties(newValue, oldExtent, getMinimum(),
117                getMaximum(), getValueIsAdjusting());
118    }
119
120    /**
121     * Returns the upper value in the range.
122     */
123    public int getUpperValue()
124    {
125        return getValue() + getExtent();
126    }
127
128    /**
129     * Sets the upper value in the range.
130     */
131    public void setUpperValue(int value)
132    {
133        // Compute new extent.
134        int lowerValue = getValue();
135        int newExtent = Math.min(Math.max(0, value - lowerValue), getMaximum() - lowerValue);
136
137        // Set extent to set upper value.
138        setExtent(newExtent);
139    }
140}
Note: See TracBrowser for help on using the repository browser.