source: trunk/src/gcc/libjava/java/beans/VetoableChangeSupport.java@ 228

Last change on this file since 228 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: 11.1 KB
Line 
1/* java.beans.VetoableChangeSupport
2 Copyright (C) 1998 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
38package java.beans;
39import java.util.Hashtable;
40import java.util.Vector;
41import java.util.Enumeration;
42import java.io.ObjectInputStream;
43import java.io.ObjectOutputStream;
44import java.io.IOException;
45import java.io.Serializable;
46
47/**
48 ** VetoableChangeSupport makes it easy to fire vetoable
49 ** change events and handle listeners as well as reversion
50 ** of old values when things go wrong.
51 **
52 ** @author John Keiser
53 ** @since JDK1.1
54 ** @version 1.2.0, 15 Mar 1998
55 **/
56
57public class VetoableChangeSupport implements java.io.Serializable {
58 transient Hashtable propertyListeners = new Hashtable();
59 transient Vector listeners = new Vector();
60 Hashtable children;
61 Object source;
62 int vetoableChangeSupportSerializedDataVersion = 2;
63 private static final long serialVersionUID = -5090210921595982017L;
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 VetoableChangeListener l = (VetoableChangeListener)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 VetoableChangeListener l;
85 while ((l = (VetoableChangeListener)stream.readObject()) != null) {
86 addVetoableChangeListener(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 // vetoableChangeSupportSerializedDataVersion is 1.
91 }
92
93
94 /** Create VetoableChangeSupport to work with a specific
95 ** source bean.
96 ** @param source the source bean to use.
97 **/
98 public VetoableChangeSupport(Object source) {
99 this.source = source;
100 }
101
102 /** Adds a VetoableChangeListener to the list of listeners.
103 ** All property change events will be sent to this listener.
104 ** <P>
105 **
106 ** The listener add is not unique: that is, <em>n</em> adds with
107 ** the same listener will result in <em>n</em> events being sent
108 ** to that listener for every property change.
109 ** <P>
110 **
111 ** Adding a null listener will cause undefined behavior.
112 **
113 ** @param l the listener to add.
114 **/
115 public void addVetoableChangeListener(VetoableChangeListener l) {
116 listeners.addElement(l);
117 }
118
119 /** Adds a VetoableChangeListener listening on the specified property.
120 ** Events will be sent to the listener for that particular property.
121 ** <P>
122 **
123 ** The listener add is not unique; that is, <em>n</em> adds on a
124 ** particular property for a particular listener will result in
125 ** <em>n</em> events being sent to that listener when that
126 ** property is changed.
127 ** <P>
128 **
129 ** The effect is cumulative, too; if you are registered to listen
130 ** to receive events on all property changes, and then you
131 ** register on a particular property, you will receive change
132 ** events for that property twice.
133 ** <P>
134 **
135 ** Adding a null listener will cause undefined behavior.
136 **
137 ** @param propertyName the name of the property to listen on.
138 ** @param l the listener to add.
139 **/
140 public void addVetoableChangeListener(String propertyName, VetoableChangeListener l) {
141 synchronized(propertyListeners) {
142 Vector v = (Vector)propertyListeners.get(propertyName);
143 try {
144 v.addElement(l);
145 } catch(NullPointerException e) {
146 /* If v is not found, create a new vector. */
147 v = new Vector();
148 v.addElement(l);
149 propertyListeners.put(propertyName, v);
150 }
151 }
152 }
153
154 /** Removes a VetoableChangeListener from the list of listeners.
155 ** If any specific properties are being listened on, they must
156 ** be deregistered by themselves; this will only remove the
157 ** general listener to all properties.
158 ** <P>
159 **
160 ** If <code>add()</code> has been called multiple times for a
161 ** particular listener, <code>remove()</code> will have to be
162 ** called the same number of times to deregister it.
163 **
164 ** @param l the listener to remove.
165 **/
166 public void removeVetoableChangeListener(VetoableChangeListener l) {
167 listeners.removeElement(l);
168 }
169
170 /** Removes a VetoableChangeListener from listening to a specific property.
171 ** <P>
172 **
173 ** If <code>add()</code> has been called multiple times for a
174 ** particular listener on a property, <code>remove()</code> will
175 ** have to be called the same number of times to deregister it.
176 **
177 ** @param propertyName the property to stop listening on.
178 ** @param l the listener to remove.
179 **/
180 public void removeVetoableChangeListener(String propertyName, VetoableChangeListener l) {
181 synchronized(propertyListeners) {
182 Vector v = (Vector)propertyListeners.get(propertyName);
183 try {
184 v.removeElement(l);
185 if(v.size() == 0) {
186 propertyListeners.remove(propertyName);
187 }
188 } catch(NullPointerException e) {
189 /* if v is not found, do nothing. */
190 }
191 }
192 }
193
194
195 /** Fire a VetoableChangeEvent to all the listeners.
196 ** If any listener objects, a reversion event will be sent to
197 ** those listeners who received the initial event.
198 **
199 ** @param proposedChange the event to send.
200 ** @exception PropertyVetoException if the change is vetoed.
201 **/
202 public void fireVetoableChange(PropertyChangeEvent proposedChange) throws PropertyVetoException {
203 int currentListener=0;
204 try {
205 for(;currentListener<listeners.size();currentListener++) {
206 ((VetoableChangeListener)listeners.elementAt(currentListener)).vetoableChange(proposedChange);
207 }
208 } catch(PropertyVetoException e) {
209 PropertyChangeEvent reversion = new PropertyChangeEvent(proposedChange.getSource(),proposedChange.getPropertyName(),proposedChange.getNewValue(),proposedChange.getOldValue());
210 for(int sendAgain=0;sendAgain<currentListener;sendAgain++) {
211 try {
212 ((VetoableChangeListener)listeners.elementAt(sendAgain)).vetoableChange(reversion);
213 } catch(PropertyVetoException e2) {
214 }
215 }
216 throw e;
217 }
218
219 Vector moreListeners = (Vector)propertyListeners.get(proposedChange.getPropertyName());
220 if(moreListeners != null) {
221 try {
222 for(currentListener = 0; currentListener < moreListeners.size(); currentListener++) {
223 ((VetoableChangeListener)moreListeners.elementAt(currentListener)).vetoableChange(proposedChange);
224 }
225 } catch(PropertyVetoException e) {
226 PropertyChangeEvent reversion = new PropertyChangeEvent(proposedChange.getSource(),proposedChange.getPropertyName(),proposedChange.getNewValue(),proposedChange.getOldValue());
227 for(int sendAgain=0;sendAgain<listeners.size();sendAgain++) {
228 try {
229 ((VetoableChangeListener)listeners.elementAt(currentListener)).vetoableChange(proposedChange);
230 } catch(PropertyVetoException e2) {
231 }
232 }
233
234 for(int sendAgain=0;sendAgain<currentListener;sendAgain++) {
235 try {
236 ((VetoableChangeListener)moreListeners.elementAt(sendAgain)).vetoableChange(reversion);
237 } catch(PropertyVetoException e2) {
238 }
239 }
240 throw e;
241 }
242 }
243 }
244
245 /** Fire a VetoableChangeEvent containing the old and new values of the property to all the listeners.
246 ** If any listener objects, a reversion event will be sent to
247 ** those listeners who received the initial event.
248 **
249 ** @param propertyName the name of the property that
250 ** changed.
251 ** @param oldVal the old value.
252 ** @param newVal the new value.
253 ** @exception PropertyVetoException if the change is vetoed.
254 **/
255 public void fireVetoableChange(String propertyName, Object oldVal, Object newVal) throws PropertyVetoException {
256 fireVetoableChange(new PropertyChangeEvent(source,propertyName,oldVal,newVal));
257 }
258
259 /** Fire a VetoableChangeEvent containing the old and new values of the property to all the listeners.
260 ** If any listener objects, a reversion event will be sent to
261 ** those listeners who received the initial event.
262 **
263 ** @param propertyName the name of the property that
264 ** changed.
265 ** @param oldVal the old value.
266 ** @param newVal the new value.
267 ** @exception PropertyVetoException if the change is vetoed.
268 **/
269 public void fireVetoableChange(String propertyName, boolean oldVal, boolean newVal) throws PropertyVetoException {
270 fireVetoableChange(new PropertyChangeEvent(source,propertyName,new Boolean(oldVal),new Boolean(newVal)));
271 }
272
273 /** Fire a VetoableChangeEvent containing the old and new values of the property to all the listeners.
274 ** If any listener objects, a reversion event will be sent to
275 ** those listeners who received the initial event.
276 **
277 ** @param propertyName the name of the property that
278 ** changed.
279 ** @param oldVal the old value.
280 ** @param newVal the new value.
281 ** @exception PropertyVetoException if the change is vetoed.
282 **/
283 public void fireVetoableChange(String propertyName, int oldVal, int newVal) throws PropertyVetoException {
284 fireVetoableChange(new PropertyChangeEvent(source,propertyName,new Integer(oldVal),new Integer(newVal)));
285 }
286
287
288 /** Tell whether the specified property is being listened on or not.
289 ** This will only return <code>true</code> if there are listeners
290 ** on all properties or if there is a listener specifically on this
291 ** property.
292 **
293 ** @param propertyName the property that may be listened on
294 ** @return whether the property is being listened on
295 **/
296 public boolean hasListeners(String propertyName) {
297 return listeners.size() > 0 || propertyListeners.get(propertyName) != null;
298 }
299}
Note: See TracBrowser for help on using the repository browser.