jaroslav@601: /* jaroslav@601: * Copyright (c) 1996, 2011, 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@604: import org.apidesign.bck2brwsr.emul.lang.System; jaroslav@601: jaroslav@601: /** jaroslav@601: * This is a utility class that can be used by beans that support constrained 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 VetoableChangeListener} 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 VetoableChangeSupport} 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 VetoableChangeSupport vcs = new VetoableChangeSupport(this); jaroslav@601: * jaroslav@601: * public void addVetoableChangeListener(VetoableChangeListener listener) { jaroslav@601: * this.vcs.addVetoableChangeListener(listener); jaroslav@601: * } jaroslav@601: * jaroslav@601: * public void removeVetoableChangeListener(VetoableChangeListener listener) { jaroslav@601: * this.vcs.removeVetoableChangeListener(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) throws PropertyVetoException { jaroslav@601: * String oldValue = this.value; jaroslav@601: * this.vcs.fireVetoableChange("value", oldValue, newValue); jaroslav@601: * this.value = newValue; jaroslav@601: * } jaroslav@601: * jaroslav@601: * [...] jaroslav@601: * } jaroslav@601: *jaroslav@601: *
jaroslav@601: * A {@code VetoableChangeSupport} 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 PropertyChangeSupport
jaroslav@601: */
jaroslav@601: public class VetoableChangeSupport implements Serializable {
jaroslav@601: private VetoableChangeListenerMap map = new VetoableChangeListenerMap();
jaroslav@601:
jaroslav@601: /**
jaroslav@601: * Constructs a VetoableChangeSupport
object.
jaroslav@601: *
jaroslav@601: * @param sourceBean The bean to be given as the source for any events.
jaroslav@601: */
jaroslav@601: public VetoableChangeSupport(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 VetoableChangeListener 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 VetoableChangeListener to be added
jaroslav@601: */
jaroslav@601: public void addVetoableChangeListener(VetoableChangeListener listener) {
jaroslav@601: if (listener == null) {
jaroslav@601: return;
jaroslav@601: }
jaroslav@601: if (listener instanceof VetoableChangeListenerProxy) {
jaroslav@601: VetoableChangeListenerProxy proxy =
jaroslav@601: (VetoableChangeListenerProxy)listener;
jaroslav@601: // Call two argument add method.
jaroslav@601: addVetoableChangeListener(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 VetoableChangeListener from the listener list.
jaroslav@601: * This removes a VetoableChangeListener 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 VetoableChangeListener to be removed
jaroslav@601: */
jaroslav@601: public void removeVetoableChangeListener(VetoableChangeListener listener) {
jaroslav@601: if (listener == null) {
jaroslav@601: return;
jaroslav@601: }
jaroslav@601: if (listener instanceof VetoableChangeListenerProxy) {
jaroslav@601: VetoableChangeListenerProxy proxy =
jaroslav@601: (VetoableChangeListenerProxy)listener;
jaroslav@601: // Call two argument remove method.
jaroslav@601: removeVetoableChangeListener(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: * VetoableChangeSupport object with addVetoableChangeListener().
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 VetoableChangeListeners
jaroslav@601: * and VetoableChangeListenerProxy
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: * VetoableChangeListenerProxy
, perform the cast, and examine
jaroslav@601: * the parameter.
jaroslav@601: *
jaroslav@601: *
jaroslav@601: * VetoableChangeListener[] listeners = bean.getVetoableChangeListeners(); jaroslav@601: * for (int i = 0; i < listeners.length; i++) { jaroslav@601: * if (listeners[i] instanceof VetoableChangeListenerProxy) { jaroslav@601: * VetoableChangeListenerProxy proxy = jaroslav@601: * (VetoableChangeListenerProxy)listeners[i]; jaroslav@601: * if (proxy.getPropertyName().equals("foo")) { jaroslav@601: * // proxy is a VetoableChangeListener which was associated jaroslav@601: * // with the property named "foo" jaroslav@601: * } jaroslav@601: * } jaroslav@601: * } jaroslav@601: *jaroslav@601: * jaroslav@601: * @see VetoableChangeListenerProxy jaroslav@601: * @return all of the
VetoableChangeListeners
added or an
jaroslav@601: * empty array if no listeners have been added
jaroslav@601: * @since 1.4
jaroslav@601: */
jaroslav@601: public VetoableChangeListener[] getVetoableChangeListeners(){
jaroslav@601: return this.map.getListeners();
jaroslav@601: }
jaroslav@601:
jaroslav@601: /**
jaroslav@601: * Add a VetoableChangeListener for a specific property. The listener
jaroslav@601: * will be invoked only when a call on fireVetoableChange 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 VetoableChangeListener to be added
jaroslav@601: */
jaroslav@601: public void addVetoableChangeListener(
jaroslav@601: String propertyName,
jaroslav@601: VetoableChangeListener 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 VetoableChangeListener 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 VetoableChangeListener to be removed
jaroslav@601: */
jaroslav@601: public void removeVetoableChangeListener(
jaroslav@601: String propertyName,
jaroslav@601: VetoableChangeListener 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 the VetoableChangeListeners
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 VetoableChangeListener[] getVetoableChangeListeners(String propertyName) {
jaroslav@601: return this.map.getListeners(propertyName);
jaroslav@601: }
jaroslav@601:
jaroslav@601: /**
jaroslav@601: * Reports a constrained 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: * Any listener can throw a {@code PropertyVetoException} to veto the update. jaroslav@601: * If one of the listeners vetoes the update, this method passes jaroslav@601: * a new "undo" {@code PropertyChangeEvent} that reverts to the old value jaroslav@601: * to all listeners that already confirmed this update jaroslav@601: * and throws the {@code PropertyVetoException} again. 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 #fireVetoableChange(PropertyChangeEvent)} method. jaroslav@601: * jaroslav@601: * @param propertyName the programmatic name of the property that is about to change jaroslav@601: * @param oldValue the old value of the property jaroslav@601: * @param newValue the new value of the property jaroslav@601: * @throws PropertyVetoException if one of listeners vetoes the property update jaroslav@601: */ jaroslav@601: public void fireVetoableChange(String propertyName, Object oldValue, Object newValue) jaroslav@601: throws PropertyVetoException { jaroslav@601: if (oldValue == null || newValue == null || !oldValue.equals(newValue)) { jaroslav@601: fireVetoableChange(new PropertyChangeEvent(this.source, propertyName, oldValue, newValue)); jaroslav@601: } jaroslav@601: } jaroslav@601: jaroslav@601: /** jaroslav@601: * Reports an integer constrained 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: * Any listener can throw a {@code PropertyVetoException} to veto the update. jaroslav@601: * If one of the listeners vetoes the update, this method passes jaroslav@601: * a new "undo" {@code PropertyChangeEvent} that reverts to the old value jaroslav@601: * to all listeners that already confirmed this update jaroslav@601: * and throws the {@code PropertyVetoException} again. 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 #fireVetoableChange(String, Object, Object)} method. jaroslav@601: * jaroslav@601: * @param propertyName the programmatic name of the property that is about to change jaroslav@601: * @param oldValue the old value of the property jaroslav@601: * @param newValue the new value of the property jaroslav@601: * @throws PropertyVetoException if one of listeners vetoes the property update jaroslav@601: */ jaroslav@601: public void fireVetoableChange(String propertyName, int oldValue, int newValue) jaroslav@601: throws PropertyVetoException { jaroslav@601: if (oldValue != newValue) { jaroslav@601: fireVetoableChange(propertyName, Integer.valueOf(oldValue), Integer.valueOf(newValue)); jaroslav@601: } jaroslav@601: } jaroslav@601: jaroslav@601: /** jaroslav@601: * Reports a boolean constrained 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: * Any listener can throw a {@code PropertyVetoException} to veto the update. jaroslav@601: * If one of the listeners vetoes the update, this method passes jaroslav@601: * a new "undo" {@code PropertyChangeEvent} that reverts to the old value jaroslav@601: * to all listeners that already confirmed this update jaroslav@601: * and throws the {@code PropertyVetoException} again. 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 #fireVetoableChange(String, Object, Object)} method. jaroslav@601: * jaroslav@601: * @param propertyName the programmatic name of the property that is about to change jaroslav@601: * @param oldValue the old value of the property jaroslav@601: * @param newValue the new value of the property jaroslav@601: * @throws PropertyVetoException if one of listeners vetoes the property update jaroslav@601: */ jaroslav@601: public void fireVetoableChange(String propertyName, boolean oldValue, boolean newValue) jaroslav@601: throws PropertyVetoException { jaroslav@601: if (oldValue != newValue) { jaroslav@601: fireVetoableChange(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: * Any listener can throw a {@code PropertyVetoException} to veto the update. jaroslav@601: * If one of the listeners vetoes the update, this method passes jaroslav@601: * a new "undo" {@code PropertyChangeEvent} that reverts to the old value jaroslav@601: * to all listeners that already confirmed this update jaroslav@601: * and throws the {@code PropertyVetoException} again. 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: * @throws PropertyVetoException if one of listeners vetoes the property update
jaroslav@601: */
jaroslav@601: public void fireVetoableChange(PropertyChangeEvent event)
jaroslav@601: throws PropertyVetoException {
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: VetoableChangeListener[] common = this.map.get(null);
jaroslav@601: VetoableChangeListener[] named = (name != null)
jaroslav@601: ? this.map.get(name)
jaroslav@601: : null;
jaroslav@601:
jaroslav@601: VetoableChangeListener[] listeners;
jaroslav@601: if (common == null) {
jaroslav@601: listeners = named;
jaroslav@601: }
jaroslav@601: else if (named == null) {
jaroslav@601: listeners = common;
jaroslav@601: }
jaroslav@601: else {
jaroslav@601: listeners = new VetoableChangeListener[common.length + named.length];
jaroslav@601: System.arraycopy(common, 0, listeners, 0, common.length);
jaroslav@601: System.arraycopy(named, 0, listeners, common.length, named.length);
jaroslav@601: }
jaroslav@601: if (listeners != null) {
jaroslav@601: int current = 0;
jaroslav@601: try {
jaroslav@601: while (current < listeners.length) {
jaroslav@601: listeners[current].vetoableChange(event);
jaroslav@601: current++;
jaroslav@601: }
jaroslav@601: }
jaroslav@601: catch (PropertyVetoException veto) {
jaroslav@601: event = new PropertyChangeEvent(this.source, name, newValue, oldValue);
jaroslav@601: for (int i = 0; i < current; i++) {
jaroslav@601: try {
jaroslav@601: listeners[i].vetoableChange(event);
jaroslav@601: }
jaroslav@601: catch (PropertyVetoException exception) {
jaroslav@601: // ignore exceptions that occur during rolling back
jaroslav@601: }
jaroslav@601: }
jaroslav@601: throw veto; // rethrow the veto exception
jaroslav@601: }
jaroslav@601: }
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 VetoableChangeListeners
.
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