VetoableChangeSupport.java 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533
  1. /* VetoableChangeSupport.java -- support to manage vetoable change listeners
  2. Copyright (C) 1998, 1999, 2000, 2002, 2005, 2006,
  3. Free Software Foundation, Inc.
  4. This file is part of GNU Classpath.
  5. GNU Classpath is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2, or (at your option)
  8. any later version.
  9. GNU Classpath is distributed in the hope that it will be useful, but
  10. WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with GNU Classpath; see the file COPYING. If not, write to the
  15. Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  16. 02110-1301 USA.
  17. Linking this library statically or dynamically with other modules is
  18. making a combined work based on this library. Thus, the terms and
  19. conditions of the GNU General Public License cover the whole
  20. combination.
  21. As a special exception, the copyright holders of this library give you
  22. permission to link this library with independent modules to produce an
  23. executable, regardless of the license terms of these independent
  24. modules, and to copy and distribute the resulting executable under
  25. terms of your choice, provided that you also meet, for each linked
  26. independent module, the terms and conditions of the license of that
  27. module. An independent module is a module which is not derived from
  28. or based on this library. If you modify this library, you may extend
  29. this exception to your version of the library, but you are not
  30. obligated to do so. If you do not wish to do so, delete this
  31. exception statement from your version. */
  32. package java.beans;
  33. import java.io.IOException;
  34. import java.io.ObjectInputStream;
  35. import java.io.ObjectOutputStream;
  36. import java.io.Serializable;
  37. import java.util.ArrayList;
  38. import java.util.Arrays;
  39. import java.util.Hashtable;
  40. import java.util.Iterator;
  41. import java.util.Map.Entry;
  42. import java.util.Vector;
  43. /**
  44. * VetoableChangeSupport makes it easy to fire vetoable change events and
  45. * handle listeners. It allows chaining of listeners, as well as filtering
  46. * by property name. In addition, it will serialize only those listeners
  47. * which are serializable, ignoring the others without problem. This class
  48. * is thread-safe.
  49. *
  50. * @author John Keiser
  51. * @author Eric Blake (ebb9@email.byu.edu)
  52. * @since 1.1
  53. * @status updated to 1.4
  54. */
  55. public class VetoableChangeSupport implements Serializable
  56. {
  57. /**
  58. * Compatible with JDK 1.1+.
  59. */
  60. private static final long serialVersionUID = -5090210921595982017L;
  61. /**
  62. * Maps property names (String) to named listeners (VetoableChangeSupport).
  63. * If this is a child instance, this field will be null.
  64. *
  65. * @serial the map of property names to named listener managers
  66. * @since 1.2
  67. */
  68. private Hashtable children;
  69. /**
  70. * The non-null source object for any generated events.
  71. *
  72. * @serial the event source
  73. */
  74. private final Object source;
  75. /**
  76. * A field to compare serialization versions - this class uses version 2.
  77. *
  78. * @serial the serialization format
  79. */
  80. private static final int vetoableChangeSupportSerializedDataVersion = 2;
  81. /**
  82. * The list of all registered vetoable listeners. If this instance was
  83. * created by user code, this only holds the global listeners (ie. not tied
  84. * to a name), and may be null. If it was created by this class, as a
  85. * helper for named properties, then this vector will be non-null, and this
  86. * instance appears as a value in the <code>children</code> hashtable of
  87. * another instance, so that the listeners are tied to the key of that
  88. * hashtable entry.
  89. */
  90. private transient Vector listeners;
  91. /**
  92. * Create a VetoableChangeSupport to work with a specific source bean.
  93. *
  94. * @param source the source bean to use
  95. * @throws NullPointerException if source is null
  96. */
  97. public VetoableChangeSupport(Object source)
  98. {
  99. this.source = source;
  100. if (source == null)
  101. throw new NullPointerException();
  102. }
  103. /**
  104. * Adds a VetoableChangeListener to the list of global listeners. All
  105. * vetoable change events will be sent to this listener. The listener add
  106. * is not unique: that is, <em>n</em> adds with the same listener will
  107. * result in <em>n</em> events being sent to that listener for every
  108. * vetoable change. This method will unwrap a VetoableChangeListenerProxy,
  109. * registering the underlying delegate to the named property list.
  110. *
  111. * @param l the listener to add (<code>null</code> ignored).
  112. */
  113. public synchronized void addVetoableChangeListener(VetoableChangeListener l)
  114. {
  115. if (l == null)
  116. return;
  117. if (l instanceof VetoableChangeListenerProxy)
  118. {
  119. VetoableChangeListenerProxy p = (VetoableChangeListenerProxy) l;
  120. addVetoableChangeListener(p.propertyName,
  121. (VetoableChangeListener) p.getListener());
  122. }
  123. else
  124. {
  125. if (listeners == null)
  126. listeners = new Vector();
  127. listeners.add(l);
  128. }
  129. }
  130. /**
  131. * Removes a VetoableChangeListener from the list of global listeners. If
  132. * any specific properties are being listened on, they must be deregistered
  133. * by themselves; this will only remove the general listener to all
  134. * properties. If <code>add()</code> has been called multiple times for a
  135. * particular listener, <code>remove()</code> will have to be called the
  136. * same number of times to deregister it. This method will unwrap a
  137. * VetoableChangeListenerProxy, removing the underlying delegate from the
  138. * named property list.
  139. *
  140. * @param l the listener to remove
  141. */
  142. public synchronized void
  143. removeVetoableChangeListener(VetoableChangeListener l)
  144. {
  145. if (l instanceof VetoableChangeListenerProxy)
  146. {
  147. VetoableChangeListenerProxy p = (VetoableChangeListenerProxy) l;
  148. removeVetoableChangeListener(p.propertyName,
  149. (VetoableChangeListener) p.getListener());
  150. }
  151. else if (listeners != null)
  152. {
  153. listeners.remove(l);
  154. if (listeners.isEmpty())
  155. listeners = null;
  156. }
  157. }
  158. /**
  159. * Returns an array of all registered vetoable change listeners. Those that
  160. * were registered under a name will be wrapped in a
  161. * <code>VetoableChangeListenerProxy</code>, so you must check whether the
  162. * listener is an instance of the proxy class in order to see what name the
  163. * real listener is registered under. If there are no registered listeners,
  164. * this returns an empty array.
  165. *
  166. * @return the array of registered listeners
  167. * @see VetoableChangeListenerProxy
  168. * @since 1.4
  169. */
  170. public synchronized VetoableChangeListener[] getVetoableChangeListeners()
  171. {
  172. ArrayList list = new ArrayList();
  173. if (listeners != null)
  174. list.addAll(listeners);
  175. if (children != null)
  176. {
  177. int i = children.size();
  178. Iterator iter = children.entrySet().iterator();
  179. while (--i >= 0)
  180. {
  181. Entry e = (Entry) iter.next();
  182. String name = (String) e.getKey();
  183. Vector v = ((VetoableChangeSupport) e.getValue()).listeners;
  184. int j = v.size();
  185. while (--j >= 0)
  186. list.add(new VetoableChangeListenerProxy
  187. (name, (VetoableChangeListener) v.get(j)));
  188. }
  189. }
  190. return (VetoableChangeListener[])
  191. list.toArray(new VetoableChangeListener[list.size()]);
  192. }
  193. /**
  194. * Adds a VetoableChangeListener listening on the specified property. Events
  195. * will be sent to the listener only if the property name matches. The
  196. * listener add is not unique; that is, <em>n</em> adds on a particular
  197. * property for a particular listener will result in <em>n</em> events
  198. * being sent to that listener when that property is changed. The effect is
  199. * cumulative, too; if you are registered to listen to receive events on
  200. * all vetoable changes, and then you register on a particular property,
  201. * you will receive change events for that property twice. This method
  202. * will unwrap a VetoableChangeListenerProxy, registering the underlying
  203. * delegate to the named property list if the names match, and discarding
  204. * it otherwise.
  205. *
  206. * @param propertyName the name of the property to listen on
  207. * @param l the listener to add
  208. */
  209. public synchronized void addVetoableChangeListener(String propertyName,
  210. VetoableChangeListener l)
  211. {
  212. if (propertyName == null || l == null)
  213. return;
  214. while (l instanceof VetoableChangeListenerProxy)
  215. {
  216. VetoableChangeListenerProxy p = (VetoableChangeListenerProxy) l;
  217. if (propertyName == null ? p.propertyName != null
  218. : ! propertyName.equals(p.propertyName))
  219. return;
  220. l = (VetoableChangeListener) p.getListener();
  221. }
  222. VetoableChangeSupport s = null;
  223. if (children == null)
  224. children = new Hashtable();
  225. else
  226. s = (VetoableChangeSupport) children.get(propertyName);
  227. if (s == null)
  228. {
  229. s = new VetoableChangeSupport(source);
  230. s.listeners = new Vector();
  231. children.put(propertyName, s);
  232. }
  233. s.listeners.add(l);
  234. }
  235. /**
  236. * Removes a VetoableChangeListener from listening to a specific property.
  237. * If <code>add()</code> has been called multiple times for a particular
  238. * listener on a property, <code>remove()</code> will have to be called the
  239. * same number of times to deregister it. This method will unwrap a
  240. * VetoableChangeListenerProxy, removing the underlying delegate from the
  241. * named property list if the names match.
  242. *
  243. * @param propertyName the property to stop listening on
  244. * @param l the listener to remove
  245. * @throws NullPointerException if propertyName is null
  246. */
  247. public synchronized void
  248. removeVetoableChangeListener(String propertyName, VetoableChangeListener l)
  249. {
  250. if (children == null)
  251. return;
  252. VetoableChangeSupport s
  253. = (VetoableChangeSupport) children.get(propertyName);
  254. if (s == null)
  255. return;
  256. while (l instanceof VetoableChangeListenerProxy)
  257. {
  258. VetoableChangeListenerProxy p = (VetoableChangeListenerProxy) l;
  259. if (propertyName == null ? p.propertyName != null
  260. : ! propertyName.equals(p.propertyName))
  261. return;
  262. l = (VetoableChangeListener) p.getListener();
  263. }
  264. s.listeners.remove(l);
  265. if (s.listeners.isEmpty())
  266. {
  267. children.remove(propertyName);
  268. if (children.isEmpty())
  269. children = null;
  270. }
  271. }
  272. /**
  273. * Returns an array of all vetoable change listeners registered under the
  274. * given property name. If there are no registered listeners, this returns
  275. * an empty array.
  276. *
  277. * @return the array of registered listeners
  278. * @throws NullPointerException if propertyName is null
  279. * @since 1.4
  280. */
  281. public synchronized VetoableChangeListener[]
  282. getVetoableChangeListeners(String propertyName)
  283. {
  284. if (children == null)
  285. return new VetoableChangeListener[0];
  286. VetoableChangeSupport s
  287. = (VetoableChangeSupport) children.get(propertyName);
  288. if (s == null)
  289. return new VetoableChangeListener[0];
  290. return (VetoableChangeListener[])
  291. s.listeners.toArray(new VetoableChangeListener[s.listeners.size()]);
  292. }
  293. /**
  294. * Fire a PropertyChangeEvent containing the old and new values of the
  295. * property to all the global listeners, and to all the listeners for the
  296. * specified property name. This does nothing if old and new are non-null
  297. * and equal. If the change is vetoed, a new event is fired to notify
  298. * listeners about the rollback before the exception is thrown.
  299. *
  300. * @param propertyName the name of the property that changed
  301. * @param oldVal the old value
  302. * @param newVal the new value
  303. * @throws PropertyVetoException if the change is vetoed by a listener
  304. */
  305. public void fireVetoableChange(String propertyName,
  306. Object oldVal, Object newVal)
  307. throws PropertyVetoException
  308. {
  309. fireVetoableChange(new PropertyChangeEvent(source, propertyName,
  310. oldVal, newVal));
  311. }
  312. /**
  313. * Fire a PropertyChangeEvent containing the old and new values of the
  314. * property to all the global listeners, and to all the listeners for the
  315. * specified property name. This does nothing if old and new are equal.
  316. * If the change is vetoed, a new event is fired to notify listeners about
  317. * the rollback before the exception is thrown.
  318. *
  319. * @param propertyName the name of the property that changed
  320. * @param oldVal the old value
  321. * @param newVal the new value
  322. * @throws PropertyVetoException if the change is vetoed by a listener
  323. */
  324. public void fireVetoableChange(String propertyName, int oldVal, int newVal)
  325. throws PropertyVetoException
  326. {
  327. if (oldVal != newVal)
  328. fireVetoableChange(new PropertyChangeEvent(source, propertyName,
  329. Integer.valueOf(oldVal),
  330. Integer.valueOf(newVal)));
  331. }
  332. /**
  333. * Fire a PropertyChangeEvent containing the old and new values of the
  334. * property to all the global listeners, and to all the listeners for the
  335. * specified property name. This does nothing if old and new are equal.
  336. * If the change is vetoed, a new event is fired to notify listeners about
  337. * the rollback before the exception is thrown.
  338. *
  339. * @param propertyName the name of the property that changed
  340. * @param oldVal the old value
  341. * @param newVal the new value
  342. * @throws PropertyVetoException if the change is vetoed by a listener
  343. */
  344. public void fireVetoableChange(String propertyName,
  345. boolean oldVal, boolean newVal)
  346. throws PropertyVetoException
  347. {
  348. if (oldVal != newVal)
  349. fireVetoableChange(new PropertyChangeEvent(source, propertyName,
  350. Boolean.valueOf(oldVal),
  351. Boolean.valueOf(newVal)));
  352. }
  353. /**
  354. * Fire a PropertyChangeEvent to all the global listeners, and to all the
  355. * listeners for the specified property name. This does nothing if old and
  356. * new values of the event are equal. If the change is vetoed, a new event
  357. * is fired to notify listeners about the rollback before the exception is
  358. * thrown.
  359. *
  360. * @param event the event to fire
  361. * @throws NullPointerException if event is null
  362. * @throws PropertyVetoException if the change is vetoed by a listener
  363. */
  364. public void fireVetoableChange(PropertyChangeEvent event)
  365. throws PropertyVetoException
  366. {
  367. if (event.oldValue != null && event.oldValue.equals(event.newValue))
  368. return;
  369. Vector v = listeners; // Be thread-safe.
  370. if (v != null)
  371. {
  372. int i = v.size();
  373. try
  374. {
  375. while (--i >= 0)
  376. ((VetoableChangeListener) v.get(i)).vetoableChange(event);
  377. }
  378. catch (PropertyVetoException e)
  379. {
  380. event = event.rollback();
  381. int limit = i;
  382. i = v.size();
  383. while (--i >= limit)
  384. ((VetoableChangeListener) v.get(i)).vetoableChange(event);
  385. throw e;
  386. }
  387. }
  388. Hashtable h = children; // Be thread-safe.
  389. if (h != null && event.propertyName != null)
  390. {
  391. VetoableChangeSupport s
  392. = (VetoableChangeSupport) h.get(event.propertyName);
  393. if (s != null)
  394. {
  395. Vector v1 = s.listeners; // Be thread-safe.
  396. int i = v1 == null ? 0 : v1.size();
  397. try
  398. {
  399. while (--i >= 0)
  400. ((VetoableChangeListener) v1.get(i)).vetoableChange(event);
  401. }
  402. catch (PropertyVetoException e)
  403. {
  404. event = event.rollback();
  405. int limit = i;
  406. i = v.size();
  407. while (--i >= 0)
  408. ((VetoableChangeListener) v.get(i)).vetoableChange(event);
  409. i = v1.size();
  410. while (--i >= limit)
  411. ((VetoableChangeListener) v1.get(i)).vetoableChange(event);
  412. throw e;
  413. }
  414. }
  415. }
  416. }
  417. /**
  418. * Tell whether the specified property is being listened on or not. This
  419. * will only return <code>true</code> if there are listeners on all
  420. * properties or if there is a listener specifically on this property.
  421. *
  422. * @param propertyName the property that may be listened on
  423. * @return whether the property is being listened on
  424. * @throws NullPointerException if propertyName is null
  425. */
  426. public synchronized boolean hasListeners(String propertyName)
  427. {
  428. return listeners != null || (children != null
  429. && children.get(propertyName) != null);
  430. }
  431. /**
  432. * Saves the state of the object to the stream.
  433. *
  434. * @param s the stream to write to
  435. * @throws IOException if anything goes wrong
  436. * @serialData this writes out a null-terminated list of serializable
  437. * global vetoable change listeners (the listeners for a named
  438. * property are written out as the global listeners of the
  439. * children, when the children hashtable is saved)
  440. */
  441. private synchronized void writeObject(ObjectOutputStream s)
  442. throws IOException
  443. {
  444. s.defaultWriteObject();
  445. if (listeners != null)
  446. {
  447. int i = listeners.size();
  448. while (--i >= 0)
  449. if (listeners.get(i) instanceof Serializable)
  450. s.writeObject(listeners.get(i));
  451. }
  452. s.writeObject(null);
  453. }
  454. /**
  455. * Reads the object back from stream (deserialization).
  456. *
  457. * XXX Since serialization for 1.1 streams was not documented, this may
  458. * not work if vetoableChangeSupportSerializedDataVersion is 1.
  459. *
  460. * @param s the stream to read from
  461. * @throws IOException if reading the stream fails
  462. * @throws ClassNotFoundException if deserialization fails
  463. * @serialData this reads in a null-terminated list of serializable
  464. * global vetoable change listeners (the listeners for a named
  465. * property are written out as the global listeners of the
  466. * children, when the children hashtable is saved)
  467. */
  468. private void readObject(ObjectInputStream s)
  469. throws IOException, ClassNotFoundException
  470. {
  471. s.defaultReadObject();
  472. VetoableChangeListener l = (VetoableChangeListener) s.readObject();
  473. while (l != null)
  474. {
  475. addVetoableChangeListener(l);
  476. l = (VetoableChangeListener) s.readObject();
  477. }
  478. // Sun is not as careful with children as we are, and lets some proxys
  479. // in that can never receive events. So, we clean up anything that got
  480. // serialized, to make sure our invariants hold.
  481. if (children != null)
  482. {
  483. int i = children.size();
  484. Iterator iter = children.entrySet().iterator();
  485. while (--i >= 0)
  486. {
  487. Entry e = (Entry) iter.next();
  488. String name = (String) e.getKey();
  489. VetoableChangeSupport vcs = (VetoableChangeSupport) e.getValue();
  490. if (vcs.listeners == null)
  491. vcs.listeners = new Vector();
  492. if (vcs.children != null)
  493. vcs.listeners.addAll
  494. (Arrays.asList(vcs.getVetoableChangeListeners(name)));
  495. if (vcs.listeners.size() == 0)
  496. iter.remove();
  497. else
  498. vcs.children = null;
  499. }
  500. if (children.size() == 0)
  501. children = null;
  502. }
  503. }
  504. } // class VetoableChangeSupport