1 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 ; 26 import java.beans.PropertyChangeListener ; 27 28 import java.util.Collections ; 29 import java.util.Set ; 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 54 public class RunIntoMethodActionProvider extends ActionsProviderSupport 55 implements PropertyChangeListener , 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 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 getActions () { 113 return Collections.singleton (ActionsManager.ACTION_RUN_INTO_METHOD); 114 } 115 116 public void doAction (Object action) { 117 if (breakpoint != null) { 118 DebuggerManager.getDebuggerManager ().removeBreakpoint (breakpoint); 119 breakpoint = null; 120 } 121 final String method = EditorContextBridge.getSelectedMethodName (); 122 final int methodLine = EditorContextBridge.getCurrentLineNumber(); 123 124 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 if (debugger.getCurrentThread().getCallStack()[0].getLineNumber(null) == 140 EditorContextBridge.getCurrentLineNumber()) { 141 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 traceLineForMethod(method, methodLine); 166 DebuggerManager.getDebuggerManager().removeBreakpoint (breakpoint); 167 } 169 }); 170 DebuggerManager.getDebuggerManager ().addBreakpoint (breakpoint); 171 if (debugger.getSuspend() == JPDADebugger.SUSPEND_EVENT_THREAD) { 172 debugger.getCurrentThread().resume(); 173 } else { 175 session.getEngineForLanguage ("Java").getActionsManager ().doAction ( 177 ActionsManager.ACTION_CONTINUE 178 ); 179 } 180 181 } 182 183 private void traceLineForMethod(final String 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 () { 187 public void propertyChange(PropertyChangeEvent evt) { 188 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 } 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 } 214 } 215 }); 216 step.addStep(debugger.getCurrentThread()); 217 } 218 219 public void actionPerformed(Object action) { 220 } 222 223 public void actionStateChanged(Object 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 |