jdk/src/share/classes/java/beans/VetoableChangeSupport.java
author ohair
Wed, 06 Apr 2011 22:06:11 -0700
changeset 9035 1255eb81cc2f
parent 8759 11b06be4fa49
child 11120 f8576c769572
permissions -rw-r--r--
7033660: Update copyright year to 2011 on any files changed in 2011 Reviewed-by: dholmes
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
9035
1255eb81cc2f 7033660: Update copyright year to 2011 on any files changed in 2011
ohair
parents: 8759
diff changeset
     2
 * Copyright (c) 1996, 2011, 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 constrained
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 VetoableChangeListener} 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 VetoableChangeSupport} 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 VetoableChangeSupport vcs = new VetoableChangeSupport(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 addVetoableChangeListener(VetoableChangeListener listener) {
7603aea0ec0e 5026703: RFE: DOC: Are PropertyChangeSupport & VetoableChangeSupport Thread-Safe? --Docs Should Say
malenkov
parents: 1292
diff changeset
    50
 *         this.vcs.addVetoableChangeListener(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 removeVetoableChangeListener(VetoableChangeListener listener) {
7603aea0ec0e 5026703: RFE: DOC: Are PropertyChangeSupport & VetoableChangeSupport Thread-Safe? --Docs Should Say
malenkov
parents: 1292
diff changeset
    54
 *         this.vcs.removeVetoableChangeListener(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) throws PropertyVetoException {
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.vcs.fireVetoableChange("value", oldValue, newValue);
7603aea0ec0e 5026703: RFE: DOC: Are PropertyChangeSupport & VetoableChangeSupport Thread-Safe? --Docs Should Say
malenkov
parents: 1292
diff changeset
    66
 *         this.value = 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 VetoableChangeSupport} 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 PropertyChangeSupport
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
public class VetoableChangeSupport implements Serializable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    private VetoableChangeListenerMap map = new VetoableChangeListenerMap();
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>VetoableChangeSupport</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 VetoableChangeSupport(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 VetoableChangeListener 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 VetoableChangeListener to be added
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    public void addVetoableChangeListener(VetoableChangeListener 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 VetoableChangeListenerProxy) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
            VetoableChangeListenerProxy proxy =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
                    (VetoableChangeListenerProxy)listener;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
            // Call two argument add method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
            addVetoableChangeListener(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 VetoableChangeListener from the listener list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     * This removes a VetoableChangeListener 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 VetoableChangeListener to be removed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    public void removeVetoableChangeListener(VetoableChangeListener 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 VetoableChangeListenerProxy) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
            VetoableChangeListenerProxy proxy =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
                    (VetoableChangeListenerProxy)listener;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
            // Call two argument remove method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
            removeVetoableChangeListener(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
     * VetoableChangeSupport object with addVetoableChangeListener().
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 VetoableChangeListeners
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     * and <code>VetoableChangeListenerProxy</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>VetoableChangeListenerProxy</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
     * VetoableChangeListener[] listeners = bean.getVetoableChangeListeners();
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 VetoableChangeListenerProxy) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     *     VetoableChangeListenerProxy proxy =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     *                    (VetoableChangeListenerProxy)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 VetoableChangeListener 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 VetoableChangeListenerProxy
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     * @return all of the <code>VetoableChangeListeners</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 VetoableChangeListener[] getVetoableChangeListeners(){
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 VetoableChangeListener for a specific property.  The listener
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
     * will be invoked only when a call on fireVetoableChange 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 VetoableChangeListener to be added
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
    public void addVetoableChangeListener(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
                                String propertyName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
                VetoableChangeListener 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 VetoableChangeListener 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 VetoableChangeListener to be removed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
    public void removeVetoableChangeListener(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
                                String propertyName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
                VetoableChangeListener 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 the <code>VetoableChangeListeners</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 VetoableChangeListener[] getVetoableChangeListeners(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 constrained 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.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
     * <p>
1292
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   252
     * Any listener can throw a {@code PropertyVetoException} to veto the update.
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   253
     * If one of the listeners vetoes the update, this method passes
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   254
     * a new "undo" {@code PropertyChangeEvent} that reverts to the old value
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   255
     * to all listeners that already confirmed this update
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   256
     * and throws the {@code PropertyVetoException} again.
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   257
     * <p>
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   258
     * No event is fired if old and new values are equal and non-null.
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   259
     * <p>
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   260
     * This is merely a convenience wrapper around the more general
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   261
     * {@link #fireVetoableChange(PropertyChangeEvent)} method.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
     *
1292
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   263
     * @param propertyName  the programmatic name of the property that is about to change
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   264
     * @param oldValue      the old value of the property
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   265
     * @param newValue      the new value of the property
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   266
     * @throws PropertyVetoException if one of listeners vetoes the property update
2
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
    public void fireVetoableChange(String propertyName, Object oldValue, Object newValue)
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   269
            throws PropertyVetoException {
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   270
        if (oldValue == null || newValue == null || !oldValue.equals(newValue)) {
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   271
            fireVetoableChange(new PropertyChangeEvent(this.source, propertyName, oldValue, newValue));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
    /**
1292
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   276
     * Reports an integer constrained property update to listeners
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   277
     * that have been registered to track updates of
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   278
     * all properties or a property with the specified name.
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   279
     * <p>
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   280
     * Any listener can throw a {@code PropertyVetoException} to veto the update.
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   281
     * If one of the listeners vetoes the update, this method passes
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   282
     * a new "undo" {@code PropertyChangeEvent} that reverts to the old value
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   283
     * to all listeners that already confirmed this update
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   284
     * and throws the {@code PropertyVetoException} again.
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   285
     * <p>
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   286
     * No event is fired if old and new values are equal.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
     * 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
   289
     * {@link #fireVetoableChange(String, Object, Object)} method.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
     *
1292
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   291
     * @param propertyName  the programmatic name of the property that is about to change
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   292
     * @param oldValue      the old value of the property
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   293
     * @param newValue      the new value of the property
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   294
     * @throws PropertyVetoException if one of listeners vetoes the property update
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
     */
1292
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   296
    public void fireVetoableChange(String propertyName, int oldValue, int newValue)
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   297
            throws PropertyVetoException {
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   298
        if (oldValue != newValue) {
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   299
            fireVetoableChange(propertyName, Integer.valueOf(oldValue), Integer.valueOf(newValue));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
    /**
1292
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   304
     * Reports a boolean constrained property update to listeners
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   305
     * that have been registered to track updates of
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   306
     * all properties or a property with the specified name.
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   307
     * <p>
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   308
     * Any listener can throw a {@code PropertyVetoException} to veto the update.
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   309
     * If one of the listeners vetoes the update, this method passes
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   310
     * a new "undo" {@code PropertyChangeEvent} that reverts to the old value
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   311
     * to all listeners that already confirmed this update
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   312
     * and throws the {@code PropertyVetoException} again.
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   313
     * <p>
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   314
     * No event is fired if old and new values are equal.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
     * 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
   317
     * {@link #fireVetoableChange(String, Object, Object)} method.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
     *
1292
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   319
     * @param propertyName  the programmatic name of the property that is about to change
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   320
     * @param oldValue      the old value of the property
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   321
     * @param newValue      the new value of the property
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   322
     * @throws PropertyVetoException if one of listeners vetoes the property update
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
     */
1292
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   324
    public void fireVetoableChange(String propertyName, boolean oldValue, boolean newValue)
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   325
            throws PropertyVetoException {
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   326
        if (oldValue != newValue) {
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   327
            fireVetoableChange(propertyName, Boolean.valueOf(oldValue), Boolean.valueOf(newValue));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
        }
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
     * Fires a property change event to listeners
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   333
     * that have been registered to track updates of
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   334
     * all properties or a property with the specified name.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
     * <p>
1292
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   336
     * Any listener can throw a {@code PropertyVetoException} to veto the update.
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   337
     * If one of the listeners vetoes the update, this method passes
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   338
     * a new "undo" {@code PropertyChangeEvent} that reverts to the old value
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   339
     * to all listeners that already confirmed this update
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   340
     * and throws the {@code PropertyVetoException} again.
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   341
     * <p>
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   342
     * No event is fired if the given event's old and new values are equal and non-null.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
     *
1292
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   344
     * @param event  the {@code PropertyChangeEvent} to be fired
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   345
     * @throws PropertyVetoException if one of listeners vetoes the property update
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
     */
1292
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   347
    public void fireVetoableChange(PropertyChangeEvent event)
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   348
            throws PropertyVetoException {
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   349
        Object oldValue = event.getOldValue();
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   350
        Object newValue = event.getNewValue();
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   351
        if (oldValue == null || newValue == null || !oldValue.equals(newValue)) {
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   352
            String name = event.getPropertyName();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
1292
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   354
            VetoableChangeListener[] common = this.map.get(null);
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   355
            VetoableChangeListener[] named = (name != null)
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   356
                        ? this.map.get(name)
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   357
                        : null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
1292
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   359
            VetoableChangeListener[] listeners;
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   360
            if (common == null) {
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   361
                listeners = named;
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   362
            }
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   363
            else if (named == null) {
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   364
                listeners = common;
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   365
            }
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   366
            else {
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   367
                listeners = new VetoableChangeListener[common.length + named.length];
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   368
                System.arraycopy(common, 0, listeners, 0, common.length);
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   369
                System.arraycopy(named, 0, listeners, common.length, named.length);
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   370
            }
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   371
            if (listeners != null) {
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   372
                int current = 0;
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   373
                try {
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   374
                    while (current < listeners.length) {
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   375
                        listeners[current].vetoableChange(event);
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   376
                        current++;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
                }
1292
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   379
                catch (PropertyVetoException veto) {
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   380
                    event = new PropertyChangeEvent(this.source, name, newValue, oldValue);
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   381
                    for (int i = 0; i < current; i++) {
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   382
                        try {
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   383
                            listeners[i].vetoableChange(event);
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   384
                        }
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   385
                        catch (PropertyVetoException exception) {
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   386
                            // ignore exceptions that occur during rolling back
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   387
                        }
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   388
                    }
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   389
                    throw veto; // rethrow the veto exception
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   390
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
     * Check if there are any listeners for a specific property, including
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
     * those registered on all properties.  If <code>propertyName</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
     * is null, only check for listeners registered on all properties.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
     * @param propertyName  the property name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
     * @return true if there are one or more listeners for the given property
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
    public boolean hasListeners(String propertyName) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
        return this.map.hasListeners(propertyName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
     * @serialData Null terminated list of <code>VetoableChangeListeners</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
     * At serialization time we skip non-serializable listeners and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
     * only serialize the serializable listeners.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
    private void writeObject(ObjectOutputStream s) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
        Hashtable<String, VetoableChangeSupport> children = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
        VetoableChangeListener[] listeners = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
        synchronized (this.map) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
            for (Entry<String, VetoableChangeListener[]> entry : this.map.getEntries()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
                String property = entry.getKey();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
                if (property == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
                    listeners = entry.getValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
                    if (children == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
                        children = new Hashtable<String, VetoableChangeSupport>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
                    VetoableChangeSupport vcs = new VetoableChangeSupport(this.source);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
                    vcs.map.set(null, entry.getValue());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
                    children.put(property, vcs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
        ObjectOutputStream.PutField fields = s.putFields();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
        fields.put("children", children);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
        fields.put("source", this.source);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
        fields.put("vetoableChangeSupportSerializedDataVersion", 2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
        s.writeFields();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
        if (listeners != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
            for (VetoableChangeListener l : listeners) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
                if (l instanceof Serializable) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
                    s.writeObject(l);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
        s.writeObject(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
    private void readObject(ObjectInputStream s) throws ClassNotFoundException, IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
        this.map = new VetoableChangeListenerMap();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
        ObjectInputStream.GetField fields = s.readFields();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
        Hashtable<String, VetoableChangeSupport> children = (Hashtable<String, VetoableChangeSupport>) fields.get("children", null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
        this.source = fields.get("source", null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
        fields.get("vetoableChangeSupportSerializedDataVersion", 2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
        Object listenerOrNull;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
        while (null != (listenerOrNull = s.readObject())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
            this.map.add(null, (VetoableChangeListener)listenerOrNull);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
        if (children != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
            for (Entry<String, VetoableChangeSupport> entry : children.entrySet()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
                for (VetoableChangeListener listener : entry.getValue().getVetoableChangeListeners()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
                    this.map.add(entry.getKey(), listener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
     * The object to be provided as the "source" for any generated events.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
    private Object source;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
     * @serialField children                                   Hashtable
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
     * @serialField source                                     Object
8759
11b06be4fa49 7027043: (doc) Confusing typo at java/beans/VetoableChangeSupport.java
malenkov
parents: 5506
diff changeset
   477
     * @serialField vetoableChangeSupportSerializedDataVersion int
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
    private static final ObjectStreamField[] serialPersistentFields = {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
            new ObjectStreamField("children", Hashtable.class),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
            new ObjectStreamField("source", Object.class),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
            new ObjectStreamField("vetoableChangeSupportSerializedDataVersion", Integer.TYPE)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
    };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
     * Serialization version ID, so we're compatible with JDK 1.1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
    static final long serialVersionUID = -5090210921595982017L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
     * This is a {@link ChangeListenerMap ChangeListenerMap} implementation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
     * that works with {@link VetoableChangeListener VetoableChangeListener} objects.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
    private static final class VetoableChangeListenerMap extends ChangeListenerMap<VetoableChangeListener> {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
        private static final VetoableChangeListener[] EMPTY = {};
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
         * Creates an array of {@link VetoableChangeListener VetoableChangeListener} objects.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
         * This method uses the same instance of the empty array
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
         * when {@code length} equals {@code 0}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
         * @param length  the array length
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
         * @return        an array with specified length
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
        @Override
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
        protected VetoableChangeListener[] newArray(int length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
            return (0 < length)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
                    ? new VetoableChangeListener[length]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
                    : EMPTY;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
         * Creates a {@link VetoableChangeListenerProxy VetoableChangeListenerProxy}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
         * object for the specified property.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
         * @param name      the name of the property to listen on
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
         * @param listener  the listener to process events
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
         * @return          a {@code VetoableChangeListenerProxy} object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
        @Override
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
        protected VetoableChangeListener newProxy(String name, VetoableChangeListener listener) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
            return new VetoableChangeListenerProxy(name, listener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
}