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