1 19 20 package org.netbeans.modules.debugger.ui; 21 22 import javax.swing.ImageIcon ; 23 24 import javax.swing.JEditorPane ; 25 import javax.swing.text.StyledDocument ; 26 27 import org.openide.cookies.EditorCookie; 28 import org.openide.nodes.Node; 29 import org.openide.text.NbDocument; 30 import org.openide.util.Utilities; 31 import org.openide.windows.TopComponent; 32 33 34 39 public class Utils { 40 41 public static String getIdentifier () { 42 EditorCookie e = getCurrentEditorCookie (); 43 if (e == null) return null; 44 JEditorPane ep = getCurrentEditor (e); 45 if (ep == null) return null; 46 return getIdentifier ( 47 e.getDocument (), 48 ep, 49 ep.getCaret ().getDot () 50 ); 51 } 52 53 private static String getIdentifier ( 54 StyledDocument doc, 55 JEditorPane ep, 56 int offset 57 ) { 58 String t = null; 59 if ( (ep.getSelectionStart () <= offset) && 60 (offset <= ep.getSelectionEnd ()) 61 ) t = ep.getSelectedText (); 62 if (t != null) return t; 63 64 int line = NbDocument.findLineNumber ( 65 doc, 66 offset 67 ); 68 int col = NbDocument.findLineColumn ( 69 doc, 70 offset 71 ); 72 try { 73 javax.swing.text.Element lineElem = 74 org.openide.text.NbDocument.findLineRootElement (doc). 75 getElement (line); 76 77 if (lineElem == null) return null; 78 int lineStartOffset = lineElem.getStartOffset (); 79 int lineLen = lineElem.getEndOffset() - lineStartOffset; 80 t = doc.getText (lineStartOffset, lineLen); 81 int identStart = col; 82 while (identStart > 0 && 83 (Character.isJavaIdentifierPart ( 84 t.charAt (identStart - 1) 85 ) || 86 (t.charAt (identStart - 1) == '.'))) { 87 identStart--; 88 } 89 int identEnd = col; 90 while (identEnd < lineLen && 91 Character.isJavaIdentifierPart(t.charAt(identEnd)) 92 ) { 93 identEnd++; 94 } 95 96 if (identStart == identEnd) return null; 97 return t.substring (identStart, identEnd); 98 } catch (javax.swing.text.BadLocationException e) { 99 return null; 100 } 101 } 102 103 108 private static JEditorPane getCurrentEditor () { 109 EditorCookie e = getCurrentEditorCookie (); 110 if (e == null) return null; 111 JEditorPane [] op = e.getOpenedPanes (); 112 if ((op == null) || (op.length < 1)) return null; 113 return op [0]; 114 } 115 116 121 private static JEditorPane getCurrentEditor (EditorCookie e) { 122 JEditorPane [] op = e.getOpenedPanes (); 123 if ((op == null) || (op.length < 1)) return null; 124 return op [0]; 125 } 126 127 132 private static String getSelectedText () { 133 JEditorPane ep = getCurrentEditor (); 134 if (ep == null) return null; 135 return ep.getSelectedText (); 136 } 137 138 143 private static EditorCookie getCurrentEditorCookie () { 144 Node[] nodes = TopComponent.getRegistry ().getActivatedNodes (); 145 if ( (nodes == null) || 146 (nodes.length != 1) ) return null; 147 Node n = nodes [0]; 148 return (EditorCookie) n.getCookie ( 149 EditorCookie.class 150 ); 151 } 152 172 public static ImageIcon getIcon (String iconBase) { 173 return new ImageIcon (Utilities.loadImage (iconBase+".gif")); 174 } 175 176 181 } 223 | Popular Tags |