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