KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > wings > AbstractActionPropertyChangeListener


1 /*
2  * $Id: AbstractActionPropertyChangeListener.java,v 1.3 2004/12/01 07:54:06 hengels Exp $
3  * Copyright 2000,2005 wingS development team.
4  *
5  * This file is part of wingS (http://www.j-wings.org).
6  *
7  * wingS is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU Lesser General Public License
9  * as published by the Free Software Foundation; either version 2.1
10  * of the License, or (at your option) any later version.
11  *
12  * Please see COPYING for the complete licence.
13  */

14 package org.wings;
15
16 import javax.swing.*;
17 import java.beans.PropertyChangeListener JavaDoc;
18 import java.lang.ref.ReferenceQueue JavaDoc;
19 import java.lang.ref.WeakReference JavaDoc;
20
21 /**
22  * @author
23  * @version $Revision: 1.3 $
24  */

25 abstract class AbstractActionPropertyChangeListener
26         implements PropertyChangeListener JavaDoc {
27     private static ReferenceQueue JavaDoc queue;
28
29     private WeakReference JavaDoc 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 JavaDoc();
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 JavaDoc {
63         private Object JavaDoc owner;
64
65         OwnedWeakReference(Object JavaDoc target, ReferenceQueue JavaDoc queue, Object JavaDoc owner) {
66             super(target, queue);
67             this.owner = owner;
68         }
69
70         public Object JavaDoc getOwner() {
71             return owner;
72         }
73     }
74 }
75
76
77
Popular Tags