1 19 20 package org.netbeans.modules.debugger.jpda.ui.actions; 21 22 import java.beans.PropertyChangeEvent ; 23 import java.beans.PropertyChangeListener ; 24 import java.net.MalformedURLException ; 25 import java.net.URL ; 26 import java.util.Collections ; 27 import java.util.Set ; 28 import org.netbeans.api.debugger.ActionsManager; 29 30 31 import org.netbeans.api.debugger.Breakpoint; 32 import org.netbeans.api.debugger.DebuggerManager; 33 import org.netbeans.spi.debugger.ContextProvider; 34 import org.netbeans.api.debugger.jpda.JPDADebugger; 35 import org.netbeans.api.debugger.jpda.LineBreakpoint; 36 import org.netbeans.modules.debugger.jpda.ui.EditorContextBridge; 37 import org.netbeans.modules.debugger.jpda.ui.breakpoints.BreakpointAnnotationListener; 38 import org.netbeans.spi.debugger.ActionsProviderSupport; 39 import org.openide.filesystems.FileObject; 40 import org.openide.filesystems.URLMapper; 41 42 import org.openide.util.NbBundle; 43 44 45 49 public class ToggleBreakpointActionProvider extends ActionsProviderSupport 50 implements PropertyChangeListener { 51 52 private JPDADebugger debugger; 53 54 55 public ToggleBreakpointActionProvider () { 56 EditorContextBridge.addPropertyChangeListener (this); 57 } 58 59 public ToggleBreakpointActionProvider (ContextProvider lookupProvider) { 60 debugger = (JPDADebugger) lookupProvider.lookupFirst 61 (null, JPDADebugger.class); 62 debugger.addPropertyChangeListener (debugger.PROP_STATE, this); 63 EditorContextBridge.addPropertyChangeListener (this); 64 } 65 66 private void destroy () { 67 debugger.removePropertyChangeListener (debugger.PROP_STATE, this); 68 EditorContextBridge.removePropertyChangeListener (this); 69 } 70 71 public void propertyChange (PropertyChangeEvent evt) { 72 String url = EditorContextBridge.getCurrentURL(); 73 FileObject fo; 74 try { 75 fo = URLMapper.findFileObject(new URL (url)); 76 } catch (MalformedURLException muex) { 77 fo = null; 78 } 79 setEnabled ( 80 ActionsManager.ACTION_TOGGLE_BREAKPOINT, 81 (EditorContextBridge.getCurrentLineNumber () >= 0) && 82 (fo != null && "text/x-java".equals(fo.getMIMEType())) ); 85 if ( debugger != null && 86 debugger.getState () == debugger.STATE_DISCONNECTED 87 ) 88 destroy (); 89 } 90 91 public Set getActions () { 92 return Collections.singleton (ActionsManager.ACTION_TOGGLE_BREAKPOINT); 93 } 94 95 public void doAction (Object action) { 96 DebuggerManager d = DebuggerManager.getDebuggerManager (); 97 98 int ln = EditorContextBridge.getCurrentLineNumber (); 100 String url = EditorContextBridge.getCurrentURL (); 101 if ("".equals (url.trim ())) return; 102 103 LineBreakpoint lb = getBreakpointAnnotationListener ().findBreakpoint ( 105 url, ln 106 ); 107 if (lb != null) { 108 d.removeBreakpoint (lb); 109 return; 110 } 111 122 lb = LineBreakpoint.create ( 124 url, 125 ln 126 ); 127 lb.setPrintText ( 128 NbBundle.getBundle (ToggleBreakpointActionProvider.class).getString 129 ("CTL_Line_Breakpoint_Print_Text") 130 ); 131 d.addBreakpoint (lb); 132 } 133 134 private BreakpointAnnotationListener breakpointAnnotationListener; 135 private BreakpointAnnotationListener getBreakpointAnnotationListener () { 136 if (breakpointAnnotationListener == null) 137 breakpointAnnotationListener = (BreakpointAnnotationListener) 138 DebuggerManager.getDebuggerManager ().lookupFirst 139 (null, BreakpointAnnotationListener.class); 140 return breakpointAnnotationListener; 141 } 142 } 143 | Popular Tags |