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