jaroslav@601: /* jaroslav@601: * Copyright (c) 1996, 2008, Oracle and/or its affiliates. All rights reserved. jaroslav@601: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. jaroslav@601: * jaroslav@601: * This code is free software; you can redistribute it and/or modify it jaroslav@601: * under the terms of the GNU General Public License version 2 only, as jaroslav@601: * published by the Free Software Foundation. Oracle designates this jaroslav@601: * particular file as subject to the "Classpath" exception as provided jaroslav@601: * by Oracle in the LICENSE file that accompanied this code. jaroslav@601: * jaroslav@601: * This code is distributed in the hope that it will be useful, but WITHOUT jaroslav@601: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or jaroslav@601: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License jaroslav@601: * version 2 for more details (a copy is included in the LICENSE file that jaroslav@601: * accompanied this code). jaroslav@601: * jaroslav@601: * You should have received a copy of the GNU General Public License version jaroslav@601: * 2 along with this work; if not, write to the Free Software Foundation, jaroslav@601: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. jaroslav@601: * jaroslav@601: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA jaroslav@601: * or visit www.oracle.com if you need additional information or have any jaroslav@601: * questions. jaroslav@601: */ jaroslav@601: package java.beans; jaroslav@601: jaroslav@601: import java.io.Serializable; jaroslav@601: import java.io.ObjectStreamField; jaroslav@601: import java.io.ObjectOutputStream; jaroslav@601: import java.io.ObjectInputStream; jaroslav@601: import java.io.IOException; jaroslav@601: import java.util.Hashtable; jaroslav@601: import java.util.Map.Entry; jaroslav@601: jaroslav@601: /** jaroslav@601: * This is a utility class that can be used by beans that support bound jaroslav@601: * properties. It manages a list of listeners and dispatches jaroslav@601: * {@link PropertyChangeEvent}s to them. You can use an instance of this class jaroslav@601: * as a member field of your bean and delegate these types of work to it. jaroslav@601: * The {@link PropertyChangeListener} can be registered for all properties jaroslav@601: * or for a property specified by name. jaroslav@601: *
jaroslav@601: * Here is an example of {@code PropertyChangeSupport} usage that follows jaroslav@601: * the rules and recommendations laid out in the JavaBeans™ specification: jaroslav@601: *
jaroslav@601: * public class MyBean { jaroslav@601: * private final PropertyChangeSupport pcs = new PropertyChangeSupport(this); jaroslav@601: * jaroslav@601: * public void addPropertyChangeListener(PropertyChangeListener listener) { jaroslav@601: * this.pcs.addPropertyChangeListener(listener); jaroslav@601: * } jaroslav@601: * jaroslav@601: * public void removePropertyChangeListener(PropertyChangeListener listener) { jaroslav@601: * this.pcs.removePropertyChangeListener(listener); jaroslav@601: * } jaroslav@601: * jaroslav@601: * private String value; jaroslav@601: * jaroslav@601: * public String getValue() { jaroslav@601: * return this.value; jaroslav@601: * } jaroslav@601: * jaroslav@601: * public void setValue(String newValue) { jaroslav@601: * String oldValue = this.value; jaroslav@601: * this.value = newValue; jaroslav@601: * this.pcs.firePropertyChange("value", oldValue, newValue); jaroslav@601: * } jaroslav@601: * jaroslav@601: * [...] jaroslav@601: * } jaroslav@601: *jaroslav@601: *
jaroslav@601: * A {@code PropertyChangeSupport} instance is thread-safe. jaroslav@601: *
jaroslav@601: * This class is serializable. When it is serialized it will save
jaroslav@601: * (and restore) any listeners that are themselves serializable. Any
jaroslav@601: * non-serializable listeners will be skipped during serialization.
jaroslav@601: *
jaroslav@601: * @see VetoableChangeSupport
jaroslav@601: */
jaroslav@601: public class PropertyChangeSupport implements Serializable {
jaroslav@601: private PropertyChangeListenerMap map = new PropertyChangeListenerMap();
jaroslav@601:
jaroslav@601: /**
jaroslav@601: * Constructs a PropertyChangeSupport
object.
jaroslav@601: *
jaroslav@601: * @param sourceBean The bean to be given as the source for any events.
jaroslav@601: */
jaroslav@601: public PropertyChangeSupport(Object sourceBean) {
jaroslav@601: if (sourceBean == null) {
jaroslav@601: throw new NullPointerException();
jaroslav@601: }
jaroslav@601: source = sourceBean;
jaroslav@601: }
jaroslav@601:
jaroslav@601: /**
jaroslav@601: * Add a PropertyChangeListener to the listener list.
jaroslav@601: * The listener is registered for all properties.
jaroslav@601: * The same listener object may be added more than once, and will be called
jaroslav@601: * as many times as it is added.
jaroslav@601: * If listener
is null, no exception is thrown and no action
jaroslav@601: * is taken.
jaroslav@601: *
jaroslav@601: * @param listener The PropertyChangeListener to be added
jaroslav@601: */
jaroslav@601: public void addPropertyChangeListener(PropertyChangeListener listener) {
jaroslav@601: if (listener == null) {
jaroslav@601: return;
jaroslav@601: }
jaroslav@601: if (listener instanceof PropertyChangeListenerProxy) {
jaroslav@601: PropertyChangeListenerProxy proxy =
jaroslav@601: (PropertyChangeListenerProxy)listener;
jaroslav@601: // Call two argument add method.
jaroslav@601: addPropertyChangeListener(proxy.getPropertyName(),
jaroslav@601: proxy.getListener());
jaroslav@601: } else {
jaroslav@601: this.map.add(null, listener);
jaroslav@601: }
jaroslav@601: }
jaroslav@601:
jaroslav@601: /**
jaroslav@601: * Remove a PropertyChangeListener from the listener list.
jaroslav@601: * This removes a PropertyChangeListener that was registered
jaroslav@601: * for all properties.
jaroslav@601: * If listener
was added more than once to the same event
jaroslav@601: * source, it will be notified one less time after being removed.
jaroslav@601: * If listener
is null, or was never added, no exception is
jaroslav@601: * thrown and no action is taken.
jaroslav@601: *
jaroslav@601: * @param listener The PropertyChangeListener to be removed
jaroslav@601: */
jaroslav@601: public void removePropertyChangeListener(PropertyChangeListener listener) {
jaroslav@601: if (listener == null) {
jaroslav@601: return;
jaroslav@601: }
jaroslav@601: if (listener instanceof PropertyChangeListenerProxy) {
jaroslav@601: PropertyChangeListenerProxy proxy =
jaroslav@601: (PropertyChangeListenerProxy)listener;
jaroslav@601: // Call two argument remove method.
jaroslav@601: removePropertyChangeListener(proxy.getPropertyName(),
jaroslav@601: proxy.getListener());
jaroslav@601: } else {
jaroslav@601: this.map.remove(null, listener);
jaroslav@601: }
jaroslav@601: }
jaroslav@601:
jaroslav@601: /**
jaroslav@601: * Returns an array of all the listeners that were added to the
jaroslav@601: * PropertyChangeSupport object with addPropertyChangeListener().
jaroslav@601: *
jaroslav@601: * If some listeners have been added with a named property, then
jaroslav@601: * the returned array will be a mixture of PropertyChangeListeners
jaroslav@601: * and PropertyChangeListenerProxy
s. If the calling
jaroslav@601: * method is interested in distinguishing the listeners then it must
jaroslav@601: * test each element to see if it's a
jaroslav@601: * PropertyChangeListenerProxy
, perform the cast, and examine
jaroslav@601: * the parameter.
jaroslav@601: *
jaroslav@601: *
jaroslav@601: * PropertyChangeListener[] listeners = bean.getPropertyChangeListeners(); jaroslav@601: * for (int i = 0; i < listeners.length; i++) { jaroslav@601: * if (listeners[i] instanceof PropertyChangeListenerProxy) { jaroslav@601: * PropertyChangeListenerProxy proxy = jaroslav@601: * (PropertyChangeListenerProxy)listeners[i]; jaroslav@601: * if (proxy.getPropertyName().equals("foo")) { jaroslav@601: * // proxy is a PropertyChangeListener which was associated jaroslav@601: * // with the property named "foo" jaroslav@601: * } jaroslav@601: * } jaroslav@601: * } jaroslav@601: *jaroslav@601: * jaroslav@601: * @see PropertyChangeListenerProxy jaroslav@601: * @return all of the
PropertyChangeListeners
added or an
jaroslav@601: * empty array if no listeners have been added
jaroslav@601: * @since 1.4
jaroslav@601: */
jaroslav@601: public PropertyChangeListener[] getPropertyChangeListeners() {
jaroslav@601: return this.map.getListeners();
jaroslav@601: }
jaroslav@601:
jaroslav@601: /**
jaroslav@601: * Add a PropertyChangeListener for a specific property. The listener
jaroslav@601: * will be invoked only when a call on firePropertyChange names that
jaroslav@601: * specific property.
jaroslav@601: * The same listener object may be added more than once. For each
jaroslav@601: * property, the listener will be invoked the number of times it was added
jaroslav@601: * for that property.
jaroslav@601: * If propertyName
or listener
is null, no
jaroslav@601: * exception is thrown and no action is taken.
jaroslav@601: *
jaroslav@601: * @param propertyName The name of the property to listen on.
jaroslav@601: * @param listener The PropertyChangeListener to be added
jaroslav@601: */
jaroslav@601: public void addPropertyChangeListener(
jaroslav@601: String propertyName,
jaroslav@601: PropertyChangeListener listener) {
jaroslav@601: if (listener == null || propertyName == null) {
jaroslav@601: return;
jaroslav@601: }
jaroslav@601: listener = this.map.extract(listener);
jaroslav@601: if (listener != null) {
jaroslav@601: this.map.add(propertyName, listener);
jaroslav@601: }
jaroslav@601: }
jaroslav@601:
jaroslav@601: /**
jaroslav@601: * Remove a PropertyChangeListener for a specific property.
jaroslav@601: * If listener
was added more than once to the same event
jaroslav@601: * source for the specified property, it will be notified one less time
jaroslav@601: * after being removed.
jaroslav@601: * If propertyName
is null, no exception is thrown and no
jaroslav@601: * action is taken.
jaroslav@601: * If listener
is null, or was never added for the specified
jaroslav@601: * property, no exception is thrown and no action is taken.
jaroslav@601: *
jaroslav@601: * @param propertyName The name of the property that was listened on.
jaroslav@601: * @param listener The PropertyChangeListener to be removed
jaroslav@601: */
jaroslav@601: public void removePropertyChangeListener(
jaroslav@601: String propertyName,
jaroslav@601: PropertyChangeListener listener) {
jaroslav@601: if (listener == null || propertyName == null) {
jaroslav@601: return;
jaroslav@601: }
jaroslav@601: listener = this.map.extract(listener);
jaroslav@601: if (listener != null) {
jaroslav@601: this.map.remove(propertyName, listener);
jaroslav@601: }
jaroslav@601: }
jaroslav@601:
jaroslav@601: /**
jaroslav@601: * Returns an array of all the listeners which have been associated
jaroslav@601: * with the named property.
jaroslav@601: *
jaroslav@601: * @param propertyName The name of the property being listened to
jaroslav@601: * @return all of the PropertyChangeListeners
associated with
jaroslav@601: * the named property. If no such listeners have been added,
jaroslav@601: * or if propertyName
is null, an empty array is
jaroslav@601: * returned.
jaroslav@601: * @since 1.4
jaroslav@601: */
jaroslav@601: public PropertyChangeListener[] getPropertyChangeListeners(String propertyName) {
jaroslav@601: return this.map.getListeners(propertyName);
jaroslav@601: }
jaroslav@601:
jaroslav@601: /**
jaroslav@601: * Reports a bound property update to listeners
jaroslav@601: * that have been registered to track updates of
jaroslav@601: * all properties or a property with the specified name.
jaroslav@601: * jaroslav@601: * No event is fired if old and new values are equal and non-null. jaroslav@601: *
jaroslav@601: * This is merely a convenience wrapper around the more general jaroslav@601: * {@link #firePropertyChange(PropertyChangeEvent)} method. jaroslav@601: * jaroslav@601: * @param propertyName the programmatic name of the property that was changed jaroslav@601: * @param oldValue the old value of the property jaroslav@601: * @param newValue the new value of the property jaroslav@601: */ jaroslav@601: public void firePropertyChange(String propertyName, Object oldValue, Object newValue) { jaroslav@601: if (oldValue == null || newValue == null || !oldValue.equals(newValue)) { jaroslav@601: firePropertyChange(new PropertyChangeEvent(this.source, propertyName, oldValue, newValue)); jaroslav@601: } jaroslav@601: } jaroslav@601: jaroslav@601: /** jaroslav@601: * Reports an integer bound property update to listeners jaroslav@601: * that have been registered to track updates of jaroslav@601: * all properties or a property with the specified name. jaroslav@601: *
jaroslav@601: * No event is fired if old and new values are equal. jaroslav@601: *
jaroslav@601: * This is merely a convenience wrapper around the more general jaroslav@601: * {@link #firePropertyChange(String, Object, Object)} method. jaroslav@601: * jaroslav@601: * @param propertyName the programmatic name of the property that was changed jaroslav@601: * @param oldValue the old value of the property jaroslav@601: * @param newValue the new value of the property jaroslav@601: */ jaroslav@601: public void firePropertyChange(String propertyName, int oldValue, int newValue) { jaroslav@601: if (oldValue != newValue) { jaroslav@601: firePropertyChange(propertyName, Integer.valueOf(oldValue), Integer.valueOf(newValue)); jaroslav@601: } jaroslav@601: } jaroslav@601: jaroslav@601: /** jaroslav@601: * Reports a boolean bound property update to listeners jaroslav@601: * that have been registered to track updates of jaroslav@601: * all properties or a property with the specified name. jaroslav@601: *
jaroslav@601: * No event is fired if old and new values are equal. jaroslav@601: *
jaroslav@601: * This is merely a convenience wrapper around the more general jaroslav@601: * {@link #firePropertyChange(String, Object, Object)} method. jaroslav@601: * jaroslav@601: * @param propertyName the programmatic name of the property that was changed jaroslav@601: * @param oldValue the old value of the property jaroslav@601: * @param newValue the new value of the property jaroslav@601: */ jaroslav@601: public void firePropertyChange(String propertyName, boolean oldValue, boolean newValue) { jaroslav@601: if (oldValue != newValue) { jaroslav@601: firePropertyChange(propertyName, Boolean.valueOf(oldValue), Boolean.valueOf(newValue)); jaroslav@601: } jaroslav@601: } jaroslav@601: jaroslav@601: /** jaroslav@601: * Fires a property change event to listeners jaroslav@601: * that have been registered to track updates of jaroslav@601: * all properties or a property with the specified name. jaroslav@601: *
jaroslav@601: * No event is fired if the given event's old and new values are equal and non-null. jaroslav@601: * jaroslav@601: * @param event the {@code PropertyChangeEvent} to be fired jaroslav@601: */ jaroslav@601: public void firePropertyChange(PropertyChangeEvent event) { jaroslav@601: Object oldValue = event.getOldValue(); jaroslav@601: Object newValue = event.getNewValue(); jaroslav@601: if (oldValue == null || newValue == null || !oldValue.equals(newValue)) { jaroslav@601: String name = event.getPropertyName(); jaroslav@601: jaroslav@601: PropertyChangeListener[] common = this.map.get(null); jaroslav@601: PropertyChangeListener[] named = (name != null) jaroslav@601: ? this.map.get(name) jaroslav@601: : null; jaroslav@601: jaroslav@601: fire(common, event); jaroslav@601: fire(named, event); jaroslav@601: } jaroslav@601: } jaroslav@601: jaroslav@601: private static void fire(PropertyChangeListener[] listeners, PropertyChangeEvent event) { jaroslav@601: if (listeners != null) { jaroslav@601: for (PropertyChangeListener listener : listeners) { jaroslav@601: listener.propertyChange(event); jaroslav@601: } jaroslav@601: } jaroslav@601: } jaroslav@601: jaroslav@601: /** jaroslav@601: * Reports a bound indexed property update to listeners jaroslav@601: * that have been registered to track updates of jaroslav@601: * all properties or a property with the specified name. jaroslav@601: *
jaroslav@601: * No event is fired if old and new values are equal and non-null. jaroslav@601: *
jaroslav@601: * This is merely a convenience wrapper around the more general jaroslav@601: * {@link #firePropertyChange(PropertyChangeEvent)} method. jaroslav@601: * jaroslav@601: * @param propertyName the programmatic name of the property that was changed jaroslav@601: * @param index the index of the property element that was changed jaroslav@601: * @param oldValue the old value of the property jaroslav@601: * @param newValue the new value of the property jaroslav@601: * @since 1.5 jaroslav@601: */ jaroslav@601: public void fireIndexedPropertyChange(String propertyName, int index, Object oldValue, Object newValue) { jaroslav@601: if (oldValue == null || newValue == null || !oldValue.equals(newValue)) { jaroslav@601: firePropertyChange(new IndexedPropertyChangeEvent(source, propertyName, oldValue, newValue, index)); jaroslav@601: } jaroslav@601: } jaroslav@601: jaroslav@601: /** jaroslav@601: * Reports an integer bound indexed property update to listeners jaroslav@601: * that have been registered to track updates of jaroslav@601: * all properties or a property with the specified name. jaroslav@601: *
jaroslav@601: * No event is fired if old and new values are equal. jaroslav@601: *
jaroslav@601: * This is merely a convenience wrapper around the more general jaroslav@601: * {@link #fireIndexedPropertyChange(String, int, Object, Object)} method. jaroslav@601: * jaroslav@601: * @param propertyName the programmatic name of the property that was changed jaroslav@601: * @param index the index of the property element that was changed jaroslav@601: * @param oldValue the old value of the property jaroslav@601: * @param newValue the new value of the property jaroslav@601: * @since 1.5 jaroslav@601: */ jaroslav@601: public void fireIndexedPropertyChange(String propertyName, int index, int oldValue, int newValue) { jaroslav@601: if (oldValue != newValue) { jaroslav@601: fireIndexedPropertyChange(propertyName, index, Integer.valueOf(oldValue), Integer.valueOf(newValue)); jaroslav@601: } jaroslav@601: } jaroslav@601: jaroslav@601: /** jaroslav@601: * Reports a boolean bound indexed property update to listeners jaroslav@601: * that have been registered to track updates of jaroslav@601: * all properties or a property with the specified name. jaroslav@601: *
jaroslav@601: * No event is fired if old and new values are equal. jaroslav@601: *
jaroslav@601: * This is merely a convenience wrapper around the more general
jaroslav@601: * {@link #fireIndexedPropertyChange(String, int, Object, Object)} method.
jaroslav@601: *
jaroslav@601: * @param propertyName the programmatic name of the property that was changed
jaroslav@601: * @param index the index of the property element that was changed
jaroslav@601: * @param oldValue the old value of the property
jaroslav@601: * @param newValue the new value of the property
jaroslav@601: * @since 1.5
jaroslav@601: */
jaroslav@601: public void fireIndexedPropertyChange(String propertyName, int index, boolean oldValue, boolean newValue) {
jaroslav@601: if (oldValue != newValue) {
jaroslav@601: fireIndexedPropertyChange(propertyName, index, Boolean.valueOf(oldValue), Boolean.valueOf(newValue));
jaroslav@601: }
jaroslav@601: }
jaroslav@601:
jaroslav@601: /**
jaroslav@601: * Check if there are any listeners for a specific property, including
jaroslav@601: * those registered on all properties. If propertyName
jaroslav@601: * is null, only check for listeners registered on all properties.
jaroslav@601: *
jaroslav@601: * @param propertyName the property name.
jaroslav@601: * @return true if there are one or more listeners for the given property
jaroslav@601: */
jaroslav@601: public boolean hasListeners(String propertyName) {
jaroslav@601: return this.map.hasListeners(propertyName);
jaroslav@601: }
jaroslav@601:
jaroslav@601: /**
jaroslav@601: * @serialData Null terminated list of PropertyChangeListeners
.
jaroslav@601: *
jaroslav@601: * At serialization time we skip non-serializable listeners and
jaroslav@601: * only serialize the serializable listeners.
jaroslav@601: */
jaroslav@601: private void writeObject(ObjectOutputStream s) throws IOException {
jaroslav@601: Hashtable