DefaultBoundedRangeModel.java: Documented API.

2004-01-07  Sascha Brawer  <brawer@dandelis.ch>

	* javax/swing/DefaultBoundedRangeModel.java: Documented API.
	(changeEvent): Create event object on demand.
	(DefaultBoundedRangeModel, toString, setValue, setExtent,
	setMinimum, setMaximum, setValueIsAdjusting, setRangeProperties,
	fireStateChanged): Re-written.
	* javax/swing/event/EventListenerList.java: Reformatted, document
	typical usage.
	(toString): Implemented.
	(getListeners): Re-written.
	(remove): Re-written.
	(add): Re-written.
	(NO_LISTENERS): New singleton field.
	(listenerList): Declare as transient; document.
	(serialVersionUID): Document.
	(getListenerCount(Class)): More efficient implementation,
	also accepts null argument.  Improve Javadoc.
	(getListenerCount()): Remove unnecessary cast; docfix.
	* javax/swing/undo/UndoableEditSupport.java:
	Re-format, document.
	(UndoableEditSupport): Set realSource field. Improve documentation.
	(_postEdit): Iterate over cloned listener vector.
	(toString): Don't emit realSource.
	(beginUpdate, endUpdate): Support nested updates.
	(postEdit): Use compound edit if present.

From-SVN: r75505
This commit is contained in:
Sascha Brawer 2004-01-07 15:42:04 +01:00 committed by Michael Koch
parent b48a0c1869
commit b3db7ef158
4 changed files with 701 additions and 437 deletions

View File

@ -1,3 +1,30 @@
2004-01-07 Sascha Brawer <brawer@dandelis.ch>
* javax/swing/DefaultBoundedRangeModel.java: Documented API.
(changeEvent): Create event object on demand.
(DefaultBoundedRangeModel, toString, setValue, setExtent,
setMinimum, setMaximum, setValueIsAdjusting, setRangeProperties,
fireStateChanged): Re-written.
* javax/swing/event/EventListenerList.java: Reformatted, document
typical usage.
(toString): Implemented.
(getListeners): Re-written.
(remove): Re-written.
(add): Re-written.
(NO_LISTENERS): New singleton field.
(listenerList): Declare as transient; document.
(serialVersionUID): Document.
(getListenerCount(Class)): More efficient implementation,
also accepts null argument. Improve Javadoc.
(getListenerCount()): Remove unnecessary cast; docfix.
* javax/swing/undo/UndoableEditSupport.java:
Re-format, document.
(UndoableEditSupport): Set realSource field. Improve documentation.
(_postEdit): Iterate over cloned listener vector.
(toString): Don't emit realSource.
(beginUpdate, endUpdate): Support nested updates.
(postEdit): Use compound edit if present.
2004-01-06 Graydon Hoare <graydon@redhat.com>
* java/awt/Container.java (swapComponents): Add forgotten

View File

@ -1,5 +1,6 @@
/* DefaultBoundedRangeModel.java --
Copyright (C) 2002 Free Software Foundation, Inc.
/* DefaultBoundedRangeModel.java -- Default implementation
of BoundedRangeModel.
Copyright (C) 2002, 2004 Free Software Foundation, Inc.
This file is part of GNU Classpath.
@ -43,306 +44,425 @@ import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import javax.swing.event.EventListenerList;
/**
* DefaultBoundedRangeModel
* @author Andrew Selkirk
* @version 1.0
* A default implementation of BoundedRangeModel.
*
* @author <a href="mailto:aselkirk@sympatico.ca">Andrew Selkirk</a>
* @author <a href="mailto:brawer@dandelis.ch">Sascha Brawer</a>
*/
public class DefaultBoundedRangeModel
implements BoundedRangeModel, Serializable
{
/**
* The identifier of this class in object serialization. Verified
* using the serialver tool of Sun J2SE 1.4.1_01.
*/
static final long serialVersionUID = 5034068491295259790L;
/**
* changeEvent
*/
protected transient ChangeEvent changeEvent = new ChangeEvent (this);
/**
* listenerList
* An event that is sent to all registered {@link ChangeListener}s
* when the state of this range model has changed.
*
* <p>The event object is created on demand, the first time it
* is actually needed.
*
* @see #fireStateChanged()
*/
protected EventListenerList listenerList = new EventListenerList ();
protected transient ChangeEvent changeEvent;
/**
* value
* The list of the currently registered EventListeners.
*/
protected EventListenerList listenerList = new EventListenerList();
/**
* The current value of the range model, which is always between
* {@link #minimum} and ({@link #maximum} - {@link #extent}). In a
* scroll bar visualization of a {@link BoundedRangeModel}, the
* <code>value</code> is displayed as the position of the thumb.
*/
private int value;
/**
* extent
* The current extent of the range model, which is a number greater
* than or equal to zero. In a scroll bar visualization of a {@link
* BoundedRangeModel}, the <code>extent</code> is displayed as the
* size of the thumb.
*/
private int extent;
/**
* minimum
* The current minimum value of the range model, which is always
* less than or equal to {@link #maximum}.
*/
private int minimum;
/**
* maximum
* The current maximum value of the range model, which is always
* greater than or equal to {@link #minimum}.
*/
private int maximum;
/**
* isAdjusting
* A property that indicates whether the value of this {@link
* BoundedRangeModel} is going to change in the immediate future.
*/
private boolean isAdjusting;
/**
* Constructor DefaultBoundedRangeModel
*/
public DefaultBoundedRangeModel ()
{
setRangeProperties (0, 0, 0, 100, false);
}
/**
* Constructor DefaultBoundedRangeModel
* @param value TODO
* @param extent TODO
* @param minimum TODO
* @param maximum TODO
* Constructs a <code>DefaultBoundedRangeModel</code> with default
* values for the properties. The properties <code>value</code>,
* <code>extent</code> and <code>minimum</code> will be initialized
* to zero; <code>maximum</code> will be set to 100; the property
* <code>valueIsAdjusting</code> will be <code>false</code>.
*/
public DefaultBoundedRangeModel (int value, int extent, int minimum,
public DefaultBoundedRangeModel()
{
// The fields value, extent, minimum have the default value 0, and
// isAdjusting is already false. These fields no not need to be
// set explicitly.
maximum = 100;
}
/**
* Constructs a <code>DefaultBoundedRangeModel</code> with the
* specified values for some properties.
*
* @param value the initial value of the range model, which must be
* a number between <code>minimum</code> and <code>(maximum -
* extent)</code>. In a scroll bar visualization of a {@link
* BoundedRangeModel}, the <code>value</code> is displayed as the
* position of the thumb.
*
* @param extent the initial extent of the range model, which is a
* number greater than or equal to zero. In a scroll bar
* visualization of a {@link BoundedRangeModel}, the
* <code>extent</code> is displayed as the size of the thumb.
*
* @param minimum the initial minimal value of the range model.
*
* @param maximum the initial maximal value of the range model.
*
* @throws IllegalArgumentException if the following condition is
* not satisfied: <code>minimum <= value <= value + extent <=
* maximum</code>.
*/
public DefaultBoundedRangeModel(int value, int extent, int minimum,
int maximum)
{
setRangeProperties(value, extent, minimum, maximum, false);
if (!(minimum <= value && extent >= 0 && (value + extent) <= maximum))
throw new IllegalArgumentException();
this.value = value;
this.extent = extent;
this.minimum = minimum;
this.maximum = maximum;
// The isAdjusting field already has a false value by default.
}
/**
* toString
* @returns String
* Returns a string with all relevant properties of this range
* model.
*/
public String toString ()
public String toString()
{
return null; // TODO
return getClass().getName()
+ "[value=" + value
+ ", extent=" + extent
+ ", min=" + minimum
+ ", max=" + maximum
+ ", adj=" + isAdjusting
+ ']';
}
/**
* getValue
* @returns int
* Returns the current value of this bounded range model. In a
* scroll bar visualization of a {@link BoundedRangeModel}, the
* <code>value</code> is displayed as the position of the thumb.
*/
public int getValue ()
public int getValue()
{
return value;
}
/**
* setValue
* @param value TODO
*/
public void setValue (int value)
{
// Validate Constraints
if (minimum > value ||
value > (value + extent) ||
(value + extent) > maximum)
{
throw new IllegalArgumentException ("Invalid value property set");
}
// Set Value
/**
* Changes the current value of this bounded range model. In a
* scroll bar visualization of a {@link BoundedRangeModel}, the
* <code>value</code> is displayed as the position of the thumb;
* changing the <code>value</code> of a scroll bar&#x2019;s model
* thus moves the thumb to a different position.
*/
public void setValue(int value)
{
value = Math.max(minimum, value);
if (value + extent > maximum)
value = maximum - extent;
if (value != this.value)
{
this.value = value;
// Notification
fireStateChanged ();
fireStateChanged();
}
}
/**
* getExtent
* @returns int
* Returns the current extent of this bounded range model, which is
* a number greater than or equal to zero. In a scroll bar
* visualization of a {@link BoundedRangeModel}, the
* <code>extent</code> is displayed as the size of the thumb.
*/
public int getExtent ()
public int getExtent()
{
return extent;
}
/**
* setExtent
* @param extent TODO
*/
public void setExtent (int extent)
{
// Validate Constraints
if (minimum > value ||
value > (value + extent) ||
(value + extent) > maximum)
{
throw new IllegalArgumentException("Invalid extent property set");
}
// Set Extent
/**
* Changes the current extent of this bounded range model. In a
* scroll bar visualization of a {@link BoundedRangeModel}, the
* <code>extent</code> is displayed as the size of the thumb.
*
* @param extent the new extent of the range model, which is a
* number greater than or equal to zero.
*/
public void setExtent(int extent)
{
extent = Math.max(extent, 0);
if (value + extent > maximum)
extent = maximum - value;
if (extent != this.extent)
{
this.extent = extent;
// Notification
fireStateChanged ();
fireStateChanged();
}
}
/**
* getMinimum
* @returns int
* Returns the current minimal value of this bounded range model.
*/
public int getMinimum ()
public int getMinimum()
{
return minimum;
}
/**
* setMinimum
* @param minimum TODO
*/
public void setMinimum (int minimum)
{
// Validate Constraints
if (minimum > value ||
value > (value + extent) ||
(value + extent) > maximum)
{
throw new IllegalArgumentException("Invalid minimum property set");
}
// Set Minimum
this.minimum = minimum;
// Notification
fireStateChanged ();
}
/**
* getMaximum
* @returns int
* Changes the current minimal value of this bounded range model.
*
* @param minimum the new minimal value.
*/
public int getMaximum() {
public void setMinimum(int minimum)
{
int value, maximum;
maximum = Math.max(minimum, this.maximum);
value = Math.max(minimum, this.value);
setRangeProperties(value, extent, minimum, maximum, isAdjusting);
}
/**
* Returns the current maximal value of this bounded range model.
*/
public int getMaximum()
{
return maximum;
}
/**
* setMaximum
* @param maximum TODO
*/
public void setMaximum (int maximum)
{
// Validate Constraints
if (minimum > value ||
value > (value + extent) ||
(value + extent) > maximum)
{
throw new IllegalArgumentException ("Invalid maximum property set");
}
// Set Maximum
this.maximum = maximum;
// Notification
fireStateChanged ();
}
/**
* getValueIsAdjusting
* @returns boolean
* Changes the current maximal value of this bounded range model.
*
* @param maximum the new maximal value.
*/
public boolean getValueIsAdjusting ()
public void setMaximum(int maximum)
{
int value, extent, minimum;
minimum = Math.min(this.minimum, maximum);
extent = Math.min(this.extent, maximum - minimum);
value = Math.min(this.value, maximum - extent);
setRangeProperties(value, extent, minimum, maximum, isAdjusting);
}
/**
* Returns whether or not the value of this bounded range model is
* going to change in the immediate future. Scroll bars set this
* property to <code>true</code> while the thumb is being dragged
* around; when the mouse is relased, they set the property to
* <code>false</code> and post a final {@link ChangeEvent}.
*
* @returns <code>true</code> if the value will change soon again;
* <code>false</code> if the value will probably not change soon.
*/
public boolean getValueIsAdjusting()
{
return isAdjusting;
}
/**
* setValueIsAdjusting
* @param isAdjusting TODO
*/
public void setValueIsAdjusting (boolean isAdjusting)
{
// Set isAdjusting
this.isAdjusting = isAdjusting;
// Notification
/**
* Specifies whether or not the value of this bounded range model is
* going to change in the immediate future. Scroll bars set this
* property to <code>true</code> while the thumb is being dragged
* around; when the mouse is relased, they set the property to
* <code>false</code>.
*
* @param isAdjusting <code>true</code> if the value will change
* soon again; <code>false</code> if the value will probably not
* change soon.
*/
public void setValueIsAdjusting(boolean isAdjusting)
{
if (isAdjusting == this.isAdjusting)
return;
this.isAdjusting = isAdjusting;
fireStateChanged();
}
/**
* setRangeProperties
* @param value TODO
* @param extent TODO
* @param minimum TODO
* @param maximum TODO
* @param isAdjusting TODO
*
* @param value the new value of the range model. In a scroll bar
* visualization of a {@link BoundedRangeModel}, the
* <code>value</code> is displayed as the position of the thumb.
*
* @param extent the new extent of the range model, which is a
* number greater than or equal to zero. In a scroll bar
* visualization of a {@link BoundedRangeModel}, the
* <code>extent</code> is displayed as the size of the thumb.
*
* @param minimum the new minimal value of the range model.
*
* @param maximum the new maximal value of the range model.
* @param isAdjusting whether or not the value of this bounded range
* model is going to change in the immediate future. Scroll bars set
* this property to <code>true</code> while the thumb is being
* dragged around; when the mouse is relased, they set the property
* to <code>false</code>.
*/
public void setRangeProperties (int value, int extent, int minimum,
public void setRangeProperties(int value, int extent, int minimum,
int maximum, boolean isAdjusting)
{
// Validate Constraints
if (minimum > value ||
value > (value + extent) ||
(value + extent) > maximum)
{
throw new IllegalArgumentException ("Invalid property set");
}
minimum = Math.min(Math.min(minimum, maximum), value);
maximum = Math.max(value, maximum);
if (extent + value > maximum)
extent = maximum - value;
extent = Math.max(0, extent);
if ((value == this.value)
&& (extent == this.extent)
&& (minimum == this.minimum)
&& (maximum == this.maximum)
&& (isAdjusting == this.isAdjusting))
return;
// Set Data
this.value = value;
this.extent = extent;
this.minimum = minimum;
this.maximum = maximum;
this.isAdjusting = isAdjusting;
// Notification
fireStateChanged ();
fireStateChanged();
}
/**
* addChangeListener
* @param listener TODO
* Subscribes a ChangeListener to state changes.
*
* @param listener the listener to be subscribed.
*/
public void addChangeListener (ChangeListener listener)
public void addChangeListener(ChangeListener listener)
{
listenerList.add (ChangeListener.class, listener);
listenerList.add(ChangeListener.class, listener);
}
/**
* removeChangeListener
* @param listener TODO
* Cancels the subscription of a ChangeListener.
*
* @param listener the listener to be unsubscribed.
*/
public void removeChangeListener (ChangeListener listener)
public void removeChangeListener(ChangeListener listener)
{
listenerList.remove (ChangeListener.class, listener);
listenerList.remove(ChangeListener.class, listener);
}
/**
* fireStateChanged
* Sends a {@link ChangeEvent} to any registered {@link
* ChangeListener}s.
*
* @see #addChangeListener(ChangeListener)
* @see #removeChangeListener(ChangeListener)
*/
protected void fireStateChanged ()
protected void fireStateChanged()
{
// Variables
ChangeListener listener;
ChangeListener[] listeners;
int index;
Object[] listeners;
// Get Listeners
listeners = getChangeListeners ();
// Process Listeners
for (index = 0; index < listeners.length; index++)
listeners = listenerList.getListenerList();
for (int i = listeners.length - 2; i >= 0; i -= 2)
if (listeners[i] == ChangeListener.class)
{
listener = listeners [index];
listener.stateChanged (changeEvent);
if (changeEvent == null)
changeEvent = new ChangeEvent(this);
((ChangeListener) listeners[i + 1]).stateChanged(changeEvent);
}
}
/**
* getListeners
* @param c TODO
* @returns EventListener[]
* Retrieves the current listeners of the specified class.
*
* @param c the class of listeners; usually {@link
* ChangeListener}<code>.class</code>.
*
* @return an array with the currently subscribed listeners, or
* an empty array if there are currently no listeners.
*
* @since 1.3
*/
public EventListener[] getListeners (Class listenerType)
public EventListener[] getListeners(Class listenerType)
{
return listenerList.getListeners (listenerType);
return listenerList.getListeners(listenerType);
}
/**
* getChangeListeners
* Returns all <code>ChangeListeners</code> that are currently
* subscribed for changes to this
* <code>DefaultBoundedRangeModel</code>.
*
* @return an array with the currently subscribed listeners, or
* an empty array if there are currently no listeners.
*
* @since 1.4
*/
public ChangeListener[] getChangeListeners ()
public ChangeListener[] getChangeListeners()
{
return (ChangeListener[]) getListeners (ChangeListener.class);
return (ChangeListener[]) getListeners(ChangeListener.class);
}
}

View File

@ -1,5 +1,5 @@
/* EventListenerList.java --
Copyright (C) 2002 Free Software Foundation, Inc.
Copyright (C) 2002, 2004 Free Software Foundation, Inc.
This file is part of GNU Classpath.
@ -37,206 +37,266 @@ exception statement from your version. */
package javax.swing.event;
// Imports
import java.io.Serializable;
import java.lang.reflect.Array;
import java.util.EventListener;
/**
* EventListenerList
* @author Andrew Selkirk
* A utility class for keeping track of {@link EventListener}s.
*
* <p><b>Example for using this class:</b>
*
* <blockquote><pre> import java.util.EventListener;
* import javax.swing.event.EventListenerList;
*
* class Foo
* {
* protected final EventListenerList listeners = new EventListenerList();
* protected BarClosedEvent barClosedEvent = null;
*
* public void addBarListener(BarListener l)
* {
* listeners.<a href="#add(java.lang.Class, java.util.EventListener)"
* >add</a>(BarListener.class, l);
* }
*
* public void removeBarListener(BarListener l)
* {
* listeners.<a href="#remove(java.lang.Class, java.util.EventListener)"
* >remove</a>(BarListener.class, l);
* }
*
* protected void fireBarClosedEvent()
* {
* Object[] l = listeners.<a href="#getListenerList()"
* >getListenerList()</a>;
*
* for (int i = l.length - 2; i >= 0; i -= 2)
* if (l[i] == BarListener.class)
* {
* // Create the event on demand, when it is needed the first time.
* if (barClosedEvent == null)
* barClosedEvent = new BarClosedEvent(this);
*
* ((BarClosedListener) l[i + 1]).barClosed(barClosedEvent);
* }
* }
* }</pre></blockquote>
*
* @author <a href="mailto:aselkirk@sympatico.ca">Andrew Selkirk</a>
* @author <a href="mailto:brawer@dandelis.ch">Sascha Brawer</a>
*/
public class EventListenerList extends Object implements Serializable
public class EventListenerList
implements Serializable
{
/**
* An ID for serializing instances of this class; verified with the
* serialver tool of Sun J2SE 1.4.1_01.
*/
static final long serialVersionUID = -5677132037850737084L;
//-------------------------------------------------------------
// Variables --------------------------------------------------
//-------------------------------------------------------------
/**
* Listener list
* An empty array that is shared by all instances of this class that
* have no listeners.
*/
protected Object[] listenerList = null;
private static final Object[] NO_LISTENERS = new Object[0];
//-------------------------------------------------------------
// Initialization ---------------------------------------------
//-------------------------------------------------------------
/**
* An array with all currently registered listeners. The array has
* twice as many elements as there are listeners. For an even
* integer <code>i</code>, <code>listenerList[i]</code> indicates
* the registered class, and <code>listenerList[i+1]</code> is the
* listener.
*/
protected transient Object[] listenerList = NO_LISTENERS;
/**
* EventListenerList constructor
*/
public EventListenerList() {
listenerList = new Object[0];
} // EventListenerList()
//-------------------------------------------------------------
// Methods ----------------------------------------------------
//-------------------------------------------------------------
/**
* Add Listener
* @param t Class type
* @param listener Listener to add
*/
public void add(Class t, EventListener listener) {
// Variables
Object[] list;
int index;
Class checkClass;
EventListener checkListener;
// Create New list in anticipation that listener is not present
list = new Object[listenerList.length + 2];
// Search through list looking for listener
for (index = 0; index < listenerList.length; index += 2) {
checkClass = (Class) listenerList[index];
checkListener = (EventListener) listenerList[index + 1];
if (checkClass.equals(t) == true &&
checkListener.equals(listener) == true) {
return;
} // if
} // for
// Add Listener
list[listenerList.length] = t;
list[listenerList.length + 1] = listener;
// Replace Listener List
listenerList = list;
} // add()
/**
* Get the total number of listeners
* @return Count of listeners
*/
public int getListenerCount() {
return (int) listenerList.length / 2;
} // getListenerCount
/**
* Get the number of listeners of a particular type
* @param t Class type to count
* @returns Count of the specified listeners
*/
public int getListenerCount(Class t) {
// Variables
int index;
int count;
String name;
// Loop through entire list
count = 0;
name = t.getName();
for (index = 0; index < listenerList.length; index += 2) {
if (((Class) listenerList[index]).getName().equals(name) == true) {
count += 1;
public EventListenerList()
{
}
} // for: index
// Return Count
return count;
} // getListenerCount()
/**
* Registers a listener of a specific type.
*
* @param t the type of the listener.
*
* @param listener the listener to add, which must be an instance of
* <code>t</code>, or of a subclass of <code>t</code>.
*
* @throws IllegalArgumentException if <code>listener</code> is not
* an instance of <code>t</code> (or a subclass thereof).
*
* @throws Exception if <code>t</code> is <code>null</code>.
*/
public void add(Class t, EventListener listener)
{
int oldLength;
Object[] newList;
if (listener == null)
return;
if (!t.isInstance(listener))
throw new IllegalArgumentException();
oldLength = listenerList.length;
newList = new Object[oldLength + 2];
if (oldLength > 0)
System.arraycopy(listenerList, 0, newList, 0, oldLength);
newList[oldLength] = t;
newList[oldLength + 1] = listener;
listenerList = newList;
}
/**
* Determines the number of listeners.
*/
public int getListenerCount()
{
return listenerList.length / 2;
}
/**
* Determines the number of listeners of a particular class.
*
* @param t the type of listeners to be counted. In order to get
* counted, a subscribed listener must be exactly of class
* <code>t</code>. Thus, subclasses of <code>t</code> will not be
* counted.
*/
public int getListenerCount(Class t)
{
int result = 0;
for (int i = 0; i < listenerList.length; i += 2)
if (t == listenerList[i])
++result;
return result;
}
/**
* Get a list of listenerType/listener pairs
* @returns Listener list
*/
public Object[] getListenerList() {
public Object[] getListenerList()
{
return listenerList;
} // getListenerList()
}
/**
* Get list of listeners of a particular type
* @param c Class type
* @returns List of listeners of the specified type
* Retrieves the currently subscribed listeners of a particular
* type. For a listener to be returned, it must have been
* registered with exactly the type <code>c</code>; subclasses are
* not considered equal.
*
* <p>The returned array can always be cast to <code>c[]</code>.
* Since it is a newly allocated copy, the caller may arbitrarily
* modify the array.
*
* @param c the class which was passed to {@link #add}.
*
* @throws ClassCastException if <code>c</code> does not implement
* the {@link EventListener} interface.
*
* @throws NullPointerException if <code>c</code> is
* <code>null</code>.
*
* @returns an array of <code>c</code> whose elements are the
* currently subscribed listeners of the specified type. If there
* are no such listeners, an empty array is returned.
*
* @since 1.3
*/
public EventListener[] getListeners(Class c) {
public EventListener[] getListeners(Class c)
{
int count, f;
EventListener[] result;
// Variables
int count;
EventListener[] list;
String name;
int index;
// Get count of listeners
count = getListenerCount(c);
result = (EventListener[]) Array.newInstance(c, count);
f = 0;
for (int i = 0; i < listenerList.length; i += 2)
if (listenerList[i] == c)
result[f++] = (EventListener) listenerList[i + 1];
// Create Event Listener list
list = new EventListener[count];
return result;
}
// Construct List
count = 0;
name = c.getName();
for (index = 0; index < listenerList.length; index += 2) {
if (((Class) listenerList[index]).getName().equals(name) == true) {
list[count] = (EventListener) listenerList[index];
count += 1;
} // if
} // for: index
// Return List
return list;
} // getListeners()
/**
* Remove a listener
* @param t Class type
* @param listener Listener to be removed
* Removes a listener of a specific type.
*
* @param t the type of the listener.
*
* @param listener the listener to remove, which must be an instance
* of <code>t</code>, or of a subclass of <code>t</code>.
*
* @throws IllegalArgumentException if <code>listener</code> is not
* an instance of <code>t</code> (or a subclass thereof).
*
* @throws Exception if <code>t</code> is <code>null</code>.
*/
public void remove(Class t, EventListener listener) {
public void remove(Class t, EventListener listener)
{
Object[] oldList, newList;
int oldLength;
// Variables
Object[] list;
int index;
Class checkClass;
EventListener checkListener;
int pointer;
boolean found;
// Create New list in anticipation that listener is not present
if (listenerList.length == 0) {
if (listener == null)
return;
} // if
list = new Object[listenerList.length - 2];
// Search through list looking for listener
pointer = 0;
found = false;
for (index = 0; index < listenerList.length - 2; index += 2) {
checkClass = (Class) listenerList[index];
checkListener = (EventListener) listenerList[index + 1];
if (checkClass.equals(t) == false ||
checkListener.equals(listener) == false) {
list[pointer] = checkClass;
list[pointer + 1] = checkListener;
pointer += 2;
} else {
found = true;
} // if
} // for
if (!t.isInstance(listener))
throw new IllegalArgumentException();
// Replace Listener List
if (found == true) {
listenerList = list;
} // if
oldList = listenerList;
oldLength = oldList.length;
for (int i = 0; i < oldLength; i += 2)
if (oldList[i] == t && oldList[i + 1] == listener)
{
if (oldLength == 2)
newList = NO_LISTENERS;
else
{
newList = new Object[oldLength - 2];
if (i > 0)
System.arraycopy(oldList, 0, newList, 0, i);
if (i < oldLength - 2)
System.arraycopy(oldList, i + 2, newList, i,
oldLength - 2 - i);
}
listenerList = newList;
return;
}
}
} // remove()
/**
* Get a string representation
* @returns String representation
* Returns a string representation of this object that may be useful
* for debugging purposes.
*/
public String toString() {
return null; // TODO
} // toString()
} // EventListenerList
public String toString()
{
StringBuffer buf = new StringBuffer("EventListenerList: ");
buf.append(listenerList.length / 2);
buf.append(" listeners: ");
for (int i = 0; i < listenerList.length; i += 2)
{
buf.append(" type ");
buf.append(((Class) listenerList[i]).getName());
buf.append(" listener ");
buf.append(listenerList[i + 1]);
}
return buf.toString();
}
}

View File

@ -1,5 +1,5 @@
/* UndoableEditSupport.java --
Copyright (C) 2002, 2003 Free Software Foundation, Inc.
Copyright (C) 2002, 2003, 2004 Free Software Foundation, Inc.
This file is part of GNU Classpath.
@ -38,98 +38,112 @@ exception statement from your version. */
package javax.swing.undo;
import java.util.Iterator;
import java.util.Vector;
import javax.swing.event.UndoableEditEvent;
import javax.swing.event.UndoableEditListener;
/**
* UndoableEditSupport
* @author Andrew Selkirk
* A helper class for supporting {@link
* javax.swing.event.UndoableEditListener}.
*
* @author <a href="mailto:aselkirk@sympatico.ca">Andrew Selkirk</a>
* @author <a href="mailto:brawer@dandelis.ch">Sascha Brawer</a>
*/
public class UndoableEditSupport
{
//-------------------------------------------------------------
// Variables --------------------------------------------------
//-------------------------------------------------------------
/**
* updateLevel
* The number of times that {@link #beginUpdate()} has been called
* without a matching call to {@link #endUpdate()}.
*/
protected int updateLevel;
/**
* compoundEdit
*/
protected CompoundEdit compoundEdit;
/**
* listeners
* The currently registered listeners.
*/
protected Vector listeners = new Vector();
/**
* realSource
* The source of the broadcast UndoableEditEvents.
*/
protected Object realSource;
//-------------------------------------------------------------
// Initialization ---------------------------------------------
//-------------------------------------------------------------
/**
* Constructor UndoableEditSupport
* Constructs a new helper for broadcasting UndoableEditEvents. The
* events will indicate the newly constructed
* <code>UndoableEditSupport</code> instance as their source.
*
* @see #UndoableEditSupport(java.lang.Object)
*/
public UndoableEditSupport()
{
realSource = this;
}
/**
* Constructor UndoableEditSupport
* @param object TODO
* Constructs a new helper for broadcasting UndoableEditEvents.
*
* @param realSource the source of the UndoableEditEvents that will
* be broadcast by this helper. If <code>realSource</code> is
* <code>null</code>, the events will indicate the newly constructed
* <code>UndoableEditSupport</code> instance as their source.
*/
public UndoableEditSupport(Object object)
public UndoableEditSupport(Object realSource)
{
realSource = object;
if (realSource == null)
realSource = this;
this.realSource = realSource;
}
//-------------------------------------------------------------
// Methods ----------------------------------------------------
//-------------------------------------------------------------
/**
* toString
* @returns String
* Returns a string representation of this object that may be useful
* for debugging.
*/
public String toString()
{
return (super.toString() + " realSource: " + realSource
+ " updateLevel: " + updateLevel);
// Note that often, this.realSource == this. Therefore, dumping
// realSource without additional checks may lead to infinite
// recursion. See Classpath bug #7119.
return super.toString() + " updateLevel: " + updateLevel
+ " listeners: " + listeners + " compoundEdit: " + compoundEdit;
}
/**
* Add a listener.
* @param val the listener
* Registers a listener.
*
* @param val the listener to be added.
*/
public synchronized void addUndoableEditListener(UndoableEditListener val)
{
listeners.add(val);
}
/**
* Remove a listener.
* @param val the listener
* Unregisters a listener.
* @param val the listener to be removed.
*/
public synchronized void removeUndoableEditListener(UndoableEditListener val)
{
listeners.removeElement(val);
}
/**
* Return an array of all listeners.
* @returns all the listeners
* Returns an array containing the currently registered listeners.
*/
public synchronized UndoableEditListener[] getUndoableEditListeners()
{
@ -137,78 +151,121 @@ public class UndoableEditSupport
return (UndoableEditListener[]) listeners.toArray(result);
}
/**
* _postEdit
* @param value0 TODO
* Notifies all registered listeners that an {@link
* UndoableEditEvent} has occured.
*
* <p><b>Lack of Thread Safety:</b> It is <em>not</em> safe to call
* this method from concurrent threads, unless the call is protected
* by a synchronization on this <code>UndoableEditSupport</code>
* instance.
*
* @param edit the edit action to be posted.
*/
protected void _postEdit(UndoableEdit edit)
{
UndoableEditEvent event = new UndoableEditEvent(realSource, edit);
int max = listeners.size();
for (int i = 0; i < max; ++i)
{
UndoableEditListener l
= (UndoableEditListener) (listeners.elementAt(i));
l.undoableEditHappened(event);
}
UndoableEditEvent event;
Iterator iter;
// Do nothing if we have no listeners.
if (listeners.isEmpty())
return;
event = new UndoableEditEvent(realSource, edit);
// We clone the vector because this allows listeners to register
// or unregister listeners in their undoableEditHappened method.
// Otherwise, this would throw exceptions (in the case of
// Iterator, a java.util.ConcurrentModificationException; in the
// case of a direct loop over the Vector elements, some
// index-out-of-bounds exception).
iter = ((Vector) listeners.clone()).iterator();
while (iter.hasNext())
((UndoableEditListener) iter.next()).undoableEditHappened(event);
}
/**
* postEdit
* @param value0 TODO
* If {@link #beginEdit} has been called (so that the current
* update level is greater than zero), adds the specified edit
* to {@link #compoundEdit}. Otherwise, notify listeners of the
* edit by calling {@link #_postEdit(UndoableEdit)}.
*
* <p><b>Thread Safety:</b> It is safe to call this method from any
* thread without external synchronization.
*
* @param edit the edit action to be posted.
*/
public synchronized void postEdit(UndoableEdit edit)
{
if (compoundEdit == null)
if (compoundEdit != null)
compoundEdit.addEdit(edit);
else
_postEdit(edit);
}
/**
* getUpdateLevel
* @returns int
* Returns the current update level.
*/
public int getUpdateLevel()
{
return updateLevel;
}
/**
* beginUpdate
* Starts a (possibly nested) update session. If the current update
* level is zero, {@link #compoundEdit} is set to the result of the
* {@link #createCompoundEdit} method. In any case, the update level
* is increased by one.
*
* <p><b>Thread Safety:</b> It is safe to call this method from any
* thread without external synchronization.
*/
public synchronized void beginUpdate()
{
if (compoundEdit != null)
{
// FIXME: what? We can't push a new one. This isn't even
// documented anyway.
endUpdate();
}
if (compoundEdit == null)
compoundEdit = createCompoundEdit();
++updateLevel;
}
/**
* createCompoundEdit
* @returns CompoundEdit
* Creates a new instance of {@link #CompoundEdit}. Called by {@link
* #beginUpdate}. If a subclass wants {@link #beginUpdate} to work
* on a specific {@link #compoundEdit}, it should override this
* method.
*
* @returns a newly created instance of {@link #CompoundEdit}.
*/
protected CompoundEdit createCompoundEdit()
{
return new CompoundEdit();
}
/**
* endUpdate
* Ends an update session. If the terminated session was the
* outermost session, {@link #compoundEdit} will receive an
* <code>end</code> message, and {@link #_postEdit} gets called in
* order to notify any listeners. Finally, the
* <code>compoundEdit</code> is discarded.
*
* <p><b>Thread Safety:</b> It is safe to call this method from any
* thread without external synchronization.
*/
public synchronized void endUpdate()
{
// FIXME: assert updateLevel == 1;
if (updateLevel == 0)
throw new IllegalStateException();
if (--updateLevel > 0)
return;
compoundEdit.end();
CompoundEdit c = compoundEdit;
_postEdit(compoundEdit);
compoundEdit = null;
--updateLevel;
_postEdit(c);
}
}