org.wings.SPagingBoundedRangeModel Maven / Gradle / Ivy
The newest version!
/*
* Copyright 2000,2005 wingS development team.
*
* This file is part of wingS (http://wingsframework.org).
*
* wingS is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License
* as published by the Free Software Foundation; either version 2.1
* of the License, or (at your option) any later version.
*
* Please see COPYING for the complete licence.
*/
package org.wings;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import javax.swing.event.EventListenerList;
import java.util.EventListener;
/**
* Paging implementation of SBoundedRangeModel
*
* @author Armin Haaf
*/
public class SPagingBoundedRangeModel implements SBoundedRangeModel {
/**
* 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 transient ChangeEvent changeEvent = null;
/**
* The listeners waiting for model changes.
*/
protected EventListenerList listenerList = new EventListenerList();
protected int value = 0;
protected int extent = 0;
protected int min = 0;
protected int max = 100;
protected boolean isAdjusting = false;
/**
* indicates if we should fire event immediately when they arise, or if we
* should collect them for a later delivery
*/
private boolean delayEvents = false;
/**
* got a delayed Event?
*/
protected boolean gotDelayedEvent = false;
public SPagingBoundedRangeModel() {
super();
}
public SPagingBoundedRangeModel(int value, int extent, int min, int max) {
setRangeProperties(value, extent, min, max, false);
}
/**
* Returns the model's current value.
*
* @return the model's current value
* @see #setValue
* @see javax.swing.BoundedRangeModel#getValue
*/
@Override
public int getValue() {
return value;
}
/**
* Returns the model's extent.
*
* @return the model's extent
* @see #setExtent
* @see javax.swing.BoundedRangeModel#getExtent
*/
@Override
public int getExtent() {
return extent;
}
/**
* Returns the model's minimum.
*
* @return the model's minimum
* @see #setMinimum
* @see javax.swing.BoundedRangeModel#getMinimum
*/
@Override
public int getMinimum() {
return min;
}
/**
* Returns the model's maximum.
*
* @return the model's maximum
* @see #setMaximum
* @see javax.swing.BoundedRangeModel#getMaximum
*/
@Override
public int getMaximum() {
return max;
}
/**
* 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 <= maximum
*
*
* @see javax.swing.BoundedRangeModel#setValue
*/
@Override
public void setValue(int n) {
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);
}
/**
* 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 <= maximum
*
*
* @see javax.swing.BoundedRangeModel#setExtent
*/
@Override
public void setExtent(int n) {
int newExtent = Math.max(0, n);
if(value + newExtent > max) {
newExtent = max - value;
}
setRangeProperties(value, newExtent, min, max, isAdjusting);
}
/**
* Sets the minimum to n after ensuring that n
* that the other three properties obey the model's constraints:
*
* minimum <= value <= maximum
*
*
* @see #getMinimum
* @see javax.swing.BoundedRangeModel#setMinimum
*/
@Override
public void setMinimum(int n) {
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);
}
/**
* Sets the maximum to n after ensuring that n
* that the other three properties obey the model's constraints:
*
* minimum <= value <= maximum
*
*
* @see javax.swing.BoundedRangeModel#setMaximum
*/
@Override
public void setMaximum(int n) {
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);
}
/**
* Sets the valueIsAdjusting
property.
*
* @see #getValueIsAdjusting
* @see #setValue
* @see javax.swing.BoundedRangeModel#setValueIsAdjusting
*/
@Override
public void setValueIsAdjusting(boolean b) {
setRangeProperties(value, extent, min, max, b);
}
/**
* 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 javax.swing.BoundedRangeModel#getValueIsAdjusting
*/
@Override
public boolean getValueIsAdjusting() {
return isAdjusting;
}
/**
* Sets all of the BoundedRangeModel
properties after forcing
* the arguments to obey the usual constraints:
*
* minimum <= value <= maximum
*
*
* At most, one ChangeEvent
is generated.
*
* @see javax.swing.BoundedRangeModel#setRangeProperties
* @see #setValue
* @see #setExtent
* @see #setMinimum
* @see #setMaximum
* @see #setValueIsAdjusting
*/
@Override
public void setRangeProperties(int newValue,
int newExtent,
int newMin,
int newMax,
boolean adjusting) {
if (newMin > newMax) {
newMin = newMax;
}
if (newValue > newMax) {
newMax = newValue;
}
if (newValue < newMin) {
newMin = newValue;
}
if (newExtent < 0) {
newExtent = 0;
}
if ((newExtent + newValue) > newMax) {
newExtent = newMax - newValue;
}
boolean isChange =
(newValue != value) ||
(newExtent != extent) ||
(newMin != min) ||
(newMax != max) ||
(adjusting != isAdjusting);
if (isChange) {
int oldVal = value;
value = newValue;
extent = newExtent;
min = newMin;
max = newMax;
isAdjusting = adjusting;
fireStateChanged();
}
}
@Override
public boolean getDelayEvents() {
return delayEvents;
}
@Override
public void setDelayEvents(boolean b) {
delayEvents = b;
}
/**
* 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 javax.swing.BoundedRangeModel#addChangeListener
*/
@Override
public void addChangeListener(ChangeListener l) {
listenerList.add(ChangeListener.class, l);
}
/**
* Removes a ChangeListener
.
*
* @param l the ChangeListener
to remove
* @see #addChangeListener
* @see javax.swing.BoundedRangeModel#removeChangeListener
*/
@Override
public void removeChangeListener(ChangeListener l) {
listenerList.remove(ChangeListener.class, l);
}
/**
* Returns an array of all the change listeners
* registered on this DefaultBoundedRangeModel
.
*
* @return all of this model's ChangeListener
s
* or an empty
* array if no change listeners are currently registered
* @see #addChangeListener
* @see #removeChangeListener
* @since 1.4
*/
public ChangeListener[] getChangeListeners() {
return listenerList.getListeners(ChangeListener.class);
}
/**
* Runs each ChangeListener
's componentChanged
method.
*
* @see #setRangeProperties
* @see EventListenerList
*/
protected void fireStateChanged() {
if (delayEvents) {
gotDelayedEvent = true;
} else {
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);
}
}
}
}
/**
* Returns a string that displays all of the
* BoundedRangeModel
properties.
*/
public String toString() {
String modelString =
"value=" + value + ", " +
"extent=" + extent + ", " +
"min=" + min + ", " +
"max=" + max + ", " +
"adj=" + isAdjusting;
return getClass().getName() + '[' + modelString + ']';
}
/**
* Returns an array of all the objects currently registered as
* FooListener
s
* upon this model.
* FooListener
s
* 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
* FooListener
s
* on this model,
* or an empty array if no such
* listeners have been added
* @throws ClassCastException if listenerType
doesn't
* specify a class or interface that implements
* java.util.EventListener
* @see #getChangeListeners
* @since 1.3
*/
public EventListener[] getListeners(Class listenerType) {
return listenerList.getListeners(listenerType);
}
/**
* fire event with isValueIsAdjusting true
*/
@Override
public void fireDelayedIntermediateEvents() {
}
@Override
public void fireDelayedFinalEvents() {
if (!delayEvents && gotDelayedEvent) {
fireStateChanged();
gotDelayedEvent = false;
}
}
}// SPagingBoundedRangeModel