1. /*
  2. * @(#)SwingPropertyChangeSupport.java 1.17 01/02/09
  3. *
  4. * Copyright 1998-2001 Sun Microsystems, Inc. All Rights Reserved.
  5. *
  6. * This software is the proprietary information of Sun Microsystems, Inc.
  7. * Use is subject to license terms.
  8. *
  9. */
  10. package javax.swing.event;
  11. import java.beans.*;
  12. import java.io.Serializable;
  13. import java.io.ObjectOutputStream;
  14. import java.io.ObjectInputStream;
  15. import java.io.IOException;
  16. /**
  17. * This subclass of java.beans.PropertyChangeSupport is identical
  18. * in functionality -- it sacrifices thread-safety (not a Swing
  19. * concern) for reduce memory consumption, which helps performance
  20. * (both big Swing concerns). Most of the overridden methods are
  21. * only necessary because all of PropertyChangeSupport's instance
  22. * data is private, without accessor methods.
  23. *
  24. * @version 1.17 02/09/01
  25. * @author unattributed
  26. */
  27. public final class SwingPropertyChangeSupport extends PropertyChangeSupport {
  28. /**
  29. * Constructs a SwingPropertyChangeSupport object.
  30. *
  31. * @param sourceBean The bean to be given as the source for any events.
  32. */
  33. public SwingPropertyChangeSupport(Object sourceBean) {
  34. super(sourceBean);
  35. source = sourceBean;
  36. }
  37. /**
  38. * Add a PropertyChangeListener to the listener list.
  39. * The listener is registered for all properties.
  40. *
  41. * @param listener The PropertyChangeListener to be added
  42. */
  43. public synchronized void addPropertyChangeListener(
  44. PropertyChangeListener listener) {
  45. if (listeners == null) {
  46. listeners = new EventListenerList();
  47. }
  48. listeners.add(PropertyChangeListener.class, listener);
  49. }
  50. /**
  51. * Remove a PropertyChangeListener from the listener list.
  52. * This removes a PropertyChangeListener that was registered
  53. * for all properties.
  54. *
  55. * @param listener The PropertyChangeListener to be removed
  56. */
  57. public synchronized void removePropertyChangeListener(
  58. PropertyChangeListener listener) {
  59. if (listeners == null) {
  60. return;
  61. }
  62. listeners.remove(PropertyChangeListener.class, listener);
  63. }
  64. /**
  65. * Add a PropertyChangeListener for a specific property. The listener
  66. * will be invoked only when a call on firePropertyChange names that
  67. * specific property.
  68. *
  69. * @param propertyName The name of the property to listen on.
  70. * @param listener The PropertyChangeListener to be added
  71. */
  72. public synchronized void addPropertyChangeListener(
  73. String propertyName,
  74. PropertyChangeListener listener) {
  75. if (children == null) {
  76. children = new java.util.Hashtable();
  77. }
  78. SwingPropertyChangeSupport child =
  79. (SwingPropertyChangeSupport)children.get(propertyName);
  80. if (child == null) {
  81. child = new SwingPropertyChangeSupport(source);
  82. children.put(propertyName, child);
  83. }
  84. child.addPropertyChangeListener(listener);
  85. }
  86. /**
  87. * Remove a PropertyChangeListener for a specific property.
  88. *
  89. * @param propertyName The name of the property that was listened on.
  90. * @param listener The PropertyChangeListener to be removed
  91. */
  92. public synchronized void removePropertyChangeListener(
  93. String propertyName,
  94. PropertyChangeListener listener) {
  95. if (children == null) {
  96. return;
  97. }
  98. SwingPropertyChangeSupport child =
  99. (SwingPropertyChangeSupport)children.get(propertyName);
  100. if (child == null) {
  101. return;
  102. }
  103. child.removePropertyChangeListener(listener);
  104. }
  105. /**
  106. * Report a bound property update to any registered listeners.
  107. * No event is fired if old and new are equal and non-null.
  108. *
  109. * @param propertyName The programmatic name of the property
  110. * that was changed.
  111. * @param oldValue The old value of the property.
  112. * @param newValue The new value of the property.
  113. */
  114. public void firePropertyChange(String propertyName,
  115. Object oldValue, Object newValue) {
  116. firePropertyChange(new PropertyChangeEvent(source, propertyName,
  117. oldValue, newValue));
  118. }
  119. /**
  120. * Fire an existing PropertyChangeEvent to any registered listeners.
  121. * No event is fired if the given event's old and new values are
  122. * equal and non-null.
  123. * @param evt The PropertyChangeEvent object.
  124. */
  125. public void firePropertyChange(PropertyChangeEvent evt) {
  126. Object oldValue = evt.getOldValue();
  127. Object newValue = evt.getNewValue();
  128. String propertyName = evt.getPropertyName();
  129. if (oldValue != null && newValue != null && oldValue.equals(newValue)) {
  130. return;
  131. }
  132. SwingPropertyChangeSupport child = null;
  133. if (children != null) {
  134. synchronized (this) {
  135. if (children != null && propertyName != null) {
  136. child = (SwingPropertyChangeSupport)children.get(propertyName);
  137. }
  138. }
  139. }
  140. if (listeners != null) {
  141. Object[] listenerList = listeners.getListenerList();
  142. for (int i = 0; i <= listenerList.length-2; i += 2) {
  143. if (listenerList[i] == PropertyChangeListener.class) {
  144. ((PropertyChangeListener)listenerList[i+1]).propertyChange(evt);
  145. }
  146. }
  147. }
  148. if (child != null) {
  149. child.firePropertyChange(evt);
  150. }
  151. }
  152. /**
  153. * Check if there are any listeners for a specific property.
  154. *
  155. * @param propertyName the property name.
  156. * @return true if there are ore or more listeners for the given property
  157. */
  158. public synchronized boolean hasListeners(String propertyName) {
  159. if (listeners != null && listeners.getListenerCount(PropertyChangeListener.class) > 0) {
  160. // there is a generic listener
  161. return true;
  162. }
  163. if (children != null) {
  164. SwingPropertyChangeSupport child =
  165. (SwingPropertyChangeSupport)children.get(propertyName);
  166. if (child != null) {
  167. // The child will always have a listeners Vector.
  168. return child.hasListeners(propertyName);
  169. }
  170. }
  171. return false;
  172. }
  173. private void writeObject(ObjectOutputStream s) throws IOException {
  174. s.defaultWriteObject();
  175. if (listeners != null) {
  176. Object[] listenerList = listeners.getListenerList();
  177. for (int i = 0; i <= listenerList.length-2; i += 2) {
  178. if (listenerList[i] == PropertyChangeListener.class &&
  179. (PropertyChangeListener)listenerList[i+1] instanceof Serializable) {
  180. s.writeObject((PropertyChangeListener)listenerList[i+1]);
  181. }
  182. }
  183. }
  184. s.writeObject(null);
  185. }
  186. private void readObject(ObjectInputStream s) throws ClassNotFoundException, IOException {
  187. s.defaultReadObject();
  188. Object listenerOrNull;
  189. while (null != (listenerOrNull = s.readObject())) {
  190. addPropertyChangeListener((PropertyChangeListener)listenerOrNull);
  191. }
  192. }
  193. // "listeners" lists all the generic listeners.
  194. transient private EventListenerList listeners;
  195. // "children" contains SwingPropertyChangeSupports for individual properties
  196. private java.util.Hashtable children;
  197. private Object source;
  198. // Serialization version ID
  199. static final long serialVersionUID = 7162625831330845068L;
  200. }