home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / ProgLangD.iso / VCAFE.3.0A / Main.bin / PropertyChangeSupport.java < prev    next >
Text File  |  1998-04-21  |  5KB  |  150 lines

  1. package com.symantec.itools.beans;
  2.  
  3.  
  4. import java.io.Serializable;
  5. import java.io.ObjectOutputStream;
  6. import java.io.ObjectInputStream;
  7. import java.io.IOException;
  8. import java.beans.PropertyVetoException;
  9. import java.beans.PropertyChangeListener;
  10. import java.beans.VetoableChangeListener;
  11. import java.beans.PropertyChangeEvent;
  12.  
  13.  
  14. /**
  15.  * This is a utility class that can be used by beans that support bound
  16.  * properties.  You can either inherit from this class or you can use
  17.  * an instance of this class as a member field of your bean and delegate
  18.  * various work to it.
  19.  * <p>
  20.  * This extension of the java.beans.PropertyChangeSupport class adds
  21.  * functionality to handle individual property changes.
  22.  *
  23.  * @author Symantec
  24.  */
  25. public class PropertyChangeSupport extends java.beans.PropertyChangeSupport implements java.io.Serializable
  26. {
  27.     /**
  28.      * Constructs a PropertyChangeSupport object.
  29.      * @param sourceBean the bean to be given as the source for any events
  30.      */
  31.     public PropertyChangeSupport(Object sourceBean)
  32.     {
  33.         super(sourceBean);
  34.         source = sourceBean;
  35.     }
  36.  
  37.     /**
  38.      * Adds a PropertyChangeListener to the listener list.
  39.      *
  40.      * @param propertyName the name of the property to add a listener for
  41.      * @param listener the PropertyChangeListener to be added
  42.      * @see #removePropertyChangeListener
  43.      */
  44.     public synchronized void addPropertyChangeListener(String propertyName, PropertyChangeListener listener)
  45.     {
  46.         java.util.Vector listenerList;
  47.  
  48.         if(listenerTable == null)
  49.         {
  50.             listenerTable = new java.util.Hashtable();
  51.         }
  52.  
  53.         if(listenerTable.containsKey(propertyName))
  54.         {
  55.             listenerList = (java.util.Vector)listenerTable.get(propertyName);
  56.         }
  57.         else
  58.         {
  59.             listenerList = new java.util.Vector();
  60.         }
  61.  
  62.         listenerList.addElement(listener);
  63.         listenerTable.put(propertyName, listenerList);
  64.     }
  65.  
  66.     /**
  67.      * Removes a PropertyChangeListener from the listener list.
  68.      *
  69.      * @param propertyName the name of the property to remove a listener for
  70.      * @param listener the PropertyChangeListener to be removed
  71.      * @see #addPropertyChangeListener
  72.      */
  73.     public synchronized void removePropertyChangeListener(String propertyName, PropertyChangeListener listener)
  74.     {
  75.         java.util.Vector listenerList;
  76.  
  77.         if (listenerTable == null || !listenerTable.containsKey(propertyName))
  78.         {
  79.             return;
  80.         }
  81.         listenerList = (java.util.Vector)listenerTable.get(propertyName);
  82.         listenerList.removeElement(listener);
  83.     }
  84.  
  85.     /**
  86.      * Report a bound property update to any registered listeners.
  87.      * <p>
  88.      * No event is fired if old and new are equal and non-null.
  89.      *
  90.      * @param propertyName the programmatic name of the property
  91.      *        that was changed
  92.      * @param oldValue the old value of the property
  93.      * @param newValue the new value of the property
  94.      */
  95.     public void firePropertyChange(String propertyName, Object oldValue, Object newValue)
  96.     {
  97.         if (oldValue != null && oldValue.equals(newValue))
  98.         {
  99.             return;
  100.         }
  101.  
  102.         super.firePropertyChange(propertyName, oldValue, newValue);
  103.  
  104.         java.util.Hashtable templistenerTable = null;
  105.  
  106.         synchronized (this)
  107.         {
  108.             if(listenerTable == null || !listenerTable.containsKey(propertyName))
  109.             {
  110.                 return;
  111.             }
  112.               templistenerTable = (java.util.Hashtable) listenerTable.clone();
  113.         }
  114.  
  115.         java.util.Vector listenerList;
  116.  
  117.         listenerList = (java.util.Vector)templistenerTable.get(propertyName);
  118.  
  119.         PropertyChangeEvent evt = new PropertyChangeEvent(source, propertyName, oldValue, newValue);
  120.  
  121.         for (int i = 0; i < listenerList.size(); i++)
  122.         {
  123.             PropertyChangeListener target = (PropertyChangeListener)listenerList.elementAt(i);
  124.             target.propertyChange(evt);
  125.         }
  126.     }
  127.  
  128.     /* !!! LAB !!!    05/06/97
  129.     If we want to support non-serializable listeners we will have to
  130.     implement the folowing functions and serialize out the listenerTable
  131.     HashTable on our own.
  132.  
  133.     private void writeObject(ObjectOutputStream s) throws IOException
  134.     private void readObject(ObjectInputStream s) throws ClassNotFoundException, IOException
  135.     */
  136.  
  137.     /**
  138.      * The listener list.
  139.      * @see #addPropertyChangeListener
  140.      * @see #removePropertyChangeListener
  141.      */
  142.     protected java.util.Hashtable listenerTable;
  143.     private Object source;
  144.     private int propertyChangeSupportSerializedDataVersion = 1;
  145. }
  146.  
  147.  
  148.  
  149.  
  150.