KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > languages > features > 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.languages.features;
21
22 import org.netbeans.api.languages.ASTPath;
23 import org.netbeans.api.languages.LanguagesManager;
24 import org.netbeans.api.languages.ParseException;
25 import org.netbeans.api.languages.ASTPath;
26 import org.netbeans.api.languages.SyntaxContext;
27 import javax.swing.JEditorPane JavaDoc;
28 import org.netbeans.api.lexer.Token;
29 import org.netbeans.api.lexer.TokenHierarchy;
30 import org.netbeans.api.lexer.TokenSequence;
31 import org.netbeans.modules.editor.NbEditorDocument;
32 import org.netbeans.api.languages.Context;
33 import org.netbeans.modules.languages.Feature;
34 import org.netbeans.modules.languages.Language;
35 import org.netbeans.modules.languages.LanguagesManagerImpl;
36 import org.netbeans.modules.languages.ParserManagerImpl;
37 import org.netbeans.api.languages.SyntaxContext;
38 import org.netbeans.api.languages.ASTNode;
39 import org.netbeans.api.languages.ParseException;
40 import org.openide.cookies.EditorCookie;
41 import org.openide.loaders.DataObject;
42 import org.openide.text.Annotation;
43 import org.openide.text.DataEditorSupport;
44 import org.openide.text.Line;
45 import org.openide.text.Line.Part;
46 import org.openide.text.NbDocument;
47
48
49 /**
50  *
51  * @author Jan Jancura
52  */

53 public class ToolTipAnnotation extends Annotation {
54
55     
56     public String JavaDoc getShortDescription () {
57         try {
58             Part lp = (Part) getAttachedAnnotatable();
59             Line line = lp.getLine ();
60             DataObject dob = DataEditorSupport.findDataObject (line);
61             EditorCookie ec = (EditorCookie) dob.getCookie (EditorCookie.class);
62             NbEditorDocument doc = (NbEditorDocument) ec.getDocument ();
63             String JavaDoc mimeType = (String JavaDoc) doc.getProperty ("mimeType");
64             int offset = NbDocument.findLineOffset (
65                     ec.getDocument (),
66                     lp.getLine ().getLineNumber ()
67                 ) + lp.getColumn ();
68             TokenHierarchy tokenHierarchy = TokenHierarchy.get (doc);
69             TokenSequence tokenSequence = tokenHierarchy.tokenSequence ();
70             tokenSequence.move (offset);
71             if (!tokenSequence.moveNext() && !tokenSequence.movePrevious()) return null;
72             Language l = ((LanguagesManagerImpl) LanguagesManager.getDefault ()).getLanguage
73                 (mimeType);
74             Token token = tokenSequence.token ();
75             Feature tooltip = l.getFeature (Language.TOOLTIP, token.id ().name ());
76             if (tooltip != null) {
77                 String JavaDoc s = c ((String JavaDoc) tooltip.getValue (Context.create (doc, tokenSequence)));
78                 return s;
79             }
80             ASTNode ast = null;
81             try {
82                 ast = ParserManagerImpl.get (doc).getAST ();
83             } catch (ParseException ex) {
84                 ast = ex.getASTNode ();
85             }
86             if (ast == null) return null;
87             ASTPath path = ast.findPath (offset);
88             if (path == null) return null;
89             int i, k = path.size ();
90             for (i = 0; i < k; i++) {
91                 ASTPath p = path.subPath (i);
92                 tooltip = l.getFeature (Language.TOOLTIP, p);
93                 if (tooltip == null) continue;
94                 String JavaDoc s = c ((String JavaDoc) tooltip.getValue (SyntaxContext.create (doc, p)));
95                 return s;
96             }
97         } catch (ParseException ex) {
98         }
99         return null;
100     }
101
102     public String JavaDoc getAnnotationType () {
103         return null; // Currently return null annotation type
104
}
105     
106     private static String JavaDoc c (String JavaDoc s) {
107         if (s == null) return null;
108         s = s.replace ("\\n", "\n");
109         s = s.replace ("\\r", "\r");
110         s = s.replace ("\\t", "\t");
111         return s;
112     }
113 }
114
115
Popular Tags