1 19 20 package org.netbeans.modules.scripting.php.dbginterface; 21 22 import java.io.IOException ; 23 import javax.swing.JEditorPane ; 24 import javax.swing.text.BadLocationException ; 25 import javax.swing.text.Element ; 26 import javax.swing.text.StyledDocument ; 27 import org.netbeans.api.debugger.DebuggerEngine; 28 import org.openide.ErrorManager; 29 import org.openide.cookies.EditorCookie; 30 import org.openide.loaders.DataObject; 31 import org.openide.text.Annotation; 32 import org.openide.text.DataEditorSupport; 33 import org.openide.text.Line; 34 import org.openide.text.NbDocument; 35 import org.openide.util.RequestProcessor; 36 import org.netbeans.api.debugger.DebuggerManager; 37 import org.netbeans.modules.scripting.php.dbginterface.api.VariableNode; 38 39 40 public class ToolTipAnnotation extends Annotation implements Runnable { 41 42 private Line.Part lp; 43 private EditorCookie ec; 44 private DbgDebuggerImpl debugger; 45 46 public String getShortDescription() { 47 DebuggerEngine currentEngine = DebuggerManager.getDebuggerManager().getCurrentEngine(); 48 49 if (currentEngine != null) { 50 DbgDebuggerImpl debugger = (DbgDebuggerImpl)currentEngine.lookupFirst(null, DbgDebuggerImpl.class); 51 52 if (debugger != null) { 53 Line.Part lp = (Line.Part)getAttachedAnnotatable(); 54 55 if (lp != null) { 56 57 Line line = lp.getLine(); 58 59 if (line != null) { 60 DataObject dob = DataEditorSupport.findDataObject(line); 61 62 if (dob != null) { 63 EditorCookie ec = (EditorCookie)dob.getCookie(EditorCookie.class); 64 65 if (ec != null) { 66 this.lp = lp; 67 this.ec = ec; 68 this.debugger = debugger; 69 RequestProcessor.getDefault().post(this); 70 } 71 } 72 } 73 } 74 } 75 } 76 77 return null; 78 } 79 80 public void run() { 81 try { 82 StyledDocument doc = ec.openDocument(); 83 84 JEditorPane ep = Utils.findJEditorPane(ec); 85 86 if (ep == null) { 87 return; 88 } 89 90 String expression = getIdentifier(doc, ep, 91 NbDocument.findLineOffset(doc, lp.getLine().getLineNumber()) + lp.getColumn()); 92 93 System.err.println("mw ToolTipAnnotation.run() expression= " + expression); 94 if (expression == null) { 95 return; 96 } 97 98 String value = null; 99 VariableNode node = debugger.evaluateExpr(debugger.getVariablesModel().getCurrentFrame(), 100 expression); 101 102 if (node != null) { 103 value = node.getTooltipValue(); 104 } 105 106 System.err.println("mw Evaluated: value = " + value + ", node = " + node); 107 if (value == null || value.equals(expression)) { 108 return; 109 } 110 111 String toolTipText = expression + " = " + value ; 112 firePropertyChange(PROP_SHORT_DESCRIPTION, null, toolTipText); 113 } catch(IOException ex) { 114 } catch(Exception ex) { 116 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ex); 118 } 119 } 120 121 public String getAnnotationType() { 122 return null; } 124 125 private static String getIdentifier(StyledDocument doc, JEditorPane ep, int offset) { 126 String t = null; 127 128 if ((ep.getSelectionStart() <= offset) && (offset <= ep.getSelectionEnd())) { 129 t = ep.getSelectedText(); 130 } 131 132 if (t != null) { 133 return t; 134 } 135 136 int line = NbDocument.findLineNumber(doc, offset); 137 int col = NbDocument.findLineColumn(doc, offset); 138 139 try { 140 Element lineElem = NbDocument.findLineRootElement(doc).getElement(line); 141 142 if (lineElem == null) { 143 return null; 144 } 145 146 int lineStartOffset = lineElem.getStartOffset(); 147 int lineLen = lineElem.getEndOffset() - lineStartOffset; 148 t = doc.getText(lineStartOffset, lineLen); 149 lineLen = t.length(); 150 151 int identStart = col; 152 while (identStart > 0) { 153 if (isIdentifierPrefixChar(t.charAt(identStart - 1))) { 154 identStart--; 155 } 156 else if (identStart > 3 && t.charAt(identStart - 1) == '>' && 157 t.charAt(identStart - 2) == '-') { 158 identStart -= 2; 159 } 160 else { 161 break; 162 } 163 } 164 165 if (t.charAt(identStart) != '$') { 166 return null; 168 } 169 170 int identEnd = Math.max(col, 1); 171 while (identEnd < lineLen && isIdentifierTrailingChar(t.charAt(identEnd - 1))) { 172 identEnd++; 173 } 174 175 if (identStart > 0 && identEnd < lineLen && 176 t.charAt(identStart - 1) == '"' && t.charAt(identEnd - 1) == '"') { 177 identStart--; 178 identEnd++; 179 } 180 181 if (identStart == identEnd) { 182 return null; 183 } 184 185 return t.substring(identStart, identEnd - 1); 186 } 187 catch (BadLocationException e) { 188 return null; 189 } 190 } 191 192 private static boolean isIdentifierPrefixChar(char c) { 193 return (c == '$') || (c == '_') || (c >= '0' && c <= '9') || 194 (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') || 195 (c >= 0x7f && c <= 0xff); 196 } 197 198 private static boolean isIdentifierTrailingChar(char c) { 199 return (c == '_') || (c >= '0' && c <= '9') || 200 (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') || 201 (c >= 0x7f && c <= 0xff); 202 } 203 } | Popular Tags |