KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > java > navigation > CaretListeningTask


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.java.navigation;
21
22 import com.sun.javadoc.Doc;
23 import com.sun.source.tree.Tree;
24 import com.sun.source.util.TreePath;
25 import java.util.regex.Matcher JavaDoc;
26 import java.util.regex.Pattern JavaDoc;
27 import javax.lang.model.element.Element;
28 import javax.lang.model.element.ElementKind;
29 import javax.swing.SwingUtilities JavaDoc;
30 import org.netbeans.api.java.source.CancellableTask;
31 import org.netbeans.api.java.source.CompilationInfo;
32 import org.netbeans.api.java.source.ElementHandle;
33 import org.netbeans.api.java.source.SourceUtils;
34 import org.openide.filesystems.FileObject;
35
36 /**
37  * This task is called every time the caret position changes in a Java editor.
38  * <p>
39  * The task finds the TreePath of the Tree under the caret, converts it to
40  * an Element and then shows the declartion of the element in Declaration window
41  * and javadoc in the Javadoc window.
42  *
43  * @author Sandip V. Chitale (Sandip.Chitale@Sun.Com)
44  */

45 public class CaretListeningTask implements CancellableTask<CompilationInfo> {
46     
47     private CaretListeningFactory caretListeningFactory;
48     private FileObject fileObject;
49     private boolean canceled;
50     
51     private ElementHandle<Element> lastEh;
52     
53     CaretListeningTask(CaretListeningFactory whichElementJavaSourceTaskFactory,FileObject fileObject) {
54         this.caretListeningFactory = whichElementJavaSourceTaskFactory;
55         this.fileObject = fileObject;
56     }
57     
58     public void run(CompilationInfo compilationInfo) {
59         resume();
60         
61         boolean navigatorShouldUpdate = false;
62         boolean javadocShouldUpdate = JavadocTopComponent.shouldUpdate();
63         boolean declarationShouldUpdate = DeclarationTopComponent.shouldUpdate();
64         
65         if ( isCancelled() || ( !navigatorShouldUpdate && !javadocShouldUpdate && !declarationShouldUpdate ) ) {
66             return;
67         }
68         
69         // Find the TreePath for the caret position
70
TreePath tp =
71                 compilationInfo.getTreeUtilities().pathFor(caretListeningFactory.getLastPosition(fileObject));
72         // if cancelled, return
73
if (isCancelled()) {
74             return;
75         }
76         
77         // Get Element
78
Element element = compilationInfo.getTrees().getElement(tp);
79                        
80         // if cancelled or no element, return
81
if (isCancelled() || element == null ) {
82             return;
83         }
84         
85         // Update the navigator
86
if ( navigatorShouldUpdate ) {
87             updateNavigatorSelection();
88         }
89                 
90         if ( isCancelled() ) {
91             return;
92         }
93         
94         // Don't update when element is the same
95
if ( lastEh != null && lastEh.signatureEquals(element) ) {
96             return;
97         }
98         else {
99             lastEh = ElementHandle.create(element);
100             // Diferent element clear data
101
setDeclaration(""); // NOI18N
102
setJavadoc("", ""); // NOI18N
103
}
104             
105         // Compute and set javadoc
106
if ( javadocShouldUpdate ) {
107             computeAndSetJavadoc(compilationInfo, element);
108         }
109         
110         if ( isCancelled() ) {
111             return;
112         }
113         
114         // Compute and set declaration
115
if ( declarationShouldUpdate ) {
116             computeAndSetDeclaration(compilationInfo, element);
117         }
118         
119     }
120     
121     private void setDeclaration(final String JavaDoc declaration) {
122         SwingUtilities.invokeLater(new Runnable JavaDoc() {
123             public void run() {
124                 DeclarationTopComponent declarationTopComponent = DeclarationTopComponent.findInstance();
125                 if (declarationTopComponent != null && declarationTopComponent.isOpened()) {
126                     declarationTopComponent.setDeclaration(declaration);
127                 }
128             }
129         });
130     }
131     
132     private void setJavadoc(final String JavaDoc header, final String JavaDoc javadoc) {
133         SwingUtilities.invokeLater(new Runnable JavaDoc() {
134             public void run() {
135                 JavadocTopComponent javadocTopComponent = JavadocTopComponent.findInstance();
136                 if (javadocTopComponent != null && javadocTopComponent.isOpened()) {
137                     javadocTopComponent.setJavadoc(header, javadoc);
138                 }
139             }
140         });
141     }
142     
143     /**
144      * After this method is called the task if running should exit the run
145      * method immediately.
146      */

147     public final synchronized void cancel() {
148         canceled = true;
149     }
150     
151     protected final synchronized boolean isCancelled() {
152         return canceled;
153     }
154     
155     protected final synchronized void resume() {
156         canceled = false;
157     }
158     
159     
160     private void computeAndSetJavadoc(CompilationInfo compilationInfo, Element element) {
161         
162         String JavaDoc javadoc;
163         
164         if ( element.getKind() == ElementKind.PACKAGE ) {
165             javadoc = ""; // NOI18N
166
}
167         else {
168             Doc doc = compilationInfo.getElementUtilities().javaDocFor(element);
169             javadoc = doc == null ? "" : doc.getRawCommentText(); // NOI18N
170
}
171         
172         if (isCancelled()) {
173             return;
174         }
175                 
176         setJavadoc(element.toString(), javadoc);
177     }
178     
179     private void computeAndSetDeclaration(CompilationInfo compilationInfo, Element element ) {
180             
181         if ( element.getKind() == ElementKind.PACKAGE ) {
182             setDeclaration("package " + element.toString() + ";");
183             return;
184         }
185             
186         if ( isCancelled() ) {
187             return;
188         }
189         
190         Tree tree = compilationInfo.getTrees().getTree(element);
191
192         if ( isCancelled()) {
193             return;
194         }
195
196         if ( tree != null ) {
197             String JavaDoc declaration = tree.toString();
198             if (element.getKind() == ElementKind.CONSTRUCTOR) {
199                 String JavaDoc constructorName = element.getEnclosingElement().getSimpleName().toString();
200                 declaration = declaration.replaceAll(Pattern.quote("<init>"), Matcher.quoteReplacement(constructorName));
201             }
202             setDeclaration(declaration);
203             return;
204         }
205         
206         tree = SourceUtils.treeFor(compilationInfo, element);
207         
208         if ( isCancelled() || tree == null ) {
209             return;
210         }
211         
212         String JavaDoc declaration = tree.toString();
213         if (element.getKind() == ElementKind.CONSTRUCTOR) {
214             String JavaDoc constructorName = element.getEnclosingElement().getSimpleName().toString();
215             declaration = declaration.replaceAll(Pattern.quote("<init>"), Matcher.quoteReplacement(constructorName));
216         }
217         setDeclaration(declaration);
218                 
219     }
220     
221     private void updateNavigatorSelection() {
222         // If navigator is visible ...
223

224     }
225     
226 }
227
Popular Tags