1 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 ; 26 import java.util.regex.Pattern ; 27 import javax.lang.model.element.Element; 28 import javax.lang.model.element.ElementKind; 29 import javax.swing.SwingUtilities ; 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 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 TreePath tp = 71 compilationInfo.getTreeUtilities().pathFor(caretListeningFactory.getLastPosition(fileObject)); 72 if (isCancelled()) { 74 return; 75 } 76 77 Element element = compilationInfo.getTrees().getElement(tp); 79 80 if (isCancelled() || element == null ) { 82 return; 83 } 84 85 if ( navigatorShouldUpdate ) { 87 updateNavigatorSelection(); 88 } 89 90 if ( isCancelled() ) { 91 return; 92 } 93 94 if ( lastEh != null && lastEh.signatureEquals(element) ) { 96 return; 97 } 98 else { 99 lastEh = ElementHandle.create(element); 100 setDeclaration(""); setJavadoc("", ""); } 104 105 if ( javadocShouldUpdate ) { 107 computeAndSetJavadoc(compilationInfo, element); 108 } 109 110 if ( isCancelled() ) { 111 return; 112 } 113 114 if ( declarationShouldUpdate ) { 116 computeAndSetDeclaration(compilationInfo, element); 117 } 118 119 } 120 121 private void setDeclaration(final String declaration) { 122 SwingUtilities.invokeLater(new Runnable () { 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 header, final String javadoc) { 133 SwingUtilities.invokeLater(new Runnable () { 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 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; 163 164 if ( element.getKind() == ElementKind.PACKAGE ) { 165 javadoc = ""; } 167 else { 168 Doc doc = compilationInfo.getElementUtilities().javaDocFor(element); 169 javadoc = doc == null ? "" : doc.getRawCommentText(); } 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 declaration = tree.toString(); 198 if (element.getKind() == ElementKind.CONSTRUCTOR) { 199 String 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 declaration = tree.toString(); 213 if (element.getKind() == ElementKind.CONSTRUCTOR) { 214 String 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 224 } 225 226 } 227 | Popular Tags |