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