KickJava   Java API By Example, From Geeks To Geeks.

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


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.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 JavaDoc;
31 import java.util.LinkedHashSet JavaDoc;
32 import java.util.List JavaDoc;
33 import java.util.Set JavaDoc;
34 import javax.lang.model.element.Element;
35 import javax.lang.model.element.ElementKind;
36 import javax.swing.JDialog JavaDoc;
37 import org.openide.filesystems.FileObject;
38 import org.openide.util.NbBundle;
39
40 /**
41  *
42  * @author Sandip Chitale (Sandip.Chitale@Sun.Com)
43  */

44 public final class JavaMembers {
45
46     /**
47      * Show the members of the types in the fileObject.
48      *
49      * @param fileObject
50      */

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 JavaDoc {
64                                 compilationController.toPhase(Phase.ELEMENTS_RESOLVED);
65
66                                 Trees trees = compilationController.getTrees();
67                                 CompilationUnitTree compilationUnitTree = compilationController.getCompilationUnit();
68                                 List JavaDoc<?extends Tree> typeDecls = compilationUnitTree.getTypeDecls();
69
70                                 Set JavaDoc<Element> elementsSet = new LinkedHashSet JavaDoc<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                                                 // add package
84
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 JavaDoc 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 JavaDoc dialog = ResizablePopup.getDialog();
108             dialog.setTitle(NbBundle.getMessage(JavaMembers.class, "TITLE_Members")); // NOI18N
109
dialog.setContentPane(new JavaMembersPanel(fileObject, elements, compilationController));
110             dialog.setVisible(true);
111         }
112     }
113
114 }
115
Popular Tags