KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > scripting > php > dbginterface > ToolTipAnnotation


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.scripting.php.dbginterface;
21
22 import java.io.IOException JavaDoc;
23 import javax.swing.JEditorPane JavaDoc;
24 import javax.swing.text.BadLocationException JavaDoc;
25 import javax.swing.text.Element JavaDoc;
26 import javax.swing.text.StyledDocument JavaDoc;
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 JavaDoc {
41
42     private Line.Part lp;
43     private EditorCookie ec;
44     private DbgDebuggerImpl debugger;
45
46     public String JavaDoc 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 JavaDoc doc = ec.openDocument();
83
84             JEditorPane JavaDoc ep = Utils.findJEditorPane(ec);
85             
86             if (ep == null) {
87                 return;
88             }
89
90             String JavaDoc 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 JavaDoc 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 JavaDoc toolTipText = expression + " = " + value ;
112             firePropertyChange(PROP_SHORT_DESCRIPTION, null, toolTipText);
113         } catch(IOException JavaDoc ex) {
114             // Ignore this one.
115
} catch(Exception JavaDoc ex) {
116             // Log exceptions occuring during evaluation.
117
ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ex);
118         }
119     }
120
121     public String JavaDoc getAnnotationType() {
122         return null; // Currently return null annotation type
123
}
124
125     private static String JavaDoc getIdentifier(StyledDocument JavaDoc doc, JEditorPane JavaDoc ep, int offset) {
126         String JavaDoc 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 JavaDoc 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                 // That's not a variable!
167
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 JavaDoc 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