Source for javax.swing.event.SwingPropertyChangeSupport

   1: /* SwingPropertyChangeSupport.java --
   2:    Copyright (C) 2002, 2004 Free Software Foundation, Inc.
   3: 
   4: This file is part of GNU Classpath.
   5: 
   6: GNU Classpath is free software; you can redistribute it and/or modify
   7: it under the terms of the GNU General Public License as published by
   8: the Free Software Foundation; either version 2, or (at your option)
   9: any later version.
  10: 
  11: GNU Classpath is distributed in the hope that it will be useful, but
  12: WITHOUT ANY WARRANTY; without even the implied warranty of
  13: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  14: General Public License for more details.
  15: 
  16: You should have received a copy of the GNU General Public License
  17: along with GNU Classpath; see the file COPYING.  If not, write to the
  18: Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  19: 02110-1301 USA.
  20: 
  21: Linking this library statically or dynamically with other modules is
  22: making a combined work based on this library.  Thus, the terms and
  23: conditions of the GNU General Public License cover the whole
  24: combination.
  25: 
  26: As a special exception, the copyright holders of this library give you
  27: permission to link this library with independent modules to produce an
  28: executable, regardless of the license terms of these independent
  29: modules, and to copy and distribute the resulting executable under
  30: terms of your choice, provided that you also meet, for each linked
  31: independent module, the terms and conditions of the license of that
  32: module.  An independent module is a module which is not derived from
  33: or based on this library.  If you modify this library, you may extend
  34: this exception to your version of the library, but you are not
  35: obligated to do so.  If you do not wish to do so, delete this
  36: exception statement from your version. */
  37: 
  38: package javax.swing.event;
  39: 
  40: import java.beans.PropertyChangeEvent;
  41: import java.beans.PropertyChangeListener;
  42: import java.beans.PropertyChangeSupport;
  43: import java.io.IOException;
  44: import java.io.ObjectInputStream;
  45: import java.io.ObjectOutputStream;
  46: import java.util.EventListener;
  47: import java.util.Hashtable;
  48: 
  49: /**
  50:  * SwingPropertyChangeSupport
  51:  * @author Andrew Selkirk
  52: */
  53: public final    class SwingPropertyChangeSupport
  54:                 extends PropertyChangeSupport {
  55: 
  56:   private static final long serialVersionUID = 7162625831330845068L;
  57: 
  58:     //-------------------------------------------------------------
  59:     // Variables --------------------------------------------------
  60:     //-------------------------------------------------------------
  61: 
  62:     /**
  63:      * listeners
  64:      */
  65:     private transient EventListenerList listeners;
  66: 
  67:     /**
  68:      * propertyListeners
  69:      */
  70:     private Hashtable propertyListeners;
  71: 
  72:     /**
  73:      * source
  74:      */
  75:     private Object source;
  76: 
  77: 
  78:     //-------------------------------------------------------------
  79:     // Initialization ---------------------------------------------
  80:     //-------------------------------------------------------------
  81: 
  82:     /**
  83:      * Constructor SwingPropertyChangeSupport
  84:      * @param source TODO
  85:      */
  86:     public SwingPropertyChangeSupport(Object source) {
  87:         super(source);
  88:         this.source = source;
  89:         this.listeners = new EventListenerList();
  90:         this.propertyListeners = new Hashtable();
  91:     } // SwingPropertyChangeSupport()
  92: 
  93: 
  94:     //-------------------------------------------------------------
  95:     // Methods ----------------------------------------------------
  96:     //-------------------------------------------------------------
  97: 
  98:     /**
  99:      * writeObject
 100:      * @param stream TODO
 101:      * @exception IOException TODO
 102:      */
 103:     private void writeObject(ObjectOutputStream stream) throws IOException {
 104:         // TODO
 105:     } // writeObject()
 106: 
 107:     /**
 108:      * readObject
 109:      * @param stream TODO
 110:      * @exception ClassNotFoundException TODO
 111:      * @exception IOException TODO
 112:      */
 113:     private void readObject(ObjectInputStream stream) throws ClassNotFoundException, IOException {
 114:         // TODO
 115:     } // readObject()
 116: 
 117:     /**
 118:      * addPropertyChangeListener
 119:      * @param listener TODO
 120:      */
 121:     public synchronized void addPropertyChangeListener(PropertyChangeListener listener) {
 122:         listeners.add(PropertyChangeListener.class, listener);
 123:     } // addPropertyChangeListener()
 124: 
 125:     /**
 126:      * addPropertyChangeListener
 127:      * @param propertyName TODO
 128:      * @param listener TODO
 129:      */
 130:     public synchronized void addPropertyChangeListener(String propertyName, PropertyChangeListener listener) {
 131: 
 132:         // Variables
 133:         EventListenerList    list;
 134: 
 135:         // Get Listener list
 136:         list = (EventListenerList) propertyListeners.get(propertyName);
 137:         if (list == null) {
 138:             list = new EventListenerList();
 139:             propertyListeners.put(propertyName, list);
 140:         } // if
 141: 
 142:         // Add Listeners
 143:         list.add(PropertyChangeListener.class, listener);
 144: 
 145:     } // addPropertyChangeListener()
 146: 
 147:     /**
 148:      * removePropertyChangeListener
 149:      * @param listener TODO
 150:      */
 151:     public synchronized void removePropertyChangeListener(PropertyChangeListener listener) {
 152:         listeners.remove(PropertyChangeListener.class, listener);
 153:     } // removePropertyChangeListener()
 154: 
 155:     /**
 156:      * removePropertyChangeListener
 157:      * @param propertyName TODO
 158:      * @param listener TODO
 159:      */
 160:     public synchronized void removePropertyChangeListener(String propertyName, PropertyChangeListener listener) {
 161: 
 162:         // Variables
 163:         EventListenerList    list;
 164: 
 165:         // Get Listener list
 166:         list = (EventListenerList) propertyListeners.get(propertyName);
 167:         if (list == null) {
 168:             return;
 169:         } // if
 170: 
 171:         // Remove Listeners
 172:         list.remove(PropertyChangeListener.class, listener);
 173: 
 174:         // Clean up propertyListeners
 175:         if (list.getListenerCount() == 0) {
 176:             propertyListeners.remove(propertyName);
 177:         } // if
 178: 
 179:     } // removePropertyChangeListener()
 180: 
 181:     /**
 182:      * firePropertyChange
 183:      * @param propertyName TODO
 184:      * @param oldValue TODO
 185:      * @param newValue TODO
 186:      */
 187:     public void firePropertyChange(String propertyName, Object oldValue, Object newValue) {
 188: 
 189:         // Variables
 190:         PropertyChangeEvent    event;
 191: 
 192:         // Create Property Change Event
 193:         event = new PropertyChangeEvent(source, propertyName, oldValue, newValue);
 194: 
 195:         // Fire Event
 196:         firePropertyChange(event);
 197: 
 198:     } // firePropertyChange()
 199: 
 200:     /**
 201:      * firePropertyChange
 202:      * @param event TODO
 203:      */
 204:     public void firePropertyChange(PropertyChangeEvent event) {
 205: 
 206:         // Variables
 207:         EventListenerList        list;
 208:         EventListener[]            listenerList;
 209:         int                        index;
 210:         PropertyChangeListener    listener;
 211: 
 212:         // Check Values if they are equal
 213:         if (event.getOldValue() == null && event.getNewValue() == null ||
 214:             (event.getOldValue() != null && event.getNewValue() != null &&
 215:                 event.getOldValue().equals(event.getNewValue()))) {
 216:             return;
 217:         } // if
 218: 
 219:         // Process Main Listener List
 220:         listenerList = listeners.getListeners(PropertyChangeListener.class);
 221:         for (index = 0; index < listenerList.length; index++) {
 222:             listener = (PropertyChangeListener) listenerList[index];
 223:             listener.propertyChange(event);
 224:         } // for
 225: 
 226:         // Process Property Listener List
 227:         list = (EventListenerList) propertyListeners.get(event.getPropertyName());
 228:         if (list != null) {
 229:             listenerList = list.getListeners(PropertyChangeListener.class);
 230:             for (index = 0; index < listenerList.length; index++) {
 231:                 listener = (PropertyChangeListener) listenerList[index];
 232:                 listener.propertyChange(event);
 233:             } // for
 234:         } // if
 235: 
 236:     } // firePropertyChange()
 237: 
 238:     /**
 239:      * hasListeners
 240:      * @param propertyName TODO
 241:      * @returns boolean
 242:      */
 243:     public synchronized boolean hasListeners(String propertyName) {
 244: 
 245:         // Get Listener list
 246:         if (propertyListeners.get(propertyName) == null) {
 247:             return false;
 248:         } // if
 249: 
 250:         return true;
 251: 
 252:     } // hasListeners()
 253: 
 254: 
 255: } // SwingPropertyChangeSupport