| 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 | * 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 | */ |
|---|
| 38 | public 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 | } |
|---|