2003-12-26 Guilhem Lavaux <guilhem@kaffe.org>
[official-gcc.git] / libjava / javax / swing / event / SwingPropertyChangeSupport.java
blob9092158df32e7bb89dc4c6dbe33c9a8dcc018fa4
1 /* SwingPropertyChangeSupport.java --
2 Copyright (C) 2002 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
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.
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.
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., 59 Temple Place, Suite 330, Boston, MA
19 02111-1307 USA.
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.
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. */
38 package javax.swing.event;
40 // Imports
41 import java.beans.*;
42 import java.io.*;
43 import java.util.*;
45 /**
46 * SwingPropertyChangeSupport
47 * @author Andrew Selkirk
49 public final class SwingPropertyChangeSupport
50 extends PropertyChangeSupport {
52 private static final long serialVersionUID = 7162625831330845068L;
54 //-------------------------------------------------------------
55 // Variables --------------------------------------------------
56 //-------------------------------------------------------------
58 /**
59 * listeners
61 private transient EventListenerList listeners;
63 /**
64 * propertyListeners
66 private Hashtable propertyListeners;
68 /**
69 * source
71 private Object source;
74 //-------------------------------------------------------------
75 // Initialization ---------------------------------------------
76 //-------------------------------------------------------------
78 /**
79 * Constructor SwingPropertyChangeSupport
80 * @param source TODO
82 public SwingPropertyChangeSupport(Object source) {
83 super(source);
84 this.source = source;
85 this.listeners = new EventListenerList();
86 this.propertyListeners = new Hashtable();
87 } // SwingPropertyChangeSupport()
90 //-------------------------------------------------------------
91 // Methods ----------------------------------------------------
92 //-------------------------------------------------------------
94 /**
95 * writeObject
96 * @param stream TODO
97 * @exception IOException TODO
99 private void writeObject(ObjectOutputStream stream) throws IOException {
100 // TODO
101 } // writeObject()
104 * readObject
105 * @param stream TODO
106 * @exception ClassNotFoundException TODO
107 * @exception IOException TODO
109 private void readObject(ObjectInputStream stream) throws ClassNotFoundException, IOException {
110 // TODO
111 } // readObject()
114 * addPropertyChangeListener
115 * @param listener TODO
117 public synchronized void addPropertyChangeListener(PropertyChangeListener listener) {
118 listeners.add(PropertyChangeListener.class, listener);
119 } // addPropertyChangeListener()
122 * addPropertyChangeListener
123 * @param propertyName TODO
124 * @param listener TODO
126 public synchronized void addPropertyChangeListener(String propertyName, PropertyChangeListener listener) {
128 // Variables
129 EventListenerList list;
131 // Get Listener list
132 list = (EventListenerList) propertyListeners.get(propertyName);
133 if (list == null) {
134 list = new EventListenerList();
135 propertyListeners.put(propertyName, list);
136 } // if
138 // Add Listeners
139 list.add(PropertyChangeListener.class, listener);
141 } // addPropertyChangeListener()
144 * removePropertyChangeListener
145 * @param listener TODO
147 public synchronized void removePropertyChangeListener(PropertyChangeListener listener) {
148 listeners.remove(PropertyChangeListener.class, listener);
149 } // removePropertyChangeListener()
152 * removePropertyChangeListener
153 * @param propertyName TODO
154 * @param listener TODO
156 public synchronized void removePropertyChangeListener(String propertyName, PropertyChangeListener listener) {
158 // Variables
159 EventListenerList list;
161 // Get Listener list
162 list = (EventListenerList) propertyListeners.get(propertyName);
163 if (list == null) {
164 return;
165 } // if
167 // Remove Listeners
168 list.remove(PropertyChangeListener.class, listener);
170 // Clean up propertyListeners
171 if (list.getListenerCount() == 0) {
172 propertyListeners.remove(propertyName);
173 } // if
175 } // removePropertyChangeListener()
178 * firePropertyChange
179 * @param propertyName TODO
180 * @param oldValue TODO
181 * @param newValue TODO
183 public void firePropertyChange(String propertyName, Object oldValue, Object newValue) {
185 // Variables
186 PropertyChangeEvent event;
188 // Create Property Change Event
189 event = new PropertyChangeEvent(source, propertyName, oldValue, newValue);
191 // Fire Event
192 firePropertyChange(event);
194 } // firePropertyChange()
197 * firePropertyChange
198 * @param event TODO
200 public void firePropertyChange(PropertyChangeEvent event) {
202 // Variables
203 EventListenerList list;
204 EventListener[] listenerList;
205 int index;
206 PropertyChangeListener listener;
208 // Check Values if they are equal
209 if (event.getOldValue() == null || event.getNewValue() == null ||
210 event.getOldValue().equals(event.getNewValue()) == true) {
211 return;
212 } // if
214 // Process Main Listener List
215 listenerList = listeners.getListeners(PropertyChangeListener.class);
216 for (index = 0; index < listenerList.length; index++) {
217 listener = (PropertyChangeListener) listenerList[index];
218 listener.propertyChange(event);
219 } // for
221 // Process Property Listener List
222 list = (EventListenerList) propertyListeners.get(event.getPropertyName());
223 if (list != null) {
224 listenerList = list.getListeners(PropertyChangeListener.class);
225 for (index = 0; index < listenerList.length; index++) {
226 listener = (PropertyChangeListener) listenerList[index];
227 listener.propertyChange(event);
228 } // for
229 } // if
231 } // firePropertyChange()
234 * hasListeners
235 * @param propertyName TODO
236 * @returns boolean
238 public synchronized boolean hasListeners(String propertyName) {
240 // Get Listener list
241 if (propertyListeners.get(propertyName) == null) {
242 return false;
243 } // if
245 return true;
247 } // hasListeners()
250 } // SwingPropertyChangeSupport