KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > web > debug > JspToolTipAnnotation


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-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.web.debug;
21
22 import java.io.*;
23 import javax.swing.JEditorPane JavaDoc;
24 import javax.swing.text.*;
25
26 import org.openide.ErrorManager;
27 import org.openide.cookies.EditorCookie;
28 import org.openide.nodes.Node;
29 import org.openide.text.*;
30 import org.openide.util.RequestProcessor;
31 import org.openide.windows.TopComponent;
32
33 import org.netbeans.modules.web.debug.util.Utils;
34 import org.netbeans.api.debugger.*;
35 import org.netbeans.api.debugger.jpda.*;
36 import org.openide.loaders.DataObject;
37
38 public class JspToolTipAnnotation extends Annotation implements Runnable JavaDoc {
39     
40     private String JavaDoc toolTipText = null;
41
42     private StyledDocument doc;
43
44     public String JavaDoc getShortDescription() {
45         Utils.getEM().log("JspTooltip: getShortDescription");
46         
47         toolTipText = null;
48         DebuggerEngine currentEngine = DebuggerManager.getDebuggerManager ().
49             getCurrentEngine ();
50         if (currentEngine == null) return null;
51         JPDADebugger d = (JPDADebugger) currentEngine.lookupFirst (null, JPDADebugger.class);
52         if (d == null) return null;
53
54         Line.Part lp = (Line.Part) getAttachedAnnotatable();
55         if (lp != null) {
56             Line line = lp.getLine ();
57             DataObject dob = DataEditorSupport.findDataObject(line);
58             EditorCookie ec = (EditorCookie) dob.getCookie(EditorCookie.class);
59
60             if (ec != null) { // Only for editable dataobjects
61
try {
62                     doc = ec.openDocument ();
63                     RequestProcessor.getDefault().post(this);
64                 } catch (IOException e) {
65                 }
66             }
67         }
68         return toolTipText;
69     }
70
71     public void run () {
72
73         Utils.getEM().log("JspTooltip: run");
74
75         //1) get tooltip text
76
Line.Part lp = (Line.Part)getAttachedAnnotatable();
77         JEditorPane JavaDoc ep = Utils.getCurrentEditor();
78         String JavaDoc textForTooltip = "";
79         
80         if ((lp == null) || (ep == null)) {
81             return;
82         }
83         
84         //first try EL
85
String JavaDoc text = Utils.getELIdentifier(doc, ep,NbDocument.findLineOffset(doc, lp.getLine().getLineNumber()) + lp.getColumn());
86         Utils.getEM().log("JspTooltip: ELIdentifier = " + text);
87
88         boolean isScriptlet = Utils.isScriptlet(
89             doc, ep, NbDocument.findLineOffset(doc, lp.getLine().getLineNumber()) + lp.getColumn()
90         );
91         Utils.getEM().log("isScriptlet: " + isScriptlet);
92         
93         //if not, try Java
94
if ((text == null) && (isScriptlet)) {
95             text = Utils.getJavaIdentifier(
96                 doc, ep, NbDocument.findLineOffset(doc, lp.getLine().getLineNumber()) + lp.getColumn()
97             );
98             textForTooltip = text;
99             Utils.getEM().log("JspTooltip: javaIdentifier = " + text);
100             if (text == null) {
101                 return;
102             }
103         } else {
104             if (text == null) {
105                 return;
106             }
107             textForTooltip = text;
108             String JavaDoc textEscaped = org.openide.util.Utilities.replaceString(text, "\"", "\\\"");
109             text = "pageContext.getExpressionEvaluator().evaluate(\"" + textEscaped +
110                                 "\", java.lang.String.class, (javax.servlet.jsp.PageContext)pageContext, null)";
111         }
112         
113         Utils.getEM().log("JspTooltip: fullWatch = " + text);
114         
115         //3) obtain text representation of value of watch
116
String JavaDoc old = toolTipText;
117         toolTipText = null;
118         
119         DebuggerEngine currentEngine = DebuggerManager.getDebuggerManager().getCurrentEngine();
120         if (currentEngine == null) return;
121         JPDADebugger d = (JPDADebugger) currentEngine.lookupFirst (null, JPDADebugger.class);
122         if (d == null) return;
123         
124         try {
125             Variable v = d.evaluate(text);
126             if (v instanceof ObjectVariable) {
127                 toolTipText = textForTooltip + " = (" + v.getType() + ")" + ((ObjectVariable)v).getToStringValue();
128             } else {
129                 toolTipText = textForTooltip + " = (" + v.getType() + ")" + v.getValue();
130             }
131         } catch (InvalidExpressionException e) {
132             toolTipText = text + " = >" + e.getMessage() + "<";
133         }
134         Utils.getEM().log("JspTooltip: " + toolTipText);
135         firePropertyChange (PROP_SHORT_DESCRIPTION, old, toolTipText);
136     }
137
138     public String JavaDoc getAnnotationType () {
139         return null;
140     }
141     
142 }
143
Popular Tags