1 19 20 package org.netbeans.modules.java.navigation; 21 22 import com.sun.source.tree.CompilationUnitTree; 23 import com.sun.source.tree.Tree; 24 import com.sun.source.util.Trees; 25 import org.netbeans.api.java.source.CancellableTask; 26 import org.netbeans.api.java.source.CompilationController; 27 import org.netbeans.api.java.source.JavaSource; 28 import org.netbeans.api.java.source.JavaSource.Phase; 29 import org.openide.ErrorManager; 30 import java.io.IOException ; 31 import java.util.LinkedHashSet ; 32 import java.util.List ; 33 import java.util.Set ; 34 import javax.lang.model.element.Element; 35 import javax.lang.model.element.ElementKind; 36 import javax.swing.JDialog ; 37 import org.openide.filesystems.FileObject; 38 import org.openide.util.NbBundle; 39 40 44 public final class JavaMembers { 45 46 51 public static void show(final FileObject fileObject) { 52 if (fileObject != null) { 53 JavaSource javaSource = JavaSource.forFileObject(fileObject); 54 55 if (javaSource != null) { 56 try { 57 javaSource.runUserActionTask(new CancellableTask<CompilationController>() { 58 public void cancel() { 59 } 60 61 public void run( 62 CompilationController compilationController) 63 throws Exception { 64 compilationController.toPhase(Phase.ELEMENTS_RESOLVED); 65 66 Trees trees = compilationController.getTrees(); 67 CompilationUnitTree compilationUnitTree = compilationController.getCompilationUnit(); 68 List <?extends Tree> typeDecls = compilationUnitTree.getTypeDecls(); 69 70 Set <Element> elementsSet = new LinkedHashSet <Element>(typeDecls.size() + 71 1); 72 73 for (Tree tree : typeDecls) { 74 Element element = trees.getElement(trees.getPath( 75 compilationUnitTree, tree)); 76 77 if (element != null) { 78 if (elementsSet.size() == 0) { 79 Element enclosingElement = element.getEnclosingElement(); 80 81 if ((enclosingElement != null) && 82 (enclosingElement.getKind() == ElementKind.PACKAGE)) { 83 elementsSet.add(enclosingElement); 85 } 86 } 87 88 elementsSet.add(element); 89 } 90 } 91 92 Element[] elements = elementsSet.toArray(JavaMembersModel.EMPTY_ELEMENTS_ARRAY); 93 show(fileObject, elements, compilationController); 94 } 95 }, false); 96 97 return; 98 } catch (IOException ioe) { 99 ErrorManager.getDefault().notify(ioe); 100 } 101 } 102 } 103 } 104 105 static void show(FileObject fileObject, Element[] elements, CompilationController compilationController) { 106 if (fileObject != null) { 107 JDialog dialog = ResizablePopup.getDialog(); 108 dialog.setTitle(NbBundle.getMessage(JavaMembers.class, "TITLE_Members")); dialog.setContentPane(new JavaMembersPanel(fileObject, elements, compilationController)); 110 dialog.setVisible(true); 111 } 112 } 113 114 } 115 | Popular Tags |