Path: blob/master/src/java.desktop/share/classes/javax/swing/DefaultBoundedRangeModel.java
41153 views
/*1* Copyright (c) 1997, 2014, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package javax.swing;2627import javax.swing.event.*;28import java.io.Serializable;29import java.util.EventListener;3031/**32* A generic implementation of BoundedRangeModel.33* <p>34* <strong>Warning:</strong>35* Serialized objects of this class will not be compatible with36* future Swing releases. The current serialization support is37* appropriate for short term storage or RMI between applications running38* the same version of Swing. As of 1.4, support for long term storage39* of all JavaBeans40* has been added to the <code>java.beans</code> package.41* Please see {@link java.beans.XMLEncoder}.42*43* @author David Kloba44* @author Hans Muller45* @see BoundedRangeModel46* @since 1.247*/48@SuppressWarnings("serial") // Same-version serialization only49public class DefaultBoundedRangeModel implements BoundedRangeModel, Serializable50{51/**52* Only one <code>ChangeEvent</code> is needed per model instance since the53* event's only (read-only) state is the source property. The source54* of events generated here is always "this".55*/56protected transient ChangeEvent changeEvent = null;5758/** The listeners waiting for model changes. */59protected EventListenerList listenerList = new EventListenerList();6061private int value = 0;62private int extent = 0;63private int min = 0;64private int max = 100;65private boolean isAdjusting = false;666768/**69* Initializes all of the properties with default values.70* Those values are:71* <ul>72* <li><code>value</code> = 073* <li><code>extent</code> = 074* <li><code>minimum</code> = 075* <li><code>maximum</code> = 10076* <li><code>adjusting</code> = false77* </ul>78*/79public DefaultBoundedRangeModel() {80}818283/**84* Initializes value, extent, minimum and maximum. Adjusting is false.85* Throws an <code>IllegalArgumentException</code> if the following86* constraints aren't satisfied:87* <pre>88* min <= value <= value+extent <= max89* </pre>90*91* @param value an int giving the current value92* @param extent the length of the inner range that begins at the model's value93* @param min an int giving the minimum value94* @param max an int giving the maximum value95*/96public DefaultBoundedRangeModel(int value, int extent, int min, int max)97{98if ((max >= min) &&99(value >= min) &&100((value + extent) >= value) &&101((value + extent) <= max)) {102this.value = value;103this.extent = extent;104this.min = min;105this.max = max;106}107else {108throw new IllegalArgumentException("invalid range properties");109}110}111112113/**114* Returns the model's current value.115* @return the model's current value116* @see #setValue117* @see BoundedRangeModel#getValue118*/119public int getValue() {120return value;121}122123124/**125* Returns the model's extent.126* @return the model's extent127* @see #setExtent128* @see BoundedRangeModel#getExtent129*/130public int getExtent() {131return extent;132}133134135/**136* Returns the model's minimum.137* @return the model's minimum138* @see #setMinimum139* @see BoundedRangeModel#getMinimum140*/141public int getMinimum() {142return min;143}144145146/**147* Returns the model's maximum.148* @return the model's maximum149* @see #setMaximum150* @see BoundedRangeModel#getMaximum151*/152public int getMaximum() {153return max;154}155156157/**158* Sets the current value of the model. For a slider, that159* determines where the knob appears. Ensures that the new160* value, <I>n</I> falls within the model's constraints:161* <pre>162* minimum <= value <= value+extent <= maximum163* </pre>164*165* @see BoundedRangeModel#setValue166*/167public void setValue(int n) {168n = Math.min(n, Integer.MAX_VALUE - extent);169170int newValue = Math.max(n, min);171if (newValue + extent > max) {172newValue = max - extent;173}174setRangeProperties(newValue, extent, min, max, isAdjusting);175}176177178/**179* Sets the extent to <I>n</I> after ensuring that <I>n</I>180* is greater than or equal to zero and falls within the model's181* constraints:182* <pre>183* minimum <= value <= value+extent <= maximum184* </pre>185* @see BoundedRangeModel#setExtent186*/187public void setExtent(int n) {188int newExtent = Math.max(0, n);189if(value + newExtent > max) {190newExtent = max - value;191}192setRangeProperties(value, newExtent, min, max, isAdjusting);193}194195196/**197* Sets the minimum to <I>n</I> after ensuring that <I>n</I>198* that the other three properties obey the model's constraints:199* <pre>200* minimum <= value <= value+extent <= maximum201* </pre>202* @see #getMinimum203* @see BoundedRangeModel#setMinimum204*/205public void setMinimum(int n) {206int newMax = Math.max(n, max);207int newValue = Math.max(n, value);208int newExtent = Math.min(newMax - newValue, extent);209setRangeProperties(newValue, newExtent, n, newMax, isAdjusting);210}211212213/**214* Sets the maximum to <I>n</I> after ensuring that <I>n</I>215* that the other three properties obey the model's constraints:216* <pre>217* minimum <= value <= value+extent <= maximum218* </pre>219* @see BoundedRangeModel#setMaximum220*/221public void setMaximum(int n) {222int newMin = Math.min(n, min);223int newExtent = Math.min(n - newMin, extent);224int newValue = Math.min(n - newExtent, value);225setRangeProperties(newValue, newExtent, newMin, n, isAdjusting);226}227228229/**230* Sets the <code>valueIsAdjusting</code> property.231*232* @see #getValueIsAdjusting233* @see #setValue234* @see BoundedRangeModel#setValueIsAdjusting235*/236public void setValueIsAdjusting(boolean b) {237setRangeProperties(value, extent, min, max, b);238}239240241/**242* Returns true if the value is in the process of changing243* as a result of actions being taken by the user.244*245* @return the value of the <code>valueIsAdjusting</code> property246* @see #setValue247* @see BoundedRangeModel#getValueIsAdjusting248*/249public boolean getValueIsAdjusting() {250return isAdjusting;251}252253254/**255* Sets all of the <code>BoundedRangeModel</code> properties after forcing256* the arguments to obey the usual constraints:257* <pre>258* minimum <= value <= value+extent <= maximum259* </pre>260* <p>261* At most, one <code>ChangeEvent</code> is generated.262*263* @see BoundedRangeModel#setRangeProperties264* @see #setValue265* @see #setExtent266* @see #setMinimum267* @see #setMaximum268* @see #setValueIsAdjusting269*/270public void setRangeProperties(int newValue, int newExtent, int newMin, int newMax, boolean adjusting)271{272if (newMin > newMax) {273newMin = newMax;274}275if (newValue > newMax) {276newMax = newValue;277}278if (newValue < newMin) {279newMin = newValue;280}281282/* Convert the addends to long so that extent can be283* Integer.MAX_VALUE without rolling over the sum.284* A JCK test covers this, see bug 4097718.285*/286if (((long)newExtent + (long)newValue) > newMax) {287newExtent = newMax - newValue;288}289290if (newExtent < 0) {291newExtent = 0;292}293294boolean isChange =295(newValue != value) ||296(newExtent != extent) ||297(newMin != min) ||298(newMax != max) ||299(adjusting != isAdjusting);300301if (isChange) {302value = newValue;303extent = newExtent;304min = newMin;305max = newMax;306isAdjusting = adjusting;307308fireStateChanged();309}310}311312313/**314* Adds a <code>ChangeListener</code>. The change listeners are run each315* time any one of the Bounded Range model properties changes.316*317* @param l the ChangeListener to add318* @see #removeChangeListener319* @see BoundedRangeModel#addChangeListener320*/321public void addChangeListener(ChangeListener l) {322listenerList.add(ChangeListener.class, l);323}324325326/**327* Removes a <code>ChangeListener</code>.328*329* @param l the <code>ChangeListener</code> to remove330* @see #addChangeListener331* @see BoundedRangeModel#removeChangeListener332*/333public void removeChangeListener(ChangeListener l) {334listenerList.remove(ChangeListener.class, l);335}336337338/**339* Returns an array of all the change listeners340* registered on this <code>DefaultBoundedRangeModel</code>.341*342* @return all of this model's <code>ChangeListener</code>s343* or an empty344* array if no change listeners are currently registered345*346* @see #addChangeListener347* @see #removeChangeListener348*349* @since 1.4350*/351public ChangeListener[] getChangeListeners() {352return listenerList.getListeners(ChangeListener.class);353}354355356/**357* Runs each <code>ChangeListener</code>'s <code>stateChanged</code> method.358*359* @see #setRangeProperties360* @see EventListenerList361*/362protected void fireStateChanged()363{364Object[] listeners = listenerList.getListenerList();365for (int i = listeners.length - 2; i >= 0; i -=2 ) {366if (listeners[i] == ChangeListener.class) {367if (changeEvent == null) {368changeEvent = new ChangeEvent(this);369}370((ChangeListener)listeners[i+1]).stateChanged(changeEvent);371}372}373}374375376/**377* Returns a string that displays all of the378* <code>BoundedRangeModel</code> properties.379*/380public String toString() {381String modelString =382"value=" + getValue() + ", " +383"extent=" + getExtent() + ", " +384"min=" + getMinimum() + ", " +385"max=" + getMaximum() + ", " +386"adj=" + getValueIsAdjusting();387388return getClass().getName() + "[" + modelString + "]";389}390391/**392* Returns an array of all the objects currently registered as393* <code><em>Foo</em>Listener</code>s394* upon this model.395* <code><em>Foo</em>Listener</code>s396* are registered using the <code>add<em>Foo</em>Listener</code> method.397* <p>398* You can specify the <code>listenerType</code> argument399* with a class literal, such as <code><em>Foo</em>Listener.class</code>.400* For example, you can query a <code>DefaultBoundedRangeModel</code>401* instance <code>m</code>402* for its change listeners403* with the following code:404*405* <pre>ChangeListener[] cls = (ChangeListener[])(m.getListeners(ChangeListener.class));</pre>406*407* If no such listeners exist,408* this method returns an empty array.409*410* @param <T> the type of {@code EventListener} class being requested411* @param listenerType the type of listeners requested;412* this parameter should specify an interface413* that descends from <code>java.util.EventListener</code>414* @return an array of all objects registered as415* <code><em>Foo</em>Listener</code>s416* on this model,417* or an empty array if no such418* listeners have been added419* @exception ClassCastException if <code>listenerType</code> doesn't420* specify a class or interface that implements421* <code>java.util.EventListener</code>422*423* @see #getChangeListeners424*425* @since 1.3426*/427public <T extends EventListener> T[] getListeners(Class<T> listenerType) {428return listenerList.getListeners(listenerType);429}430}431432433