1 19 20 package org.netbeans.modules.ant.debugger.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 AntBreakpointActionProvider extends ActionsProviderSupport 51 implements PropertyChangeListener { 52 53 private static final Set actions = Collections.singleton ( 54 ActionsManager.ACTION_TOGGLE_BREAKPOINT 55 ); 56 57 58 public AntBreakpointActionProvider () { 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 if (line == null) return ; 72 Breakpoint[] breakpoints = DebuggerManager.getDebuggerManager (). 73 getBreakpoints (); 74 int i, k = breakpoints.length; 75 for (i = 0; i < k; i++) 76 if ( breakpoints [i] instanceof AntBreakpoint && 77 ((AntBreakpoint) breakpoints [i]).getLine ().equals (line) 78 ) { 79 DebuggerManager.getDebuggerManager ().removeBreakpoint 80 (breakpoints [i]); 81 break; 82 } 83 if (i == k) 84 DebuggerManager.getDebuggerManager ().addBreakpoint ( 85 new AntBreakpoint (line) 86 ); 87 } 89 90 95 public Set getActions () { 96 return actions; 97 } 98 99 100 static Line getCurrentLine () { 101 Node[] nodes = TopComponent.getRegistry ().getCurrentNodes (); 102 if (nodes == null) return null; 103 if (nodes.length != 1) return null; 104 Node n = nodes [0]; 105 FileObject fo = (FileObject) n.getLookup ().lookup(FileObject.class); 107 if (fo == null) { 108 DataObject dobj = (DataObject) n.getLookup().lookup(DataObject.class); 109 if (dobj != null) { 110 fo = dobj.getPrimaryFile(); 111 } 112 } 113 if (!isAntFile(fo)) { 115 return null; 116 } 117 LineCookie lineCookie = (LineCookie) n.getCookie (LineCookie.class); 118 if (lineCookie == null) return null; 120 EditorCookie editorCookie = (EditorCookie) n.getCookie (EditorCookie.class); 121 if (editorCookie == null) return null; 122 JEditorPane jEditorPane = getEditorPane(editorCookie); 123 if (jEditorPane == null) return null; 124 StyledDocument document = editorCookie.getDocument (); 125 if (document == null) return null; 126 Caret caret = jEditorPane.getCaret (); 127 if (caret == null) return null; 128 int lineNumber = NbDocument.findLineNumber ( 129 document, 130 caret.getDot () 131 ); 132 try { 133 Line.Set lineSet = lineCookie.getLineSet(); 134 assert lineSet != null : lineCookie; 135 return lineSet.getCurrent(lineNumber); 136 } catch (IndexOutOfBoundsException ex) { 137 return null; 138 } 139 } 140 141 private static JEditorPane getEditorPane_(EditorCookie editorCookie) { 142 JEditorPane [] op = editorCookie.getOpenedPanes (); 143 if ((op == null) || (op.length < 1)) return null; 144 return op [0]; 145 } 146 147 private static JEditorPane getEditorPane(final EditorCookie editorCookie) { 148 if (SwingUtilities.isEventDispatchThread()) { 149 return getEditorPane_(editorCookie); 150 } else { 151 final JEditorPane [] ce = new JEditorPane [1]; 152 try { 153 SwingUtilities.invokeAndWait(new Runnable () { 154 public void run() { 155 ce[0] = getEditorPane_(editorCookie); 156 } 157 }); 158 } catch (InvocationTargetException ex) { 159 ErrorManager.getDefault().notify(ex.getTargetException()); 160 } catch (InterruptedException ex) { 161 ErrorManager.getDefault().notify(ex); 162 } 163 return ce[0]; 164 } 165 } 166 167 168 169 private static boolean isAntFile(FileObject fo) { 170 if (fo == null) { 171 return false; 172 } else { 173 return fo.getMIMEType().equals("text/x-ant+xml"); 174 } 175 } 176 177 public void propertyChange(PropertyChangeEvent evt) { 178 boolean enabled = getCurrentLine() != null; 180 setEnabled (ActionsManager.ACTION_TOGGLE_BREAKPOINT, enabled); 181 } 182 183 } 184 | Popular Tags |