001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.web.debug;
043:
044: import java.io.*;
045: import javax.swing.JEditorPane;
046: import javax.swing.text.*;
047:
048: import org.openide.cookies.EditorCookie;
049: import org.openide.text.*;
050: import org.openide.util.RequestProcessor;
051:
052: import org.netbeans.modules.web.debug.util.Utils;
053: import org.netbeans.api.debugger.*;
054: import org.netbeans.api.debugger.jpda.*;
055: import org.openide.loaders.DataObject;
056:
057: public class JspToolTipAnnotation extends Annotation implements
058: Runnable {
059:
060: private String toolTipText = null;
061:
062: private StyledDocument doc;
063:
064: public String getShortDescription() {
065: Utils.log("JspTooltip: getShortDescription");
066:
067: toolTipText = null;
068: DebuggerEngine currentEngine = DebuggerManager
069: .getDebuggerManager().getCurrentEngine();
070: if (currentEngine == null)
071: return null;
072: JPDADebugger d = (JPDADebugger) currentEngine.lookupFirst(null,
073: JPDADebugger.class);
074: if (d == null)
075: return null;
076:
077: Line.Part lp = (Line.Part) getAttachedAnnotatable();
078: if (lp != null) {
079: Line line = lp.getLine();
080: DataObject dob = DataEditorSupport.findDataObject(line);
081: EditorCookie ec = (EditorCookie) dob
082: .getCookie(EditorCookie.class);
083:
084: if (ec != null) { // Only for editable dataobjects
085: try {
086: doc = ec.openDocument();
087: RequestProcessor.getDefault().post(this );
088: } catch (IOException e) {
089: }
090: }
091: }
092: return toolTipText;
093: }
094:
095: public void run() {
096:
097: Utils.log("JspTooltip: run");
098:
099: //1) get tooltip text
100: Line.Part lp = (Line.Part) getAttachedAnnotatable();
101: JEditorPane ep = Utils.getCurrentEditor();
102: String textForTooltip = "";
103:
104: if ((lp == null) || (ep == null)) {
105: return;
106: }
107:
108: //first try EL
109: String text = Utils.getELIdentifier(doc, ep, NbDocument
110: .findLineOffset(doc, lp.getLine().getLineNumber())
111: + lp.getColumn());
112: Utils.log("JspTooltip: ELIdentifier = " + text);
113:
114: boolean isScriptlet = Utils.isScriptlet(doc, ep, NbDocument
115: .findLineOffset(doc, lp.getLine().getLineNumber())
116: + lp.getColumn());
117: Utils.log("isScriptlet: " + isScriptlet);
118:
119: //if not, try Java
120: if ((text == null) && (isScriptlet)) {
121: text = Utils.getJavaIdentifier(doc, ep, NbDocument
122: .findLineOffset(doc, lp.getLine().getLineNumber())
123: + lp.getColumn());
124: textForTooltip = text;
125: Utils.log("JspTooltip: javaIdentifier = " + text);
126: if (text == null) {
127: return;
128: }
129: } else {
130: if (text == null) {
131: return;
132: }
133: textForTooltip = text;
134: String textEscaped = org.openide.util.Utilities
135: .replaceString(text, "\"", "\\\"");
136: text = "pageContext.getExpressionEvaluator().evaluate(\""
137: + textEscaped
138: + "\", java.lang.String.class, (javax.servlet.jsp.PageContext)pageContext, null)";
139: }
140:
141: Utils.log("JspTooltip: fullWatch = " + text);
142:
143: //3) obtain text representation of value of watch
144: String old = toolTipText;
145: toolTipText = null;
146:
147: DebuggerEngine currentEngine = DebuggerManager
148: .getDebuggerManager().getCurrentEngine();
149: if (currentEngine == null)
150: return;
151: JPDADebugger d = (JPDADebugger) currentEngine.lookupFirst(null,
152: JPDADebugger.class);
153: if (d == null)
154: return;
155:
156: try {
157: Variable v = d.evaluate(text);
158: if (v instanceof ObjectVariable) {
159: toolTipText = textForTooltip + " = (" + v.getType()
160: + ")" + ((ObjectVariable) v).getToStringValue();
161: } else {
162: toolTipText = textForTooltip + " = (" + v.getType()
163: + ")" + v.getValue();
164: }
165: } catch (InvalidExpressionException e) {
166: toolTipText = text + " = >" + e.getMessage() + "<";
167: }
168: Utils.log("JspTooltip: " + toolTipText);
169: firePropertyChange(PROP_SHORT_DESCRIPTION, old, toolTipText);
170: }
171:
172: public String getAnnotationType() {
173: return null;
174: }
175:
176: }
|