KickJava   Java API By Example, From Geeks To Geeks.

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


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 package org.netbeans.modules.debugger.jpda.ui.actions;
20
21 import com.sun.jdi.VMDisconnectedException;
22 import com.sun.jdi.request.EventRequestManager;
23 import com.sun.jdi.event.Event;
24 import com.sun.jdi.event.BreakpointEvent;
25 import java.beans.PropertyChangeEvent JavaDoc;
26 import java.beans.PropertyChangeListener JavaDoc;
27
28 import java.util.Collections JavaDoc;
29 import java.util.Set JavaDoc;
30 import org.netbeans.api.debugger.ActionsManager;
31 import org.netbeans.api.debugger.ActionsManagerListener;
32
33 import org.netbeans.api.debugger.DebuggerManager;
34 import org.netbeans.spi.debugger.ContextProvider;
35 import org.netbeans.api.debugger.Session;
36 import org.netbeans.api.debugger.jpda.JPDADebugger;
37 import org.netbeans.api.debugger.jpda.LineBreakpoint;
38 import org.netbeans.api.debugger.jpda.event.JPDABreakpointListener;
39 import org.netbeans.api.debugger.jpda.event.JPDABreakpointEvent;
40 import org.netbeans.api.debugger.jpda.JPDAStep;
41 import org.netbeans.modules.debugger.jpda.ui.EditorContextBridge;
42 import org.netbeans.spi.debugger.ActionsProviderSupport;
43 import org.openide.DialogDisplayer;
44 import org.openide.NotifyDescriptor;
45
46 import org.netbeans.modules.debugger.jpda.ui.SourcePath;
47
48 import org.openide.util.NbBundle;
49
50 /**
51  *
52  * @author Jan Jancura, Roman Ondruska
53  */

54 public class RunIntoMethodActionProvider extends ActionsProviderSupport
55                                          implements PropertyChangeListener JavaDoc,
56                                                     ActionsManagerListener {
57
58     private JPDADebugger debugger;
59     private Session session;
60     private LineBreakpoint breakpoint;
61     private ActionsManager lastActionsManager;
62     private SourcePath sourcePath;
63     
64     public RunIntoMethodActionProvider(ContextProvider lookupProvider) {
65         debugger = (JPDADebugger) lookupProvider.lookupFirst
66                 (null, JPDADebugger.class);
67         session = (Session) lookupProvider.lookupFirst
68                 (null, Session.class);
69         sourcePath = (SourcePath) lookupProvider.lookupFirst
70                 (null, SourcePath.class);
71         debugger.addPropertyChangeListener (debugger.PROP_STATE, this);
72         EditorContextBridge.addPropertyChangeListener (this);
73     }
74     
75     private void destroy () {
76         debugger.removePropertyChangeListener (debugger.PROP_STATE, this);
77         EditorContextBridge.removePropertyChangeListener (this);
78     }
79     
80     private ActionsManager getActionsManager () {
81         ActionsManager current = RunToCursorActionProvider.getCurrentActionsManager();
82         if (current != lastActionsManager) {
83             if (lastActionsManager != null) {
84                 lastActionsManager.removeActionsManagerListener(
85                         ActionsManagerListener.PROP_ACTION_STATE_CHANGED, this);
86             }
87             current.addActionsManagerListener(
88                     ActionsManagerListener.PROP_ACTION_STATE_CHANGED, this);
89             lastActionsManager = current;
90         }
91         return current;
92     }
93
94     public void propertyChange (PropertyChangeEvent JavaDoc evt) {
95         setEnabled (
96             ActionsManager.ACTION_RUN_INTO_METHOD,
97             getActionsManager().isEnabled(ActionsManager.ACTION_CONTINUE) &&
98             (debugger.getState () == debugger.STATE_STOPPED) &&
99             (EditorContextBridge.getCurrentLineNumber () >= 0) &&
100             (EditorContextBridge.getCurrentURL ().endsWith (".java"))
101         );
102         if ( (debugger.getState () != debugger.STATE_RUNNING) &&
103              (breakpoint != null)
104         ) {
105             DebuggerManager.getDebuggerManager ().removeBreakpoint (breakpoint);
106             breakpoint = null;
107         }
108         if (debugger.getState () == debugger.STATE_DISCONNECTED)
109             destroy ();
110     }
111     
112     public Set JavaDoc getActions () {
113         return Collections.singleton (ActionsManager.ACTION_RUN_INTO_METHOD);
114     }
115      
116     public void doAction (Object JavaDoc action) {
117         if (breakpoint != null) {
118             DebuggerManager.getDebuggerManager ().removeBreakpoint (breakpoint);
119             breakpoint = null;
120         }
121         final String JavaDoc method = EditorContextBridge.getSelectedMethodName ();
122         final int methodLine = EditorContextBridge.getCurrentLineNumber();
123         
124 // System.out.println("Currently in the method: " + EditorContextBridge.getCurrentMethodName() +
125
// "on line" + methodLine);
126

127         if (method.length () < 1) {
128             NotifyDescriptor.Message descriptor = new NotifyDescriptor.Message (
129                 NbBundle.getMessage (
130                 RunIntoMethodActionProvider.class,
131                 "MSG_Put_cursor_on_some_method_call"
132                 )
133             );
134             DialogDisplayer.getDefault ().notify (descriptor);
135             return;
136         }
137         try {
138             //Is debugger already at line with cursor?
139
if (debugger.getCurrentThread().getCallStack()[0].getLineNumber(null) ==
140                     EditorContextBridge.getCurrentLineNumber()) {
141 // System.out.println("The same line");
142
traceLineForMethod(method, methodLine);
143                     if (debugger.getSuspend() == JPDADebugger.SUSPEND_EVENT_THREAD) {
144                         debugger.getCurrentThread().resume();
145                     } else {
146                         session.getEngineForLanguage ("Java").getActionsManager ().doAction (
147                             ActionsManager.ACTION_CONTINUE
148                         );
149                     }
150                     return;
151             }
152         } catch (com.sun.jdi.AbsentInformationException e) {
153             e.printStackTrace();
154         }
155         
156         breakpoint = LineBreakpoint.create (
157             EditorContextBridge.getCurrentURL (),
158             EditorContextBridge.getCurrentLineNumber ()
159         );
160         breakpoint.setHidden (true);
161         breakpoint.setSuspend(LineBreakpoint.SUSPEND_NONE);
162         breakpoint.addJPDABreakpointListener(new JPDABreakpointListener() {
163             public void breakpointReached(JPDABreakpointEvent event) {
164 // System.out.println("Breakpoint reached");
165
traceLineForMethod(method, methodLine);
166                 DebuggerManager.getDebuggerManager().removeBreakpoint (breakpoint);
167 // System.out.println("End of Breakpoint reached");
168
}
169         });
170         DebuggerManager.getDebuggerManager ().addBreakpoint (breakpoint);
171         if (debugger.getSuspend() == JPDADebugger.SUSPEND_EVENT_THREAD) {
172             debugger.getCurrentThread().resume();
173             //((JPDADebuggerImpl) debugger).resumeCurrentThread();
174
} else {
175             //((JPDADebuggerImpl) debugger).resume();
176
session.getEngineForLanguage ("Java").getActionsManager ().doAction (
177                 ActionsManager.ACTION_CONTINUE
178             );
179         }
180         
181     }
182     
183     private void traceLineForMethod(final String JavaDoc method, final int methodLine) {
184         final int depth = debugger.getCurrentThread().getStackDepth();
185         final JPDAStep step = debugger.createJPDAStep(JPDAStep.STEP_LINE, JPDAStep.STEP_INTO);
186                 step.addPropertyChangeListener(JPDAStep.PROP_STATE_EXEC, new PropertyChangeListener JavaDoc() {
187                     public void propertyChange(PropertyChangeEvent JavaDoc evt) {
188 // System.out.println("Step fired.");
189
// System.out.println(debugger.getCurrentThread().getMethodName());
190

191                         if (
192                                 (
193                                     debugger.getCurrentThread().getMethodName().
194                                     equals(method) ||
195                                     (debugger.getCurrentThread().getStackDepth() == depth &&
196                                     debugger.getCurrentCallStackFrame().getLineNumber("java")
197                                     != methodLine)
198                                 ) &&
199                                 sourcePath.sourceAvailable(debugger.getCurrentThread(),
200                                     "java", false)
201                             ) {
202                             step.setHidden(false);
203 // System.out.println("Hidden -- false");
204
} else {
205                             step.setHidden(true);
206                             
207                             if (step.getDepth() == JPDAStep.STEP_INTO)
208                                 step.setDepth(JPDAStep.STEP_OUT);
209                             else step.setDepth(JPDAStep.STEP_INTO);
210                             
211                             step.addStep(debugger.getCurrentThread());
212 // System.out.println("Hidden -- true");
213
}
214                     }
215                 });
216                 step.addStep(debugger.getCurrentThread());
217     }
218
219     public void actionPerformed(Object JavaDoc action) {
220         // Is never called
221
}
222
223     public void actionStateChanged(Object JavaDoc action, boolean enabled) {
224         if (ActionsManager.ACTION_CONTINUE == action) {
225             setEnabled (
226                 ActionsManager.ACTION_RUN_INTO_METHOD,
227                 enabled &&
228                 (debugger.getState () == debugger.STATE_STOPPED) &&
229                 (EditorContextBridge.getCurrentLineNumber () >= 0) &&
230                 (EditorContextBridge.getCurrentURL ().endsWith (".java"))
231             );
232         }
233     }
234 }
235
Popular Tags