1 14 package org.wings; 15 16 import javax.swing.*; 17 import java.beans.PropertyChangeListener ; 18 import java.lang.ref.ReferenceQueue ; 19 import java.lang.ref.WeakReference ; 20 21 25 abstract class AbstractActionPropertyChangeListener 26 implements PropertyChangeListener { 27 private static ReferenceQueue queue; 28 29 private WeakReference target; 30 private Action action; 31 32 AbstractActionPropertyChangeListener(SComponent c, Action a) { 33 super(); 34 setTarget(c); 35 this.action = a; 36 } 37 38 public void setTarget(SComponent c) { 39 if (queue == null) { 40 queue = new ReferenceQueue (); 41 } 42 43 OwnedWeakReference r; 44 while ((r = (OwnedWeakReference) queue.poll()) != null) { 45 AbstractActionPropertyChangeListener oldPCL = 46 (AbstractActionPropertyChangeListener) r.getOwner(); 47 Action oldAction = oldPCL.getAction(); 48 if (oldAction != null) 49 oldAction.removePropertyChangeListener(oldPCL); 50 } 51 this.target = new OwnedWeakReference(c, queue, this); 52 } 53 54 public SComponent getTarget() { 55 return (SComponent) this.target.get(); 56 } 57 58 public Action getAction() { 59 return action; 60 } 61 62 private static class OwnedWeakReference extends WeakReference { 63 private Object owner; 64 65 OwnedWeakReference(Object target, ReferenceQueue queue, Object owner) { 66 super(target, queue); 67 this.owner = owner; 68 } 69 70 public Object getOwner() { 71 return owner; 72 } 73 } 74 } 75 76 77 | Popular Tags |