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