jdk/src/java.desktop/share/classes/java/beans/VetoableChangeSupport.java
author serb
Sat, 18 Jul 2015 15:09:28 +0300
changeset 32109 b89c59200379
parent 26037 508779ce6619
child 35667 ed476aba94de
permissions -rw-r--r--
8130937: Several methods in BeanProperty return null instead of boolean value Reviewed-by: alexsch
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
19213
c360667a0da2 8022406: Fix doclint issues in java.beans
darcy
parents: 12032
diff changeset
     2
 * Copyright (c) 1996, 2013, 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:
19213
c360667a0da2 8022406: Fix doclint issues in java.beans
darcy
parents: 12032
diff changeset
    45
 * <pre>{@code
1307
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
 * }
19213
c360667a0da2 8022406: Fix doclint issues in java.beans
darcy
parents: 12032
diff changeset
    71
 * }</pre>
1307
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
25130
adfaa02ea516 8044855: Add missing @since tag under java.beans.*
henryjen
parents: 19213
diff changeset
    80
 * @since 1.1
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
public class VetoableChangeSupport implements Serializable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    private VetoableChangeListenerMap map = new VetoableChangeListenerMap();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     * Constructs a <code>VetoableChangeSupport</code> object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     * @param sourceBean  The bean to be given as the source for any events.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    public VetoableChangeSupport(Object sourceBean) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        if (sourceBean == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
            throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        source = sourceBean;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     * Add a VetoableChangeListener to the listener list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     * The listener is registered for all properties.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     * The same listener object may be added more than once, and will be called
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     * as many times as it is added.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     * If <code>listener</code> is null, no exception is thrown and no action
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     * is taken.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     * @param listener  The VetoableChangeListener to be added
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    public void addVetoableChangeListener(VetoableChangeListener listener) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        if (listener == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        if (listener instanceof VetoableChangeListenerProxy) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
            VetoableChangeListenerProxy proxy =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
                    (VetoableChangeListenerProxy)listener;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
            // Call two argument add method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
            addVetoableChangeListener(proxy.getPropertyName(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
                                      proxy.getListener());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
            this.map.add(null, listener);
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
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     * Remove a VetoableChangeListener from the listener list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     * This removes a VetoableChangeListener that was registered
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     * for all properties.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     * If <code>listener</code> was added more than once to the same event
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     * source, it will be notified one less time after being removed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     * If <code>listener</code> is null, or was never added, no exception is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     * thrown and no action is taken.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     * @param listener  The VetoableChangeListener to be removed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
    public void removeVetoableChangeListener(VetoableChangeListener listener) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        if (listener == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        if (listener instanceof VetoableChangeListenerProxy) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
            VetoableChangeListenerProxy proxy =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
                    (VetoableChangeListenerProxy)listener;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
            // Call two argument remove method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
            removeVetoableChangeListener(proxy.getPropertyName(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
                                         proxy.getListener());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
            this.map.remove(null, listener);
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
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     * Returns an array of all the listeners that were added to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     * VetoableChangeSupport object with addVetoableChangeListener().
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     * If some listeners have been added with a named property, then
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     * the returned array will be a mixture of VetoableChangeListeners
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     * and <code>VetoableChangeListenerProxy</code>s. If the calling
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     * method is interested in distinguishing the listeners then it must
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     * test each element to see if it's a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     * <code>VetoableChangeListenerProxy</code>, perform the cast, and examine
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     * the parameter.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     *
19213
c360667a0da2 8022406: Fix doclint issues in java.beans
darcy
parents: 12032
diff changeset
   160
     * <pre>{@code
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     * VetoableChangeListener[] listeners = bean.getVetoableChangeListeners();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     * for (int i = 0; i < listeners.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     *        if (listeners[i] instanceof VetoableChangeListenerProxy) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     *     VetoableChangeListenerProxy proxy =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     *                    (VetoableChangeListenerProxy)listeners[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     *     if (proxy.getPropertyName().equals("foo")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     *       // proxy is a VetoableChangeListener which was associated
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     *       // with the property named "foo"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     *     }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     *   }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     * }
19213
c360667a0da2 8022406: Fix doclint issues in java.beans
darcy
parents: 12032
diff changeset
   172
     * }</pre>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     * @see VetoableChangeListenerProxy
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     * @return all of the <code>VetoableChangeListeners</code> added or an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     *         empty array if no listeners have been added
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
    public VetoableChangeListener[] getVetoableChangeListeners(){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        return this.map.getListeners();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
     * Add a VetoableChangeListener for a specific property.  The listener
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
     * will be invoked only when a call on fireVetoableChange names that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
     * specific property.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
     * The same listener object may be added more than once.  For each
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     * property,  the listener will be invoked the number of times it was added
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
     * for that property.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
     * If <code>propertyName</code> or <code>listener</code> is null, no
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
     * exception is thrown and no action is taken.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
     * @param propertyName  The name of the property to listen on.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
     * @param listener  The VetoableChangeListener to be added
25130
adfaa02ea516 8044855: Add missing @since tag under java.beans.*
henryjen
parents: 19213
diff changeset
   195
     * @since 1.2
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
    public void addVetoableChangeListener(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
                                String propertyName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
                VetoableChangeListener listener) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        if (listener == null || propertyName == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        listener = this.map.extract(listener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        if (listener != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
            this.map.add(propertyName, listener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
     * Remove a VetoableChangeListener for a specific property.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
     * If <code>listener</code> was added more than once to the same event
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
     * source for the specified property, it will be notified one less time
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
     * after being removed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
     * If <code>propertyName</code> is null, no exception is thrown and no
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
     * action is taken.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
     * If <code>listener</code> is null, or was never added for the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
     * property, no exception is thrown and no action is taken.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
     * @param propertyName  The name of the property that was listened on.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
     * @param listener  The VetoableChangeListener to be removed
25130
adfaa02ea516 8044855: Add missing @since tag under java.beans.*
henryjen
parents: 19213
diff changeset
   221
     * @since 1.2
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
    public void removeVetoableChangeListener(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
                                String propertyName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
                VetoableChangeListener listener) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
        if (listener == null || propertyName == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        listener = this.map.extract(listener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
        if (listener != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
            this.map.remove(propertyName, listener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
     * Returns an array of all the listeners which have been associated
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
     * with the named property.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
     * @param propertyName  The name of the property being listened to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
     * @return all the <code>VetoableChangeListeners</code> associated with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
     *         the named property.  If no such listeners have been added,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
     *         or if <code>propertyName</code> is null, an empty array is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
     *         returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
    public VetoableChangeListener[] getVetoableChangeListeners(String propertyName) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
        return this.map.getListeners(propertyName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
    /**
1292
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   251
     * Reports a constrained property update to listeners
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   252
     * that have been registered to track updates of
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   253
     * all properties or a property with the specified name.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
     * <p>
1292
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   255
     * 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
   256
     * 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
   257
     * 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
   258
     * to all listeners that already confirmed this update
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   259
     * and throws the {@code PropertyVetoException} again.
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   260
     * <p>
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   261
     * 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
   262
     * <p>
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   263
     * 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
   264
     * {@link #fireVetoableChange(PropertyChangeEvent)} method.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
     *
1292
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   266
     * @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
   267
     * @param oldValue      the old value of the property
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   268
     * @param newValue      the new value of the property
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   269
     * @throws PropertyVetoException if one of listeners vetoes the property update
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
     */
1292
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   271
    public void fireVetoableChange(String propertyName, Object oldValue, Object newValue)
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   272
            throws PropertyVetoException {
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   273
        if (oldValue == null || newValue == null || !oldValue.equals(newValue)) {
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   274
            fireVetoableChange(new PropertyChangeEvent(this.source, propertyName, oldValue, newValue));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
    /**
1292
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   279
     * Reports an integer constrained property update to listeners
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   280
     * that have been registered to track updates of
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   281
     * all properties or a property with the specified name.
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   282
     * <p>
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   283
     * 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
   284
     * 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
   285
     * 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
   286
     * to all listeners that already confirmed this update
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   287
     * and throws the {@code PropertyVetoException} again.
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   288
     * <p>
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   289
     * No event is fired if old and new values are equal.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
     * 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
   292
     * {@link #fireVetoableChange(String, Object, Object)} method.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
     *
1292
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   294
     * @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
   295
     * @param oldValue      the old value of the property
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   296
     * @param newValue      the new value of the property
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   297
     * @throws PropertyVetoException if one of listeners vetoes the property update
25130
adfaa02ea516 8044855: Add missing @since tag under java.beans.*
henryjen
parents: 19213
diff changeset
   298
     * @since 1.2
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
     */
1292
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   300
    public void fireVetoableChange(String propertyName, int oldValue, int newValue)
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   301
            throws PropertyVetoException {
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   302
        if (oldValue != newValue) {
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   303
            fireVetoableChange(propertyName, Integer.valueOf(oldValue), Integer.valueOf(newValue));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
    /**
1292
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   308
     * Reports a boolean constrained property update to listeners
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   309
     * that have been registered to track updates of
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   310
     * all properties or a property with the specified name.
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   311
     * <p>
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   312
     * 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
   313
     * 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
   314
     * 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
   315
     * to all listeners that already confirmed this update
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   316
     * and throws the {@code PropertyVetoException} again.
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   317
     * <p>
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   318
     * No event is fired if old and new values are equal.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
     * 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
   321
     * {@link #fireVetoableChange(String, Object, Object)} method.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
     *
1292
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   323
     * @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
   324
     * @param oldValue      the old value of the property
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   325
     * @param newValue      the new value of the property
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   326
     * @throws PropertyVetoException if one of listeners vetoes the property update
25130
adfaa02ea516 8044855: Add missing @since tag under java.beans.*
henryjen
parents: 19213
diff changeset
   327
     * @since 1.2
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
     */
1292
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   329
    public void fireVetoableChange(String propertyName, boolean oldValue, boolean newValue)
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   330
            throws PropertyVetoException {
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   331
        if (oldValue != newValue) {
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   332
            fireVetoableChange(propertyName, Boolean.valueOf(oldValue), Boolean.valueOf(newValue));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
    /**
1292
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   337
     * Fires a property change event to listeners
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   338
     * that have been registered to track updates of
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   339
     * all properties or a property with the specified name.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
     * <p>
1292
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   341
     * 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
   342
     * 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
   343
     * 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
   344
     * to all listeners that already confirmed this update
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   345
     * and throws the {@code PropertyVetoException} again.
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   346
     * <p>
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   347
     * 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
   348
     *
1292
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   349
     * @param event  the {@code PropertyChangeEvent} to be fired
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   350
     * @throws PropertyVetoException if one of listeners vetoes the property update
25130
adfaa02ea516 8044855: Add missing @since tag under java.beans.*
henryjen
parents: 19213
diff changeset
   351
     * @since 1.2
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
     */
1292
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   353
    public void fireVetoableChange(PropertyChangeEvent event)
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   354
            throws PropertyVetoException {
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   355
        Object oldValue = event.getOldValue();
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   356
        Object newValue = event.getNewValue();
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   357
        if (oldValue == null || newValue == null || !oldValue.equals(newValue)) {
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   358
            String name = event.getPropertyName();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
1292
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   360
            VetoableChangeListener[] common = this.map.get(null);
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   361
            VetoableChangeListener[] named = (name != null)
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   362
                        ? this.map.get(name)
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   363
                        : null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
1292
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   365
            VetoableChangeListener[] listeners;
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   366
            if (common == null) {
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   367
                listeners = named;
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   368
            }
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   369
            else if (named == null) {
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   370
                listeners = common;
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   371
            }
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   372
            else {
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   373
                listeners = new VetoableChangeListener[common.length + named.length];
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   374
                System.arraycopy(common, 0, listeners, 0, common.length);
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   375
                System.arraycopy(named, 0, listeners, common.length, named.length);
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   376
            }
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   377
            if (listeners != null) {
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   378
                int current = 0;
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   379
                try {
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   380
                    while (current < listeners.length) {
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   381
                        listeners[current].vetoableChange(event);
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   382
                        current++;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
                }
1292
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   385
                catch (PropertyVetoException veto) {
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   386
                    event = new PropertyChangeEvent(this.source, name, newValue, oldValue);
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   387
                    for (int i = 0; i < current; i++) {
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   388
                        try {
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   389
                            listeners[i].vetoableChange(event);
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   390
                        }
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   391
                        catch (PropertyVetoException exception) {
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   392
                            // ignore exceptions that occur during rolling back
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   393
                        }
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   394
                    }
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   395
                    throw veto; // rethrow the veto exception
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   396
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
     * Check if there are any listeners for a specific property, including
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
     * those registered on all properties.  If <code>propertyName</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
     * is null, only check for listeners registered on all properties.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
     * @param propertyName  the property name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
     * @return true if there are one or more listeners for the given property
25130
adfaa02ea516 8044855: Add missing @since tag under java.beans.*
henryjen
parents: 19213
diff changeset
   408
     * @since 1.2
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
    public boolean hasListeners(String propertyName) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
        return this.map.hasListeners(propertyName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
     * @serialData Null terminated list of <code>VetoableChangeListeners</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
     * At serialization time we skip non-serializable listeners and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
     * only serialize the serializable listeners.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
    private void writeObject(ObjectOutputStream s) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
        Hashtable<String, VetoableChangeSupport> children = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
        VetoableChangeListener[] listeners = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
        synchronized (this.map) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
            for (Entry<String, VetoableChangeListener[]> entry : this.map.getEntries()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
                String property = entry.getKey();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
                if (property == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
                    listeners = entry.getValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
                    if (children == null) {
11120
f8576c769572 7116954: Misc warnings in java.beans/java.beans.context
mcimadamore
parents: 9035
diff changeset
   430
                        children = new Hashtable<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
                    VetoableChangeSupport vcs = new VetoableChangeSupport(this.source);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
                    vcs.map.set(null, entry.getValue());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
                    children.put(property, vcs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
        ObjectOutputStream.PutField fields = s.putFields();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
        fields.put("children", children);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
        fields.put("source", this.source);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
        fields.put("vetoableChangeSupportSerializedDataVersion", 2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
        s.writeFields();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
        if (listeners != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
            for (VetoableChangeListener l : listeners) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
                if (l instanceof Serializable) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
                    s.writeObject(l);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
        s.writeObject(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
    private void readObject(ObjectInputStream s) throws ClassNotFoundException, IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
        this.map = new VetoableChangeListenerMap();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
        ObjectInputStream.GetField fields = s.readFields();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
11120
f8576c769572 7116954: Misc warnings in java.beans/java.beans.context
mcimadamore
parents: 9035
diff changeset
   459
        @SuppressWarnings("unchecked")
f8576c769572 7116954: Misc warnings in java.beans/java.beans.context
mcimadamore
parents: 9035
diff changeset
   460
        Hashtable<String, VetoableChangeSupport> children = (Hashtable<String, VetoableChangeSupport>)fields.get("children", null);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
        this.source = fields.get("source", null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
        fields.get("vetoableChangeSupportSerializedDataVersion", 2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
        Object listenerOrNull;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
        while (null != (listenerOrNull = s.readObject())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
            this.map.add(null, (VetoableChangeListener)listenerOrNull);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
        if (children != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
            for (Entry<String, VetoableChangeSupport> entry : children.entrySet()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
                for (VetoableChangeListener listener : entry.getValue().getVetoableChangeListeners()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
                    this.map.add(entry.getKey(), listener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
     * The object to be provided as the "source" for any generated events.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
    private Object source;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
     * @serialField children                                   Hashtable
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
     * @serialField source                                     Object
8759
11b06be4fa49 7027043: (doc) Confusing typo at java/beans/VetoableChangeSupport.java
malenkov
parents: 5506
diff changeset
   485
     * @serialField vetoableChangeSupportSerializedDataVersion int
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
    private static final ObjectStreamField[] serialPersistentFields = {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
            new ObjectStreamField("children", Hashtable.class),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
            new ObjectStreamField("source", Object.class),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
            new ObjectStreamField("vetoableChangeSupportSerializedDataVersion", Integer.TYPE)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
    };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
     * Serialization version ID, so we're compatible with JDK 1.1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
    static final long serialVersionUID = -5090210921595982017L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
     * This is a {@link ChangeListenerMap ChangeListenerMap} implementation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
     * that works with {@link VetoableChangeListener VetoableChangeListener} objects.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
    private static final class VetoableChangeListenerMap extends ChangeListenerMap<VetoableChangeListener> {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
        private static final VetoableChangeListener[] EMPTY = {};
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
         * Creates an array of {@link VetoableChangeListener VetoableChangeListener} objects.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
         * This method uses the same instance of the empty array
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
         * when {@code length} equals {@code 0}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
         * @param length  the array length
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
         * @return        an array with specified length
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
        @Override
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
        protected VetoableChangeListener[] newArray(int length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
            return (0 < length)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
                    ? new VetoableChangeListener[length]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
                    : EMPTY;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
         * Creates a {@link VetoableChangeListenerProxy VetoableChangeListenerProxy}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
         * object for the specified property.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
         * @param name      the name of the property to listen on
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
         * @param listener  the listener to process events
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
         * @return          a {@code VetoableChangeListenerProxy} object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
        @Override
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
        protected VetoableChangeListener newProxy(String name, VetoableChangeListener listener) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
            return new VetoableChangeListenerProxy(name, listener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
        }
12032
49ac5a2b1d5c 7148143: PropertyChangeSupport.addPropertyChangeListener can throw ClassCastException
malenkov
parents: 11120
diff changeset
   532
49ac5a2b1d5c 7148143: PropertyChangeSupport.addPropertyChangeListener can throw ClassCastException
malenkov
parents: 11120
diff changeset
   533
        /**
49ac5a2b1d5c 7148143: PropertyChangeSupport.addPropertyChangeListener can throw ClassCastException
malenkov
parents: 11120
diff changeset
   534
         * {@inheritDoc}
49ac5a2b1d5c 7148143: PropertyChangeSupport.addPropertyChangeListener can throw ClassCastException
malenkov
parents: 11120
diff changeset
   535
         */
26004
7507a1b93f67 6521783: Unnecessary final modifier for a method in a final class
serb
parents: 25130
diff changeset
   536
        public VetoableChangeListener extract(VetoableChangeListener listener) {
12032
49ac5a2b1d5c 7148143: PropertyChangeSupport.addPropertyChangeListener can throw ClassCastException
malenkov
parents: 11120
diff changeset
   537
            while (listener instanceof VetoableChangeListenerProxy) {
49ac5a2b1d5c 7148143: PropertyChangeSupport.addPropertyChangeListener can throw ClassCastException
malenkov
parents: 11120
diff changeset
   538
                listener = ((VetoableChangeListenerProxy) listener).getListener();
49ac5a2b1d5c 7148143: PropertyChangeSupport.addPropertyChangeListener can throw ClassCastException
malenkov
parents: 11120
diff changeset
   539
            }
49ac5a2b1d5c 7148143: PropertyChangeSupport.addPropertyChangeListener can throw ClassCastException
malenkov
parents: 11120
diff changeset
   540
            return listener;
49ac5a2b1d5c 7148143: PropertyChangeSupport.addPropertyChangeListener can throw ClassCastException
malenkov
parents: 11120
diff changeset
   541
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
}