KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > debugger > jpda > ui > actions > EvaluateAction


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 package org.netbeans.modules.debugger.jpda.ui.actions;
21
22 import java.awt.event.ActionEvent JavaDoc;
23 import java.beans.PropertyChangeEvent JavaDoc;
24 import java.beans.PropertyChangeListener JavaDoc;
25 import java.lang.ref.Reference JavaDoc;
26 import java.lang.ref.WeakReference JavaDoc;
27 import javax.swing.AbstractAction JavaDoc;
28 import javax.swing.Action JavaDoc;
29 import javax.swing.SwingUtilities JavaDoc;
30 import org.netbeans.api.debugger.DebuggerEngine;
31 import org.netbeans.api.debugger.DebuggerManager;
32 import org.netbeans.api.debugger.DebuggerManagerAdapter;
33 import org.netbeans.api.debugger.jpda.JPDADebugger;
34 import org.netbeans.modules.debugger.jpda.ui.Evaluator;
35 import org.openide.util.HelpCtx;
36 import org.openide.util.NbBundle;
37 import org.openide.util.actions.CallableSystemAction;
38
39 /**
40  * Invokes the expression evaluator GUI
41  *
42  * @author Martin Entlicher
43  */

44 public class EvaluateAction extends AbstractAction JavaDoc implements PropertyChangeListener JavaDoc,
45                                                               Runnable JavaDoc {
46     
47     private EnableListener listener;
48     private transient Reference JavaDoc lastDebuggerRef = new WeakReference JavaDoc(null);
49
50     public EvaluateAction () {
51         listener = new EnableListener (this);
52         DebuggerManager.getDebuggerManager().addDebuggerListener(
53                 DebuggerManager.PROP_CURRENT_ENGINE,
54                 listener);
55         putValue (
56             Action.NAME,
57             NbBundle.getMessage(EvaluateAction.class, "CTL_Evaluate") // NOI18N
58
);
59         checkEnabled();
60     }
61     
62     private synchronized boolean canBeEnabled() {
63         DebuggerEngine de = DebuggerManager.getDebuggerManager().getCurrentEngine();
64         if (de == null) return false;
65         JPDADebugger debugger = (JPDADebugger) de.lookupFirst(null, JPDADebugger.class);
66         JPDADebugger lastDebugger = (JPDADebugger) lastDebuggerRef.get();
67         if (lastDebugger != null && debugger != lastDebugger) {
68             lastDebugger.removePropertyChangeListener(
69                     JPDADebugger.PROP_CURRENT_THREAD,
70                     this);
71             lastDebuggerRef = new WeakReference JavaDoc(null);
72         }
73         if (debugger != null) {
74             lastDebuggerRef = new WeakReference JavaDoc(debugger);
75             debugger.addPropertyChangeListener(
76                     JPDADebugger.PROP_CURRENT_THREAD,
77                     this);
78             return (debugger.getCurrentThread() != null);
79         } else {
80             return false;
81         }
82     }
83     
84     private void checkEnabled() {
85         SwingUtilities.invokeLater(this);
86     }
87     
88     public void run() {
89         setEnabled(canBeEnabled());
90     }
91     
92     public void actionPerformed (ActionEvent JavaDoc evt) {
93         DebuggerEngine de = DebuggerManager.getDebuggerManager().getCurrentEngine();
94         if (de == null) return ;
95         JPDADebugger debugger = (JPDADebugger) de.lookupFirst(null, JPDADebugger.class);
96         if (debugger == null) return ;
97         Evaluator.open(debugger);
98     }
99     
100     public void propertyChange(PropertyChangeEvent JavaDoc propertyChangeEvent) {
101         SwingUtilities.invokeLater(new Runnable JavaDoc() {
102             public void run() {
103                 synchronized (this) {
104                     JPDADebugger lastDebugger = (JPDADebugger) lastDebuggerRef.get();
105                     if (lastDebugger != null) {
106                         setEnabled(lastDebugger.getCurrentThread() != null);
107                     }
108                 }
109             }
110         });
111     }
112     
113     protected void finalize() throws Throwable JavaDoc {
114         DebuggerManager.getDebuggerManager().removeDebuggerListener(
115                 DebuggerManager.PROP_CURRENT_ENGINE,
116                 listener);
117     }
118
119         
120     private static class EnableListener extends DebuggerManagerAdapter {
121         
122         private Reference JavaDoc actionRef;
123         
124         public EnableListener(EvaluateAction action) {
125             actionRef = new WeakReference JavaDoc(action);
126         }
127         
128         public void propertyChange(PropertyChangeEvent JavaDoc evt) {
129             EvaluateAction action = (EvaluateAction) actionRef.get();
130             if (action != null) {
131                 action.checkEnabled();
132             }
133         }
134         
135     }
136
137 }
138
Popular Tags