1 19 20 package org.netbeans.modules.debugger.jpda.projects; 21 22 import java.awt.event.ActionEvent ; 23 import java.beans.PropertyChangeEvent ; 24 import java.beans.PropertyChangeListener ; 25 import java.beans.PropertyChangeSupport ; 26 import java.util.Collections ; 27 import java.util.Set ; 28 import javax.swing.AbstractAction ; 29 import javax.swing.Action ; 30 import javax.swing.SwingUtilities ; 31 import org.netbeans.api.debugger.ActionsManager; 32 33 34 import org.netbeans.api.debugger.Breakpoint; 35 import org.netbeans.api.debugger.DebuggerEngine; 36 37 import org.netbeans.api.debugger.DebuggerManager; 38 import org.netbeans.api.debugger.DebuggerManagerAdapter; 39 40 import org.netbeans.api.debugger.DebuggerManagerListener; 41 import org.netbeans.spi.debugger.ContextProvider; 42 import org.netbeans.api.debugger.Session; 43 import org.netbeans.api.debugger.Watch; 44 import org.netbeans.api.debugger.jpda.JPDADebugger; 45 46 import org.netbeans.api.debugger.jpda.LineBreakpoint; 47 import org.netbeans.api.java.project.JavaProjectConstants; 48 import org.netbeans.api.project.Project; 49 import org.netbeans.spi.debugger.ActionsProvider; 50 import org.netbeans.spi.debugger.ActionsProviderListener; 51 import org.netbeans.spi.debugger.ActionsProviderSupport; 52 import org.netbeans.spi.debugger.jpda.EditorContext; 53 import org.netbeans.spi.project.ActionProvider; 54 import org.openide.ErrorManager; 55 import org.openide.util.RequestProcessor; 56 57 import org.openide.windows.TopComponent; 58 59 60 64 public class RunToCursorActionProvider extends ActionsProviderSupport { 65 66 private EditorContext editor; 67 private LineBreakpoint breakpoint; 68 69 { 70 editor = (EditorContext) DebuggerManager. 71 getDebuggerManager ().lookupFirst (null, EditorContext.class); 72 73 Listener listener = new Listener (); 74 MainProjectManager.getDefault ().addPropertyChangeListener (listener); 75 TopComponent.getRegistry ().addPropertyChangeListener (listener); 76 DebuggerManager.getDebuggerManager ().addDebuggerListener ( 77 DebuggerManager.PROP_DEBUGGER_ENGINES, 78 listener 79 ); 80 81 SwingUtilities.invokeLater (new Runnable () { 84 public void run () { 85 setEnabled ( 86 ActionsManager.ACTION_RUN_TO_CURSOR, 87 shouldBeEnabled () 88 ); 89 } 90 }); 91 } 93 94 public Set getActions () { 95 return Collections.singleton (ActionsManager.ACTION_RUN_TO_CURSOR); 96 } 97 98 public void doAction (Object action) { 99 100 removeBreakpoint (); 102 createBreakpoint (LineBreakpoint.create ( 103 editor.getCurrentURL (), 104 editor.getCurrentLineNumber () 105 )); 106 107 invokeAction(); 109 } 110 111 public void postAction(Object action, final Runnable actionPerformedNotifier) { 112 final LineBreakpoint newBreakpoint = LineBreakpoint.create ( 113 editor.getCurrentURL (), 114 editor.getCurrentLineNumber () 115 ); 116 RequestProcessor.getDefault().post(new Runnable () { 117 public void run() { 118 removeBreakpoint (); 120 createBreakpoint (newBreakpoint); 121 try { 122 SwingUtilities.invokeAndWait(new Runnable () { 123 public void run() { 124 invokeAction(); 125 } 126 }); 127 } catch (InterruptedException iex) { 128 } catch (java.lang.reflect.InvocationTargetException itex) { 130 ErrorManager.getDefault().notify(itex); 131 } finally { 132 actionPerformedNotifier.run(); 133 } 134 } 135 }); 136 } 137 138 private void invokeAction() { 139 ((ActionProvider) MainProjectManager.getDefault (). 140 getMainProject ().getLookup ().lookup ( 141 ActionProvider.class 142 )).invokeAction ( 143 ActionProvider.COMMAND_DEBUG, 144 MainProjectManager.getDefault ().getMainProject ().getLookup () 145 ); 146 } 147 148 private boolean shouldBeEnabled () { 149 if (editor.getCurrentLineNumber () < 0) return false; 150 if (!editor.getCurrentURL ().endsWith (".java")) return false; 151 152 Project p = MainProjectManager.getDefault ().getMainProject (); 154 if (p == null) return false; 155 ActionProvider actionProvider = (ActionProvider) p.getLookup (). 156 lookup (ActionProvider.class); 157 if (actionProvider == null) return false; 158 String [] sa = actionProvider.getSupportedActions (); 159 int i, k = sa.length; 160 for (i = 0; i < k; i++) 161 if (ActionProvider.COMMAND_DEBUG.equals (sa [i])) 162 break; 163 if (i == k) return false; 164 165 return ((ActionProvider) p.getLookup ().lookup ( 167 ActionProvider.class 168 )).isActionEnabled ( 169 ActionProvider.COMMAND_DEBUG, 170 MainProjectManager.getDefault ().getMainProject ().getLookup () 171 ); 172 } 173 174 private void createBreakpoint (LineBreakpoint breakpoint) { 175 breakpoint.setHidden (true); 176 DebuggerManager.getDebuggerManager ().addBreakpoint (breakpoint); 177 this.breakpoint = breakpoint; 178 } 179 180 private void removeBreakpoint () { 181 if (breakpoint != null) { 182 DebuggerManager.getDebuggerManager ().removeBreakpoint (breakpoint); 183 breakpoint = null; 184 } 185 } 186 187 private class Listener extends DebuggerManagerAdapter { 188 public void propertyChange (PropertyChangeEvent e) { 189 if (e.getPropertyName () == JPDADebugger.PROP_STATE) { 190 int state = ((Integer ) e.getNewValue ()).intValue (); 191 if ( (state == JPDADebugger.STATE_DISCONNECTED) || 192 (state == JPDADebugger.STATE_STOPPED) 193 ) removeBreakpoint (); 194 return; 195 } 196 setEnabled ( 197 ActionsManager.ACTION_RUN_TO_CURSOR, 198 shouldBeEnabled () 199 ); 200 } 201 202 public void engineAdded (DebuggerEngine engine) { 203 JPDADebugger debugger = (JPDADebugger) engine.lookupFirst 204 (null, JPDADebugger.class); 205 if (debugger == null) return; 206 debugger.addPropertyChangeListener ( 207 JPDADebugger.PROP_STATE, 208 this 209 ); 210 } 211 212 public void engineRemoved (DebuggerEngine engine) { 213 JPDADebugger debugger = (JPDADebugger) engine.lookupFirst 214 (null, JPDADebugger.class); 215 if (debugger == null) return; 216 debugger.removePropertyChangeListener ( 217 JPDADebugger.PROP_STATE, 218 this 219 ); 220 } 221 } 222 } 223 | Popular Tags |