Merge from mainline (gomp-merge-2005-02-26).
[official-gcc.git] / libjava / javax / swing / DefaultBoundedRangeModel.java
blob9c1962379a83e3f15e104050112388a188b1b429
1 /* DefaultBoundedRangeModel.java -- Default implementation
2 of BoundedRangeModel.
3 Copyright (C) 2002, 2004, 2005 Free Software Foundation, Inc.
5 This file is part of GNU Classpath.
7 GNU Classpath is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
12 GNU Classpath is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GNU Classpath; see the file COPYING. If not, write to the
19 Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
20 02111-1307 USA.
22 Linking this library statically or dynamically with other modules is
23 making a combined work based on this library. Thus, the terms and
24 conditions of the GNU General Public License cover the whole
25 combination.
27 As a special exception, the copyright holders of this library give you
28 permission to link this library with independent modules to produce an
29 executable, regardless of the license terms of these independent
30 modules, and to copy and distribute the resulting executable under
31 terms of your choice, provided that you also meet, for each linked
32 independent module, the terms and conditions of the license of that
33 module. An independent module is a module which is not derived from
34 or based on this library. If you modify this library, you may extend
35 this exception to your version of the library, but you are not
36 obligated to do so. If you do not wish to do so, delete this
37 exception statement from your version. */
40 package javax.swing;
42 import java.io.Serializable;
43 import java.util.EventListener;
45 import javax.swing.event.ChangeEvent;
46 import javax.swing.event.ChangeListener;
47 import javax.swing.event.EventListenerList;
49 /**
50 * A default implementation of <code>BoundedRangeModel</code>.
52 * @author Andrew Selkirk (aselkirk@sympatico.ca)
53 * @author Sascha Brawer (brawer@dandelis.ch)
55 public class DefaultBoundedRangeModel
56 implements BoundedRangeModel, Serializable
58 /**
59 * The identifier of this class in object serialization. Verified
60 * using the serialver tool of Sun J2SE 1.4.1_01.
62 private static final long serialVersionUID = 5034068491295259790L;
65 /**
66 * An event that is sent to all registered {@link ChangeListener}s
67 * when the state of this range model has changed.
69 * <p>The event object is created on demand, the first time it
70 * is actually needed.</p>
72 * @see #fireStateChanged()
74 protected transient ChangeEvent changeEvent;
77 /**
78 * The list of the currently registered EventListeners.
80 protected EventListenerList listenerList = new EventListenerList();
83 /**
84 * The current value of the range model, which is always between
85 * {@link #minimum} and ({@link #maximum} - {@link #extent}). In a
86 * scroll bar visualization of a {@link BoundedRangeModel}, the
87 * <code>value</code> is displayed as the position of the thumb.
89 private int value;
92 /**
93 * The current extent of the range model, which is a number greater
94 * than or equal to zero. In a scroll bar visualization of a {@link
95 * BoundedRangeModel}, the <code>extent</code> is displayed as the
96 * size of the thumb.
98 private int extent;
102 * The current minimum value of the range model, which is always
103 * less than or equal to {@link #maximum}.
105 private int minimum;
109 * The current maximum value of the range model, which is always
110 * greater than or equal to {@link #minimum}.
112 private int maximum;
116 * A property that indicates whether the value of this {@link
117 * BoundedRangeModel} is going to change in the immediate future.
119 private boolean isAdjusting;
123 * Constructs a <code>DefaultBoundedRangeModel</code> with default
124 * values for the properties. The properties <code>value</code>,
125 * <code>extent</code> and <code>minimum</code> will be initialized
126 * to zero; <code>maximum</code> will be set to 100; the property
127 * <code>valueIsAdjusting</code> will be <code>false</code>.
129 public DefaultBoundedRangeModel()
131 // The fields value, extent, minimum have the default value 0, and
132 // isAdjusting is already false. These fields no not need to be
133 // set explicitly.
134 maximum = 100;
139 * Constructs a <code>DefaultBoundedRangeModel</code> with the
140 * specified values for some properties.
142 * @param value the initial value of the range model, which must be
143 * a number between <code>minimum</code> and <code>(maximum -
144 * extent)</code>. In a scroll bar visualization of a {@link
145 * BoundedRangeModel}, the <code>value</code> is displayed as the
146 * position of the thumb.
148 * @param extent the initial extent of the range model, which is a
149 * number greater than or equal to zero. In a scroll bar
150 * visualization of a {@link BoundedRangeModel}, the
151 * <code>extent</code> is displayed as the size of the thumb.
153 * @param minimum the initial minimal value of the range model.
155 * @param maximum the initial maximal value of the range model.
157 * @throws IllegalArgumentException if the following condition is
158 * not satisfied: <code>minimum &lt;= value &lt;= value + extent &lt;=
159 * maximum</code>.
161 public DefaultBoundedRangeModel(int value, int extent, int minimum,
162 int maximum)
164 if (!(minimum <= value && extent >= 0 && (value + extent) <= maximum))
165 throw new IllegalArgumentException();
167 this.value = value;
168 this.extent = extent;
169 this.minimum = minimum;
170 this.maximum = maximum;
172 // The isAdjusting field already has a false value by default.
177 * Returns a string with all relevant properties of this range
178 * model.
180 * @return a string representing the object
182 public String toString()
184 return getClass().getName()
185 + "[value=" + value
186 + ", extent=" + extent
187 + ", min=" + minimum
188 + ", max=" + maximum
189 + ", adj=" + isAdjusting
190 + ']';
195 * Returns the current value of this bounded range model. In a
196 * scroll bar visualization of a {@link BoundedRangeModel}, the
197 * <code>value</code> is displayed as the position of the thumb.
199 * @return the value
201 public int getValue()
203 return value;
208 * Changes the current value of this bounded range model. In a
209 * scroll bar visualization of a {@link BoundedRangeModel}, the
210 * <code>value</code> is displayed as the position of the thumb;
211 * changing the <code>value</code> of a scroll bar's model
212 * thus moves the thumb to a different position.
214 * @param value the value
216 public void setValue(int value)
218 value = Math.max(minimum, value);
219 if (value + extent > maximum)
220 value = maximum - extent;
222 if (value != this.value)
224 this.value = value;
225 fireStateChanged();
231 * Returns the current extent of this bounded range model, which is
232 * a number greater than or equal to zero. In a scroll bar
233 * visualization of a {@link BoundedRangeModel}, the
234 * <code>extent</code> is displayed as the size of the thumb.
236 * @return the extent
238 public int getExtent()
240 return extent;
245 * Changes the current extent of this bounded range model. In a
246 * scroll bar visualization of a {@link BoundedRangeModel}, the
247 * <code>extent</code> is displayed as the size of the thumb.
249 * @param extent the new extent of the range model, which is a
250 * number greater than or equal to zero.
252 public void setExtent(int extent)
254 extent = Math.max(extent, 0);
255 if (value + extent > maximum)
256 extent = maximum - value;
258 if (extent != this.extent)
260 this.extent = extent;
261 fireStateChanged();
267 * Returns the current minimal value of this bounded range model.
269 public int getMinimum()
271 return minimum;
276 * Changes the current minimal value of this bounded range model.
278 * @param minimum the new minimal value.
280 public void setMinimum(int minimum)
282 int value, maximum;
284 maximum = Math.max(minimum, this.maximum);
285 value = Math.max(minimum, this.value);
287 setRangeProperties(value, extent, minimum, maximum, isAdjusting);
292 * Returns the current maximal value of this bounded range model.
294 * @return the maximum
296 public int getMaximum()
298 return maximum;
303 * Changes the current maximal value of this bounded range model.
305 * @param maximum the new maximal value.
307 public void setMaximum(int maximum)
309 int value, extent, minimum;
311 minimum = Math.min(this.minimum, maximum);
312 extent = Math.min(this.extent, maximum - minimum);
313 value = Math.min(this.value, maximum - extent);
315 setRangeProperties(value, extent, minimum, maximum, isAdjusting);
320 * Returns whether or not the value of this bounded range model is
321 * going to change in the immediate future. Scroll bars set this
322 * property to <code>true</code> while the thumb is being dragged
323 * around; when the mouse is relased, they set the property to
324 * <code>false</code> and post a final {@link ChangeEvent}.
326 * @return <code>true</code> if the value will change soon again;
327 * <code>false</code> if the value will probably not change soon.
329 public boolean getValueIsAdjusting()
331 return isAdjusting;
336 * Specifies whether or not the value of this bounded range model is
337 * going to change in the immediate future. Scroll bars set this
338 * property to <code>true</code> while the thumb is being dragged
339 * around; when the mouse is relased, they set the property to
340 * <code>false</code>.
342 * @param isAdjusting <code>true</code> if the value will change
343 * soon again; <code>false</code> if the value will probably not
344 * change soon.
346 public void setValueIsAdjusting(boolean isAdjusting)
348 if (isAdjusting == this.isAdjusting)
349 return;
351 this.isAdjusting = isAdjusting;
352 fireStateChanged();
357 * Sets all properties.
359 * @param value the new value of the range model. In a scroll bar
360 * visualization of a {@link BoundedRangeModel}, the
361 * <code>value</code> is displayed as the position of the thumb.
363 * @param extent the new extent of the range model, which is a
364 * number greater than or equal to zero. In a scroll bar
365 * visualization of a {@link BoundedRangeModel}, the
366 * <code>extent</code> is displayed as the size of the thumb.
368 * @param minimum the new minimal value of the range model.
370 * @param maximum the new maximal value of the range model.
372 * @param isAdjusting whether or not the value of this bounded range
373 * model is going to change in the immediate future. Scroll bars set
374 * this property to <code>true</code> while the thumb is being
375 * dragged around; when the mouse is relased, they set the property
376 * to <code>false</code>.
378 public void setRangeProperties(int value, int extent, int minimum,
379 int maximum, boolean isAdjusting)
381 minimum = Math.min(Math.min(minimum, maximum), value);
382 maximum = Math.max(value, maximum);
383 if (extent + value > maximum)
384 extent = maximum - value;
385 extent = Math.max(0, extent);
387 if ((value == this.value)
388 && (extent == this.extent)
389 && (minimum == this.minimum)
390 && (maximum == this.maximum)
391 && (isAdjusting == this.isAdjusting))
392 return;
394 this.value = value;
395 this.extent = extent;
396 this.minimum = minimum;
397 this.maximum = maximum;
398 this.isAdjusting = isAdjusting;
400 fireStateChanged();
405 * Subscribes a ChangeListener to state changes.
407 * @param listener the listener to be subscribed.
409 public void addChangeListener(ChangeListener listener)
411 listenerList.add(ChangeListener.class, listener);
416 * Cancels the subscription of a ChangeListener.
418 * @param listener the listener to be unsubscribed.
420 public void removeChangeListener(ChangeListener listener)
422 listenerList.remove(ChangeListener.class, listener);
427 * Sends a {@link ChangeEvent} to any registered {@link
428 * ChangeListener}s.
430 * @see #addChangeListener(ChangeListener)
431 * @see #removeChangeListener(ChangeListener)
433 protected void fireStateChanged()
435 ChangeListener[] listeners = getChangeListeners();
437 if (changeEvent == null)
438 changeEvent = new ChangeEvent(this);
440 for (int i = listeners.length - 1; i >= 0; --i)
441 listeners[i].stateChanged(changeEvent);
446 * Retrieves the current listeners of the specified class.
448 * @param c the class of listeners; usually {@link
449 * ChangeListener}<code>.class</code>.
451 * @return an array with the currently subscribed listeners, or
452 * an empty array if there are currently no listeners.
454 * @since 1.3
456 public EventListener[] getListeners(Class listenerType)
458 return listenerList.getListeners(listenerType);
463 * Returns all <code>ChangeListeners</code> that are currently
464 * subscribed for changes to this
465 * <code>DefaultBoundedRangeModel</code>.
467 * @return an array with the currently subscribed listeners, or
468 * an empty array if there are currently no listeners.
470 * @since 1.4
472 public ChangeListener[] getChangeListeners()
474 return (ChangeListener[]) getListeners(ChangeListener.class);