1 19 20 package org.netbeans.modules.web.debug.actions; 21 22 import java.beans.PropertyChangeEvent ; 23 import java.beans.PropertyChangeListener ; 24 import java.util.Collections ; 25 import java.util.Set ; 26 import org.netbeans.api.debugger.ActionsManager; 27 import org.netbeans.api.debugger.Breakpoint; 28 import org.netbeans.api.debugger.DebuggerEngine; 29 import org.netbeans.api.debugger.DebuggerManager; 30 import org.netbeans.api.debugger.DebuggerManagerListener; 31 import org.netbeans.api.debugger.Session; 32 import org.netbeans.api.debugger.Watch; 33 import org.netbeans.api.debugger.jpda.JPDADebugger; 34 import org.netbeans.api.project.Project; 35 import org.netbeans.spi.debugger.jpda.EditorContext; 36 import org.netbeans.spi.project.ActionProvider; 37 import org.openide.windows.TopComponent; 38 import org.netbeans.modules.web.debug.breakpoints.JspLineBreakpoint; 39 import org.netbeans.modules.web.debug.util.Utils; 40 import org.netbeans.spi.debugger.ActionsProviderSupport; 41 42 46 public class JspRunToCursorActionProvider extends ActionsProviderSupport { 47 48 private EditorContext editorContext; 49 private JspLineBreakpoint breakpoint; 50 51 { 52 editorContext = (EditorContext) DebuggerManager. 53 getDebuggerManager().lookupFirst(null, EditorContext.class); 54 55 Listener listener = new Listener (); 56 MainProjectManager.getDefault ().addPropertyChangeListener (listener); 57 TopComponent.getRegistry ().addPropertyChangeListener (listener); 58 DebuggerManager.getDebuggerManager ().addDebuggerListener (listener); 59 60 setEnabled(ActionsManager.ACTION_RUN_TO_CURSOR, shouldBeEnabled()); 61 } 62 63 public Set getActions() { 64 return Collections.singleton (ActionsManager.ACTION_RUN_TO_CURSOR); 65 } 66 67 public void doAction (Object action) { 68 69 removeBreakpoint(); 71 createBreakpoint(); 72 73 ((ActionProvider) MainProjectManager.getDefault(). 75 getMainProject().getLookup().lookup( 76 ActionProvider.class 77 )).invokeAction ( 78 ActionProvider.COMMAND_DEBUG, 79 MainProjectManager.getDefault ().getMainProject ().getLookup () 80 ); 81 } 82 83 private boolean shouldBeEnabled () { 84 85 if ( editorContext == null || !Utils.isJsp(editorContext.getCurrentURL())) { 86 return false; 87 } 88 89 Project p = MainProjectManager.getDefault ().getMainProject (); 91 if (p == null) return false; 92 ActionProvider actionProvider = (ActionProvider)p.getLookup ().lookup (ActionProvider.class); 93 if (actionProvider == null) return false; 94 95 String [] sa = actionProvider.getSupportedActions (); 96 int i, k = sa.length; 97 for (i = 0; i < k; i++) { 98 if (ActionProvider.COMMAND_DEBUG.equals (sa [i])) { 99 break; 100 } 101 } 102 if (i == k) { 103 return false; 104 } 105 106 return ((ActionProvider) p.getLookup ().lookup ( 108 ActionProvider.class 109 )).isActionEnabled ( 110 ActionProvider.COMMAND_DEBUG, 111 p.getLookup () 112 ); 113 } 114 115 private void createBreakpoint() { 116 breakpoint = JspLineBreakpoint.create ( 117 editorContext.getCurrentURL (), 118 editorContext.getCurrentLineNumber () 119 ); 120 breakpoint.setHidden (true); 121 DebuggerManager.getDebuggerManager ().addBreakpoint (breakpoint); 122 } 123 124 private void removeBreakpoint() { 125 if (breakpoint != null) { 126 DebuggerManager.getDebuggerManager ().removeBreakpoint (breakpoint); 127 breakpoint = null; 128 } 129 } 130 131 private class Listener implements PropertyChangeListener , DebuggerManagerListener { 132 public void propertyChange (PropertyChangeEvent e) { 133 if ((e == null) || (TopComponent.Registry.PROP_OPENED.equals(e.getPropertyName()))) 134 return; 135 if (e.getPropertyName () == JPDADebugger.PROP_STATE) { 136 int state = ((Integer ) e.getNewValue ()).intValue (); 137 if (state == JPDADebugger.STATE_DISCONNECTED || state == JPDADebugger.STATE_STOPPED) 138 removeBreakpoint (); 139 return; 140 } 141 142 setEnabled ( 143 ActionsManager.ACTION_RUN_TO_CURSOR, 144 shouldBeEnabled () 145 ); 146 } 147 148 public void sessionRemoved (Session session) { 149 removeBreakpoint(); 150 } 151 152 public void breakpointAdded (Breakpoint breakpoint) {} 153 public void breakpointRemoved (Breakpoint breakpoint) {} 154 public Breakpoint[] initBreakpoints () { 155 return new Breakpoint [0]; 156 } 157 public void initWatches () {} 158 public void sessionAdded (Session session) {} 159 public void watchAdded (Watch watch) {} 160 public void watchRemoved (Watch watch) {} 161 162 public void engineAdded (DebuggerEngine engine) { 163 JPDADebugger debugger = (JPDADebugger) engine.lookupFirst 164 (null, JPDADebugger.class); 165 if (debugger == null) return; 166 debugger.addPropertyChangeListener ( 167 JPDADebugger.PROP_STATE, 168 this 169 ); 170 } 171 172 public void engineRemoved (DebuggerEngine engine) { 173 JPDADebugger debugger = (JPDADebugger) engine.lookupFirst 174 (null, JPDADebugger.class); 175 if (debugger == null) return; 176 debugger.removePropertyChangeListener ( 177 JPDADebugger.PROP_STATE, 178 this 179 ); 180 } 181 182 } 183 } 184 | Popular Tags |