FileDocCategorySizeDatePackage
PropertyChangeSupport.javaAPI DocAndroid 1.5 API18110Wed May 06 22:41:02 BST 2009java.beans

PropertyChangeSupport

public class PropertyChangeSupport extends Object implements Serializable
This utility class

Fields Summary
private static final long
serialVersionUID
private transient Object
sourceBean
private transient List
allPropertiesChangeListeners
private transient Map
selectedPropertiesChangeListeners
private Hashtable
children
private Object
source
private int
propertyChangeSupportSerializedDataVersion
Constructors Summary
public PropertyChangeSupport(Object sourceBean)
Creates a new instance that uses the source bean as source for any event.

param
sourceBean the bean used as source for all events.


                                             
       
        if (sourceBean == null) {
            throw new NullPointerException();
        }
        this.sourceBean = sourceBean;
    
Methods Summary
public synchronized voidaddPropertyChangeListener(java.beans.PropertyChangeListener listener)
Registers a listener with all properties.

param
listener the listener to register

        if (listener != null) {
            if (listener instanceof PropertyChangeListenerProxy) {
                String name = ((PropertyChangeListenerProxy) listener)
                        .getPropertyName();
                PropertyChangeListener lst = (PropertyChangeListener)
                        ((PropertyChangeListenerProxy) listener).getListener();
                addPropertyChangeListener(name, lst);
            } else {
                allPropertiesChangeListeners.add(listener);
            }
        }
    
public synchronized voidaddPropertyChangeListener(java.lang.String propertyName, java.beans.PropertyChangeListener listener)
Adds a listener to a specific property. Nothing happens if the property name or the listener is null.

param
propertyName the name of the property
param
listener the listener to register for the property with the given name

        if ((listener != null) && (propertyName != null)) {
            List<PropertyChangeListener> listeners =
                    selectedPropertiesChangeListeners.get(propertyName);

            if (listeners == null) {
                listeners = new ArrayList<PropertyChangeListener>();
                selectedPropertiesChangeListeners.put(propertyName, listeners);
            }

            // RI compatibility
            if (listener instanceof PropertyChangeListenerProxy) {
                PropertyChangeListenerProxy proxy =
                        (PropertyChangeListenerProxy) listener;

                listeners.add(new PropertyChangeListenerProxy(
                        proxy.getPropertyName(),
                        (PropertyChangeListener) proxy.getListener()));
            } else {
                listeners.add(listener);
            }
        }
    
private java.beans.PropertyChangeEventcreatePropertyChangeEvent(java.lang.String propertyName, java.lang.Object oldValue, java.lang.Object newValue)

        return new PropertyChangeEvent(sourceBean, propertyName, oldValue,
                newValue);
    
private java.beans.PropertyChangeEventcreatePropertyChangeEvent(java.lang.String propertyName, boolean oldValue, boolean newValue)

        return new PropertyChangeEvent(sourceBean, propertyName, oldValue,
                newValue);
    
private java.beans.PropertyChangeEventcreatePropertyChangeEvent(java.lang.String propertyName, int oldValue, int newValue)

        return new PropertyChangeEvent(sourceBean, propertyName, oldValue,
                newValue);
    
private voiddoFirePropertyChange(java.beans.PropertyChangeEvent event)

        String propertyName = event.getPropertyName();
        Object oldValue = event.getOldValue();
        Object newValue = event.getNewValue();

        if ((newValue != null) && (oldValue != null)
                && newValue.equals(oldValue)) {
            return;
        }

        /*
         * Copy the listeners collections so they can be modified while we fire
         * events.
         */

        // Listeners to all property change events
        PropertyChangeListener[] listensToAll;
        // Listens to a given property change
        PropertyChangeListener[] listensToOne = null;
        synchronized (this) {
            listensToAll = allPropertiesChangeListeners
                    .toArray(new PropertyChangeListener[allPropertiesChangeListeners
                            .size()]);

            List<PropertyChangeListener> listeners = selectedPropertiesChangeListeners
                    .get(propertyName);
            if (listeners != null) {
                listensToOne = listeners
                        .toArray(new PropertyChangeListener[listeners.size()]);
            }
        }

        // Fire the listeners
        for (PropertyChangeListener listener : listensToAll) {
            listener.propertyChange(event);
        }
        if (listensToOne != null) {
            for (PropertyChangeListener listener : listensToOne) {
                listener.propertyChange(event);
            }
        }
    
public voidfireIndexedPropertyChange(java.lang.String propertyName, int index, int oldValue, int newValue)
Fires a property change of an integer property with the given name. If the old value and the new value are not null and equal the event will not be fired.

param
propertyName the property name
param
index the index of the changed property
param
oldValue the old value
param
newValue the new value


        if (oldValue != newValue) {
            fireIndexedPropertyChange(propertyName, index,
                    new Integer(oldValue), new Integer(newValue));
        }
    
public voidfireIndexedPropertyChange(java.lang.String propertyName, int index, java.lang.Object oldValue, java.lang.Object newValue)
Fires an {@link IndexedPropertyChangeEvent} with the given name, old value, new value and index. As source the bean used to initialize this instance is used. If the old value and the new value are not null and equal the event will not be fired.

param
propertyName the name of the property
param
index the index
param
oldValue the old value of the property
param
newValue the new value of the property


        // nulls and equals check done in doFire...
        doFirePropertyChange(new IndexedPropertyChangeEvent(sourceBean,
                propertyName, oldValue, newValue, index));
    
public voidfireIndexedPropertyChange(java.lang.String propertyName, int index, boolean oldValue, boolean newValue)
Fires a property change of a boolean property with the given name. If the old value and the new value are not null and equal the event will not be fired.

param
propertyName the property name
param
index the index of the changed property
param
oldValue the old value
param
newValue the new value


        if (oldValue != newValue) {
            fireIndexedPropertyChange(propertyName, index, Boolean
                    .valueOf(oldValue), Boolean.valueOf(newValue));
        }
    
public voidfirePropertyChange(java.beans.PropertyChangeEvent event)
Fires a property change event to all listeners of that property.

param
event the event to fire

        doFirePropertyChange(event);
    
public voidfirePropertyChange(java.lang.String propertyName, java.lang.Object oldValue, java.lang.Object newValue)
Fires a {@link PropertyChangeEvent} with the given name, old value and new value. As source the bean used to initialize this instance is used. If the old value and the new value are not null and equal the event will not be fired.

param
propertyName the name of the property
param
oldValue the old value of the property
param
newValue the new value of the property

        PropertyChangeEvent event = createPropertyChangeEvent(propertyName,
                oldValue, newValue);
        doFirePropertyChange(event);
    
public voidfirePropertyChange(java.lang.String propertyName, boolean oldValue, boolean newValue)
Fires a property change of a boolean property with the given name. If the old value and the new value are not null and equal the event will not be fired.

param
propertyName the property name
param
oldValue the old value
param
newValue the new value

        PropertyChangeEvent event = createPropertyChangeEvent(propertyName,
                oldValue, newValue);
        doFirePropertyChange(event);
    
public voidfirePropertyChange(java.lang.String propertyName, int oldValue, int newValue)
Fires a property change of an integer property with the given name. If the old value and the new value are not null and equal the event will not be fired.

param
propertyName the property name
param
oldValue the old value
param
newValue the new value

        PropertyChangeEvent event = createPropertyChangeEvent(propertyName,
                oldValue, newValue);
        doFirePropertyChange(event);
    
public synchronized java.beans.PropertyChangeListener[]getPropertyChangeListeners()
Returns an array with the listeners that registered to all properties.

return
the array of listeners

        ArrayList<PropertyChangeListener> result =
                new ArrayList<PropertyChangeListener>(
                        allPropertiesChangeListeners);

        for (String propertyName : selectedPropertiesChangeListeners.keySet()) {
            List<PropertyChangeListener> selectedListeners =
                    selectedPropertiesChangeListeners.get(propertyName);

            if (selectedListeners != null) {

                for (PropertyChangeListener listener : selectedListeners) {
                    result.add(new PropertyChangeListenerProxy(propertyName,
                            listener));
                }
            }
        }

        return result.toArray(new PropertyChangeListener[result.size()]);
    
public synchronized java.beans.PropertyChangeListener[]getPropertyChangeListeners(java.lang.String propertyName)
Returns an array of listeners that registered to the property with the given name. If the property name is null an empty array is returned.

param
propertyName the name of the property whose listeners should be returned
return
the array of listeners to the property with the given name.

        List<PropertyChangeListener> listeners = null;

        if (propertyName != null) {
            listeners = selectedPropertiesChangeListeners.get(propertyName);
        }

        return (listeners == null) ? new PropertyChangeListener[] {}
                : listeners.toArray(
                        new PropertyChangeListener[listeners.size()]);
    
public synchronized booleanhasListeners(java.lang.String propertyName)
Returns true if there are listeners registered to the property with the given name.

param
propertyName the name of the property
return
true if there are listeners registered to that property, false otherwise.

        boolean result = allPropertiesChangeListeners.size() > 0;
        if (!result && (propertyName != null)) {
            List<PropertyChangeListener> listeners =
                    selectedPropertiesChangeListeners.get(propertyName);
            if (listeners != null) {
                result = listeners.size() > 0;
            }
        }
        return result;
    
private voidreadObject(java.io.ObjectInputStream ois)

        children = (Hashtable<String, List<PropertyChangeListener>>) ois
                .readObject();

        selectedPropertiesChangeListeners = new HashMap<String, List<PropertyChangeListener>>(
                children);
        allPropertiesChangeListeners = selectedPropertiesChangeListeners
                .remove(""); //$NON-NLS-1$
        if (allPropertiesChangeListeners == null) {
            allPropertiesChangeListeners = new ArrayList<PropertyChangeListener>();
        }

        sourceBean = ois.readObject();
        propertyChangeSupportSerializedDataVersion = ois.readInt();
    
public synchronized voidremovePropertyChangeListener(java.beans.PropertyChangeListener listener)
removes a property change listener that was registered to all properties.

param
listener the listener to remove

        if (listener != null) {
            if (listener instanceof PropertyChangeListenerProxy) {
                String name = ((PropertyChangeListenerProxy) listener)
                        .getPropertyName();
                PropertyChangeListener lst = (PropertyChangeListener)
                        ((PropertyChangeListenerProxy) listener).getListener();

                removePropertyChangeListener(name, lst);
            } else {
                allPropertiesChangeListeners.remove(listener);
            }
        }
    
public synchronized voidremovePropertyChangeListener(java.lang.String propertyName, java.beans.PropertyChangeListener listener)
Removes the listener from the specific property. This only happens if it was registered to this property. Nothing happens if it was not registered with this property or if the property name or the listener is null.

param
propertyName the property name the listener is listening to
param
listener the listener to remove

        if ((propertyName != null) && (listener != null)) {
            List<PropertyChangeListener> listeners =
                    selectedPropertiesChangeListeners.get(propertyName);

            if (listeners != null) {
                listeners.remove(listener);
            }
        }
    
private voidwriteObject(java.io.ObjectOutputStream oos)

        List<PropertyChangeListener> allSerializedPropertiesChangeListeners =
                new ArrayList<PropertyChangeListener>();

        for (PropertyChangeListener pcl : allPropertiesChangeListeners) {
            if (pcl instanceof Serializable) {
                allSerializedPropertiesChangeListeners.add(pcl);
            }
        }

        Map<String, List<PropertyChangeListener>>
                selectedSerializedPropertiesChangeListeners =
                        new HashMap<String, List<PropertyChangeListener>>();

        for (String propertyName : selectedPropertiesChangeListeners.keySet()) {
            List<PropertyChangeListener> keyValues =
                    selectedPropertiesChangeListeners.get(propertyName);

            if (keyValues != null) {
                List<PropertyChangeListener> serializedPropertiesChangeListeners
                        = new ArrayList<PropertyChangeListener>();

                for (PropertyChangeListener pcl : keyValues) {
                    if (pcl instanceof Serializable) {
                        serializedPropertiesChangeListeners.add(pcl);
                    }
                }

                if (!serializedPropertiesChangeListeners.isEmpty()) {
                    selectedSerializedPropertiesChangeListeners.put(
                            propertyName, serializedPropertiesChangeListeners);
                }
            }
        }

        children = new Hashtable<String, List<PropertyChangeListener>>(
                selectedSerializedPropertiesChangeListeners);
        children.put("", allSerializedPropertiesChangeListeners); //$NON-NLS-1$
        oos.writeObject(children);

        Object source = null;
        if (sourceBean instanceof Serializable) {
            source = sourceBean;
        }
        oos.writeObject(source);

        oos.writeInt(propertyChangeSupportSerializedDataVersion);