01: /*
02: * Copyright 2000,2005 wingS development team.
03: *
04: * This file is part of wingS (http://wingsframework.org).
05: *
06: * wingS is free software; you can redistribute it and/or modify
07: * it under the terms of the GNU Lesser General Public License
08: * as published by the Free Software Foundation; either version 2.1
09: * of the License, or (at your option) any later version.
10: *
11: * Please see COPYING for the complete licence.
12: */
13: package org.wings;
14:
15: import javax.swing.*;
16: import java.beans.PropertyChangeListener;
17: import java.lang.ref.ReferenceQueue;
18: import java.lang.ref.WeakReference;
19:
20: /**
21: */
22: abstract class AbstractActionPropertyChangeListener implements
23: PropertyChangeListener {
24: private static ReferenceQueue queue;
25: private WeakReference target;
26: private Action action;
27:
28: AbstractActionPropertyChangeListener(SComponent c, Action a) {
29: super ();
30: setTarget(c);
31: this .action = a;
32: }
33:
34: public void setTarget(SComponent c) {
35: if (queue == null) {
36: queue = new ReferenceQueue();
37: }
38:
39: OwnedWeakReference r;
40: while ((r = (OwnedWeakReference) queue.poll()) != null) {
41: AbstractActionPropertyChangeListener oldPCL = (AbstractActionPropertyChangeListener) r
42: .getOwner();
43: Action oldAction = oldPCL.getAction();
44: if (oldAction != null)
45: oldAction.removePropertyChangeListener(oldPCL);
46: }
47: this .target = new OwnedWeakReference(c, queue, this );
48: }
49:
50: public SComponent getTarget() {
51: return (SComponent) this .target.get();
52: }
53:
54: public Action getAction() {
55: return action;
56: }
57:
58: private static class OwnedWeakReference extends WeakReference {
59: private Object owner;
60:
61: OwnedWeakReference(Object target, ReferenceQueue queue,
62: Object owner) {
63: super (target, queue);
64: this .owner = owner;
65: }
66:
67: public Object getOwner() {
68: return owner;
69: }
70: }
71: }
|