KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > java > navigation > actions > InspectHierarchyAtCaretAction


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.actions;
21
22 import com.sun.source.util.TreePath;
23 import java.awt.Toolkit JavaDoc;
24 import java.awt.event.ActionEvent JavaDoc;
25
26 import org.netbeans.api.java.source.CancellableTask;
27 import org.netbeans.api.java.source.CompilationController;
28 import org.netbeans.api.java.source.JavaSource;
29 import org.netbeans.api.java.source.JavaSource.Phase;
30 import org.netbeans.api.java.source.SourceUtils;
31 import org.netbeans.editor.BaseAction;
32
33 import org.netbeans.editor.ext.ExtKit;
34 import org.netbeans.modules.java.navigation.JavaHierarchy;
35
36 import org.openide.filesystems.FileObject;
37
38 import org.openide.util.NbBundle;
39
40 import java.io.IOException JavaDoc;
41
42 import java.util.logging.Level JavaDoc;
43 import java.util.logging.Logger JavaDoc;
44
45 import javax.lang.model.element.Element;
46 import javax.lang.model.element.ElementKind;
47 import javax.lang.model.element.ExecutableElement;
48 import javax.lang.model.element.TypeElement;
49 import javax.lang.model.element.VariableElement;
50 import javax.lang.model.type.DeclaredType;
51 import javax.lang.model.type.TypeKind;
52 import javax.lang.model.type.TypeMirror;
53
54 import javax.swing.text.Document JavaDoc;
55 import javax.swing.text.JTextComponent JavaDoc;
56
57 /**
58  * This actions shows the hierarchy of the type of the element under the caret
59  * in a popup window.
60  *
61  * @author Sandip Chitale (Sandip.Chitale@Sun.Com)
62  */

63 public final class InspectHierarchyAtCaretAction extends BaseAction {
64
65     private static final String JavaDoc INSPECT_HIERARCHY_AT_CARET = "inspect-hierarchy-at-caret"; // NOI18N
66

67     /**
68      *
69      */

70     public InspectHierarchyAtCaretAction() {
71         super(NbBundle.getMessage(InspectMembersAtCaretAction.class, INSPECT_HIERARCHY_AT_CARET), 0);
72
73         putValue(ACTION_COMMAND_KEY, INSPECT_HIERARCHY_AT_CARET);
74         putValue(SHORT_DESCRIPTION, getValue(NAME));
75         putValue(ExtKit.TRIMMED_TEXT,getValue(NAME));
76         putValue(POPUP_MENU_TEXT, getValue(NAME));
77
78         putValue("noIconInMenu", Boolean.TRUE); // NOI18N
79
}
80
81     public void actionPerformed(ActionEvent JavaDoc evt, final JTextComponent JavaDoc target) {
82         if (target == null) {
83             Toolkit.getDefaultToolkit().beep();
84             return;
85         }
86
87         JavaSource javaSource = JavaSource.forDocument(target.getDocument());
88
89         if (javaSource == null) {
90             Toolkit.getDefaultToolkit().beep();
91             return;
92         }
93
94         try {
95             javaSource.runUserActionTask(new CancellableTask<CompilationController>() {
96                     public void cancel() {
97                     }
98
99                     public void run(
100                         CompilationController compilationController)
101                         throws IOException JavaDoc {
102                         // Move to resolved phase
103
compilationController.toPhase(Phase.ELEMENTS_RESOLVED);
104
105                         // Get document if open
106
Document JavaDoc document = compilationController.getDocument();
107
108                         if (document != null) {
109                             // Get Caret position
110
int dot = target.getCaret().getDot();
111
112                             // Find the TreePath for the caret position
113
TreePath tp = compilationController.getTreeUtilities()
114                                                                .pathFor(dot);
115
116                             // Get Element
117
Element element = compilationController.getTrees()
118                                                                    .getElement(tp);
119
120                             if (element instanceof TypeElement) {
121                                 FileObject elementFileObject = SourceUtils.getFile(element,
122                                         compilationController.getClasspathInfo());
123
124                                 if (elementFileObject != null) {
125                                     JavaHierarchy.show(elementFileObject);
126                                 }
127
128                             } else if (element instanceof VariableElement) {
129                                 TypeMirror typeMirror = ((VariableElement) element).asType();
130
131                                 if (typeMirror.getKind() == TypeKind.DECLARED) {
132                                     element = ((DeclaredType) typeMirror).asElement();
133
134                                     if (element != null) {
135                                         FileObject elementFileObject =
136                                             SourceUtils.getFile(element,
137                                                 compilationController.getClasspathInfo());
138
139                                         if (elementFileObject != null) {
140                                             JavaHierarchy.show(elementFileObject);
141                                         }
142                                     }
143                                 }
144                             } else if (element instanceof ExecutableElement) {
145                                 // Method
146
if (element.getKind() == ElementKind.METHOD) {
147                                     TypeMirror typeMirror = ((ExecutableElement) element).getReturnType();
148
149                                     if (typeMirror.getKind() == TypeKind.DECLARED) {
150                                         element = ((DeclaredType) typeMirror).asElement();
151
152                                         if (element != null) {
153                                             FileObject elementFileObject =
154                                                 SourceUtils.getFile(element,
155                                                     compilationController.getClasspathInfo());
156
157                                             if (elementFileObject != null) {
158                                                 JavaHierarchy.show(elementFileObject);
159                                             }
160                                         }
161                                     }
162                                 } else if (element.getKind() == ElementKind.CONSTRUCTOR) {
163                                     element = element.getEnclosingElement();
164
165                                     if (element != null) {
166                                         FileObject elementFileObject =
167                                             SourceUtils.getFile(element,
168                                                 compilationController.getClasspathInfo());
169
170                                         if (elementFileObject != null) {
171                                             JavaHierarchy.show(elementFileObject);
172                                         }
173                                     }
174                                 }
175                             }
176                         }
177                     }
178                 }, true);
179         } catch (IOException JavaDoc e) {
180             Logger.global.log(Level.WARNING, e.getMessage(), e);
181         }
182     }
183 }
184
Popular Tags