KickJava   Java API By Example, From Geeks To Geeks.

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


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.swing.JDialog JavaDoc;
36 import org.openide.filesystems.FileObject;
37 import org.openide.util.NbBundle;
38
39 /**
40  *
41  * @author Sandip Chitale (Sandip.Chitale@Sun.Com)
42  */

43 public final class JavaHierarchy {
44
45     /**
46      * Show the hierarchy of the types in the fileObject.
47      *
48      * @param fileObject
49      */

50     public static void show(final FileObject fileObject) {
51         if (fileObject != null) {
52             JavaSource javaSource = JavaSource.forFileObject(fileObject);
53
54             if (javaSource != null) {
55                 try {
56                     javaSource.runUserActionTask(new CancellableTask<CompilationController>() {
57                             public void cancel() {
58                             }
59
60                             public void run(
61                                 CompilationController compilationController)
62                                 throws Exception JavaDoc {
63                                 compilationController.toPhase(Phase.ELEMENTS_RESOLVED);
64
65                                 Trees trees = compilationController.getTrees();
66                                 CompilationUnitTree compilationUnitTree = compilationController.getCompilationUnit();
67                                 List JavaDoc<?extends Tree> typeDecls = compilationUnitTree.getTypeDecls();
68
69                                 Set JavaDoc<Element> elementsSet = new LinkedHashSet JavaDoc<Element>(typeDecls.size() + 1);
70
71                                 for (Tree tree : typeDecls) {
72                                     Element element = trees.getElement(trees.getPath(compilationUnitTree, tree));
73
74                                     if (element != null) {
75                                         elementsSet.add(element);
76                                     }
77                                 }
78
79                                 Element[] elements = elementsSet.toArray(JavaMembersModel.EMPTY_ELEMENTS_ARRAY);
80                                 show(fileObject, elements, compilationController);
81                             }
82                         }, false);
83
84                     return;
85                 } catch (IOException JavaDoc ioe) {
86                     ErrorManager.getDefault().notify(ioe);
87                 }
88             }
89         }
90     }
91
92     static void show(FileObject fileObject, Element[] elements,
93         CompilationController compilationController) {
94         if (fileObject != null) {
95             JDialog JavaDoc dialog = ResizablePopup.getDialog();
96             dialog.setTitle(NbBundle.getMessage(JavaHierarchy.class, "TITLE_Hierarchy")); // NOI18N
97
dialog.setContentPane(new JavaHierarchyPanel(fileObject, elements, compilationController));
98             dialog.setVisible(true);
99         }
100     }
101
102 }
103
Popular Tags