source: trunk/src/gcc/libjava/java/beans/PropertyChangeSupport.java@ 435

Last change on this file since 435 was 2, checked in by bird, 23 years ago

Initial revision

  • Property cvs2svn:cvs-rev set to 1.1
  • Property svn:eol-style set to native
  • Property svn:executable set to *
File size: 9.0 KB
Line 
1/* java.beans.PropertyChangeSupport
2 Copyright (C) 1998, 1999, 2000 Free Software Foundation, Inc.
3
4This file is part of GNU Classpath.
5
6GNU Classpath is free software; you can redistribute it and/or modify
7it under the terms of the GNU General Public License as published by
8the Free Software Foundation; either version 2, or (at your option)
9any later version.
10
11GNU Classpath is distributed in the hope that it will be useful, but
12WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14General Public License for more details.
15
16You should have received a copy of the GNU General Public License
17along with GNU Classpath; see the file COPYING. If not, write to the
18Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
1902111-1307 USA.
20
21Linking this library statically or dynamically with other modules is
22making a combined work based on this library. Thus, the terms and
23conditions of the GNU General Public License cover the whole
24combination.
25
26As a special exception, the copyright holders of this library give you
27permission to link this library with independent modules to produce an
28executable, regardless of the license terms of these independent
29modules, and to copy and distribute the resulting executable under
30terms of your choice, provided that you also meet, for each linked
31independent module, the terms and conditions of the license of that
32module. An independent module is a module which is not derived from
33or based on this library. If you modify this library, you may extend
34this exception to your version of the library, but you are not
35obligated to do so. If you do not wish to do so, delete this
36exception statement from your version. */
37
38
39package java.beans;
40import java.util.Hashtable;
41import java.util.Vector;
42import java.util.Enumeration;
43import java.io.ObjectInputStream;
44import java.io.ObjectOutputStream;
45import java.io.IOException;
46import java.io.Serializable;
47
48/**
49 ** PropertyChangeSupport makes it easy to fire property
50 ** change events and handle listeners.
51 **
52 ** @author John Keiser
53 ** @since JDK1.1
54 ** @version 1.2.0, 15 Mar 1999
55 **/
56
57public class PropertyChangeSupport implements java.io.Serializable {
58 transient Hashtable propertyListeners = new Hashtable();
59 transient Vector listeners = new Vector();
60 Hashtable children;
61 Object source;
62 int propertyChangeSupportSerializedDataVersion = 2;
63 private static final long serialVersionUID = 6401253773779951803L;
64
65 /**
66 * Saves the state of the object to the stream. */
67 private void writeObject(ObjectOutputStream stream) throws IOException {
68 children = propertyListeners.isEmpty() ? null : propertyListeners;
69 stream.defaultWriteObject();
70 for (Enumeration e = listeners.elements(); e.hasMoreElements(); ) {
71 PropertyChangeListener l = (PropertyChangeListener)e.nextElement();
72 if (l instanceof Serializable)
73 stream.writeObject(l);
74 }
75 stream.writeObject(null);
76 }
77
78 /**
79 * Reads the object back from stream (deserialization).
80 */
81 private void readObject(ObjectInputStream stream) throws IOException, ClassNotFoundException {
82 stream.defaultReadObject();
83 propertyListeners = (children == null) ? new Hashtable() : children;
84 PropertyChangeListener l;
85 while ((l = (PropertyChangeListener)stream.readObject()) != null) {
86 addPropertyChangeListener(l);
87 }
88 // FIXME: XXX: There is no spec for JDK 1.1 serialization
89 // so it is unclear what to do if the value of
90 // propertyChangeSupportSerializedDataVersion is 1.
91 }
92
93 /** Create PropertyChangeSupport to work with a specific
94 ** source bean.
95 ** @param source the source bean to use.
96 **/
97 public PropertyChangeSupport(Object source) {
98 this.source = source;
99 }
100
101 /** Adds a PropertyChangeListener to the list of listeners.
102 ** All property change events will be sent to this listener.
103 ** <P>
104 **
105 ** The listener add is not unique: that is, <em>n</em> adds with
106 ** the same listener will result in <em>n</em> events being sent
107 ** to that listener for every property change.
108 ** <P>
109 **
110 ** Adding a null listener will cause undefined behavior.
111 **
112 ** @param l the listener to add.
113 **/
114 public void addPropertyChangeListener(PropertyChangeListener l) {
115 listeners.addElement(l);
116 }
117
118 /** Adds a PropertyChangeListener listening on the specified property.
119 ** Events will be sent to the listener for that particular property.
120 ** <P>
121 **
122 ** The listener add is not unique; that is, <em>n</em> adds on a
123 ** particular property for a particular listener will result in
124 ** <em>n</em> events being sent to that listener when that
125 ** property is changed.
126 ** <P>
127 **
128 ** The effect is cumulative, too; if you are registered to listen
129 ** to receive events on all property changes, and then you
130 ** register on a particular property, you will receive change
131 ** events for that property twice.
132 ** <P>
133 **
134 ** Adding a null listener will cause undefined behavior.
135 **
136 ** @param propertyName the name of the property to listen on.
137 ** @param l the listener to add.
138 **/
139 public void addPropertyChangeListener(String propertyName, PropertyChangeListener l) {
140 synchronized(propertyListeners) {
141 Vector v = (Vector)propertyListeners.get(propertyName);
142 try {
143 v.addElement(l);
144 } catch(NullPointerException e) {
145 /* If v is not found, create a new vector. */
146 v = new Vector();
147 v.addElement(l);
148 propertyListeners.put(propertyName, v);
149 }
150 }
151 }
152
153 /** Removes a PropertyChangeListener from the list of listeners.
154 ** If any specific properties are being listened on, they must
155 ** be deregistered by themselves; this will only remove the
156 ** general listener to all properties.
157 ** <P>
158 **
159 ** If <code>add()</code> has been called multiple times for a
160 ** particular listener, <code>remove()</code> will have to be
161 ** called the same number of times to deregister it.
162 **
163 ** @param l the listener to remove.
164 **/
165 public void removePropertyChangeListener(PropertyChangeListener l) {
166 listeners.removeElement(l);
167 }
168
169 /** Removes a PropertyChangeListener from listening to a specific property.
170 ** <P>
171 **
172 ** If <code>add()</code> has been called multiple times for a
173 ** particular listener on a property, <code>remove()</code> will
174 ** have to be called the same number of times to deregister it.
175 **
176 ** @param propertyName the property to stop listening on.
177 ** @param l the listener to remove.
178 **/
179 public void removePropertyChangeListener(String propertyName, PropertyChangeListener l) {
180 synchronized(propertyListeners) {
181 Vector v = (Vector)propertyListeners.get(propertyName);
182 try {
183 v.removeElement(l);
184 if(v.size() == 0) {
185 propertyListeners.remove(propertyName);
186 }
187 } catch(NullPointerException e) {
188 /* if v is not found, do nothing. */
189 }
190 }
191 }
192
193 /** Fire a PropertyChangeEvent to all the listeners.
194 **
195 ** @param event the event to fire.
196 **/
197 public void firePropertyChange(PropertyChangeEvent event) {
198 for(int i=0;i<listeners.size();i++) {
199 ((PropertyChangeListener)listeners.elementAt(i)).propertyChange(event);
200 }
201 Vector moreListeners = (Vector)propertyListeners.get(event.getPropertyName());
202 if(moreListeners != null) {
203 for(int i=0;i<moreListeners.size();i++) {
204 ((PropertyChangeListener)moreListeners.elementAt(i)).propertyChange(event);
205 }
206 }
207 }
208
209 /** Fire a PropertyChangeEvent containing the old and new values of the property to all the listeners.
210 **
211 ** @param propertyName the name of the property that changed.
212 ** @param oldVal the old value.
213 ** @param newVal the new value.
214 **/
215 public void firePropertyChange(String propertyName, Object oldVal, Object newVal) {
216 firePropertyChange(new PropertyChangeEvent(source,propertyName,oldVal,newVal));
217 }
218
219 /** Fire a PropertyChangeEvent containing the old and new values of the property to all the listeners.
220 **
221 ** @param propertyName the name of the property that changed.
222 ** @param oldVal the old value.
223 ** @param newVal the new value.
224 **/
225 public void firePropertyChange(String propertyName, boolean oldVal, boolean newVal) {
226 firePropertyChange(new PropertyChangeEvent(source, propertyName, new Boolean(oldVal), new Boolean(newVal)));
227 }
228
229 /** Fire a PropertyChangeEvent containing the old and new values of the property to all the listeners.
230 **
231 ** @param propertyName the name of the property that changed.
232 ** @param oldVal the old value.
233 ** @param newVal the new value.
234 **/
235 public void firePropertyChange(String propertyName, int oldVal, int newVal) {
236 firePropertyChange(new PropertyChangeEvent(source, propertyName, new Integer(oldVal), new Integer(newVal)));
237 }
238
239 /** Tell whether the specified property is being listened on or not.
240 ** This will only return <code>true</code> if there are listeners
241 ** on all properties or if there is a listener specifically on this
242 ** property.
243 **
244 ** @param propertyName the property that may be listened on
245 ** @return whether the property is being listened on
246 **/
247 public boolean hasListeners(String propertyName) {
248 return listeners.size() > 0 || propertyListeners.get(propertyName) != null;
249 }
250}
Note: See TracBrowser for help on using the repository browser.