KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javax > swing > AbstractActionPropertyChangeListener


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
9 import java.beans.PropertyChangeEvent JavaDoc;
10 import java.beans.PropertyChangeListener JavaDoc;
11 import java.lang.ref.WeakReference JavaDoc;
12 import java.lang.ref.ReferenceQueue JavaDoc;
13
14 /**
15  * A package-private PropertyChangeListener which listens for
16  * property changes on an Action and updates the properties
17  * of an ActionEvent source.
18  * <p>
19  * <strong>Warning:</strong>
20  * Serialized objects of this class will not be compatible with
21  * future Swing releases. The current serialization support is
22  * appropriate for short term storage or RMI between applications running
23  * the same version of Swing. As of 1.4, support for long term storage
24  * of all JavaBeans<sup><font size="-2">TM</font></sup>
25  * has been added to the <code>java.beans</code> package.
26  * Please see {@link java.beans.XMLEncoder}.
27  *
28  * @version 1.11 12/19/03
29  * @author Georges Saab
30  * @see AbstractButton
31  */

32
33 abstract class AbstractActionPropertyChangeListener implements PropertyChangeListener JavaDoc {
34     private static ReferenceQueue JavaDoc queue;
35     private WeakReference JavaDoc target;
36     private Action JavaDoc action;
37     
38     AbstractActionPropertyChangeListener(JComponent JavaDoc c, Action JavaDoc a) {
39     super();
40     setTarget(c);
41     this.action = a;
42     }
43
44     public void setTarget(JComponent JavaDoc c) {
45         if (queue==null) {
46         queue = new ReferenceQueue JavaDoc();
47     }
48     // Check to see whether any old buttons have
49
// been enqueued for GC. If so, look up their
50
// PCL instance and remove it from its Action.
51
OwnedWeakReference r;
52     while ( (r = (OwnedWeakReference)queue.poll()) != null) {
53         AbstractActionPropertyChangeListener JavaDoc oldPCL =
54             (AbstractActionPropertyChangeListener JavaDoc)r.getOwner();
55         Action JavaDoc oldAction = oldPCL.getAction();
56         if (oldAction!=null) {
57             oldAction.removePropertyChangeListener(oldPCL);
58         }
59     }
60     this.target = new OwnedWeakReference(c, queue, this);
61     }
62     
63     public JComponent JavaDoc getTarget() {
64         return (JComponent JavaDoc)this.target.get();
65     }
66
67     public Action JavaDoc getAction() {
68       return action;
69     }
70
71     private static class OwnedWeakReference extends WeakReference JavaDoc {
72         private Object JavaDoc owner;
73
74         OwnedWeakReference(Object JavaDoc target, ReferenceQueue JavaDoc queue, Object JavaDoc owner) {
75         super(target, queue);
76         this.owner = owner;
77     }
78
79     public Object JavaDoc getOwner() {
80         return owner;
81     }
82     }
83 }
84
Popular Tags