1. /*
  2. * @(#)AbstractActionPropertyChangeListener.java 1.11 03/12/19
  3. *
  4. * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. package javax.swing;
  8. import java.beans.PropertyChangeEvent;
  9. import java.beans.PropertyChangeListener;
  10. import java.lang.ref.WeakReference;
  11. import java.lang.ref.ReferenceQueue;
  12. /**
  13. * A package-private PropertyChangeListener which listens for
  14. * property changes on an Action and updates the properties
  15. * of an ActionEvent source.
  16. * <p>
  17. * <strong>Warning:</strong>
  18. * Serialized objects of this class will not be compatible with
  19. * future Swing releases. The current serialization support is
  20. * appropriate for short term storage or RMI between applications running
  21. * the same version of Swing. As of 1.4, support for long term storage
  22. * of all JavaBeans<sup><font size="-2">TM</font></sup>
  23. * has been added to the <code>java.beans</code> package.
  24. * Please see {@link java.beans.XMLEncoder}.
  25. *
  26. * @version 1.11 12/19/03
  27. * @author Georges Saab
  28. * @see AbstractButton
  29. */
  30. abstract class AbstractActionPropertyChangeListener implements PropertyChangeListener {
  31. private static ReferenceQueue queue;
  32. private WeakReference target;
  33. private Action action;
  34. AbstractActionPropertyChangeListener(JComponent c, Action a) {
  35. super();
  36. setTarget(c);
  37. this.action = a;
  38. }
  39. public void setTarget(JComponent c) {
  40. if (queue==null) {
  41. queue = new ReferenceQueue();
  42. }
  43. // Check to see whether any old buttons have
  44. // been enqueued for GC. If so, look up their
  45. // PCL instance and remove it from its Action.
  46. OwnedWeakReference r;
  47. while ( (r = (OwnedWeakReference)queue.poll()) != null) {
  48. AbstractActionPropertyChangeListener oldPCL =
  49. (AbstractActionPropertyChangeListener)r.getOwner();
  50. Action oldAction = oldPCL.getAction();
  51. if (oldAction!=null) {
  52. oldAction.removePropertyChangeListener(oldPCL);
  53. }
  54. }
  55. this.target = new OwnedWeakReference(c, queue, this);
  56. }
  57. public JComponent getTarget() {
  58. return (JComponent)this.target.get();
  59. }
  60. public Action getAction() {
  61. return action;
  62. }
  63. private static class OwnedWeakReference extends WeakReference {
  64. private Object owner;
  65. OwnedWeakReference(Object target, ReferenceQueue queue, Object owner) {
  66. super(target, queue);
  67. this.owner = owner;
  68. }
  69. public Object getOwner() {
  70. return owner;
  71. }
  72. }
  73. }