FileDocCategorySizeDatePackage
DefaultBoundedRangeModel.javaAPI DocJava SE 5 API11939Fri Aug 26 14:57:54 BST 2005javax.swing

DefaultBoundedRangeModel

public class DefaultBoundedRangeModel extends Object implements BoundedRangeModel, Serializable
A generic implementation of BoundedRangeModel.

Warning: Serialized objects of this class will not be compatible with future Swing releases. The current serialization support is appropriate for short term storage or RMI between applications running the same version of Swing. As of 1.4, support for long term storage of all JavaBeansTM has been added to the java.beans package. Please see {@link java.beans.XMLEncoder}.

version
1.46 05/05/04
author
David Kloba
author
Hans Muller
see
BoundedRangeModel

Fields Summary
protected transient ChangeEvent
changeEvent
Only one ChangeEvent is needed per model instance since the event's only (read-only) state is the source property. The source of events generated here is always "this".
protected EventListenerList
listenerList
The listeners waiting for model changes.
private int
value
private int
extent
private int
min
private int
max
private boolean
isAdjusting
Constructors Summary
public DefaultBoundedRangeModel()
Initializes all of the properties with default values. Those values are:
  • value = 0
  • extent = 0
  • minimum = 0
  • maximum = 100
  • adjusting = false



                                     
      
    
public DefaultBoundedRangeModel(int value, int extent, int min, int max)
Initializes value, extent, minimum and maximum. Adjusting is false. Throws an IllegalArgumentException if the following constraints aren't satisfied:
min <= value <= value+extent <= max

        if ((max >= min) && 
	    (value >= min) && 
	    ((value + extent) >= value) &&   
	    ((value + extent) <= max)) {
            this.value = value;
            this.extent = extent;
            this.min = min;
            this.max = max;
        }
        else {
            throw new IllegalArgumentException("invalid range properties");
        }
    
Methods Summary
public voidaddChangeListener(javax.swing.event.ChangeListener l)
Adds a ChangeListener. The change listeners are run each time any one of the Bounded Range model properties changes.

param
l the ChangeListener to add
see
#removeChangeListener
see
BoundedRangeModel#addChangeListener

        listenerList.add(ChangeListener.class, l);
    
protected voidfireStateChanged()
Runs each ChangeListener's stateChanged method.

see
#setRangeProperties
see
EventListenerList

        Object[] listeners = listenerList.getListenerList();
        for (int i = listeners.length - 2; i >= 0; i -=2 ) {
            if (listeners[i] == ChangeListener.class) {
                if (changeEvent == null) {
                    changeEvent = new ChangeEvent(this);
                }
                ((ChangeListener)listeners[i+1]).stateChanged(changeEvent);
            }          
        }
    
public javax.swing.event.ChangeListener[]getChangeListeners()
Returns an array of all the change listeners registered on this DefaultBoundedRangeModel.

return
all of this model's ChangeListeners or an empty array if no change listeners are currently registered
see
#addChangeListener
see
#removeChangeListener
since
1.4

        return (ChangeListener[])listenerList.getListeners(
                ChangeListener.class);
    
public intgetExtent()
Returns the model's extent.

return
the model's extent
see
#setExtent
see
BoundedRangeModel#getExtent

      return extent; 
    
public T[]getListeners(java.lang.Class listenerType)
Returns an array of all the objects currently registered as FooListeners upon this model. FooListeners are registered using the addFooListener method.

You can specify the listenerType argument with a class literal, such as FooListener.class. For example, you can query a DefaultBoundedRangeModel instance m for its change listeners with the following code:

ChangeListener[] cls = (ChangeListener[])(m.getListeners(ChangeListener.class));
If no such listeners exist, this method returns an empty array.

param
listenerType the type of listeners requested; this parameter should specify an interface that descends from java.util.EventListener
return
an array of all objects registered as FooListeners on this model, or an empty array if no such listeners have been added
exception
ClassCastException if listenerType doesn't specify a class or interface that implements java.util.EventListener
see
#getChangeListeners
since
1.3

 
	return listenerList.getListeners(listenerType); 
    
public intgetMaximum()
Returns the model's maximum.

return
the model's maximum
see
#setMaximum
see
BoundedRangeModel#getMaximum

        return max; 
    
public intgetMinimum()
Returns the model's minimum.

return
the model's minimum
see
#setMinimum
see
BoundedRangeModel#getMinimum

      return min; 
    
public intgetValue()
Returns the model's current value.

return
the model's current value
see
#setValue
see
BoundedRangeModel#getValue

      return value; 
    
public booleangetValueIsAdjusting()
Returns true if the value is in the process of changing as a result of actions being taken by the user.

return
the value of the valueIsAdjusting property
see
#setValue
see
BoundedRangeModel#getValueIsAdjusting

        return isAdjusting; 
    
public voidremoveChangeListener(javax.swing.event.ChangeListener l)
Removes a ChangeListener.

param
l the ChangeListener to remove
see
#addChangeListener
see
BoundedRangeModel#removeChangeListener

        listenerList.remove(ChangeListener.class, l);
    
public voidsetExtent(int n)
Sets the extent to n after ensuring that n is greater than or equal to zero and falls within the model's constraints:
minimum <= value <= value+extent <= maximum

see
BoundedRangeModel#setExtent

        int newExtent = Math.max(0, n);
        if(value + newExtent > max) {
            newExtent = max - value;
        }
        setRangeProperties(value, newExtent, min, max, isAdjusting);
    
public voidsetMaximum(int n)
Sets the maximum to n after ensuring that n that the other three properties obey the model's constraints:
minimum <= value <= value+extent <= maximum

see
BoundedRangeModel#setMaximum

        int newMin = Math.min(n, min);
        int newExtent = Math.min(n - newMin, extent);
        int newValue = Math.min(n - newExtent, value);
        setRangeProperties(newValue, newExtent, newMin, n, isAdjusting);
    
public voidsetMinimum(int n)
Sets the minimum to n after ensuring that n that the other three properties obey the model's constraints:
minimum <= value <= value+extent <= maximum

see
#getMinimum
see
BoundedRangeModel#setMinimum

        int newMax = Math.max(n, max);
        int newValue = Math.max(n, value);
        int newExtent = Math.min(newMax - newValue, extent);
        setRangeProperties(newValue, newExtent, n, newMax, isAdjusting);
    
public voidsetRangeProperties(int newValue, int newExtent, int newMin, int newMax, boolean adjusting)
Sets all of the BoundedRangeModel properties after forcing the arguments to obey the usual constraints:
minimum <= value <= value+extent <= maximum

At most, one ChangeEvent is generated.

see
BoundedRangeModel#setRangeProperties
see
#setValue
see
#setExtent
see
#setMinimum
see
#setMaximum
see
#setValueIsAdjusting

        if (newMin > newMax) {
            newMin = newMax;
	}
        if (newValue > newMax) {
            newMax = newValue;
	}
        if (newValue < newMin) {
            newMin = newValue;
	}

	/* Convert the addends to long so that extent can be 
	 * Integer.MAX_VALUE without rolling over the sum.
	 * A JCK test covers this, see bug 4097718.
	 */
        if (((long)newExtent + (long)newValue) > newMax) {
            newExtent = newMax - newValue;
	}
	
        if (newExtent < 0) {
            newExtent = 0;
	}

        boolean isChange =
            (newValue != value) ||
            (newExtent != extent) ||
            (newMin != min) ||
            (newMax != max) ||
            (adjusting != isAdjusting);

        if (isChange) {
            value = newValue;
            extent = newExtent;
            min = newMin;
            max = newMax;
            isAdjusting = adjusting;

            fireStateChanged();
        }
    
public voidsetValue(int n)
Sets the current value of the model. For a slider, that determines where the knob appears. Ensures that the new value, n falls within the model's constraints:
minimum <= value <= value+extent <= maximum

see
BoundedRangeModel#setValue

        n = Math.min(n, Integer.MAX_VALUE - extent);

        int newValue = Math.max(n, min);
        if (newValue + extent > max) {
            newValue = max - extent; 
        }
        setRangeProperties(newValue, extent, min, max, isAdjusting);
    
public voidsetValueIsAdjusting(boolean b)
Sets the valueIsAdjusting property.

see
#getValueIsAdjusting
see
#setValue
see
BoundedRangeModel#setValueIsAdjusting

        setRangeProperties(value, extent, min, max, b);
    
public java.lang.StringtoString()
Returns a string that displays all of the BoundedRangeModel properties.

        String modelString =
            "value=" + getValue() + ", " +
            "extent=" + getExtent() + ", " +
            "min=" + getMinimum() + ", " +
            "max=" + getMaximum() + ", " +
            "adj=" + getValueIsAdjusting();

        return getClass().getName() + "[" + modelString + "]";