KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > debugger > jpda > actions > JPDADebuggerActionProvider


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.actions;
21
22 import com.sun.jdi.ThreadReference;
23 import com.sun.jdi.VMDisconnectedException;
24 import com.sun.jdi.VirtualMachine;
25 import com.sun.jdi.request.EventRequestManager;
26 import com.sun.jdi.request.InvalidRequestStateException;
27 import com.sun.jdi.request.StepRequest;
28 import java.beans.PropertyChangeEvent JavaDoc;
29 import java.beans.PropertyChangeListener JavaDoc;
30 import java.util.HashSet JavaDoc;
31 import java.util.Iterator JavaDoc;
32 import java.util.List JavaDoc;
33 import java.util.Set JavaDoc;
34 import org.netbeans.modules.debugger.jpda.JPDADebuggerImpl;
35 import org.netbeans.modules.debugger.jpda.JPDAStepImpl.SingleThreadedStepWatch;
36 import org.netbeans.spi.debugger.ActionsProviderSupport;
37 import org.openide.util.RequestProcessor;
38 import org.openide.util.WeakSet;
39
40
41 /**
42 * Representation of a debugging session.
43 *
44 * @author Jan Jancura
45 * @author Marian Petras
46 */

47 abstract class JPDADebuggerActionProvider extends ActionsProviderSupport
48 implements PropertyChangeListener JavaDoc {
49     
50     private JPDADebuggerImpl debugger;
51     
52     /** The ReqeustProcessor used by action performers. */
53     private static RequestProcessor actionsRequestProcessor;
54     
55     private static Set JavaDoc<JPDADebuggerActionProvider> providersToDisableOnLazyActions = new WeakSet<JPDADebuggerActionProvider>();
56     
57     private volatile boolean disabled;
58     
59     JPDADebuggerActionProvider (JPDADebuggerImpl debugger) {
60         this.debugger = debugger;
61         debugger.addPropertyChangeListener (debugger.PROP_STATE, this);
62     }
63     
64     public void propertyChange (PropertyChangeEvent JavaDoc evt) {
65         if (debugger.getState() == JPDADebuggerImpl.STATE_DISCONNECTED) {
66             synchronized (JPDADebuggerActionProvider.class) {
67                 if (actionsRequestProcessor != null) {
68                     actionsRequestProcessor.stop();
69                     actionsRequestProcessor = null;
70                 }
71             }
72         }
73         checkEnabled (debugger.getState ());
74     }
75     
76     protected abstract void checkEnabled (int debuggerState);
77     
78     public boolean isEnabled (Object JavaDoc action) {
79         if (!disabled) {
80             checkEnabled (debugger.getState ());
81         }
82         return super.isEnabled (action);
83     }
84     
85     JPDADebuggerImpl getDebuggerImpl () {
86         return debugger;
87     }
88     
89     protected void removeStepRequests (ThreadReference tr) {
90         //S ystem.out.println ("removeStepRequests");
91
try {
92             VirtualMachine vm = getDebuggerImpl ().getVirtualMachine ();
93             if (vm == null) return;
94             EventRequestManager erm = vm.eventRequestManager ();
95             List JavaDoc<StepRequest> l = erm.stepRequests ();
96             Iterator JavaDoc<StepRequest> it = l.iterator ();
97             while (it.hasNext ()) {
98                 StepRequest stepRequest = it.next ();
99                 if (stepRequest.thread ().equals (tr)) {
100                     //S ystem.out.println(" remove request " + stepRequest);
101
erm.deleteEventRequest (stepRequest);
102                     SingleThreadedStepWatch.stepRequestDeleted(stepRequest);
103                     break;
104                 }
105                 //S ystem.out.println(" do not remove " + stepRequest + " : " + stepRequest.thread ());
106
}
107         } catch (VMDisconnectedException e) {
108         } catch (IllegalThreadStateException JavaDoc e) {
109             e.printStackTrace();
110         } catch (InvalidRequestStateException e) {
111             e.printStackTrace();
112         }
113     }
114     
115     /**
116      * Mark the provided action provider to be disabled when a lazy action is to be performed.
117      */

118     protected final void setProviderToDisableOnLazyAction(JPDADebuggerActionProvider provider) {
119         synchronized (JPDADebuggerActionProvider.class) {
120             providersToDisableOnLazyActions.add(provider);
121         }
122     }
123     
124     /**
125      * Do the action lazily in a RequestProcessor.
126      * @param run The action to perform.
127      */

128     protected final void doLazyAction(final Runnable JavaDoc run) {
129         final Set JavaDoc<JPDADebuggerActionProvider> disabledActions;
130         synchronized (JPDADebuggerActionProvider.class) {
131             if (actionsRequestProcessor == null) {
132                 actionsRequestProcessor = new RequestProcessor("JPDA Processor", 1); // NOI18N
133
}
134             disabledActions = new HashSet JavaDoc<JPDADebuggerActionProvider>(providersToDisableOnLazyActions);
135         }
136         for (Iterator JavaDoc<JPDADebuggerActionProvider> it = disabledActions.iterator(); it.hasNext(); ) {
137             JPDADebuggerActionProvider ap = it.next();
138             Set JavaDoc actions = ap.getActions();
139             ap.disabled = true;
140             for (Iterator JavaDoc ait = actions.iterator(); ait.hasNext(); ) {
141                 Object JavaDoc action = ait.next();
142                 ap.setEnabled (action, false);
143                 //System.out.println(ap+".setEnabled("+action+", "+false+")");
144
}
145         }
146         actionsRequestProcessor.post(new Runnable JavaDoc() {
147             public void run() {
148                 try {
149                     run.run();
150                     for (Iterator JavaDoc<JPDADebuggerActionProvider> it = disabledActions.iterator(); it.hasNext(); ) {
151                         JPDADebuggerActionProvider ap = it.next();
152                         Set JavaDoc actions = ap.getActions();
153                         ap.disabled = false;
154                         ap.checkEnabled (debugger.getState ());
155                     }
156                 } catch (com.sun.jdi.VMDisconnectedException e) {
157                     // Causes kill action when something is being evaluated
158
}
159             }
160         });
161     }
162 }
163
Popular Tags