KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > core > output2 > ui > WeakAction


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19 /*
20  * WeakAction.java
21  *
22  * Created on May 14, 2004, 11:01 PM
23  */

24
25 package org.netbeans.core.output2.ui;
26
27 import org.openide.util.WeakListeners;
28
29 import javax.swing.*;
30 import java.awt.event.ActionEvent JavaDoc;
31 import java.beans.PropertyChangeEvent JavaDoc;
32 import java.beans.PropertyChangeListener JavaDoc;
33 import java.lang.ref.Reference JavaDoc;
34 import java.lang.ref.WeakReference JavaDoc;
35 import java.util.ArrayList JavaDoc;
36 import java.util.Iterator JavaDoc;
37 import java.util.List JavaDoc;
38
39 /**
40  * Action which delegates to a weakly referenced original.
41  *
42  * @author Tim Boudreau
43  */

44 class WeakAction implements Action, PropertyChangeListener JavaDoc {
45     private Reference JavaDoc original;
46     private Icon icon;
47     private List JavaDoc listeners = new ArrayList JavaDoc();
48     private String JavaDoc name = null;
49
50     /** Creates a new instance of WeakAction */
51     public WeakAction(Action original) {
52         wasEnabled = original.isEnabled();
53         icon = (Icon) original.getValue (SMALL_ICON);
54         name = (String JavaDoc) original.getValue (NAME);
55         this.original = new WeakReference JavaDoc (original);
56         original.addPropertyChangeListener(WeakListeners.propertyChange(this, original));
57     }
58     
59     public void actionPerformed(ActionEvent JavaDoc actionEvent) {
60         Action orig = getOriginal();
61         if (orig != null) {
62             orig.actionPerformed (actionEvent);
63         }
64     }
65     
66     public synchronized void addPropertyChangeListener(PropertyChangeListener JavaDoc pce) {
67         listeners.add (pce);
68     }
69     
70     public Object JavaDoc getValue(String JavaDoc str) {
71         if (SMALL_ICON.equals(str)) {
72             return icon;
73         } else {
74             Action orig = getOriginal();
75             if (orig != null) {
76                 return orig.getValue(str);
77             } else if (NAME.equals(str)) {
78                 //Avoid NPE if action is disposed but shown in popup
79
return name;
80             }
81         }
82         return null;
83     }
84     
85     private boolean wasEnabled = true;
86     public boolean isEnabled() {
87         Action orig = getOriginal();
88         if (orig != null) {
89             wasEnabled = orig.isEnabled();
90             return wasEnabled;
91         }
92         return false;
93     }
94     
95     public void putValue(String JavaDoc str, Object JavaDoc obj) {
96         if (SMALL_ICON.equals(str)) {
97             icon = (Icon) obj;
98         } else {
99             Action orig = getOriginal();
100             if (orig != null) {
101                 orig.putValue(str, obj);
102             }
103         }
104     }
105     
106     public synchronized void removePropertyChangeListener(PropertyChangeListener JavaDoc pce) {
107         listeners.remove (pce);
108     }
109     
110     public void setEnabled(boolean val) {
111         Action orig = getOriginal();
112         if (orig != null) {
113             orig.setEnabled(val);
114         }
115     }
116     
117     private boolean hadOriginal = true;
118     private Action getOriginal() {
119         Action result = (Action) original.get();
120         if (result == null && hadOriginal && wasEnabled) {
121             hadOriginal = false;
122             firePropertyChange ("enabled", Boolean.TRUE, Boolean.FALSE); //NOI18N
123
}
124         return result;
125     }
126     
127     private synchronized void firePropertyChange(String JavaDoc nm, Object JavaDoc old, Object JavaDoc nue) {
128         PropertyChangeEvent JavaDoc pce = new PropertyChangeEvent JavaDoc (this, nm, old, nue);
129         for (Iterator JavaDoc i=listeners.iterator(); i.hasNext();) {
130             PropertyChangeListener JavaDoc pcl = (PropertyChangeListener JavaDoc) i.next();
131             pcl.propertyChange(pce);
132         }
133     }
134     
135     public void propertyChange(java.beans.PropertyChangeEvent JavaDoc pce) {
136         firePropertyChange (pce.getPropertyName(), pce.getOldValue(),
137             pce.getNewValue());
138         if ("enabled".equals(pce.getPropertyName())) { //NOI18n
139
wasEnabled = Boolean.TRUE.equals(pce.getNewValue());
140        }
141     }
142     
143 }
144
Popular Tags