PropertyChangeSupport.java 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546
  1. /* PropertyChangeSupport.java -- support to manage property 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. * PropertyChangeSupport makes it easy to fire property 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 PropertyChangeSupport implements Serializable
  56. {
  57. /**
  58. * Compatible with JDK 1.1+.
  59. */
  60. private static final long serialVersionUID = 6401253773779951803L;
  61. /**
  62. * Maps property names (String) to named listeners (PropertyChangeSupport).
  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 propertyChangeSupportSerializedDataVersion = 2;
  81. /**
  82. * The list of all registered property 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 PropertyChangeSupport 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 PropertyChangeSupport(Object source)
  98. {
  99. this.source = source;
  100. if (source == null)
  101. throw new NullPointerException();
  102. }
  103. /**
  104. * Adds a PropertyChangeListener to the list of global listeners. All
  105. * property 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. * property change. Adding a null listener is silently ignored.
  109. * This method will unwrap a PropertyChangeListenerProxy,
  110. * registering the underlying delegate to the named property list.
  111. *
  112. * @param l the listener to add
  113. */
  114. public synchronized void addPropertyChangeListener(PropertyChangeListener l)
  115. {
  116. if (l == null)
  117. return;
  118. if (l instanceof PropertyChangeListenerProxy)
  119. {
  120. PropertyChangeListenerProxy p = (PropertyChangeListenerProxy) l;
  121. addPropertyChangeListener(p.propertyName,
  122. (PropertyChangeListener) p.getListener());
  123. }
  124. else
  125. {
  126. if (listeners == null)
  127. listeners = new Vector();
  128. listeners.add(l);
  129. }
  130. }
  131. /**
  132. * Removes a PropertyChangeListener from the list of global listeners. If
  133. * any specific properties are being listened on, they must be deregistered
  134. * by themselves; this will only remove the general listener to all
  135. * properties. If <code>add()</code> has been called multiple times for a
  136. * particular listener, <code>remove()</code> will have to be called the
  137. * same number of times to deregister it. This method will unwrap a
  138. * PropertyChangeListenerProxy, removing the underlying delegate from the
  139. * named property list.
  140. *
  141. * @param l the listener to remove
  142. */
  143. public synchronized void
  144. removePropertyChangeListener(PropertyChangeListener l)
  145. {
  146. if (l instanceof PropertyChangeListenerProxy)
  147. {
  148. PropertyChangeListenerProxy p = (PropertyChangeListenerProxy) l;
  149. removePropertyChangeListener(p.propertyName,
  150. (PropertyChangeListener) p.getListener());
  151. }
  152. else if (listeners != null)
  153. {
  154. listeners.remove(l);
  155. if (listeners.isEmpty())
  156. listeners = null;
  157. }
  158. }
  159. /**
  160. * Returns an array of all registered property change listeners. Those that
  161. * were registered under a name will be wrapped in a
  162. * <code>PropertyChangeListenerProxy</code>, so you must check whether the
  163. * listener is an instance of the proxy class in order to see what name the
  164. * real listener is registered under. If there are no registered listeners,
  165. * this returns an empty array.
  166. *
  167. * @return the array of registered listeners
  168. * @see PropertyChangeListenerProxy
  169. * @since 1.4
  170. */
  171. public synchronized PropertyChangeListener[] getPropertyChangeListeners()
  172. {
  173. ArrayList list = new ArrayList();
  174. if (listeners != null)
  175. list.addAll(listeners);
  176. if (children != null)
  177. {
  178. int i = children.size();
  179. Iterator iter = children.entrySet().iterator();
  180. while (--i >= 0)
  181. {
  182. Entry e = (Entry) iter.next();
  183. String name = (String) e.getKey();
  184. Vector v = ((PropertyChangeSupport) e.getValue()).listeners;
  185. int j = v.size();
  186. while (--j >= 0)
  187. list.add(new PropertyChangeListenerProxy
  188. (name, (PropertyChangeListener) v.get(j)));
  189. }
  190. }
  191. return (PropertyChangeListener[])
  192. list.toArray(new PropertyChangeListener[list.size()]);
  193. }
  194. /**
  195. * Adds a PropertyChangeListener listening on the specified property. Events
  196. * will be sent to the listener only if the property name matches. The
  197. * listener add is not unique; that is, <em>n</em> adds on a particular
  198. * property for a particular listener will result in <em>n</em> events
  199. * being sent to that listener when that property is changed. The effect is
  200. * cumulative, too; if you are registered to listen to receive events on
  201. * all property changes, and then you register on a particular property,
  202. * you will receive change events for that property twice. Adding a null
  203. * listener is silently ignored. This method will unwrap a
  204. * PropertyChangeListenerProxy, registering the underlying
  205. * delegate to the named property list if the names match, and discarding
  206. * it otherwise.
  207. *
  208. * @param propertyName the name of the property to listen on
  209. * @param l the listener to add
  210. * @throws NullPointerException if propertyName is null
  211. */
  212. public synchronized void addPropertyChangeListener(String propertyName,
  213. PropertyChangeListener l)
  214. {
  215. if (l == null)
  216. return;
  217. while (l instanceof PropertyChangeListenerProxy)
  218. {
  219. PropertyChangeListenerProxy p = (PropertyChangeListenerProxy) l;
  220. if (propertyName == null ? p.propertyName != null
  221. : ! propertyName.equals(p.propertyName))
  222. return;
  223. l = (PropertyChangeListener) p.getListener();
  224. }
  225. PropertyChangeSupport s = null;
  226. if (children == null)
  227. children = new Hashtable();
  228. else
  229. s = (PropertyChangeSupport) children.get(propertyName);
  230. if (s == null)
  231. {
  232. s = new PropertyChangeSupport(source);
  233. s.listeners = new Vector();
  234. children.put(propertyName, s);
  235. }
  236. s.listeners.add(l);
  237. }
  238. /**
  239. * Removes a PropertyChangeListener from listening to a specific property.
  240. * If <code>add()</code> has been called multiple times for a particular
  241. * listener on a property, <code>remove()</code> will have to be called the
  242. * same number of times to deregister it. This method will unwrap a
  243. * PropertyChangeListenerProxy, removing the underlying delegate from the
  244. * named property list if the names match.
  245. *
  246. * @param propertyName the property to stop listening on
  247. * @param l the listener to remove
  248. * @throws NullPointerException if propertyName is null
  249. */
  250. public synchronized void
  251. removePropertyChangeListener(String propertyName, PropertyChangeListener l)
  252. {
  253. if (children == null)
  254. return;
  255. PropertyChangeSupport s
  256. = (PropertyChangeSupport) children.get(propertyName);
  257. if (s == null)
  258. return;
  259. while (l instanceof PropertyChangeListenerProxy)
  260. {
  261. PropertyChangeListenerProxy p = (PropertyChangeListenerProxy) l;
  262. if (propertyName == null ? p.propertyName != null
  263. : ! propertyName.equals(p.propertyName))
  264. return;
  265. l = (PropertyChangeListener) p.getListener();
  266. }
  267. s.listeners.remove(l);
  268. if (s.listeners.isEmpty())
  269. {
  270. children.remove(propertyName);
  271. if (children.isEmpty())
  272. children = null;
  273. }
  274. }
  275. /**
  276. * Returns an array of all property change listeners registered under the
  277. * given property name. If there are no registered listeners, or
  278. * propertyName is null, this returns an empty array.
  279. *
  280. * @return the array of registered listeners
  281. * @since 1.4
  282. */
  283. public synchronized PropertyChangeListener[]
  284. getPropertyChangeListeners(String propertyName)
  285. {
  286. if (children == null || propertyName == null)
  287. return new PropertyChangeListener[0];
  288. PropertyChangeSupport s
  289. = (PropertyChangeSupport) children.get(propertyName);
  290. if (s == null)
  291. return new PropertyChangeListener[0];
  292. return (PropertyChangeListener[])
  293. s.listeners.toArray(new PropertyChangeListener[s.listeners.size()]);
  294. }
  295. /**
  296. * Fire a PropertyChangeEvent containing the old and new values of the
  297. * property to all the global listeners, and to all the listeners for the
  298. * specified property name. This does nothing if old and new are non-null
  299. * and equal.
  300. *
  301. * @param propertyName the name of the property that changed
  302. * @param oldVal the old value
  303. * @param newVal the new value
  304. */
  305. public void firePropertyChange(String propertyName,
  306. Object oldVal, Object newVal)
  307. {
  308. firePropertyChange(new PropertyChangeEvent(source, propertyName,
  309. oldVal, newVal));
  310. }
  311. /**
  312. * Fire a PropertyChangeEvent containing the old and new values of the
  313. * property to all the global listeners, and to all the listeners for the
  314. * specified property name. This does nothing if old and new are equal.
  315. *
  316. * @param propertyName the name of the property that changed
  317. * @param oldVal the old value
  318. * @param newVal the new value
  319. */
  320. public void firePropertyChange(String propertyName, int oldVal, int newVal)
  321. {
  322. if (oldVal != newVal)
  323. firePropertyChange(new PropertyChangeEvent(source, propertyName,
  324. Integer.valueOf(oldVal),
  325. Integer.valueOf(newVal)));
  326. }
  327. /**
  328. * Fire a PropertyChangeEvent containing the old and new values of the
  329. * property to all the global listeners, and to all the listeners for the
  330. * specified property name. This does nothing if old and new are equal.
  331. *
  332. * @param propertyName the name of the property that changed
  333. * @param oldVal the old value
  334. * @param newVal the new value
  335. */
  336. public void firePropertyChange(String propertyName,
  337. boolean oldVal, boolean newVal)
  338. {
  339. if (oldVal != newVal)
  340. firePropertyChange(new PropertyChangeEvent(source, propertyName,
  341. Boolean.valueOf(oldVal),
  342. Boolean.valueOf(newVal)));
  343. }
  344. /**
  345. * Fire a PropertyChangeEvent to all the global listeners, and to all the
  346. * listeners for the specified property name. This does nothing if old and
  347. * new values of the event are equal.
  348. *
  349. * @param event the event to fire
  350. * @throws NullPointerException if event is null
  351. */
  352. public void firePropertyChange(PropertyChangeEvent event)
  353. {
  354. if (event.oldValue != null && event.oldValue.equals(event.newValue))
  355. return;
  356. Vector v = listeners; // Be thread-safe.
  357. if (v != null)
  358. {
  359. int i = v.size();
  360. while (--i >= 0)
  361. ((PropertyChangeListener) v.get(i)).propertyChange(event);
  362. }
  363. Hashtable h = children; // Be thread-safe.
  364. if (h != null && event.propertyName != null)
  365. {
  366. PropertyChangeSupport s
  367. = (PropertyChangeSupport) h.get(event.propertyName);
  368. if (s != null)
  369. {
  370. v = s.listeners; // Be thread-safe.
  371. int i = v == null ? 0 : v.size();
  372. while (--i >= 0)
  373. ((PropertyChangeListener) v.get(i)).propertyChange(event);
  374. }
  375. }
  376. }
  377. /**
  378. * Fire an indexed property change event. This will only fire
  379. * an event if the old and new values are not equal and not null.
  380. * @param name the name of the property which changed
  381. * @param index the index of the property which changed
  382. * @param oldValue the old value of the property
  383. * @param newValue the new value of the property
  384. * @since 1.5
  385. */
  386. public void fireIndexedPropertyChange(String name, int index,
  387. Object oldValue, Object newValue)
  388. {
  389. // Argument checking is done in firePropertyChange(PropertyChangeEvent) .
  390. firePropertyChange(new IndexedPropertyChangeEvent(source, name,
  391. oldValue, newValue,
  392. index));
  393. }
  394. /**
  395. * Fire an indexed property change event. This will only fire
  396. * an event if the old and new values are not equal.
  397. * @param name the name of the property which changed
  398. * @param index the index of the property which changed
  399. * @param oldValue the old value of the property
  400. * @param newValue the new value of the property
  401. * @since 1.5
  402. */
  403. public void fireIndexedPropertyChange(String name, int index,
  404. int oldValue, int newValue)
  405. {
  406. if (oldValue != newValue)
  407. fireIndexedPropertyChange(name, index, Integer.valueOf(oldValue),
  408. Integer.valueOf(newValue));
  409. }
  410. /**
  411. * Fire an indexed property change event. This will only fire
  412. * an event if the old and new values are not equal.
  413. * @param name the name of the property which changed
  414. * @param index the index of the property which changed
  415. * @param oldValue the old value of the property
  416. * @param newValue the new value of the property
  417. * @since 1.5
  418. */
  419. public void fireIndexedPropertyChange(String name, int index,
  420. boolean oldValue, boolean newValue)
  421. {
  422. if (oldValue != newValue)
  423. fireIndexedPropertyChange(name, index, Boolean.valueOf(oldValue),
  424. Boolean.valueOf(newValue));
  425. }
  426. /**
  427. * Tell whether the specified property is being listened on or not. This
  428. * will only return <code>true</code> if there are listeners on all
  429. * properties or if there is a listener specifically on this property.
  430. *
  431. * @param propertyName the property that may be listened on
  432. * @return whether the property is being listened on
  433. */
  434. public synchronized boolean hasListeners(String propertyName)
  435. {
  436. return listeners != null || (children != null
  437. && children.get(propertyName) != null);
  438. }
  439. /**
  440. * Saves the state of the object to the stream.
  441. *
  442. * @param s the stream to write to
  443. * @throws IOException if anything goes wrong
  444. * @serialData this writes out a null-terminated list of serializable
  445. * global property change listeners (the listeners for a named
  446. * property are written out as the global listeners of the
  447. * children, when the children hashtable is saved)
  448. */
  449. private synchronized void writeObject(ObjectOutputStream s)
  450. throws IOException
  451. {
  452. s.defaultWriteObject();
  453. if (listeners != null)
  454. {
  455. int i = listeners.size();
  456. while (--i >= 0)
  457. if (listeners.get(i) instanceof Serializable)
  458. s.writeObject(listeners.get(i));
  459. }
  460. s.writeObject(null);
  461. }
  462. /**
  463. * Reads the object back from stream (deserialization).
  464. *
  465. * XXX Since serialization for 1.1 streams was not documented, this may
  466. * not work if propertyChangeSupportSerializedDataVersion is 1.
  467. *
  468. * @param s the stream to read from
  469. * @throws IOException if reading the stream fails
  470. * @throws ClassNotFoundException if deserialization fails
  471. * @serialData this reads in a null-terminated list of serializable
  472. * global property change listeners (the listeners for a named
  473. * property are written out as the global listeners of the
  474. * children, when the children hashtable is saved)
  475. */
  476. private void readObject(ObjectInputStream s)
  477. throws IOException, ClassNotFoundException
  478. {
  479. s.defaultReadObject();
  480. PropertyChangeListener l = (PropertyChangeListener) s.readObject();
  481. while (l != null)
  482. {
  483. addPropertyChangeListener(l);
  484. l = (PropertyChangeListener) s.readObject();
  485. }
  486. // Sun is not as careful with children as we are, and lets some proxys
  487. // in that can never receive events. So, we clean up anything that got
  488. // serialized, to make sure our invariants hold.
  489. if (children != null)
  490. {
  491. int i = children.size();
  492. Iterator iter = children.entrySet().iterator();
  493. while (--i >= 0)
  494. {
  495. Entry e = (Entry) iter.next();
  496. String name = (String) e.getKey();
  497. PropertyChangeSupport pcs = (PropertyChangeSupport) e.getValue();
  498. if (pcs.listeners == null)
  499. pcs.listeners = new Vector();
  500. if (pcs.children != null)
  501. pcs.listeners.addAll
  502. (Arrays.asList(pcs.getPropertyChangeListeners(name)));
  503. if (pcs.listeners.size() == 0)
  504. iter.remove();
  505. else
  506. pcs.children = null;
  507. }
  508. if (children.size() == 0)
  509. children = null;
  510. }
  511. }
  512. } // class PropertyChangeSupport