KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > debugger > jpda > projects > 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.debugger.jpda.projects;
21
22 import java.io.IOException JavaDoc;
23 import java.lang.reflect.InvocationTargetException JavaDoc;
24 import java.util.List JavaDoc;
25 import javax.swing.JEditorPane JavaDoc;
26 import javax.swing.SwingUtilities JavaDoc;
27 import javax.swing.text.BadLocationException JavaDoc;
28 import javax.swing.text.Element JavaDoc;
29 import javax.swing.text.StyledDocument JavaDoc;
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.DebuggerEngine;
41 import org.netbeans.api.debugger.DebuggerManager;
42 import org.netbeans.api.debugger.jpda.InvalidExpressionException;
43 import org.netbeans.api.debugger.Watch;
44 import org.netbeans.api.debugger.jpda.JPDADebugger;
45 import org.netbeans.api.debugger.jpda.JPDAThread;
46 import org.netbeans.api.debugger.jpda.JPDAWatch;
47 import org.netbeans.api.debugger.jpda.ObjectVariable;
48 import org.netbeans.api.debugger.jpda.Variable;
49 import org.netbeans.spi.debugger.jpda.EditorContext.Operation;
50
51 import org.openide.nodes.Node;
52 import org.openide.windows.TopComponent;
53
54
55 public class ToolTipAnnotation extends Annotation implements Runnable JavaDoc {
56
57     private Part lp;
58     private EditorCookie ec;
59
60     public String JavaDoc getShortDescription () {
61         DebuggerEngine currentEngine = DebuggerManager.getDebuggerManager ().
62             getCurrentEngine ();
63         if (currentEngine == null) return null;
64         JPDADebugger d = (JPDADebugger) currentEngine.lookupFirst
65             (null, JPDADebugger.class);
66         if (d == null) return null;
67
68         Part lp = (Part) getAttachedAnnotatable();
69         if (lp == null) return null;
70         Line line = lp.getLine ();
71         DataObject dob = DataEditorSupport.findDataObject (line);
72         if (dob == null) return null;
73         EditorCookie ec = (EditorCookie) dob.getCookie (EditorCookie.class);
74         if (ec == null)
75             return null;
76             // Only for editable dataobjects
77

78         this.lp = lp;
79         this.ec = ec;
80         RequestProcessor.getDefault ().post (this);
81         return null;
82     }
83
84     public void run () {
85         if (lp == null || ec == null) return ;
86         StyledDocument JavaDoc doc;
87         try {
88             doc = ec.openDocument();
89         } catch (IOException JavaDoc ex) {
90             return ;
91         }
92         JEditorPane JavaDoc ep = getCurrentEditor ();
93         if (ep == null) return ;
94         int offset;
95         String JavaDoc expression = getIdentifier (
96             doc,
97             ep,
98             offset = NbDocument.findLineOffset (
99                 doc,
100                 lp.getLine ().getLineNumber ()
101             ) + lp.getColumn ()
102         );
103         if (expression == null) return ;
104         DebuggerEngine currentEngine = DebuggerManager.getDebuggerManager ().
105             getCurrentEngine ();
106         if (currentEngine == null) return;
107         JPDADebugger d = (JPDADebugger) currentEngine.lookupFirst
108             (null, JPDADebugger.class);
109         if (d == null) return;
110         JPDAThread t = d.getCurrentThread();
111         if (t == null || !t.isSuspended()) return ;
112         String JavaDoc toolTipText = null;
113         try {
114             Variable v = null;
115             List JavaDoc<Operation> operations = t.getLastOperations();
116             if (operations != null) {
117                 for (Operation operation: operations) {
118                     if (!expression.endsWith(operation.getMethodName())) {
119                         continue;
120                     }
121                     if (operation.getMethodStartPosition().getOffset() <= offset &&
122                         offset <= operation.getMethodEndPosition().getOffset()) {
123                         v = operation.getReturnValue();
124                     }
125                 }
126             }
127             if (v == null) {
128                 v = d.evaluate (expression);
129             }
130             String JavaDoc type = v.getType ();
131             String JavaDoc value = v.getValue ();
132             if (v instanceof ObjectVariable)
133                 try {
134                     toolTipText = expression + " = " +
135                         (type.length () == 0 ?
136                             "" :
137                             "(" + type + ") ") +
138                         ((ObjectVariable) v).getToStringValue ();
139                 } catch (InvalidExpressionException ex) {
140                     toolTipText = expression + " = " +
141                         (type.length () == 0 ?
142                             "" :
143                             "(" + type + ") ") +
144                         value;
145                 }
146             else
147                 toolTipText = expression + " = " +
148                     (type.length () == 0 ?
149                         "" :
150                         "(" + type + ") ") +
151                     value;
152         } catch (InvalidExpressionException e) {
153             toolTipText = expression + " = >" + e.getMessage () + "<";
154         }
155         firePropertyChange (PROP_SHORT_DESCRIPTION, null, toolTipText);
156     }
157
158     public String JavaDoc getAnnotationType () {
159         return null; // Currently return null annotation type
160
}
161
162     private static String JavaDoc getIdentifier (
163         StyledDocument JavaDoc doc,
164         JEditorPane JavaDoc ep,
165         int offset
166     ) {
167         String JavaDoc t = null;
168         if ( (ep.getSelectionStart () <= offset) &&
169              (offset <= ep.getSelectionEnd ())
170         ) t = ep.getSelectedText ();
171         if (t != null) return t;
172         
173         int line = NbDocument.findLineNumber (
174             doc,
175             offset
176         );
177         int col = NbDocument.findLineColumn (
178             doc,
179             offset
180         );
181         try {
182             Element JavaDoc lineElem =
183                 NbDocument.findLineRootElement (doc).
184                 getElement (line);
185
186             if (lineElem == null) return null;
187             int lineStartOffset = lineElem.getStartOffset ();
188             int lineLen = lineElem.getEndOffset() - lineStartOffset;
189             t = doc.getText (lineStartOffset, lineLen);
190             int identStart = col;
191             while (identStart > 0 &&
192                 (Character.isJavaIdentifierPart (
193                     t.charAt (identStart - 1)
194                 ) ||
195                 (t.charAt (identStart - 1) == '.'))) {
196                 identStart--;
197             }
198             int identEnd = col;
199             while (identEnd < lineLen &&
200                    Character.isJavaIdentifierPart(t.charAt(identEnd))
201             ) {
202                 identEnd++;
203             }
204
205             if (identStart == identEnd) return null;
206             return t.substring (identStart, identEnd);
207         } catch (BadLocationException JavaDoc e) {
208             return null;
209         }
210     }
211     
212     /**
213      * Returns current editor component instance.
214      *
215      * Used in: ToolTipAnnotation
216      */

217     private static JEditorPane JavaDoc getCurrentEditor_() {
218         EditorCookie e = getCurrentEditorCookie ();
219         if (e == null) return null;
220         JEditorPane JavaDoc[] op = e.getOpenedPanes ();
221         if ((op == null) || (op.length < 1)) return null;
222         return op [0];
223     }
224     
225     private static JEditorPane JavaDoc getCurrentEditor () {
226         if (SwingUtilities.isEventDispatchThread()) {
227             return getCurrentEditor_();
228         } else {
229             final JEditorPane JavaDoc[] ce = new JEditorPane JavaDoc[1];
230             try {
231                 SwingUtilities.invokeAndWait(new Runnable JavaDoc() {
232                     public void run() {
233                         ce[0] = getCurrentEditor_();
234                     }
235                 });
236             } catch (InvocationTargetException JavaDoc ex) {
237                 ErrorManager.getDefault().notify(ex.getTargetException());
238             } catch (InterruptedException JavaDoc ex) {
239                 ErrorManager.getDefault().notify(ex);
240             }
241             return ce[0];
242         }
243     }
244     
245     /**
246      * Returns current editor component instance.
247      *
248      * @return current editor component instance
249      */

250     private static EditorCookie getCurrentEditorCookie () {
251         Node[] nodes = TopComponent.getRegistry ().getActivatedNodes ();
252         if ( (nodes == null) ||
253              (nodes.length != 1) ) return null;
254         Node n = nodes [0];
255         return (EditorCookie) n.getCookie (
256             EditorCookie.class
257         );
258     }
259 }
260
261
Popular Tags