1 19 20 package org.netbeans.modules.scripting.php.dbginterface; 21 22 import java.lang.reflect.InvocationTargetException ; 23 import javax.swing.JEditorPane ; 24 import javax.swing.SwingUtilities ; 25 import javax.swing.text.Caret ; 26 import javax.swing.text.DefaultEditorKit ; 27 import javax.swing.text.EditorKit ; 28 import javax.swing.text.StyledDocument ; 29 import org.openide.ErrorManager; 30 import org.openide.cookies.EditorCookie; 31 import org.openide.cookies.LineCookie; 32 import org.openide.filesystems.FileObject; 33 import org.openide.loaders.DataObject; 34 import org.openide.nodes.Node; 35 import org.openide.text.Line; 36 import org.openide.text.NbDocument; 37 import org.openide.windows.TopComponent; 38 39 43 public class Utils { 44 45 private static final boolean DEBUG = false; 46 47 82 83 public static Line getLine(DataObject dataObject, int linenum) { 84 Line result = null; 85 86 try { 87 EditorCookie editorCookie = (EditorCookie) 88 dataObject.getCookie(EditorCookie.class); 89 LineCookie lineCookie = (LineCookie) 90 dataObject.getCookie(LineCookie.class); 91 result = lineCookie.getLineSet().getOriginal(linenum - 1); 92 } catch(Exception e) { 93 } 94 95 return result; 96 } 97 98 public static Line getCurrentLine() { 99 Node[] nodes = TopComponent.getRegistry().getCurrentNodes(); 100 101 if(nodes == null || nodes.length != 1) { 102 return null; 103 } 104 105 Node n = nodes[0]; 106 LineCookie lineCookie = (LineCookie) n.getCookie(LineCookie.class); 107 108 if (lineCookie == null) { 109 return null; 110 } 111 112 EditorCookie editorCookie = (EditorCookie)n.getCookie(EditorCookie.class); 113 114 if (editorCookie == null) { 115 return null; 116 } 117 118 JEditorPane jEditorPane = findJEditorPane(editorCookie); 119 120 if (jEditorPane == null) { 121 return null; 122 } 123 124 StyledDocument document = editorCookie.getDocument(); 125 126 if (document == null) { 127 return null; 128 } 129 130 Caret caret = jEditorPane .getCaret(); 131 132 if (caret == null) { 133 return null; 134 } 135 136 int lineNumber = NbDocument.findLineNumber(document, caret.getDot()); 137 138 try { 139 return lineCookie.getLineSet().getCurrent(lineNumber); 140 } catch (IndexOutOfBoundsException ex) { 141 return null; 142 } 143 } 144 145 public static boolean isPHPFile(FileObject fo) { 146 if (fo == null) { 147 return false; 148 } else { 149 return fo.getMIMEType().equals("application/x-php"); 150 } 151 } 152 153 public static JEditorPane findJEditorPane(final EditorCookie editor) { 155 if (SwingUtilities.isEventDispatchThread()) { 156 JEditorPane p[] = editor.getOpenedPanes(); 157 if((p == null) || (p.length < 1)){ 158 return null; 159 } else{ 160 return editor.getOpenedPanes()[0]; 161 162 } 163 164 } else { final JEditorPane ek[] = new JEditorPane [1]; 166 try { 167 SwingUtilities.invokeAndWait(new Runnable () { 168 public void run() { 169 170 JEditorPane p[] = editor.getOpenedPanes(); 171 if((p == null) || (p.length < 1)){ 172 ek[0] = null; 173 } else{ 174 ek[0]=editor.getOpenedPanes()[0]; 175 176 } 177 } 178 }); 179 } catch (InvocationTargetException ex) { 180 ErrorManager.getDefault().notify(ex.getTargetException()); 181 } catch (InterruptedException ex) { 182 ErrorManager.getDefault().notify(ex); 183 } 184 return ek[0]; 185 } 186 } 187 } 188 | Popular Tags |