1 19 20 package org.netbeans.modules.ant.debugger; 21 22 import java.io.IOException ; 23 import java.lang.reflect.InvocationTargetException ; 24 import javax.swing.JEditorPane ; 25 import javax.swing.SwingUtilities ; 26 import javax.swing.text.BadLocationException ; 27 import javax.swing.text.Element ; 28 import javax.swing.text.StyledDocument ; 29 import org.netbeans.api.debugger.DebuggerEngine; 30 import org.openide.ErrorManager; 31 32 import org.openide.cookies.EditorCookie; 33 import org.openide.loaders.DataObject; 34 import org.openide.text.Annotation; 35 import org.openide.text.DataEditorSupport; 36 import org.openide.text.Line; 37 import org.openide.text.NbDocument; 38 import org.openide.text.Line.Part; 39 import org.openide.util.RequestProcessor; 40 import org.netbeans.api.debugger.DebuggerManager; 41 import org.netbeans.api.debugger.Watch; 42 43 import org.openide.nodes.Node; 44 import org.openide.windows.TopComponent; 45 46 47 public class ToolTipAnnotation extends Annotation implements Runnable { 48 49 private Part lp; 50 private EditorCookie ec; 51 52 public String getShortDescription () { 53 DebuggerEngine currentEngine = DebuggerManager.getDebuggerManager (). 54 getCurrentEngine (); 55 if (currentEngine == null) return null; 56 AntDebugger d = (AntDebugger) currentEngine.lookupFirst 57 (null, AntDebugger.class); 58 if (d == null) return null; 59 60 Part lp = (Part) 61 getAttachedAnnotatable(); 62 Line line = lp.getLine (); 63 DataObject dob = DataEditorSupport.findDataObject (line); 64 if (dob == null) return null; 65 EditorCookie ec = 66 (EditorCookie) dob.getCookie 67 (EditorCookie.class); 68 69 if (ec == null) return null; 70 this.lp = lp; 71 this.ec = ec; 72 RequestProcessor.getDefault ().post (this); 73 return null; 74 } 75 76 public void run () { 77 if (lp == null || ec == null) return ; 79 StyledDocument doc; 80 try { 81 doc = ec.openDocument(); 82 } catch (IOException ex) { 83 return ; 84 } 85 JEditorPane ep = getCurrentEditor (); 86 if (ep == null) return ; 87 String expression = getIdentifier ( 88 doc, 89 ep, 90 NbDocument.findLineOffset ( 91 doc, 92 lp.getLine ().getLineNumber () 93 ) + lp.getColumn () 94 ); 95 if (expression == null) return ; 96 DebuggerEngine currentEngine = DebuggerManager.getDebuggerManager (). 97 getCurrentEngine (); 98 if (currentEngine == null) return; 99 AntDebugger d = (AntDebugger) currentEngine.lookupFirst 100 (null, AntDebugger.class); 101 if (d == null) return; 102 String value = d.evaluate (expression); 103 if ( value == null || 104 value.equals (expression) 105 ) return; 106 String toolTipText = expression + " = " + value; 107 firePropertyChange (PROP_SHORT_DESCRIPTION, null, toolTipText); 108 } 109 110 public String getAnnotationType () { 111 return null; } 113 114 private static String getIdentifier ( 115 StyledDocument doc, 116 JEditorPane ep, 117 int offset 118 ) { 119 String t = null; 120 if ( (ep.getSelectionStart () <= offset) && 121 (offset <= ep.getSelectionEnd ()) 122 ) t = ep.getSelectedText (); 123 if (t != null) return t; 124 125 int line = NbDocument.findLineNumber ( 126 doc, 127 offset 128 ); 129 int col = NbDocument.findLineColumn ( 130 doc, 131 offset 132 ); 133 try { 134 Element lineElem = 135 NbDocument.findLineRootElement (doc). 136 getElement (line); 137 138 if (lineElem == null) return null; 139 int lineStartOffset = lineElem.getStartOffset (); 140 int lineLen = lineElem.getEndOffset() - lineStartOffset; 141 t = doc.getText (lineStartOffset, lineLen); 142 lineLen = t.length (); 143 int identStart = col; 144 while ( (identStart > 0) && 145 (t.charAt (identStart - 1) != '"') 146 ) { 147 identStart--; 148 } 149 int identEnd = Math.max (col, 1); 150 while ( (identEnd < lineLen) && 151 (t.charAt (identEnd - 1) != '"') 152 ) { 153 identEnd++; 154 } 155 156 if (identStart == identEnd) return null; 157 return t.substring (identStart, identEnd - 1); 158 } catch (BadLocationException e) { 159 return null; 160 } 161 } 162 163 168 private static JEditorPane getCurrentEditor_() { 169 EditorCookie e = getCurrentEditorCookie (); 170 if (e == null) return null; 171 JEditorPane [] op = e.getOpenedPanes (); 172 if ((op == null) || (op.length < 1)) return null; 173 return op [0]; 174 } 175 176 static JEditorPane getCurrentEditor () { 177 if (SwingUtilities.isEventDispatchThread()) { 178 return getCurrentEditor_(); 179 } else { 180 final JEditorPane [] ce = new JEditorPane [1]; 181 try { 182 SwingUtilities.invokeAndWait(new Runnable () { 183 public void run() { 184 ce[0] = getCurrentEditor_(); 185 } 186 }); 187 } catch (InvocationTargetException ex) { 188 ErrorManager.getDefault().notify(ex.getTargetException()); 189 } catch (InterruptedException ex) { 190 ErrorManager.getDefault().notify(ex); 191 } 192 return ce[0]; 193 } 194 } 195 196 201 private static EditorCookie getCurrentEditorCookie () { 202 Node[] nodes = TopComponent.getRegistry ().getActivatedNodes (); 203 if ( (nodes == null) || 204 (nodes.length != 1) ) return null; 205 Node n = nodes [0]; 206 return (EditorCookie) n.getCookie ( 207 EditorCookie.class 208 ); 209 } 210 } 211 212 | Popular Tags |