1 19 20 package org.netbeans.modules.scripting.php.dbginterface.breakpoints; 21 22 import java.beans.PropertyChangeEvent ; 23 import java.beans.PropertyChangeListener ; 24 import java.lang.reflect.InvocationTargetException ; 25 import java.util.Collections ; 26 import java.util.Set ; 27 import javax.swing.JEditorPane ; 28 import javax.swing.SwingUtilities ; 29 import javax.swing.text.Caret ; 30 import javax.swing.text.StyledDocument ; 31 import org.netbeans.api.debugger.ActionsManager; 32 import org.netbeans.api.debugger.Breakpoint; 33 import org.netbeans.api.debugger.DebuggerManager; 34 import org.netbeans.spi.debugger.ActionsProviderSupport; 35 import org.openide.ErrorManager; 36 import org.openide.cookies.EditorCookie; 37 import org.openide.cookies.LineCookie; 38 import org.openide.filesystems.FileObject; 39 import org.openide.loaders.DataObject; 40 import org.openide.nodes.Node; 41 import org.openide.text.Line; 42 import org.openide.text.NbDocument; 43 import org.openide.util.WeakListeners; 44 import org.openide.windows.TopComponent; 45 46 50 public class PhpBreakpointActionProvider extends ActionsProviderSupport 51 implements PropertyChangeListener { 52 53 private static final Set actions = Collections.singleton( 54 ActionsManager.ACTION_TOGGLE_BREAKPOINT 55 ); 56 57 58 public PhpBreakpointActionProvider() { 59 setEnabled(ActionsManager.ACTION_TOGGLE_BREAKPOINT, true); 60 TopComponent.getRegistry().addPropertyChangeListener( 61 WeakListeners.propertyChange(this, TopComponent.getRegistry())); 62 } 63 64 69 public void doAction(Object action) { 70 Line line = getCurrentLine(); 71 72 if (line == null) { 73 return; 74 } 75 76 Breakpoint[] breakpoints = DebuggerManager.getDebuggerManager().getBreakpoints(); 77 int i, k = breakpoints.length; 78 for (i = 0; i < k; i++) { 79 if (breakpoints[i] instanceof PhpBreakpoint && 80 ((PhpBreakpoint)breakpoints[i]).getLine().equals(line)) { 81 DebuggerManager.getDebuggerManager(). 82 removeBreakpoint(breakpoints[i]); 83 break; 84 } 85 } 86 87 if (i == k) { 88 DebuggerManager.getDebuggerManager(). 89 addBreakpoint(new PhpBreakpoint(line)); 90 } 91 } 92 93 98 public Set getActions() { 99 return actions; 100 } 101 102 static Line getCurrentLine() { 103 Node[] nodes = TopComponent.getRegistry().getCurrentNodes(); 104 105 if (nodes == null || nodes.length != 1) { 106 return null; 107 } 108 109 Node n = nodes [0]; 110 FileObject fo = (FileObject)n.getLookup().lookup(FileObject.class); 112 113 if (fo == null) { 114 DataObject dobj = (DataObject) n.getLookup().lookup(DataObject.class); 115 116 if (dobj != null) { 117 fo = dobj.getPrimaryFile(); 118 } 119 } 120 121 if (!isPHPFile(fo)) { 123 return null; 124 } 125 126 LineCookie lineCookie = (LineCookie)n.getCookie(LineCookie.class); 127 129 if (lineCookie == null) { 130 return null; 131 } 132 133 EditorCookie editorCookie = (EditorCookie)n.getCookie(EditorCookie.class); 134 135 if (editorCookie == null) { 136 return null; 137 } 138 139 JEditorPane jEditorPane = getEditorPane(editorCookie); 140 141 if (jEditorPane == null) { 142 return null; 143 } 144 145 StyledDocument document = editorCookie.getDocument(); 146 if (document == null) { 147 return null; 148 } 149 150 Caret caret = jEditorPane.getCaret(); 151 152 if (caret == null) { 153 return null; 154 } 155 156 int lineNumber = NbDocument.findLineNumber(document, caret.getDot()); 157 158 try { 159 Line.Set lineSet = lineCookie.getLineSet(); 160 assert lineSet != null : lineCookie; 161 162 return lineSet.getCurrent(lineNumber); 163 } catch (IndexOutOfBoundsException ex) { 164 return null; 165 } 166 } 167 168 private static JEditorPane getEditorPane_(EditorCookie editorCookie) { 169 JEditorPane [] op = editorCookie.getOpenedPanes(); 170 171 if (op == null || op.length < 1) { 172 return null; 173 } 174 175 return op[0]; 176 } 177 178 private static JEditorPane getEditorPane(final EditorCookie editorCookie) { 179 if (SwingUtilities.isEventDispatchThread()) { 180 return getEditorPane_(editorCookie); 181 } else { 182 final JEditorPane [] ce = new JEditorPane [1]; 183 try { 184 SwingUtilities.invokeAndWait(new Runnable () { 185 public void run() { 186 ce[0] = getEditorPane_(editorCookie); 187 } 188 }); 189 } catch (InvocationTargetException ex) { 190 ErrorManager.getDefault().notify(ex.getTargetException()); 191 } catch (InterruptedException ex) { 192 ErrorManager.getDefault().notify(ex); 193 } 194 return ce[0]; 195 } 196 } 197 198 private static boolean isPHPFile(FileObject fo) { 199 if (fo == null) { 200 return false; 201 } 202 else { 203 return fo.getExt().equalsIgnoreCase("php"); 204 } 205 } 206 207 public void propertyChange(PropertyChangeEvent evt) { 208 boolean enabled = getCurrentLine() != null; 210 setEnabled(ActionsManager.ACTION_TOGGLE_BREAKPOINT, enabled); 211 } 212 213 } 214 | Popular Tags |