001/* PropertyChangeEvent.java -- describes a change in a property
002   Copyright (C) 1998, 2000, 2002, 2005  Free Software Foundation, Inc.
003
004This file is part of GNU Classpath.
005
006GNU Classpath is free software; you can redistribute it and/or modify
007it under the terms of the GNU General Public License as published by
008the Free Software Foundation; either version 2, or (at your option)
009any later version.
010
011GNU Classpath is distributed in the hope that it will be useful, but
012WITHOUT ANY WARRANTY; without even the implied warranty of
013MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
014General Public License for more details.
015
016You should have received a copy of the GNU General Public License
017along with GNU Classpath; see the file COPYING.  If not, write to the
018Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
01902110-1301 USA.
020
021Linking this library statically or dynamically with other modules is
022making a combined work based on this library.  Thus, the terms and
023conditions of the GNU General Public License cover the whole
024combination.
025
026As a special exception, the copyright holders of this library give you
027permission to link this library with independent modules to produce an
028executable, regardless of the license terms of these independent
029modules, and to copy and distribute the resulting executable under
030terms of your choice, provided that you also meet, for each linked
031independent module, the terms and conditions of the license of that
032module.  An independent module is a module which is not derived from
033or based on this library.  If you modify this library, you may extend
034this exception to your version of the library, but you are not
035obligated to do so.  If you do not wish to do so, delete this
036exception statement from your version. */
037
038
039package java.beans;
040
041import java.util.EventObject;
042
043/**
044 * PropertyChangeEvents are fired in the PropertyChange and VetoableChange
045 * event classes.  They represent the old and new values as well as the
046 * source Bean. If the old or new value is a primitive type, it must be
047 * wrapped in the appropriate wrapper type (java.lang.Integer for int, etc.,
048 * etc.).
049 *
050 * <p>If the old or new values are unknown (although why that would be I do
051 * not know), they may be null. Also, if the set of properties itself has
052 * changed, the name should be null, and the old and new values may also be
053 * null. Right now Sun put in a propagationId, reserved for future use. Read
054 * the comments on the constructor and on setPropagationId for more
055 * information.
056 *
057 * @author John Keiser
058 * @author Eric Blake (ebb9@email.byu.edu)
059 * @since 1.1
060 * @status udpated to 1.4
061 */
062public class PropertyChangeEvent extends EventObject
063{
064  /**
065   * Compatible with JDK 1.1+.
066   */
067  private static final long serialVersionUID = 7042693688939648123L;
068
069  /**
070   * The name of the property that changed, may be null. Package visible for
071   * use by PropertyChangeSupport.
072   *
073   * @serial the changed property name
074   */
075  final String propertyName;
076
077  /**
078   * The new value of the property, may be null. Package visible for use by
079   * PropertyChangeSupport.
080   *
081   * @serial the new property value
082   */
083  final Object newValue;
084
085  /**
086   * The old value of the property, may be null. Package visible for use by
087   * PropertyChangeSupport.
088   *
089   * @serial the old property value
090   */
091  final Object oldValue;
092
093  /**
094   * The propagation ID, reserved for future use. May be null.
095   *
096   * @see #getPropagationId()
097   * @serial the Propagation ID
098   */
099  private Object propagationId;
100
101  /**
102   * Create a new PropertyChangeEvent. Remember that if you received a
103   * PropertyChangeEvent and are sending a new one, you should also set the
104   * propagation ID from the old PropertyChangeEvent.
105   *
106   * @param source the Bean containing the property
107   * @param propertyName the property's name
108   * @param oldVal the old value of the property
109   * @param newVal the new value of the property
110   * @throws IllegalArgumentException if source is null
111   */
112  public PropertyChangeEvent(Object source, String propertyName,
113                             Object oldVal, Object newVal)
114  {
115    super(source);
116    this.propertyName = propertyName;
117    oldValue = oldVal;
118    newValue = newVal;
119  }
120
121  /**
122   * Get the property name. May be null if multiple properties changed.
123   *
124   * @return the property name
125   */
126  public String getPropertyName()
127  {
128    return propertyName;
129  }
130
131  /**
132   * Get the property's new value. May be null if multiple properties changed.
133   *
134   * @return the property's new value
135   */
136  public Object getNewValue()
137  {
138    return newValue;
139  }
140
141  /**
142   * Get the property's old value. May be null if multiple properties changed.
143   *
144   * @return the property's old value
145   */
146  public Object getOldValue()
147  {
148    return oldValue;
149  }
150
151  /**
152   * Set the propagation ID.  This is a way for the event to be passed from
153   * hand to hand and retain a little extra state.  Right now it is unused,
154   * but it should be propagated anyway so that future versions of JavaBeans
155   * can use it, for God knows what.
156   *
157   * @param propagationId the propagation ID
158   * @see #getPropagationId()
159   */
160  public void setPropagationId(Object propagationId)
161  {
162    this.propagationId = propagationId;
163  }
164
165  /**
166   * Get the propagation ID. Right now, it is not used for anything.
167   *
168   * @return the propagation ID
169   * @see #setPropagationId(Object)
170   */
171  public Object getPropagationId()
172  {
173    return propagationId;
174  }
175
176  /**
177   * Utility method to rollback a change.
178   *
179   * @param event the event to rollback
180   * @return a new event with old and new swapped
181   */
182  PropertyChangeEvent rollback()
183  {
184    PropertyChangeEvent result
185      = new PropertyChangeEvent(source, propertyName, newValue, oldValue);
186    result.propagationId = propagationId;
187    return result;
188  }
189} // class PropertyChangeEvent