1 7 package javax.swing; 8 9 import java.beans.PropertyChangeEvent ; 10 import java.beans.PropertyChangeListener ; 11 import java.lang.ref.WeakReference ; 12 import java.lang.ref.ReferenceQueue ; 13 14 32 33 abstract class AbstractActionPropertyChangeListener implements PropertyChangeListener { 34 private static ReferenceQueue queue; 35 private WeakReference target; 36 private Action action; 37 38 AbstractActionPropertyChangeListener(JComponent c, Action a) { 39 super(); 40 setTarget(c); 41 this.action = a; 42 } 43 44 public void setTarget(JComponent c) { 45 if (queue==null) { 46 queue = new ReferenceQueue (); 47 } 48 OwnedWeakReference r; 52 while ( (r = (OwnedWeakReference)queue.poll()) != null) { 53 AbstractActionPropertyChangeListener oldPCL = 54 (AbstractActionPropertyChangeListener )r.getOwner(); 55 Action 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 getTarget() { 64 return (JComponent )this.target.get(); 65 } 66 67 public Action getAction() { 68 return action; 69 } 70 71 private static class OwnedWeakReference extends WeakReference { 72 private Object owner; 73 74 OwnedWeakReference(Object target, ReferenceQueue queue, Object owner) { 75 super(target, queue); 76 this.owner = owner; 77 } 78 79 public Object getOwner() { 80 return owner; 81 } 82 } 83 } 84 | Popular Tags |