home *** CD-ROM | disk | FTP | other *** search
/ Chip 1998 November / Chip_1998-11_cd.bin / tema / Cafe / main.bin / PropertyChangeSupport.java < prev    next >
Text File  |  1997-10-01  |  4KB  |  147 lines

  1. /*
  2.  * @(#)PropertyChangeSupport.java    1.12 97/05/26  
  3.  * 
  4.  * Copyright (c) 1996 Sun Microsystems, Inc. All Rights Reserved.
  5.  * 
  6.  * This software is the confidential and proprietary information of Sun
  7.  * Microsystems, Inc. ("Confidential Information").  You shall not
  8.  * disclose such Confidential Information and shall use it only in
  9.  * accordance with the terms of the license agreement you entered into
  10.  * with Sun.
  11.  * 
  12.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
  13.  * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  14.  * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
  15.  * PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR ANY DAMAGES
  16.  * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING
  17.  * THIS SOFTWARE OR ITS DERIVATIVES.
  18.  * 
  19.  * CopyrightVersion bdk_beta
  20.  * 
  21.  */
  22.  
  23. package java.beans;
  24.  
  25. import java.io.Serializable;
  26. import java.io.ObjectOutputStream;
  27. import java.io.ObjectInputStream;
  28. import java.io.IOException;
  29.  
  30.  
  31. /**
  32.  * This is a utility class that can be used by beans that support bound
  33.  * properties.  You can use an instance of this class as a member field
  34.  * of your bean and delegate various work to it.
  35.  */
  36.  
  37. public class PropertyChangeSupport implements java.io.Serializable {
  38.  
  39.     /**
  40.      * @sourceBean  The bean to be given as the source for any events.
  41.      */
  42.  
  43.     public PropertyChangeSupport(Object sourceBean) {
  44.     source = sourceBean;
  45.     }
  46.  
  47.     /**
  48.      * Add a PropertyChangeListener to the listener list.
  49.      *
  50.      * @param listener  The PropertyChangeListener to be added
  51.      */
  52.  
  53.     public synchronized void addPropertyChangeListener(
  54.                 PropertyChangeListener listener) {
  55.     if (listeners == null) {
  56.         listeners = new java.util.Vector();
  57.     }
  58.     listeners.addElement(listener);
  59.     }
  60.  
  61.     /**
  62.      * Remove a PropertyChangeListener from the listener list.
  63.      *
  64.      * @param listener  The PropertyChangeListener to be removed
  65.      */
  66.  
  67.     public synchronized void removePropertyChangeListener(
  68.                 PropertyChangeListener listener) {
  69.     if (listeners == null) {
  70.         return;
  71.     }
  72.     listeners.removeElement(listener);
  73.     }
  74.  
  75.     /**
  76.      * Report a bound property update to any registered listeners.
  77.      * No event is fired if old and new are equal and non-null.
  78.      *
  79.      * @param propertyName  The programmatic name of the property
  80.      *        that was changed.
  81.      * @param oldValue  The old value of the property.
  82.      * @param newValue  The new value of the property.
  83.      */
  84.     public void firePropertyChange(String propertyName, 
  85.                     Object oldValue, Object newValue) {
  86.  
  87.     if (oldValue != null && oldValue.equals(newValue)) {
  88.         return;
  89.     }
  90.  
  91.     java.util.Vector targets;
  92.     synchronized (this) {
  93.         if (listeners == null) {
  94.             return;
  95.         }
  96.         targets = (java.util.Vector) listeners.clone();
  97.     }
  98.         PropertyChangeEvent evt = new PropertyChangeEvent(source,
  99.                         propertyName, oldValue, newValue);
  100.  
  101.     for (int i = 0; i < targets.size(); i++) {
  102.         PropertyChangeListener target = (PropertyChangeListener)targets.elementAt(i);
  103.         target.propertyChange(evt);
  104.     }
  105.     }
  106.  
  107.  
  108.     private void writeObject(ObjectOutputStream s) throws IOException {
  109.         s.defaultWriteObject();
  110.  
  111.     java.util.Vector v = null;
  112.     synchronized (this) {
  113.         if (listeners != null) {
  114.             v = (java.util.Vector) listeners.clone();
  115.             }
  116.     }
  117.  
  118.     if (v != null) {
  119.         for(int i = 0; i < v.size(); i++) {
  120.             PropertyChangeListener l = (PropertyChangeListener)v.elementAt(i);
  121.             if (l instanceof Serializable) {
  122.                 s.writeObject(l);
  123.             }
  124.             }
  125.         }
  126.         s.writeObject(null);
  127.     }
  128.  
  129.  
  130.     private void readObject(ObjectInputStream s) throws ClassNotFoundException, IOException {
  131.         s.defaultReadObject();
  132.       
  133.         Object listenerOrNull;
  134.         while(null != (listenerOrNull = s.readObject())) {
  135.       addPropertyChangeListener((PropertyChangeListener)listenerOrNull);
  136.         }
  137.     }
  138.  
  139.     transient private java.util.Vector listeners;
  140.     private Object source;
  141.     private int propertyChangeSupportSerializedDataVersion = 1;
  142. }
  143.  
  144.  
  145.  
  146.  
  147.