KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > retouche > editor > hyperlink > GoToSupport


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 package org.netbeans.modules.retouche.editor.hyperlink;
20
21 import java.awt.Toolkit JavaDoc;
22 import java.io.IOException JavaDoc;
23 import java.net.URL JavaDoc;
24 import java.util.Arrays JavaDoc;
25 import java.util.HashSet JavaDoc;
26 import java.util.Set JavaDoc;
27 import javax.swing.text.Document JavaDoc;
28 import org.netbeans.api.gsf.DeclarationFinder;
29 import org.netbeans.api.gsf.DeclarationFinder.DeclarationLocation;
30 import org.netbeans.api.gsf.OffsetRange;
31 import org.netbeans.api.gsf.GsfTokenId;
32 import org.netbeans.api.gsf.CancellableTask;
33 import org.netbeans.api.gsf.ElementHandle;
34 import org.netbeans.api.lexer.Token;
35 import org.netbeans.api.lexer.TokenHierarchy;
36 import org.netbeans.api.lexer.TokenId;
37 import org.netbeans.api.lexer.TokenSequence;
38 import org.netbeans.api.retouche.source.CompilationController;
39 import org.netbeans.api.retouche.source.Phase;
40 import org.netbeans.api.retouche.source.Source;
41 import org.netbeans.api.retouche.source.UiUtils;
42 import org.netbeans.modules.gsf.Language;
43 import org.netbeans.modules.gsf.LanguageRegistry;
44 import org.openide.awt.HtmlBrowser;
45 import org.openide.filesystems.FileObject;
46 import org.openide.loaders.DataObject;
47
48
49 /**
50  *
51  */

52 public class GoToSupport {
53     private static final Set JavaDoc<GsfTokenId> USABLE_TOKEN_IDS =
54         new HashSet JavaDoc(Arrays.asList(GsfTokenId.IDENTIFIER /*, JavaTokenId.THIS, JavaTokenId.SUPER*/));
55
56     /** Utilities only */
57     private GoToSupport() {
58     }
59     
60     public static String JavaDoc performGoTo(final Document JavaDoc doc, final int offset) {
61         try {
62             final FileObject fo = getFileObject(doc);
63
64             if (fo == null) {
65                 return null;
66             }
67
68             Source js = Source.forFileObject(fo);
69             final String JavaDoc[] result = new String JavaDoc[1];
70
71             js.runUserActionTask(new CancellableTask<CompilationController>() {
72                     public void cancel() {
73                     }
74
75                     public void run(CompilationController controller)
76                         throws Exception JavaDoc {
77                         if (controller.toPhase(Phase.RESOLVED).compareTo(Phase.RESOLVED) < 0) {
78                             return;
79                         }
80
81                         Language language = controller.getLanguage();
82
83                         if (language != null) {
84                             DeclarationFinder finder = language.getDeclarationFinder();
85
86                             if (finder != null) {
87                                 Token[] tokens = new Token[1];
88                                 getIdentifierSpan(doc, offset);
89
90                                 DeclarationLocation location =
91                                     finder.findDeclaration(controller, offset);
92
93                                 if (location != DeclarationLocation.NONE) {
94                                     URL JavaDoc url = location.getUrl();
95                                     if (url != null) {
96                                         HtmlBrowser.URLDisplayer.getDefault().showURL(url);
97                                     } else {
98                                         UiUtils.open(location.getFileObject(), location.getOffset());
99
100                                         String JavaDoc desc = "Description not yet implemented";
101                                         result[0] = "<html><body>" + desc;
102                                     }
103
104                                     return;
105                                 }
106                             }
107                         }
108
109                         Toolkit.getDefaultToolkit().beep();
110                         result[0] = null;
111
112                         return;
113                     }
114                 }, true);
115
116             return result[0];
117         } catch (IOException JavaDoc ioe) {
118             throw new IllegalStateException JavaDoc(ioe);
119         }
120     }
121
122     private static FileObject getFileObject(Document JavaDoc doc) {
123         DataObject od = (DataObject)doc.getProperty(Document.StreamDescriptionProperty);
124
125         return (od != null) ? od.getPrimaryFile() : null;
126     }
127
128     public int[] getHyperlinkSpan(Document JavaDoc doc, int offset) {
129         return GoToSupport.getIdentifierSpan(doc, offset);
130     }
131
132     public static int[] getIdentifierSpan(Document JavaDoc doc, int offset) {
133         FileObject fo = getFileObject(doc);
134
135         if (fo == null) {
136             //do nothing if FO is not attached to the document - the goto would not work anyway:
137
return null;
138         }
139
140         Language language = LanguageRegistry.getInstance().getLanguageByMimeType(fo.getMIMEType());
141
142         if (language == null) {
143             return null;
144         }
145
146         DeclarationFinder finder = language.getDeclarationFinder();
147
148         if (finder == null) {
149             return null;
150         }
151
152         OffsetRange range = finder.getReferenceSpan(doc, offset);
153         if (range != OffsetRange.NONE) {
154             return new int[] { range.getStart(), range.getEnd() };
155         }
156         
157         return null;
158     }
159 }
160
Popular Tags