jdk/src/java.desktop/share/classes/java/beans/PropertyChangeSupport.java
author serb
Fri, 12 Feb 2016 16:09:39 +0300
changeset 36451 0f5d1613d67d
parent 35667 ed476aba94de
permissions -rw-r--r--
8136382: SimpleBeanInfo.loadImage succeeds when running with a security manager Reviewed-by: alanb
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 bound
1307
7603aea0ec0e 5026703: RFE: DOC: Are PropertyChangeSupport & VetoableChangeSupport Thread-Safe? --Docs Should Say
malenkov
parents: 1292
diff changeset
    37
 * properties.  It manages a list of listeners and dispatches
7603aea0ec0e 5026703: RFE: DOC: Are PropertyChangeSupport & VetoableChangeSupport Thread-Safe? --Docs Should Say
malenkov
parents: 1292
diff changeset
    38
 * {@link PropertyChangeEvent}s to them.  You can use an instance of this class
7603aea0ec0e 5026703: RFE: DOC: Are PropertyChangeSupport & VetoableChangeSupport Thread-Safe? --Docs Should Say
malenkov
parents: 1292
diff changeset
    39
 * as a member field of your bean and delegate these types of work to it.
7603aea0ec0e 5026703: RFE: DOC: Are PropertyChangeSupport & VetoableChangeSupport Thread-Safe? --Docs Should Say
malenkov
parents: 1292
diff changeset
    40
 * The {@link PropertyChangeListener} can be registered for all properties
7603aea0ec0e 5026703: RFE: DOC: Are PropertyChangeSupport & VetoableChangeSupport Thread-Safe? --Docs Should Say
malenkov
parents: 1292
diff changeset
    41
 * or for a property specified by name.
7603aea0ec0e 5026703: RFE: DOC: Are PropertyChangeSupport & VetoableChangeSupport Thread-Safe? --Docs Should Say
malenkov
parents: 1292
diff changeset
    42
 * <p>
7603aea0ec0e 5026703: RFE: DOC: Are PropertyChangeSupport & VetoableChangeSupport Thread-Safe? --Docs Should Say
malenkov
parents: 1292
diff changeset
    43
 * Here is an example of {@code PropertyChangeSupport} usage that follows
7603aea0ec0e 5026703: RFE: DOC: Are PropertyChangeSupport & VetoableChangeSupport Thread-Safe? --Docs Should Say
malenkov
parents: 1292
diff changeset
    44
 * the rules and recommendations laid out in the JavaBeans&trade; specification:
7603aea0ec0e 5026703: RFE: DOC: Are PropertyChangeSupport & VetoableChangeSupport Thread-Safe? --Docs Should Say
malenkov
parents: 1292
diff changeset
    45
 * <pre>
7603aea0ec0e 5026703: RFE: DOC: Are PropertyChangeSupport & VetoableChangeSupport Thread-Safe? --Docs Should Say
malenkov
parents: 1292
diff changeset
    46
 * public class MyBean {
7603aea0ec0e 5026703: RFE: DOC: Are PropertyChangeSupport & VetoableChangeSupport Thread-Safe? --Docs Should Say
malenkov
parents: 1292
diff changeset
    47
 *     private final PropertyChangeSupport pcs = new PropertyChangeSupport(this);
7603aea0ec0e 5026703: RFE: DOC: Are PropertyChangeSupport & VetoableChangeSupport Thread-Safe? --Docs Should Say
malenkov
parents: 1292
diff changeset
    48
 *
7603aea0ec0e 5026703: RFE: DOC: Are PropertyChangeSupport & VetoableChangeSupport Thread-Safe? --Docs Should Say
malenkov
parents: 1292
diff changeset
    49
 *     public void addPropertyChangeListener(PropertyChangeListener listener) {
7603aea0ec0e 5026703: RFE: DOC: Are PropertyChangeSupport & VetoableChangeSupport Thread-Safe? --Docs Should Say
malenkov
parents: 1292
diff changeset
    50
 *         this.pcs.addPropertyChangeListener(listener);
7603aea0ec0e 5026703: RFE: DOC: Are PropertyChangeSupport & VetoableChangeSupport Thread-Safe? --Docs Should Say
malenkov
parents: 1292
diff changeset
    51
 *     }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 *
1307
7603aea0ec0e 5026703: RFE: DOC: Are PropertyChangeSupport & VetoableChangeSupport Thread-Safe? --Docs Should Say
malenkov
parents: 1292
diff changeset
    53
 *     public void removePropertyChangeListener(PropertyChangeListener listener) {
7603aea0ec0e 5026703: RFE: DOC: Are PropertyChangeSupport & VetoableChangeSupport Thread-Safe? --Docs Should Say
malenkov
parents: 1292
diff changeset
    54
 *         this.pcs.removePropertyChangeListener(listener);
7603aea0ec0e 5026703: RFE: DOC: Are PropertyChangeSupport & VetoableChangeSupport Thread-Safe? --Docs Should Say
malenkov
parents: 1292
diff changeset
    55
 *     }
7603aea0ec0e 5026703: RFE: DOC: Are PropertyChangeSupport & VetoableChangeSupport Thread-Safe? --Docs Should Say
malenkov
parents: 1292
diff changeset
    56
 *
7603aea0ec0e 5026703: RFE: DOC: Are PropertyChangeSupport & VetoableChangeSupport Thread-Safe? --Docs Should Say
malenkov
parents: 1292
diff changeset
    57
 *     private String value;
7603aea0ec0e 5026703: RFE: DOC: Are PropertyChangeSupport & VetoableChangeSupport Thread-Safe? --Docs Should Say
malenkov
parents: 1292
diff changeset
    58
 *
7603aea0ec0e 5026703: RFE: DOC: Are PropertyChangeSupport & VetoableChangeSupport Thread-Safe? --Docs Should Say
malenkov
parents: 1292
diff changeset
    59
 *     public String getValue() {
7603aea0ec0e 5026703: RFE: DOC: Are PropertyChangeSupport & VetoableChangeSupport Thread-Safe? --Docs Should Say
malenkov
parents: 1292
diff changeset
    60
 *         return this.value;
7603aea0ec0e 5026703: RFE: DOC: Are PropertyChangeSupport & VetoableChangeSupport Thread-Safe? --Docs Should Say
malenkov
parents: 1292
diff changeset
    61
 *     }
7603aea0ec0e 5026703: RFE: DOC: Are PropertyChangeSupport & VetoableChangeSupport Thread-Safe? --Docs Should Say
malenkov
parents: 1292
diff changeset
    62
 *
7603aea0ec0e 5026703: RFE: DOC: Are PropertyChangeSupport & VetoableChangeSupport Thread-Safe? --Docs Should Say
malenkov
parents: 1292
diff changeset
    63
 *     public void setValue(String newValue) {
7603aea0ec0e 5026703: RFE: DOC: Are PropertyChangeSupport & VetoableChangeSupport Thread-Safe? --Docs Should Say
malenkov
parents: 1292
diff changeset
    64
 *         String oldValue = this.value;
7603aea0ec0e 5026703: RFE: DOC: Are PropertyChangeSupport & VetoableChangeSupport Thread-Safe? --Docs Should Say
malenkov
parents: 1292
diff changeset
    65
 *         this.value = newValue;
7603aea0ec0e 5026703: RFE: DOC: Are PropertyChangeSupport & VetoableChangeSupport Thread-Safe? --Docs Should Say
malenkov
parents: 1292
diff changeset
    66
 *         this.pcs.firePropertyChange("value", oldValue, newValue);
7603aea0ec0e 5026703: RFE: DOC: Are PropertyChangeSupport & VetoableChangeSupport Thread-Safe? --Docs Should Say
malenkov
parents: 1292
diff changeset
    67
 *     }
7603aea0ec0e 5026703: RFE: DOC: Are PropertyChangeSupport & VetoableChangeSupport Thread-Safe? --Docs Should Say
malenkov
parents: 1292
diff changeset
    68
 *
7603aea0ec0e 5026703: RFE: DOC: Are PropertyChangeSupport & VetoableChangeSupport Thread-Safe? --Docs Should Say
malenkov
parents: 1292
diff changeset
    69
 *     [...]
7603aea0ec0e 5026703: RFE: DOC: Are PropertyChangeSupport & VetoableChangeSupport Thread-Safe? --Docs Should Say
malenkov
parents: 1292
diff changeset
    70
 * }
7603aea0ec0e 5026703: RFE: DOC: Are PropertyChangeSupport & VetoableChangeSupport Thread-Safe? --Docs Should Say
malenkov
parents: 1292
diff changeset
    71
 * </pre>
7603aea0ec0e 5026703: RFE: DOC: Are PropertyChangeSupport & VetoableChangeSupport Thread-Safe? --Docs Should Say
malenkov
parents: 1292
diff changeset
    72
 * <p>
7603aea0ec0e 5026703: RFE: DOC: Are PropertyChangeSupport & VetoableChangeSupport Thread-Safe? --Docs Should Say
malenkov
parents: 1292
diff changeset
    73
 * A {@code PropertyChangeSupport} instance is thread-safe.
7603aea0ec0e 5026703: RFE: DOC: Are PropertyChangeSupport & VetoableChangeSupport Thread-Safe? --Docs Should Say
malenkov
parents: 1292
diff changeset
    74
 * <p>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
 * This class is serializable.  When it is serialized it will save
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
 * (and restore) any listeners that are themselves serializable.  Any
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
 * non-serializable listeners will be skipped during serialization.
1307
7603aea0ec0e 5026703: RFE: DOC: Are PropertyChangeSupport & VetoableChangeSupport Thread-Safe? --Docs Should Say
malenkov
parents: 1292
diff changeset
    78
 *
7603aea0ec0e 5026703: RFE: DOC: Are PropertyChangeSupport & VetoableChangeSupport Thread-Safe? --Docs Should Say
malenkov
parents: 1292
diff changeset
    79
 * @see VetoableChangeSupport
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 PropertyChangeSupport implements Serializable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    private PropertyChangeListenerMap map = new PropertyChangeListenerMap();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    /**
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 26037
diff changeset
    86
     * Constructs a {@code PropertyChangeSupport} object.
2
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 PropertyChangeSupport(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 PropertyChangeListener 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.
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 26037
diff changeset
   102
     * If {@code listener} is null, no exception is thrown and no action
2
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 PropertyChangeListener to be added
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    public void addPropertyChangeListener(PropertyChangeListener 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 PropertyChangeListenerProxy) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
            PropertyChangeListenerProxy proxy =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
                   (PropertyChangeListenerProxy)listener;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
            // Call two argument add method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
            addPropertyChangeListener(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 PropertyChangeListener from the listener list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     * This removes a PropertyChangeListener that was registered
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     * for all properties.
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 26037
diff changeset
   126
     * If {@code listener} was added more than once to the same event
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     * source, it will be notified one less time after being removed.
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 26037
diff changeset
   128
     * If {@code listener} is null, or was never added, no exception is
2
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 PropertyChangeListener to be removed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
    public void removePropertyChangeListener(PropertyChangeListener 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 PropertyChangeListenerProxy) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
            PropertyChangeListenerProxy proxy =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
                    (PropertyChangeListenerProxy)listener;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
            // Call two argument remove method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
            removePropertyChangeListener(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
     * PropertyChangeSupport object with addPropertyChangeListener().
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 PropertyChangeListeners
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 26037
diff changeset
   154
     * and {@code PropertyChangeListenerProxy}s. If the calling
2
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
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 26037
diff changeset
   157
     * {@code PropertyChangeListenerProxy}, perform the cast, and examine
2
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
     * PropertyChangeListener[] listeners = bean.getPropertyChangeListeners();
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 PropertyChangeListenerProxy) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     *     PropertyChangeListenerProxy proxy =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     *                    (PropertyChangeListenerProxy)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 PropertyChangeListener 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 PropertyChangeListenerProxy
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 26037
diff changeset
   175
     * @return all of the {@code PropertyChangeListeners} added or an
2
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 PropertyChangeListener[] getPropertyChangeListeners() {
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 PropertyChangeListener for a specific property.  The listener
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
     * will be invoked only when a call on firePropertyChange 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.
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 26037
diff changeset
   190
     * If {@code propertyName} or {@code listener} is null, no
2
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 PropertyChangeListener 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 addPropertyChangeListener(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
                String propertyName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
                PropertyChangeListener 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 PropertyChangeListener for a specific property.
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 26037
diff changeset
   211
     * If {@code listener} was added more than once to the same event
2
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.
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 26037
diff changeset
   214
     * If {@code propertyName} is null,  no exception is thrown and no
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
     * action is taken.
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 26037
diff changeset
   216
     * If {@code listener} is null, or was never added for the specified
2
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 PropertyChangeListener 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 removePropertyChangeListener(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
                String propertyName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
                PropertyChangeListener 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
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 26037
diff changeset
   240
     * @return all of the {@code PropertyChangeListeners} associated with
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
     *         the named property.  If no such listeners have been added,
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 26037
diff changeset
   242
     *         or if {@code propertyName} is null, an empty array is
2
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 PropertyChangeListener[] getPropertyChangeListeners(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 bound 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.
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   254
     * <p>
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   255
     * No event is fired if old and new values are equal and non-null.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
     * 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
   258
     * {@link #firePropertyChange(PropertyChangeEvent)} method.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
     *
1292
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   260
     * @param propertyName  the programmatic name of the property that was changed
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   261
     * @param oldValue      the old value of the property
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   262
     * @param newValue      the new value of the property
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
     */
1292
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   264
    public void firePropertyChange(String propertyName, Object oldValue, Object newValue) {
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   265
        if (oldValue == null || newValue == null || !oldValue.equals(newValue)) {
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   266
            firePropertyChange(new PropertyChangeEvent(this.source, propertyName, oldValue, newValue));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
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
     * Reports an integer bound property update to listeners
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   272
     * that have been registered to track updates of
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   273
     * all properties or a property with the specified name.
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   274
     * <p>
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   275
     * No event is fired if old and new values are equal.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
     * 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
   278
     * {@link #firePropertyChange(String, Object, Object)}  method.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
     *
1292
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   280
     * @param propertyName  the programmatic name of the property that was changed
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   281
     * @param oldValue      the old value of the property
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   282
     * @param newValue      the new value of the property
25130
adfaa02ea516 8044855: Add missing @since tag under java.beans.*
henryjen
parents: 19213
diff changeset
   283
     * @since 1.2
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
     */
1292
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   285
    public void firePropertyChange(String propertyName, int oldValue, int newValue) {
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   286
        if (oldValue != newValue) {
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   287
            firePropertyChange(propertyName, Integer.valueOf(oldValue), Integer.valueOf(newValue));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
    /**
1292
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   292
     * Reports a boolean bound property update to listeners
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   293
     * that have been registered to track updates of
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   294
     * all properties or a property with the specified name.
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   295
     * <p>
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   296
     * No event is fired if old and new values are equal.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
     * 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
   299
     * {@link #firePropertyChange(String, Object, Object)}  method.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
     *
1292
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   301
     * @param propertyName  the programmatic name of the property that was changed
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   302
     * @param oldValue      the old value of the property
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   303
     * @param newValue      the new value of the property
25130
adfaa02ea516 8044855: Add missing @since tag under java.beans.*
henryjen
parents: 19213
diff changeset
   304
     * @since 1.2
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
     */
1292
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   306
    public void firePropertyChange(String propertyName, boolean oldValue, boolean newValue) {
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   307
        if (oldValue != newValue) {
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   308
            firePropertyChange(propertyName, Boolean.valueOf(oldValue), Boolean.valueOf(newValue));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
    /**
1292
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   313
     * Fires a property change event to listeners
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   314
     * that have been registered to track updates of
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   315
     * all properties or a property with the specified name.
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   316
     * <p>
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   317
     * No event is fired if the given event's old and new values are equal and non-null.
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   318
     *
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   319
     * @param event  the {@code PropertyChangeEvent} to be fired
25130
adfaa02ea516 8044855: Add missing @since tag under java.beans.*
henryjen
parents: 19213
diff changeset
   320
     * @since 1.2
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
     */
1292
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   322
    public void firePropertyChange(PropertyChangeEvent event) {
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   323
        Object oldValue = event.getOldValue();
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   324
        Object newValue = event.getNewValue();
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   325
        if (oldValue == null || newValue == null || !oldValue.equals(newValue)) {
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   326
            String name = event.getPropertyName();
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   327
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   328
            PropertyChangeListener[] common = this.map.get(null);
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   329
            PropertyChangeListener[] named = (name != null)
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   330
                        ? this.map.get(name)
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   331
                        : null;
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   332
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   333
            fire(common, event);
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   334
            fire(named, event);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
1292
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   338
    private static void fire(PropertyChangeListener[] listeners, PropertyChangeEvent event) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
        if (listeners != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
            for (PropertyChangeListener listener : listeners) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
                listener.propertyChange(event);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
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
     * Reports a bound indexed property update to listeners
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   348
     * that have been registered to track updates of
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   349
     * all properties or a property with the specified name.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
     * <p>
1292
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   351
     * No event is fired if old and new values are equal and non-null.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
     * 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
   354
     * {@link #firePropertyChange(PropertyChangeEvent)} method.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
     *
1292
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   356
     * @param propertyName  the programmatic name of the property that was changed
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   357
     * @param index         the index of the property element that was changed
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   358
     * @param oldValue      the old value of the property
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   359
     * @param newValue      the new value of the property
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
     */
1292
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   362
    public void fireIndexedPropertyChange(String propertyName, int index, Object oldValue, Object newValue) {
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   363
        if (oldValue == null || newValue == null || !oldValue.equals(newValue)) {
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   364
            firePropertyChange(new IndexedPropertyChangeEvent(source, propertyName, oldValue, newValue, index));
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   365
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
    /**
1292
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   369
     * Reports an integer bound indexed property update to listeners
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   370
     * that have been registered to track updates of
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   371
     * all properties or a property with the specified name.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
     * No event is fired if old and new values are equal.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
     * 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
   376
     * {@link #fireIndexedPropertyChange(String, int, Object, Object)} method.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
     *
1292
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   378
     * @param propertyName  the programmatic name of the property that was changed
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   379
     * @param index         the index of the property element that was changed
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   380
     * @param oldValue      the old value of the property
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   381
     * @param newValue      the new value of the property
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
     */
1292
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   384
    public void fireIndexedPropertyChange(String propertyName, int index, int oldValue, int newValue) {
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   385
        if (oldValue != newValue) {
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   386
            fireIndexedPropertyChange(propertyName, index, Integer.valueOf(oldValue), Integer.valueOf(newValue));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
    /**
1292
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   391
     * Reports a boolean bound indexed property update to listeners
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   392
     * that have been registered to track updates of
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   393
     * all properties or a property with the specified name.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
     * No event is fired if old and new values are equal.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
     * 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
   398
     * {@link #fireIndexedPropertyChange(String, int, Object, Object)} method.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
     *
1292
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   400
     * @param propertyName  the programmatic name of the property that was changed
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   401
     * @param index         the index of the property element that was changed
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   402
     * @param oldValue      the old value of the property
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   403
     * @param newValue      the new value of the property
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
     */
1292
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   406
    public void fireIndexedPropertyChange(String propertyName, int index, boolean oldValue, boolean newValue) {
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   407
        if (oldValue != newValue) {
e91af84fbe9f 6630275: The spec on VetoableChangeSupport.fireVetoableChange should be updated
malenkov
parents: 2
diff changeset
   408
            fireIndexedPropertyChange(propertyName, index, Boolean.valueOf(oldValue), Boolean.valueOf(newValue));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
     * Check if there are any listeners for a specific property, including
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 26037
diff changeset
   414
     * those registered on all properties.  If {@code propertyName}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
     * is null, only check for listeners registered on all properties.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
     * @param propertyName  the property name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
     * @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
   419
     * @since 1.2
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
    public boolean hasListeners(String propertyName) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
        return this.map.hasListeners(propertyName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
    /**
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 26037
diff changeset
   426
     * @serialData Null terminated list of {@code PropertyChangeListeners}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
     * At serialization time we skip non-serializable listeners and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
     * only serialize the serializable listeners.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
    private void writeObject(ObjectOutputStream s) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
        Hashtable<String, PropertyChangeSupport> children = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
        PropertyChangeListener[] listeners = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
        synchronized (this.map) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
            for (Entry<String, PropertyChangeListener[]> entry : this.map.getEntries()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
                String property = entry.getKey();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
                if (property == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
                    listeners = entry.getValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
                    if (children == null) {
11120
f8576c769572 7116954: Misc warnings in java.beans/java.beans.context
mcimadamore
parents: 5506
diff changeset
   441
                        children = new Hashtable<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
                    PropertyChangeSupport pcs = new PropertyChangeSupport(this.source);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
                    pcs.map.set(null, entry.getValue());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
                    children.put(property, pcs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
        ObjectOutputStream.PutField fields = s.putFields();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
        fields.put("children", children);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
        fields.put("source", this.source);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
        fields.put("propertyChangeSupportSerializedDataVersion", 2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
        s.writeFields();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
        if (listeners != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
            for (PropertyChangeListener l : listeners) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
                if (l instanceof Serializable) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
                    s.writeObject(l);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
        s.writeObject(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
    private void readObject(ObjectInputStream s) throws ClassNotFoundException, IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
        this.map = new PropertyChangeListenerMap();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
        ObjectInputStream.GetField fields = s.readFields();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
11120
f8576c769572 7116954: Misc warnings in java.beans/java.beans.context
mcimadamore
parents: 5506
diff changeset
   470
        @SuppressWarnings("unchecked")
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
        Hashtable<String, PropertyChangeSupport> children = (Hashtable<String, PropertyChangeSupport>) fields.get("children", null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
        this.source = fields.get("source", null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
        fields.get("propertyChangeSupportSerializedDataVersion", 2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
        Object listenerOrNull;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
        while (null != (listenerOrNull = s.readObject())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
            this.map.add(null, (PropertyChangeListener)listenerOrNull);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
        if (children != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
            for (Entry<String, PropertyChangeSupport> entry : children.entrySet()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
                for (PropertyChangeListener listener : entry.getValue().getPropertyChangeListeners()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
                    this.map.add(entry.getKey(), listener);
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
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
     * The object to be provided as the "source" for any generated events.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
    private Object source;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
     * @serialField children                                   Hashtable
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
     * @serialField source                                     Object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
     * @serialField propertyChangeSupportSerializedDataVersion int
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
    private static final ObjectStreamField[] serialPersistentFields = {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
            new ObjectStreamField("children", Hashtable.class),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
            new ObjectStreamField("source", Object.class),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
            new ObjectStreamField("propertyChangeSupportSerializedDataVersion", Integer.TYPE)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
    };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
     * Serialization version ID, so we're compatible with JDK 1.1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
    static final long serialVersionUID = 6401253773779951803L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
     * This is a {@link ChangeListenerMap ChangeListenerMap} implementation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
     * that works with {@link PropertyChangeListener PropertyChangeListener} objects.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
    private static final class PropertyChangeListenerMap extends ChangeListenerMap<PropertyChangeListener> {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
        private static final PropertyChangeListener[] EMPTY = {};
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
         * Creates an array of {@link PropertyChangeListener PropertyChangeListener} objects.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
         * This method uses the same instance of the empty array
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
         * when {@code length} equals {@code 0}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
         * @param length  the array length
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
         * @return        an array with specified length
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
        @Override
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
        protected PropertyChangeListener[] newArray(int length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
            return (0 < length)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
                    ? new PropertyChangeListener[length]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
                    : EMPTY;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
         * Creates a {@link PropertyChangeListenerProxy PropertyChangeListenerProxy}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
         * object for the specified property.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
         * @param name      the name of the property to listen on
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
         * @param listener  the listener to process events
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
         * @return          a {@code PropertyChangeListenerProxy} object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
        @Override
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
        protected PropertyChangeListener newProxy(String name, PropertyChangeListener listener) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
            return new PropertyChangeListenerProxy(name, listener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
        }
12032
49ac5a2b1d5c 7148143: PropertyChangeSupport.addPropertyChangeListener can throw ClassCastException
malenkov
parents: 11120
diff changeset
   543
49ac5a2b1d5c 7148143: PropertyChangeSupport.addPropertyChangeListener can throw ClassCastException
malenkov
parents: 11120
diff changeset
   544
        /**
49ac5a2b1d5c 7148143: PropertyChangeSupport.addPropertyChangeListener can throw ClassCastException
malenkov
parents: 11120
diff changeset
   545
         * {@inheritDoc}
49ac5a2b1d5c 7148143: PropertyChangeSupport.addPropertyChangeListener can throw ClassCastException
malenkov
parents: 11120
diff changeset
   546
         */
26004
7507a1b93f67 6521783: Unnecessary final modifier for a method in a final class
serb
parents: 25130
diff changeset
   547
        public PropertyChangeListener extract(PropertyChangeListener listener) {
12032
49ac5a2b1d5c 7148143: PropertyChangeSupport.addPropertyChangeListener can throw ClassCastException
malenkov
parents: 11120
diff changeset
   548
            while (listener instanceof PropertyChangeListenerProxy) {
49ac5a2b1d5c 7148143: PropertyChangeSupport.addPropertyChangeListener can throw ClassCastException
malenkov
parents: 11120
diff changeset
   549
                listener = ((PropertyChangeListenerProxy) listener).getListener();
49ac5a2b1d5c 7148143: PropertyChangeSupport.addPropertyChangeListener can throw ClassCastException
malenkov
parents: 11120
diff changeset
   550
            }
49ac5a2b1d5c 7148143: PropertyChangeSupport.addPropertyChangeListener can throw ClassCastException
malenkov
parents: 11120
diff changeset
   551
            return listener;
49ac5a2b1d5c 7148143: PropertyChangeSupport.addPropertyChangeListener can throw ClassCastException
malenkov
parents: 11120
diff changeset
   552
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
}