jdk/src/share/classes/java/beans/PropertyChangeSupport.java
author mcimadamore
Thu, 01 Dec 2011 18:34:23 +0000
changeset 11120 f8576c769572
parent 5506 202f599c92aa
child 12032 49ac5a2b1d5c
permissions -rw-r--r--
7116954: Misc warnings in java.beans/java.beans.context Summary: Remove generic warnings form java.beans and java.beans.context Reviewed-by: alanb, chegar
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1307
diff changeset
     2
 * Copyright (c) 1996, 2008, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1307
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1307
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1307
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1307
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1307
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
package java.beans;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
import java.io.Serializable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.io.ObjectStreamField;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.io.ObjectOutputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.io.ObjectInputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.util.Hashtable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.util.Map.Entry;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * This is a utility class that can be used by beans that support bound
1307
7603aea0ec0e 5026703: RFE: DOC: Are PropertyChangeSupport & VetoableChangeSupport Thread-Safe? --Docs Should Say
malenkov
parents: 1292
diff changeset
    37
 * properties.  It manages a list of listeners and dispatches
7603aea0ec0e 5026703: RFE: DOC: Are PropertyChangeSupport & VetoableChangeSupport Thread-Safe? --Docs Should Say
malenkov
parents: 1292
diff changeset
    38
 * {@link PropertyChangeEvent}s to them.  You can use an instance of this class
7603aea0ec0e 5026703: RFE: DOC: Are PropertyChangeSupport & VetoableChangeSupport Thread-Safe? --Docs Should Say
malenkov
parents: 1292
diff changeset
    39
 * as a member field of your bean and delegate these types of work to it.
7603aea0ec0e 5026703: RFE: DOC: Are PropertyChangeSupport & VetoableChangeSupport Thread-Safe? --Docs Should Say
malenkov
parents: 1292
diff changeset
    40
 * The {@link PropertyChangeListener} can be registered for all properties
7603aea0ec0e 5026703: RFE: DOC: Are PropertyChangeSupport & VetoableChangeSupport Thread-Safe? --Docs Should Say
malenkov
parents: 1292
diff changeset
    41
 * or for a property specified by name.
7603aea0ec0e 5026703: RFE: DOC: Are PropertyChangeSupport & VetoableChangeSupport Thread-Safe? --Docs Should Say
malenkov
parents: 1292
diff changeset
    42
 * <p>
7603aea0ec0e 5026703: RFE: DOC: Are PropertyChangeSupport & VetoableChangeSupport Thread-Safe? --Docs Should Say
malenkov
parents: 1292
diff changeset
    43
 * Here is an example of {@code PropertyChangeSupport} usage that follows
7603aea0ec0e 5026703: RFE: DOC: Are PropertyChangeSupport & VetoableChangeSupport Thread-Safe? --Docs Should Say
malenkov
parents: 1292
diff changeset
    44
 * the rules and recommendations laid out in the JavaBeans&trade; specification:
7603aea0ec0e 5026703: RFE: DOC: Are PropertyChangeSupport & VetoableChangeSupport Thread-Safe? --Docs Should Say
malenkov
parents: 1292
diff changeset
    45
 * <pre>
7603aea0ec0e 5026703: RFE: DOC: Are PropertyChangeSupport & VetoableChangeSupport Thread-Safe? --Docs Should Say
malenkov
parents: 1292
diff changeset
    46
 * public class MyBean {
7603aea0ec0e 5026703: RFE: DOC: Are PropertyChangeSupport & VetoableChangeSupport Thread-Safe? --Docs Should Say
malenkov
parents: 1292
diff changeset
    47
 *     private final PropertyChangeSupport pcs = new PropertyChangeSupport(this);
7603aea0ec0e 5026703: RFE: DOC: Are PropertyChangeSupport & VetoableChangeSupport Thread-Safe? --Docs Should Say
malenkov
parents: 1292
diff changeset
    48
 *
7603aea0ec0e 5026703: RFE: DOC: Are PropertyChangeSupport & VetoableChangeSupport Thread-Safe? --Docs Should Say
malenkov
parents: 1292
diff changeset
    49
 *     public void addPropertyChangeListener(PropertyChangeListener listener) {
7603aea0ec0e 5026703: RFE: DOC: Are PropertyChangeSupport & VetoableChangeSupport Thread-Safe? --Docs Should Say
malenkov
parents: 1292
diff changeset
    50
 *         this.pcs.addPropertyChangeListener(listener);
7603aea0ec0e 5026703: RFE: DOC: Are PropertyChangeSupport & VetoableChangeSupport Thread-Safe? --Docs Should Say
malenkov
parents: 1292
diff changeset
    51
 *     }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 *
1307
7603aea0ec0e 5026703: RFE: DOC: Are PropertyChangeSupport & VetoableChangeSupport Thread-Safe? --Docs Should Say
malenkov
parents: 1292
diff changeset
    53
 *     public void removePropertyChangeListener(PropertyChangeListener listener) {
7603aea0ec0e 5026703: RFE: DOC: Are PropertyChangeSupport & VetoableChangeSupport Thread-Safe? --Docs Should Say
malenkov
parents: 1292
diff changeset
    54
 *         this.pcs.removePropertyChangeListener(listener);
7603aea0ec0e 5026703: RFE: DOC: Are PropertyChangeSupport & VetoableChangeSupport Thread-Safe? --Docs Should Say
malenkov
parents: 1292
diff changeset
    55
 *     }
7603aea0ec0e 5026703: RFE: DOC: Are PropertyChangeSupport & VetoableChangeSupport Thread-Safe? --Docs Should Say
malenkov
parents: 1292
diff changeset
    56
 *
7603aea0ec0e 5026703: RFE: DOC: Are PropertyChangeSupport & VetoableChangeSupport Thread-Safe? --Docs Should Say
malenkov
parents: 1292
diff changeset
    57
 *     private String value;
7603aea0ec0e 5026703: RFE: DOC: Are PropertyChangeSupport & VetoableChangeSupport Thread-Safe? --Docs Should Say
malenkov
parents: 1292
diff changeset
    58
 *
7603aea0ec0e 5026703: RFE: DOC: Are PropertyChangeSupport & VetoableChangeSupport Thread-Safe? --Docs Should Say
malenkov
parents: 1292
diff changeset
    59
 *     public String getValue() {
7603aea0ec0e 5026703: RFE: DOC: Are PropertyChangeSupport & VetoableChangeSupport Thread-Safe? --Docs Should Say
malenkov
parents: 1292
diff changeset
    60
 *         return this.value;
7603aea0ec0e 5026703: RFE: DOC: Are PropertyChangeSupport & VetoableChangeSupport Thread-Safe? --Docs Should Say
malenkov
parents: 1292
diff changeset
    61
 *     }
7603aea0ec0e 5026703: RFE: DOC: Are PropertyChangeSupport & VetoableChangeSupport Thread-Safe? --Docs Should Say
malenkov
parents: 1292
diff changeset
    62
 *
7603aea0ec0e 5026703: RFE: DOC: Are PropertyChangeSupport & VetoableChangeSupport Thread-Safe? --Docs Should Say
malenkov
parents: 1292
diff changeset
    63
 *     public void setValue(String newValue) {
7603aea0ec0e 5026703: RFE: DOC: Are PropertyChangeSupport & VetoableChangeSupport Thread-Safe? --Docs Should Say
malenkov
parents: 1292
diff changeset
    64
 *         String oldValue = this.value;
7603aea0ec0e 5026703: RFE: DOC: Are PropertyChangeSupport & VetoableChangeSupport Thread-Safe? --Docs Should Say
malenkov
parents: 1292
diff changeset
    65
 *         this.value = newValue;
7603aea0ec0e 5026703: RFE: DOC: Are PropertyChangeSupport & VetoableChangeSupport Thread-Safe? --Docs Should Say
malenkov
parents: 1292
diff changeset
    66
 *         this.pcs.firePropertyChange("value", oldValue, newValue);
7603aea0ec0e 5026703: RFE: DOC: Are PropertyChangeSupport & VetoableChangeSupport Thread-Safe? --Docs Should Say
malenkov
parents: 1292
diff changeset
    67
 *     }
7603aea0ec0e 5026703: RFE: DOC: Are PropertyChangeSupport & VetoableChangeSupport Thread-Safe? --Docs Should Say
malenkov
parents: 1292
diff changeset
    68
 *
7603aea0ec0e 5026703: RFE: DOC: Are PropertyChangeSupport & VetoableChangeSupport Thread-Safe? --Docs Should Say
malenkov
parents: 1292
diff changeset
    69
 *     [...]
7603aea0ec0e 5026703: RFE: DOC: Are PropertyChangeSupport & VetoableChangeSupport Thread-Safe? --Docs Should Say
malenkov
parents: 1292
diff changeset
    70
 * }
7603aea0ec0e 5026703: RFE: DOC: Are PropertyChangeSupport & VetoableChangeSupport Thread-Safe? --Docs Should Say
malenkov
parents: 1292
diff changeset
    71
 * </pre>
7603aea0ec0e 5026703: RFE: DOC: Are PropertyChangeSupport & VetoableChangeSupport Thread-Safe? --Docs Should Say
malenkov
parents: 1292
diff changeset
    72
 * <p>
7603aea0ec0e 5026703: RFE: DOC: Are PropertyChangeSupport & VetoableChangeSupport Thread-Safe? --Docs Should Say
malenkov
parents: 1292
diff changeset
    73
 * A {@code PropertyChangeSupport} instance is thread-safe.
7603aea0ec0e 5026703: RFE: DOC: Are PropertyChangeSupport & VetoableChangeSupport Thread-Safe? --Docs Should Say
malenkov
parents: 1292
diff changeset
    74
 * <p>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
 * This class is serializable.  When it is serialized it will save
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
 * (and restore) any listeners that are themselves serializable.  Any
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
 * non-serializable listeners will be skipped during serialization.
1307
7603aea0ec0e 5026703: RFE: DOC: Are PropertyChangeSupport & VetoableChangeSupport Thread-Safe? --Docs Should Say
malenkov
parents: 1292
diff changeset
    78
 *
7603aea0ec0e 5026703: RFE: DOC: Are PropertyChangeSupport & VetoableChangeSupport Thread-Safe? --Docs Should Say
malenkov
parents: 1292
diff changeset
    79
 * @see VetoableChangeSupport
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
public class PropertyChangeSupport implements Serializable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    private PropertyChangeListenerMap map = new PropertyChangeListenerMap();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     * Constructs a <code>PropertyChangeSupport</code> object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     * @param sourceBean  The bean to be given as the source for any events.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    public PropertyChangeSupport(Object sourceBean) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        if (sourceBean == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
            throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        source = sourceBean;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     * Add a PropertyChangeListener to the listener list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     * The listener is registered for all properties.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     * The same listener object may be added more than once, and will be called
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     * as many times as it is added.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     * If <code>listener</code> is null, no exception is thrown and no action
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     * is taken.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     * @param listener  The PropertyChangeListener to be added
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    public void addPropertyChangeListener(PropertyChangeListener listener) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        if (listener == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        if (listener instanceof PropertyChangeListenerProxy) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
            PropertyChangeListenerProxy proxy =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
                   (PropertyChangeListenerProxy)listener;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
            // Call two argument add method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
            addPropertyChangeListener(proxy.getPropertyName(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
                                      proxy.getListener());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
            this.map.add(null, listener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     * Remove a PropertyChangeListener from the listener list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     * This removes a PropertyChangeListener that was registered
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     * for all properties.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     * If <code>listener</code> was added more than once to the same event
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     * source, it will be notified one less time after being removed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     * If <code>listener</code> is null, or was never added, no exception is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     * thrown and no action is taken.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     * @param listener  The PropertyChangeListener to be removed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    public void removePropertyChangeListener(PropertyChangeListener listener) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        if (listener == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        if (listener instanceof PropertyChangeListenerProxy) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
            PropertyChangeListenerProxy proxy =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
                    (PropertyChangeListenerProxy)listener;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
            // Call two argument remove method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
            removePropertyChangeListener(proxy.getPropertyName(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
                                         proxy.getListener());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
            this.map.remove(null, listener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     * Returns an array of all the listeners that were added to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     * PropertyChangeSupport object with addPropertyChangeListener().
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     * If some listeners have been added with a named property, then
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     * the returned array will be a mixture of PropertyChangeListeners
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     * and <code>PropertyChangeListenerProxy</code>s. If the calling
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     * method is interested in distinguishing the listeners then it must
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     * test each element to see if it's a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     * <code>PropertyChangeListenerProxy</code>, perform the cast, and examine
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     * the parameter.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     * PropertyChangeListener[] listeners = bean.getPropertyChangeListeners();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     * for (int i = 0; i < listeners.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     *   if (listeners[i] instanceof PropertyChangeListenerProxy) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     *     PropertyChangeListenerProxy proxy =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     *                    (PropertyChangeListenerProxy)listeners[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     *     if (proxy.getPropertyName().equals("foo")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     *       // proxy is a PropertyChangeListener which was associated
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     *       // with the property named "foo"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     *     }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     *   }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     * }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     *</pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     * @see PropertyChangeListenerProxy
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     * @return all of the <code>PropertyChangeListeners</code> added or an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     *         empty array if no listeners have been added
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
    public PropertyChangeListener[] getPropertyChangeListeners() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        return this.map.getListeners();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
     * Add a PropertyChangeListener for a specific property.  The listener
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
     * will be invoked only when a call on firePropertyChange names that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
     * specific property.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
     * The same listener object may be added more than once.  For each
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
     * property,  the listener will be invoked the number of times it was added
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     * for that property.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
     * If <code>propertyName</code> or <code>listener</code> is null, no
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
     * exception is thrown and no action is taken.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
     * @param propertyName  The name of the property to listen on.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
     * @param listener  The PropertyChangeListener to be added
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
    public void addPropertyChangeListener(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
                String propertyName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
                PropertyChangeListener listener) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        if (listener == null || propertyName == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        listener = this.map.extract(listener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
        if (listener != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
            this.map.add(propertyName, listener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
     * Remove a PropertyChangeListener for a specific property.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
     * If <code>listener</code> was added more than once to the same event
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
     * source for the specified property, it will be notified one less time
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
     * after being removed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
     * If <code>propertyName</code> is null,  no exception is thrown and no
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
     * action is taken.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
     * If <code>listener</code> is null, or was never added for the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
     * property, no exception is thrown and no action is taken.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
     * @param propertyName  The name of the property that was listened on.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
     * @param listener  The PropertyChangeListener to be removed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
    public void removePropertyChangeListener(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
                String propertyName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
                PropertyChangeListener listener) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
        if (listener == null || propertyName == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
        listener = this.map.extract(listener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
        if (listener != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
            this.map.remove(propertyName, listener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
     * Returns an array of all the listeners which have been associated
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
     * with the named property.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
     * @param propertyName  The name of the property being listened to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
     * @return all of the <code>PropertyChangeListeners</code> associated with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
     *         the named property.  If no such listeners have been added,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
     *         or if <code>propertyName</code> is null, an empty array is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
     *         returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
    public PropertyChangeListener[] getPropertyChangeListeners(String propertyName) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
        return this.map.getListeners(propertyName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
    /**
1292
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   248
     * Reports a bound property update to listeners
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   249
     * that have been registered to track updates of
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   250
     * all properties or a property with the specified name.
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   251
     * <p>
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   252
     * No event is fired if old and new values are equal and non-null.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
     * This is merely a convenience wrapper around the more general
1292
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   255
     * {@link #firePropertyChange(PropertyChangeEvent)} method.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
     *
1292
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   257
     * @param propertyName  the programmatic name of the property that was changed
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   258
     * @param oldValue      the old value of the property
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   259
     * @param newValue      the new value of the property
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
     */
1292
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   261
    public void firePropertyChange(String propertyName, Object oldValue, Object newValue) {
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   262
        if (oldValue == null || newValue == null || !oldValue.equals(newValue)) {
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   263
            firePropertyChange(new PropertyChangeEvent(this.source, propertyName, oldValue, newValue));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
    /**
1292
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   268
     * Reports an integer bound property update to listeners
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   269
     * that have been registered to track updates of
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   270
     * all properties or a property with the specified name.
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   271
     * <p>
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   272
     * No event is fired if old and new values are equal.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
     * This is merely a convenience wrapper around the more general
1292
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   275
     * {@link #firePropertyChange(String, Object, Object)}  method.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
     *
1292
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   277
     * @param propertyName  the programmatic name of the property that was changed
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   278
     * @param oldValue      the old value of the property
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   279
     * @param newValue      the new value of the property
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
     */
1292
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   281
    public void firePropertyChange(String propertyName, int oldValue, int newValue) {
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   282
        if (oldValue != newValue) {
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   283
            firePropertyChange(propertyName, Integer.valueOf(oldValue), Integer.valueOf(newValue));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
    /**
1292
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   288
     * Reports a boolean bound property update to listeners
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   289
     * that have been registered to track updates of
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   290
     * all properties or a property with the specified name.
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   291
     * <p>
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   292
     * No event is fired if old and new values are equal.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
     * This is merely a convenience wrapper around the more general
1292
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   295
     * {@link #firePropertyChange(String, Object, Object)}  method.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
     *
1292
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   297
     * @param propertyName  the programmatic name of the property that was changed
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   298
     * @param oldValue      the old value of the property
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   299
     * @param newValue      the new value of the property
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
     */
1292
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   301
    public void firePropertyChange(String propertyName, boolean oldValue, boolean newValue) {
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   302
        if (oldValue != newValue) {
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   303
            firePropertyChange(propertyName, Boolean.valueOf(oldValue), Boolean.valueOf(newValue));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
    /**
1292
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   308
     * Fires a property change event to listeners
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   309
     * that have been registered to track updates of
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   310
     * all properties or a property with the specified name.
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   311
     * <p>
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   312
     * No event is fired if the given event's old and new values are equal and non-null.
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   313
     *
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   314
     * @param event  the {@code PropertyChangeEvent} to be fired
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
     */
1292
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   316
    public void firePropertyChange(PropertyChangeEvent event) {
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   317
        Object oldValue = event.getOldValue();
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   318
        Object newValue = event.getNewValue();
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   319
        if (oldValue == null || newValue == null || !oldValue.equals(newValue)) {
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   320
            String name = event.getPropertyName();
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   321
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   322
            PropertyChangeListener[] common = this.map.get(null);
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   323
            PropertyChangeListener[] named = (name != null)
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   324
                        ? this.map.get(name)
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   325
                        : null;
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   326
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   327
            fire(common, event);
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   328
            fire(named, event);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
1292
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   332
    private static void fire(PropertyChangeListener[] listeners, PropertyChangeEvent event) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
        if (listeners != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
            for (PropertyChangeListener listener : listeners) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
                listener.propertyChange(event);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
    /**
1292
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   341
     * Reports a bound indexed property update to listeners
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   342
     * that have been registered to track updates of
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   343
     * all properties or a property with the specified name.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
     * <p>
1292
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   345
     * No event is fired if old and new values are equal and non-null.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
     * This is merely a convenience wrapper around the more general
1292
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   348
     * {@link #firePropertyChange(PropertyChangeEvent)} method.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
     *
1292
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   350
     * @param propertyName  the programmatic name of the property that was changed
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   351
     * @param index         the index of the property element that was changed
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   352
     * @param oldValue      the old value of the property
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   353
     * @param newValue      the new value of the property
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
     */
1292
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   356
    public void fireIndexedPropertyChange(String propertyName, int index, Object oldValue, Object newValue) {
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   357
        if (oldValue == null || newValue == null || !oldValue.equals(newValue)) {
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   358
            firePropertyChange(new IndexedPropertyChangeEvent(source, propertyName, oldValue, newValue, index));
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   359
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
    /**
1292
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   363
     * Reports an integer bound indexed property update to listeners
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   364
     * that have been registered to track updates of
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   365
     * all properties or a property with the specified name.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
     * No event is fired if old and new values are equal.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
     * This is merely a convenience wrapper around the more general
1292
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   370
     * {@link #fireIndexedPropertyChange(String, int, Object, Object)} method.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
     *
1292
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   372
     * @param propertyName  the programmatic name of the property that was changed
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   373
     * @param index         the index of the property element that was changed
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   374
     * @param oldValue      the old value of the property
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   375
     * @param newValue      the new value of the property
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
     */
1292
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   378
    public void fireIndexedPropertyChange(String propertyName, int index, int oldValue, int newValue) {
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   379
        if (oldValue != newValue) {
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   380
            fireIndexedPropertyChange(propertyName, index, Integer.valueOf(oldValue), Integer.valueOf(newValue));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
    /**
1292
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   385
     * Reports a boolean bound indexed property update to listeners
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   386
     * that have been registered to track updates of
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   387
     * all properties or a property with the specified name.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
     * No event is fired if old and new values are equal.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
     * This is merely a convenience wrapper around the more general
1292
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   392
     * {@link #fireIndexedPropertyChange(String, int, Object, Object)} method.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
     *
1292
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   394
     * @param propertyName  the programmatic name of the property that was changed
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   395
     * @param index         the index of the property element that was changed
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   396
     * @param oldValue      the old value of the property
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   397
     * @param newValue      the new value of the property
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
     */
1292
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   400
    public void fireIndexedPropertyChange(String propertyName, int index, boolean oldValue, boolean newValue) {
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   401
        if (oldValue != newValue) {
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   402
            fireIndexedPropertyChange(propertyName, index, Boolean.valueOf(oldValue), Boolean.valueOf(newValue));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
     * Check if there are any listeners for a specific property, including
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
     * those registered on all properties.  If <code>propertyName</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
     * is null, only check for listeners registered on all properties.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
     * @param propertyName  the property name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
     * @return true if there are one or more listeners for the given property
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
    public boolean hasListeners(String propertyName) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
        return this.map.hasListeners(propertyName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
     * @serialData Null terminated list of <code>PropertyChangeListeners</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
     * At serialization time we skip non-serializable listeners and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
     * only serialize the serializable listeners.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
    private void writeObject(ObjectOutputStream s) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
        Hashtable<String, PropertyChangeSupport> children = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
        PropertyChangeListener[] listeners = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
        synchronized (this.map) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
            for (Entry<String, PropertyChangeListener[]> entry : this.map.getEntries()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
                String property = entry.getKey();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
                if (property == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
                    listeners = entry.getValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
                    if (children == null) {
11120
f8576c769572 7116954: Misc warnings in java.beans/java.beans.context
mcimadamore
parents: 5506
diff changeset
   434
                        children = new Hashtable<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
                    PropertyChangeSupport pcs = new PropertyChangeSupport(this.source);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
                    pcs.map.set(null, entry.getValue());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
                    children.put(property, pcs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
        ObjectOutputStream.PutField fields = s.putFields();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
        fields.put("children", children);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
        fields.put("source", this.source);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
        fields.put("propertyChangeSupportSerializedDataVersion", 2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
        s.writeFields();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
        if (listeners != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
            for (PropertyChangeListener l : listeners) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
                if (l instanceof Serializable) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
                    s.writeObject(l);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
        s.writeObject(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
    private void readObject(ObjectInputStream s) throws ClassNotFoundException, IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
        this.map = new PropertyChangeListenerMap();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
        ObjectInputStream.GetField fields = s.readFields();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
11120
f8576c769572 7116954: Misc warnings in java.beans/java.beans.context
mcimadamore
parents: 5506
diff changeset
   463
        @SuppressWarnings("unchecked")
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
        Hashtable<String, PropertyChangeSupport> children = (Hashtable<String, PropertyChangeSupport>) fields.get("children", null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
        this.source = fields.get("source", null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
        fields.get("propertyChangeSupportSerializedDataVersion", 2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
        Object listenerOrNull;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
        while (null != (listenerOrNull = s.readObject())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
            this.map.add(null, (PropertyChangeListener)listenerOrNull);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
        if (children != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
            for (Entry<String, PropertyChangeSupport> entry : children.entrySet()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
                for (PropertyChangeListener listener : entry.getValue().getPropertyChangeListeners()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
                    this.map.add(entry.getKey(), listener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
     * The object to be provided as the "source" for any generated events.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
    private Object source;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
     * @serialField children                                   Hashtable
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
     * @serialField source                                     Object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
     * @serialField propertyChangeSupportSerializedDataVersion int
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
    private static final ObjectStreamField[] serialPersistentFields = {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
            new ObjectStreamField("children", Hashtable.class),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
            new ObjectStreamField("source", Object.class),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
            new ObjectStreamField("propertyChangeSupportSerializedDataVersion", Integer.TYPE)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
    };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
     * Serialization version ID, so we're compatible with JDK 1.1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
    static final long serialVersionUID = 6401253773779951803L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
     * This is a {@link ChangeListenerMap ChangeListenerMap} implementation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
     * that works with {@link PropertyChangeListener PropertyChangeListener} objects.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
    private static final class PropertyChangeListenerMap extends ChangeListenerMap<PropertyChangeListener> {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
        private static final PropertyChangeListener[] EMPTY = {};
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
         * Creates an array of {@link PropertyChangeListener PropertyChangeListener} objects.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
         * This method uses the same instance of the empty array
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
         * when {@code length} equals {@code 0}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
         * @param length  the array length
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
         * @return        an array with specified length
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
        @Override
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
        protected PropertyChangeListener[] newArray(int length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
            return (0 < length)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
                    ? new PropertyChangeListener[length]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
                    : EMPTY;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
         * Creates a {@link PropertyChangeListenerProxy PropertyChangeListenerProxy}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
         * object for the specified property.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
         * @param name      the name of the property to listen on
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
         * @param listener  the listener to process events
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
         * @return          a {@code PropertyChangeListenerProxy} object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
        @Override
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
        protected PropertyChangeListener newProxy(String name, PropertyChangeListener listener) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
            return new PropertyChangeListenerProxy(name, listener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
}