Path: blob/master/src/java.desktop/share/classes/java/beans/PropertyChangeEvent.java
41152 views
/*1* Copyright (c) 1996, 2021, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package java.beans;2627import java.io.Serial;28import java.util.EventObject;2930/**31* A "PropertyChange" event gets delivered whenever a bean changes a "bound"32* or "constrained" property. A PropertyChangeEvent object is sent as an33* argument to the PropertyChangeListener and VetoableChangeListener methods.34* <P>35* Normally PropertyChangeEvents are accompanied by the name and the old36* and new value of the changed property. If the new value is a primitive37* type (such as int or boolean) it must be wrapped as the38* corresponding java.lang.* Object type (such as Integer or Boolean).39* <P>40* Null values may be provided for the old and the new values if their41* true values are not known.42* <P>43* An event source may send a null object as the name to indicate that an44* arbitrary set of if its properties have changed. In this case the45* old and new values should also be null.46*47* @since 1.148*/49public class PropertyChangeEvent extends EventObject {5051/**52* Use serialVersionUID from JDK 1.7 for interoperability.53*/54@Serial55private static final long serialVersionUID = 7042693688939648123L;5657/**58* Constructs a new {@code PropertyChangeEvent}.59*60* @param source the bean that fired the event61* @param propertyName the programmatic name of the property that was changed62* @param oldValue the old value of the property63* @param newValue the new value of the property64*65* @throws IllegalArgumentException if {@code source} is {@code null}66*/67public PropertyChangeEvent(Object source, String propertyName,68Object oldValue, Object newValue) {69super(source);70this.propertyName = propertyName;71this.newValue = newValue;72this.oldValue = oldValue;73}7475/**76* Gets the programmatic name of the property that was changed.77*78* @return The programmatic name of the property that was changed.79* May be null if multiple properties have changed.80*/81public String getPropertyName() {82return propertyName;83}8485/**86* Gets the new value for the property, expressed as an Object.87*88* @return The new value for the property, expressed as an Object.89* May be null if multiple properties have changed.90*/91public Object getNewValue() {92return newValue;93}9495/**96* Gets the old value for the property, expressed as an Object.97*98* @return The old value for the property, expressed as an Object.99* May be null if multiple properties have changed.100*/101public Object getOldValue() {102return oldValue;103}104105/**106* Sets the propagationId object for the event.107*108* @param propagationId The propagationId object for the event.109*/110public void setPropagationId(Object propagationId) {111this.propagationId = propagationId;112}113114/**115* The "propagationId" field is reserved for future use. In Beans 1.0116* the sole requirement is that if a listener catches a PropertyChangeEvent117* and then fires a PropertyChangeEvent of its own, then it should118* make sure that it propagates the propagationId field from its119* incoming event to its outgoing event.120*121* @return the propagationId object associated with a bound/constrained122* property update.123*/124public Object getPropagationId() {125return propagationId;126}127128/**129* name of the property that changed. May be null, if not known.130* @serial131*/132private String propertyName;133134/**135* New value for property. May be null if not known.136* @serial137*/138@SuppressWarnings("serial") // Not statically typed as Serializable139private Object newValue;140141/**142* Previous value for property. May be null if not known.143* @serial144*/145@SuppressWarnings("serial") // Not statically typed as Serializable146private Object oldValue;147148/**149* Propagation ID. May be null.150* @serial151* @see #getPropagationId152*/153@SuppressWarnings("serial") // Not statically typed as Serializable154private Object propagationId;155156/**157* Returns a string representation of the object.158*159* @return a string representation of the object160*161* @since 1.7162*/163public String toString() {164StringBuilder sb = new StringBuilder(getClass().getName());165sb.append("[propertyName=").append(getPropertyName());166appendTo(sb);167sb.append("; oldValue=").append(getOldValue());168sb.append("; newValue=").append(getNewValue());169sb.append("; propagationId=").append(getPropagationId());170sb.append("; source=").append(getSource());171return sb.append("]").toString();172}173174void appendTo(StringBuilder sb) {175}176}177178179