001: /*
002: * @(#)RangeSlider.java 11/22/2005
003: *
004: * Copyright 2002 - 2005 JIDE Software Inc. All rights reserved.
005: */
006: package com.jidesoft.swing;
007:
008: import com.jidesoft.plaf.LookAndFeelFactory;
009: import com.jidesoft.plaf.UIDefaultsLookup;
010:
011: import javax.swing.*;
012:
013: /**
014: * <tt>RangeSlider</tt> is a slider that can be used to select a range. A regular slider has only one thumb.
015: * So it can only be used to select one value. <tt>RangeSlider</tt> has two thumbs. Each one can be moved
016: * independently or both are moved together.
017: * <p/>
018: * {@link #getLowValue()} will return the value of low range and {@link #getHighValue()} is the high range.
019: */
020: public class RangeSlider extends JSlider {
021:
022: private static final String uiClassID = "RangeSliderUI";
023:
024: /**
025: * Creates a horizontal range slider with the range 0 to 100 and
026: * initial low and high values both at 50.
027: */
028: public RangeSlider() {
029: }
030:
031: /**
032: * Creates a range slider using the specified orientation with the
033: * range 0 to 100 and initial low and high values both at 50.
034: *
035: * @param orientation the orientation of the <code>RangeSlider</code>.
036: */
037: public RangeSlider(int orientation) {
038: super (orientation);
039: }
040:
041: /**
042: * Creates a horizontal slider using the specified min and max
043: * with an initial value equal to the average of the min plus max.
044: * and initial low and high values both at 50.
045: *
046: * @param min the minimum value of the slider.
047: * @param max the maximum value of the slider.
048: */
049: public RangeSlider(int min, int max) {
050: super (min, max);
051: }
052:
053: /**
054: * Creates a horizontal slider using the specified min, max, low and high value.
055: *
056: * @param min the minimum value of the slider.
057: * @param max the maximum value of the slider.
058: * @param low the low value of the slider since it is a range.
059: * @param high the high value of the slider since it is a range.
060: */
061: public RangeSlider(int min, int max, int low, int high) {
062: super (new DefaultBoundedRangeModel(low, high - low, min, max));
063: }
064:
065: /**
066: * Resets the UI property to a value from the current look and
067: * feel.
068: *
069: * @see javax.swing.JComponent#updateUI
070: */
071: @Override
072: public void updateUI() {
073: if (UIDefaultsLookup.get(uiClassID) == null) {
074: LookAndFeelFactory.installJideExtension();
075: }
076: setUI(UIManager.getUI(this ));
077: }
078:
079: /**
080: * Returns a string that specifies the name of the L&F class
081: * that renders this component.
082: *
083: * @return the string "RangeSliderUI"
084: * @see javax.swing.JComponent#getUIClassID
085: * @see javax.swing.UIDefaults#getUI
086: */
087: @Override
088: public String getUIClassID() {
089: return uiClassID;
090: }
091:
092: /**
093: * Returns the range slider's low value.
094: *
095: * @return the range slider's low value.
096: */
097: public int getLowValue() {
098: return getModel().getValue();
099: }
100:
101: /**
102: * Returns the range slider's high value.
103: *
104: * @return the range slider's high value.
105: */
106: public int getHighValue() {
107: return getModel().getValue() + getModel().getExtent();
108: }
109:
110: /**
111: * Returns true if the specified value is within the range slider's range.
112: *
113: * @param value value
114: * @return true if the specified value is within the range slider's range.
115: */
116: public boolean contains(int value) {
117: return (value >= getLowValue() && value <= getHighValue());
118: }
119:
120: /**
121: * Sets the range slider's low value. This method just forwards
122: * the value to the model.
123: *
124: * @param lowValue the new low value
125: */
126: public void setLowValue(int lowValue) {
127: int high;
128: if ((lowValue + getModel().getExtent()) > getMaximum()) {
129: high = getMaximum();
130: } else {
131: high = getHighValue();
132: }
133: int extent = high - lowValue;
134:
135: getModel().setRangeProperties(lowValue, extent, getMinimum(),
136: getMaximum(), true);
137: }
138:
139: /**
140: * Sets the range slider's high value. This method just forwards
141: * the value to the model.
142: *
143: * @param highValue the new high value
144: */
145: public void setHighValue(int highValue) {
146: getModel().setExtent(highValue - getLowValue());
147: }
148: }
|