Commit b3db7ef1 by Sascha Brawer Committed by Michael Koch

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
parent b48a0c18
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> 2004-01-06 Graydon Hoare <graydon@redhat.com>
* java/awt/Container.java (swapComponents): Add forgotten * java/awt/Container.java (swapComponents): Add forgotten
......
/* DefaultBoundedRangeModel.java -- /* DefaultBoundedRangeModel.java -- Default implementation
Copyright (C) 2002 Free Software Foundation, Inc. of BoundedRangeModel.
Copyright (C) 2002, 2004 Free Software Foundation, Inc.
This file is part of GNU Classpath. This file is part of GNU Classpath.
...@@ -43,306 +44,425 @@ import javax.swing.event.ChangeEvent; ...@@ -43,306 +44,425 @@ import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener; import javax.swing.event.ChangeListener;
import javax.swing.event.EventListenerList; import javax.swing.event.EventListenerList;
/** /**
* DefaultBoundedRangeModel * A default implementation of BoundedRangeModel.
* @author Andrew Selkirk *
* @version 1.0 * @author <a href="mailto:aselkirk@sympatico.ca">Andrew Selkirk</a>
* @author <a href="mailto:brawer@dandelis.ch">Sascha Brawer</a>
*/ */
public class DefaultBoundedRangeModel public class DefaultBoundedRangeModel
implements BoundedRangeModel, Serializable 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; static final long serialVersionUID = 5034068491295259790L;
/** /**
* changeEvent * 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 transient ChangeEvent changeEvent = new ChangeEvent (this); protected transient ChangeEvent changeEvent;
/** /**
* listenerList * The list of the currently registered EventListeners.
*/ */
protected EventListenerList listenerList = new EventListenerList (); protected EventListenerList listenerList = new EventListenerList();
/** /**
* value * 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; 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; 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; 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; 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; private boolean isAdjusting;
/** /**
* Constructor DefaultBoundedRangeModel * 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 () public DefaultBoundedRangeModel()
{ {
setRangeProperties (0, 0, 0, 100, false); // 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;
} }
/** /**
* Constructor DefaultBoundedRangeModel * Constructs a <code>DefaultBoundedRangeModel</code> with the
* @param value TODO * specified values for some properties.
* @param extent TODO *
* @param minimum TODO * @param value the initial value of the range model, which must be
* @param maximum TODO * 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, public DefaultBoundedRangeModel(int value, int extent, int minimum,
int maximum) 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 a string with all relevant properties of this range
* @returns String * 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 the current value of this bounded range model. In a
* @returns int * 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; return value;
} }
/** /**
* setValue * Changes the current value of this bounded range model. In a
* @param value TODO * 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) public void setValue(int value)
{ {
// Validate Constraints value = Math.max(minimum, value);
if (minimum > value || if (value + extent > maximum)
value > (value + extent) || value = maximum - extent;
(value + extent) > maximum)
{
throw new IllegalArgumentException ("Invalid value property set");
}
// Set Value if (value != this.value)
{
this.value = value; this.value = value;
fireStateChanged();
// Notification }
fireStateChanged ();
} }
/** /**
* getExtent * Returns the current extent of this bounded range model, which is
* @returns int * 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; return extent;
} }
/** /**
* setExtent * Changes the current extent of this bounded range model. In a
* @param extent TODO * 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) public void setExtent(int extent)
{
// Validate Constraints
if (minimum > value ||
value > (value + extent) ||
(value + extent) > maximum)
{ {
throw new IllegalArgumentException("Invalid extent property set"); extent = Math.max(extent, 0);
} if (value + extent > maximum)
extent = maximum - value;
// Set Extent if (extent != this.extent)
{
this.extent = extent; this.extent = extent;
fireStateChanged();
// Notification
fireStateChanged ();
} }
}
/** /**
* getMinimum * Returns the current minimal value of this bounded range model.
* @returns int
*/ */
public int getMinimum () public int getMinimum()
{ {
return minimum; return minimum;
} }
/** /**
* setMinimum * Changes the current minimal value of this bounded range model.
* @param minimum TODO *
* @param minimum the new minimal value.
*/ */
public void setMinimum (int minimum) public void setMinimum(int minimum)
{
// Validate Constraints
if (minimum > value ||
value > (value + extent) ||
(value + extent) > maximum)
{ {
throw new IllegalArgumentException("Invalid minimum property set"); int value, maximum;
}
// Set Minimum maximum = Math.max(minimum, this.maximum);
this.minimum = minimum; value = Math.max(minimum, this.value);
// Notification setRangeProperties(value, extent, minimum, maximum, isAdjusting);
fireStateChanged ();
} }
/** /**
* getMaximum * Returns the current maximal value of this bounded range model.
* @returns int
*/ */
public int getMaximum() { public int getMaximum()
{
return maximum; return maximum;
} }
/** /**
* setMaximum * Changes the current maximal value of this bounded range model.
* @param maximum TODO *
* @param maximum the new maximal value.
*/ */
public void setMaximum (int maximum) public void setMaximum(int maximum)
{
// Validate Constraints
if (minimum > value ||
value > (value + extent) ||
(value + extent) > maximum)
{ {
throw new IllegalArgumentException ("Invalid maximum property set"); int value, extent, minimum;
}
// Set Maximum minimum = Math.min(this.minimum, maximum);
this.maximum = maximum; extent = Math.min(this.extent, maximum - minimum);
value = Math.min(this.value, maximum - extent);
// Notification setRangeProperties(value, extent, minimum, maximum, isAdjusting);
fireStateChanged ();
} }
/** /**
* getValueIsAdjusting * Returns whether or not the value of this bounded range model is
* @returns boolean * 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 () public boolean getValueIsAdjusting()
{ {
return isAdjusting; return isAdjusting;
} }
/** /**
* setValueIsAdjusting * Specifies whether or not the value of this bounded range model is
* @param isAdjusting TODO * 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) public void setValueIsAdjusting(boolean isAdjusting)
{ {
// Set isAdjusting if (isAdjusting == this.isAdjusting)
this.isAdjusting = isAdjusting; return;
// Notification this.isAdjusting = isAdjusting;
fireStateChanged(); fireStateChanged();
} }
/** /**
* setRangeProperties * setRangeProperties
* @param value TODO *
* @param extent TODO * @param value the new value of the range model. In a scroll bar
* @param minimum TODO * visualization of a {@link BoundedRangeModel}, the
* @param maximum TODO * <code>value</code> is displayed as the position of the thumb.
* @param isAdjusting TODO *
* @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) int maximum, boolean isAdjusting)
{ {
// Validate Constraints minimum = Math.min(Math.min(minimum, maximum), value);
if (minimum > value || maximum = Math.max(value, maximum);
value > (value + extent) || if (extent + value > maximum)
(value + extent) > maximum) extent = maximum - value;
{ extent = Math.max(0, extent);
throw new IllegalArgumentException ("Invalid property set");
} if ((value == this.value)
&& (extent == this.extent)
&& (minimum == this.minimum)
&& (maximum == this.maximum)
&& (isAdjusting == this.isAdjusting))
return;
// Set Data
this.value = value; this.value = value;
this.extent = extent; this.extent = extent;
this.minimum = minimum; this.minimum = minimum;
this.maximum = maximum; this.maximum = maximum;
this.isAdjusting = isAdjusting; this.isAdjusting = isAdjusting;
// Notification fireStateChanged();
fireStateChanged ();
} }
/** /**
* addChangeListener * Subscribes a ChangeListener to state changes.
* @param listener TODO *
* @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 * Cancels the subscription of a ChangeListener.
* @param listener TODO *
* @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 Object[] listeners;
ChangeListener listener;
ChangeListener[] listeners;
int index;
// Get Listeners listeners = listenerList.getListenerList();
listeners = getChangeListeners (); for (int i = listeners.length - 2; i >= 0; i -= 2)
if (listeners[i] == ChangeListener.class)
// Process Listeners
for (index = 0; index < listeners.length; index++)
{ {
listener = listeners [index]; if (changeEvent == null)
listener.stateChanged (changeEvent); changeEvent = new ChangeEvent(this);
((ChangeListener) listeners[i + 1]).stateChanged(changeEvent);
} }
} }
/** /**
* getListeners * Retrieves the current listeners of the specified class.
* @param c TODO *
* @returns EventListener[] * @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);
} }
} }
/* EventListenerList.java -- /* EventListenerList.java --
Copyright (C) 2002 Free Software Foundation, Inc. Copyright (C) 2002, 2004 Free Software Foundation, Inc.
This file is part of GNU Classpath. This file is part of GNU Classpath.
...@@ -37,206 +37,266 @@ exception statement from your version. */ ...@@ -37,206 +37,266 @@ exception statement from your version. */
package javax.swing.event; package javax.swing.event;
// Imports
import java.io.Serializable; import java.io.Serializable;
import java.lang.reflect.Array;
import java.util.EventListener; import java.util.EventListener;
/** /**
* EventListenerList * A utility class for keeping track of {@link EventListener}s.
* @author Andrew Selkirk *
* <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; 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];
/**
* 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;
//-------------------------------------------------------------
// Initialization ---------------------------------------------
//-------------------------------------------------------------
/** /**
* EventListenerList constructor * EventListenerList constructor
*/ */
public EventListenerList() { public EventListenerList()
listenerList = new Object[0]; {
} // EventListenerList() }
//-------------------------------------------------------------
// Methods ----------------------------------------------------
//-------------------------------------------------------------
/** /**
* Add Listener * Registers a listener of a specific type.
* @param t Class type *
* @param listener Listener to add * @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) { public void add(Class t, EventListener listener)
{
// Variables int oldLength;
Object[] list; Object[] newList;
int index;
Class checkClass; if (listener == null)
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; return;
} // if
} // for
// Add Listener if (!t.isInstance(listener))
list[listenerList.length] = t; throw new IllegalArgumentException();
list[listenerList.length + 1] = listener;
oldLength = listenerList.length;
newList = new Object[oldLength + 2];
if (oldLength > 0)
System.arraycopy(listenerList, 0, newList, 0, oldLength);
// Replace Listener List newList[oldLength] = t;
listenerList = list; newList[oldLength + 1] = listener;
listenerList = newList;
}
} // add()
/** /**
* Get the total number of listeners * Determines the number of listeners.
* @return Count of listeners
*/ */
public int getListenerCount() { public int getListenerCount()
return (int) listenerList.length / 2; {
} // getListenerCount return listenerList.length / 2;
}
/** /**
* Get the number of listeners of a particular type * Determines the number of listeners of a particular class.
* @param t Class type to count *
* @returns Count of the specified listeners * @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) { public int getListenerCount(Class t)
{
// Variables int result = 0;
int index; for (int i = 0; i < listenerList.length; i += 2)
int count; if (t == listenerList[i])
String name; ++result;
// Loop through entire list return result;
count = 0;
name = t.getName();
for (index = 0; index < listenerList.length; index += 2) {
if (((Class) listenerList[index]).getName().equals(name) == true) {
count += 1;
} }
} // for: index
// Return Count
return count;
} // getListenerCount()
/** /**
* Get a list of listenerType/listener pairs * Get a list of listenerType/listener pairs
* @returns Listener list * @returns Listener list
*/ */
public Object[] getListenerList() { public Object[] getListenerList()
{
return listenerList; return listenerList;
} // getListenerList() }
/** /**
* Get list of listeners of a particular type * Retrieves the currently subscribed listeners of a particular
* @param c Class type * type. For a listener to be returned, it must have been
* @returns List of listeners of the specified type * 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); 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 return result;
list = new EventListener[count]; }
// 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 * Removes a listener of a specific type.
* @param t Class type *
* @param listener Listener to be removed * @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)
{
// Variables Object[] oldList, newList;
Object[] list; int oldLength;
int index;
Class checkClass; if (listener == null)
EventListener checkListener;
int pointer;
boolean found;
// Create New list in anticipation that listener is not present
if (listenerList.length == 0) {
return; 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
// Replace Listener List
if (found == true) {
listenerList = list;
} // if
} // remove()
/** if (!t.isInstance(listener))
* Get a string representation throw new IllegalArgumentException();
* @returns String representation
*/ oldList = listenerList;
public String toString() { oldLength = oldList.length;
return null; // TODO for (int i = 0; i < oldLength; i += 2)
} // toString() 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;
}
}
} // EventListenerList /**
* Returns a string representation of this object that may be useful
* for debugging purposes.
*/
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();
}
}
/* UndoableEditSupport.java -- /* 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. This file is part of GNU Classpath.
...@@ -38,98 +38,112 @@ exception statement from your version. */ ...@@ -38,98 +38,112 @@ exception statement from your version. */
package javax.swing.undo; package javax.swing.undo;
import java.util.Iterator;
import java.util.Vector; import java.util.Vector;
import javax.swing.event.UndoableEditEvent; import javax.swing.event.UndoableEditEvent;
import javax.swing.event.UndoableEditListener; import javax.swing.event.UndoableEditListener;
/** /**
* UndoableEditSupport * A helper class for supporting {@link
* @author Andrew Selkirk * 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 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; protected int updateLevel;
/** /**
* compoundEdit * compoundEdit
*/ */
protected CompoundEdit compoundEdit; protected CompoundEdit compoundEdit;
/** /**
* listeners * The currently registered listeners.
*/ */
protected Vector listeners = new Vector(); protected Vector listeners = new Vector();
/** /**
* realSource * The source of the broadcast UndoableEditEvents.
*/ */
protected Object realSource; 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() public UndoableEditSupport()
{ {
realSource = this;
} }
/** /**
* Constructor UndoableEditSupport * Constructs a new helper for broadcasting UndoableEditEvents.
* @param object TODO *
* @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 a string representation of this object that may be useful
* @returns String * for debugging.
*/ */
public String toString() public String toString()
{ {
return (super.toString() + " realSource: " + realSource // Note that often, this.realSource == this. Therefore, dumping
+ " updateLevel: " + updateLevel); // 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. * Registers a listener.
* @param val the listener *
* @param val the listener to be added.
*/ */
public synchronized void addUndoableEditListener(UndoableEditListener val) public synchronized void addUndoableEditListener(UndoableEditListener val)
{ {
listeners.add(val); listeners.add(val);
} }
/** /**
* Remove a listener. * Unregisters a listener.
* @param val the listener * @param val the listener to be removed.
*/ */
public synchronized void removeUndoableEditListener(UndoableEditListener val) public synchronized void removeUndoableEditListener(UndoableEditListener val)
{ {
listeners.removeElement(val); listeners.removeElement(val);
} }
/** /**
* Return an array of all listeners. * Returns an array containing the currently registered listeners.
* @returns all the listeners
*/ */
public synchronized UndoableEditListener[] getUndoableEditListeners() public synchronized UndoableEditListener[] getUndoableEditListeners()
{ {
...@@ -137,78 +151,121 @@ public class UndoableEditSupport ...@@ -137,78 +151,121 @@ public class UndoableEditSupport
return (UndoableEditListener[]) listeners.toArray(result); return (UndoableEditListener[]) listeners.toArray(result);
} }
/** /**
* _postEdit * Notifies all registered listeners that an {@link
* @param value0 TODO * 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) protected void _postEdit(UndoableEdit edit)
{ {
UndoableEditEvent event = new UndoableEditEvent(realSource, edit); UndoableEditEvent event;
int max = listeners.size(); Iterator iter;
for (int i = 0; i < max; ++i)
{ // Do nothing if we have no listeners.
UndoableEditListener l if (listeners.isEmpty())
= (UndoableEditListener) (listeners.elementAt(i)); return;
l.undoableEditHappened(event);
} 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 * If {@link #beginEdit} has been called (so that the current
* @param value0 TODO * 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) public synchronized void postEdit(UndoableEdit edit)
{ {
if (compoundEdit == null) if (compoundEdit != null)
compoundEdit.addEdit(edit); compoundEdit.addEdit(edit);
else else
_postEdit(edit); _postEdit(edit);
} }
/** /**
* getUpdateLevel * Returns the current update level.
* @returns int
*/ */
public int getUpdateLevel() public int getUpdateLevel()
{ {
return updateLevel; 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() public synchronized void beginUpdate()
{ {
if (compoundEdit != null) if (compoundEdit == null)
{
// FIXME: what? We can't push a new one. This isn't even
// documented anyway.
endUpdate();
}
compoundEdit = createCompoundEdit(); compoundEdit = createCompoundEdit();
++updateLevel; ++updateLevel;
} }
/** /**
* createCompoundEdit * Creates a new instance of {@link #CompoundEdit}. Called by {@link
* @returns CompoundEdit * #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() protected CompoundEdit createCompoundEdit()
{ {
return new CompoundEdit(); 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() public synchronized void endUpdate()
{ {
// FIXME: assert updateLevel == 1; if (updateLevel == 0)
throw new IllegalStateException();
if (--updateLevel > 0)
return;
compoundEdit.end(); compoundEdit.end();
CompoundEdit c = compoundEdit; _postEdit(compoundEdit);
compoundEdit = null; compoundEdit = null;
--updateLevel;
_postEdit(c);
} }
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment