KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > ant > debugger > 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-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.ant.debugger;
21
22 import java.io.IOException JavaDoc;
23 import java.lang.reflect.InvocationTargetException JavaDoc;
24 import javax.swing.JEditorPane JavaDoc;
25 import javax.swing.SwingUtilities JavaDoc;
26 import javax.swing.text.BadLocationException JavaDoc;
27 import javax.swing.text.Element JavaDoc;
28 import javax.swing.text.StyledDocument JavaDoc;
29 import org.netbeans.api.debugger.DebuggerEngine;
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.DebuggerManager;
41 import org.netbeans.api.debugger.Watch;
42
43 import org.openide.nodes.Node;
44 import org.openide.windows.TopComponent;
45
46
47 public class ToolTipAnnotation extends Annotation implements Runnable JavaDoc {
48
49     private Part lp;
50     private EditorCookie ec;
51
52     public String JavaDoc getShortDescription () {
53         DebuggerEngine currentEngine = DebuggerManager.getDebuggerManager ().
54             getCurrentEngine ();
55         if (currentEngine == null) return null;
56         AntDebugger d = (AntDebugger) currentEngine.lookupFirst
57             (null, AntDebugger.class);
58         if (d == null) return null;
59
60         Part lp = (Part)
61             getAttachedAnnotatable();
62         Line line = lp.getLine ();
63         DataObject dob = DataEditorSupport.findDataObject (line);
64         if (dob == null) return null;
65         EditorCookie ec =
66             (EditorCookie) dob.getCookie
67             (EditorCookie.class);
68
69         if (ec == null) return null;
70         this.lp = lp;
71         this.ec = ec;
72         RequestProcessor.getDefault ().post (this);
73         return null;
74     }
75
76     public void run () {
77         //if (expression == null) return;
78
if (lp == null || ec == null) return ;
79         StyledDocument JavaDoc doc;
80         try {
81             doc = ec.openDocument();
82         } catch (IOException JavaDoc ex) {
83             return ;
84         }
85         JEditorPane JavaDoc ep = getCurrentEditor ();
86         if (ep == null) return ;
87         String JavaDoc expression = getIdentifier (
88             doc,
89             ep,
90             NbDocument.findLineOffset (
91                 doc,
92                 lp.getLine ().getLineNumber ()
93             ) + lp.getColumn ()
94         );
95         if (expression == null) return ;
96         DebuggerEngine currentEngine = DebuggerManager.getDebuggerManager ().
97             getCurrentEngine ();
98         if (currentEngine == null) return;
99         AntDebugger d = (AntDebugger) currentEngine.lookupFirst
100             (null, AntDebugger.class);
101         if (d == null) return;
102         String JavaDoc value = d.evaluate (expression);
103         if ( value == null ||
104              value.equals (expression)
105         ) return;
106         String JavaDoc toolTipText = expression + " = " + value;
107         firePropertyChange (PROP_SHORT_DESCRIPTION, null, toolTipText);
108     }
109
110     public String JavaDoc getAnnotationType () {
111         return null; // Currently return null annotation type
112
}
113
114     private static String JavaDoc getIdentifier (
115         StyledDocument JavaDoc doc,
116         JEditorPane JavaDoc ep,
117         int offset
118     ) {
119         String JavaDoc t = null;
120         if ( (ep.getSelectionStart () <= offset) &&
121              (offset <= ep.getSelectionEnd ())
122         ) t = ep.getSelectedText ();
123         if (t != null) return t;
124         
125         int line = NbDocument.findLineNumber (
126             doc,
127             offset
128         );
129         int col = NbDocument.findLineColumn (
130             doc,
131             offset
132         );
133         try {
134             Element JavaDoc lineElem =
135                 NbDocument.findLineRootElement (doc).
136                 getElement (line);
137
138             if (lineElem == null) return null;
139             int lineStartOffset = lineElem.getStartOffset ();
140             int lineLen = lineElem.getEndOffset() - lineStartOffset;
141             t = doc.getText (lineStartOffset, lineLen);
142             lineLen = t.length ();
143             int identStart = col;
144             while ( (identStart > 0) &&
145                     (t.charAt (identStart - 1) != '"')
146             ) {
147                 identStart--;
148             }
149             int identEnd = Math.max (col, 1);
150             while ( (identEnd < lineLen) &&
151                     (t.charAt (identEnd - 1) != '"')
152             ) {
153                 identEnd++;
154             }
155
156             if (identStart == identEnd) return null;
157             return t.substring (identStart, identEnd - 1);
158         } catch (BadLocationException JavaDoc e) {
159             return null;
160         }
161     }
162     
163     /**
164      * Returns current editor component instance.
165      *
166      * Used in: ToolTipAnnotation
167      */

168     private static JEditorPane JavaDoc getCurrentEditor_() {
169         EditorCookie e = getCurrentEditorCookie ();
170         if (e == null) return null;
171         JEditorPane JavaDoc[] op = e.getOpenedPanes ();
172         if ((op == null) || (op.length < 1)) return null;
173         return op [0];
174     }
175
176     static JEditorPane JavaDoc getCurrentEditor () {
177         if (SwingUtilities.isEventDispatchThread()) {
178             return getCurrentEditor_();
179         } else {
180             final JEditorPane JavaDoc[] ce = new JEditorPane JavaDoc[1];
181             try {
182                 SwingUtilities.invokeAndWait(new Runnable JavaDoc() {
183                     public void run() {
184                         ce[0] = getCurrentEditor_();
185                     }
186                 });
187             } catch (InvocationTargetException JavaDoc ex) {
188                 ErrorManager.getDefault().notify(ex.getTargetException());
189             } catch (InterruptedException JavaDoc ex) {
190                 ErrorManager.getDefault().notify(ex);
191             }
192             return ce[0];
193         }
194     }
195     
196     /**
197      * Returns current editor component instance.
198      *
199      * @return current editor component instance
200      */

201     private static EditorCookie getCurrentEditorCookie () {
202         Node[] nodes = TopComponent.getRegistry ().getActivatedNodes ();
203         if ( (nodes == null) ||
204              (nodes.length != 1) ) return null;
205         Node n = nodes [0];
206         return (EditorCookie) n.getCookie (
207             EditorCookie.class
208         );
209     }
210 }
211
212
Popular Tags