PropertyChangeEvent.java 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. /* PropertyChangeEvent.java -- describes a change in a property
  2. Copyright (C) 1998, 2000, 2002, 2005 Free Software Foundation, Inc.
  3. This file is part of GNU Classpath.
  4. GNU Classpath is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2, or (at your option)
  7. any later version.
  8. GNU Classpath is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GNU Classpath; see the file COPYING. If not, write to the
  14. Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  15. 02110-1301 USA.
  16. Linking this library statically or dynamically with other modules is
  17. making a combined work based on this library. Thus, the terms and
  18. conditions of the GNU General Public License cover the whole
  19. combination.
  20. As a special exception, the copyright holders of this library give you
  21. permission to link this library with independent modules to produce an
  22. executable, regardless of the license terms of these independent
  23. modules, and to copy and distribute the resulting executable under
  24. terms of your choice, provided that you also meet, for each linked
  25. independent module, the terms and conditions of the license of that
  26. module. An independent module is a module which is not derived from
  27. or based on this library. If you modify this library, you may extend
  28. this exception to your version of the library, but you are not
  29. obligated to do so. If you do not wish to do so, delete this
  30. exception statement from your version. */
  31. package java.beans;
  32. import java.util.EventObject;
  33. /**
  34. * PropertyChangeEvents are fired in the PropertyChange and VetoableChange
  35. * event classes. They represent the old and new values as well as the
  36. * source Bean. If the old or new value is a primitive type, it must be
  37. * wrapped in the appropriate wrapper type (java.lang.Integer for int, etc.,
  38. * etc.).
  39. *
  40. * <p>If the old or new values are unknown (although why that would be I do
  41. * not know), they may be null. Also, if the set of properties itself has
  42. * changed, the name should be null, and the old and new values may also be
  43. * null. Right now Sun put in a propagationId, reserved for future use. Read
  44. * the comments on the constructor and on setPropagationId for more
  45. * information.
  46. *
  47. * @author John Keiser
  48. * @author Eric Blake (ebb9@email.byu.edu)
  49. * @since 1.1
  50. * @status udpated to 1.4
  51. */
  52. public class PropertyChangeEvent extends EventObject
  53. {
  54. /**
  55. * Compatible with JDK 1.1+.
  56. */
  57. private static final long serialVersionUID = 7042693688939648123L;
  58. /**
  59. * The name of the property that changed, may be null. Package visible for
  60. * use by PropertyChangeSupport.
  61. *
  62. * @serial the changed property name
  63. */
  64. final String propertyName;
  65. /**
  66. * The new value of the property, may be null. Package visible for use by
  67. * PropertyChangeSupport.
  68. *
  69. * @serial the new property value
  70. */
  71. final Object newValue;
  72. /**
  73. * The old value of the property, may be null. Package visible for use by
  74. * PropertyChangeSupport.
  75. *
  76. * @serial the old property value
  77. */
  78. final Object oldValue;
  79. /**
  80. * The propagation ID, reserved for future use. May be null.
  81. *
  82. * @see #getPropagationId()
  83. * @serial the Propagation ID
  84. */
  85. private Object propagationId;
  86. /**
  87. * Create a new PropertyChangeEvent. Remember that if you received a
  88. * PropertyChangeEvent and are sending a new one, you should also set the
  89. * propagation ID from the old PropertyChangeEvent.
  90. *
  91. * @param source the Bean containing the property
  92. * @param propertyName the property's name
  93. * @param oldVal the old value of the property
  94. * @param newVal the new value of the property
  95. * @throws IllegalArgumentException if source is null
  96. */
  97. public PropertyChangeEvent(Object source, String propertyName,
  98. Object oldVal, Object newVal)
  99. {
  100. super(source);
  101. this.propertyName = propertyName;
  102. oldValue = oldVal;
  103. newValue = newVal;
  104. }
  105. /**
  106. * Get the property name. May be null if multiple properties changed.
  107. *
  108. * @return the property name
  109. */
  110. public String getPropertyName()
  111. {
  112. return propertyName;
  113. }
  114. /**
  115. * Get the property's new value. May be null if multiple properties changed.
  116. *
  117. * @return the property's new value
  118. */
  119. public Object getNewValue()
  120. {
  121. return newValue;
  122. }
  123. /**
  124. * Get the property's old value. May be null if multiple properties changed.
  125. *
  126. * @return the property's old value
  127. */
  128. public Object getOldValue()
  129. {
  130. return oldValue;
  131. }
  132. /**
  133. * Set the propagation ID. This is a way for the event to be passed from
  134. * hand to hand and retain a little extra state. Right now it is unused,
  135. * but it should be propagated anyway so that future versions of JavaBeans
  136. * can use it, for God knows what.
  137. *
  138. * @param propagationId the propagation ID
  139. * @see #getPropagationId()
  140. */
  141. public void setPropagationId(Object propagationId)
  142. {
  143. this.propagationId = propagationId;
  144. }
  145. /**
  146. * Get the propagation ID. Right now, it is not used for anything.
  147. *
  148. * @return the propagation ID
  149. * @see #setPropagationId(Object)
  150. */
  151. public Object getPropagationId()
  152. {
  153. return propagationId;
  154. }
  155. /**
  156. * Utility method to rollback a change.
  157. *
  158. * @param event the event to rollback
  159. * @return a new event with old and new swapped
  160. */
  161. PropertyChangeEvent rollback()
  162. {
  163. PropertyChangeEvent result
  164. = new PropertyChangeEvent(source, propertyName, newValue, oldValue);
  165. result.propagationId = propagationId;
  166. return result;
  167. }
  168. } // class PropertyChangeEvent