KickJava   Java API By Example, From Geeks To Geeks.

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


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.JavaMembers;
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 members 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 InspectMembersAtCaretAction extends BaseAction {
64
65     private static final String JavaDoc INSPECT_MEMBERS_AT_CARET = "inspect-members-at-caret"; // NOI18N
66

67     /**
68      *
69      */

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