001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.java.navigation.actions;
043:
044: import com.sun.source.util.TreePath;
045: import java.awt.Toolkit;
046: import java.awt.event.ActionEvent;
047:
048: import org.netbeans.api.java.source.Task;
049: import org.netbeans.api.java.source.CompilationController;
050: import org.netbeans.api.java.source.JavaSource;
051: import org.netbeans.api.java.source.JavaSource.Phase;
052: import org.netbeans.editor.BaseAction;
053:
054: import org.netbeans.editor.ext.ExtKit;
055: import org.netbeans.modules.editor.NbEditorUtilities;
056: import org.netbeans.modules.java.navigation.JavaMembers;
057:
058: import org.openide.filesystems.FileObject;
059:
060: import org.openide.util.NbBundle;
061:
062: import java.io.IOException;
063:
064: import java.util.logging.Level;
065: import java.util.logging.Logger;
066:
067: import javax.lang.model.element.Element;
068: import javax.lang.model.element.ElementKind;
069: import javax.lang.model.element.ExecutableElement;
070: import javax.lang.model.element.TypeElement;
071: import javax.lang.model.element.VariableElement;
072: import javax.lang.model.type.DeclaredType;
073: import javax.lang.model.type.TypeKind;
074: import javax.lang.model.type.TypeMirror;
075:
076: import javax.swing.text.Document;
077: import javax.swing.text.JTextComponent;
078: import org.netbeans.api.editor.EditorRegistry;
079: import org.netbeans.api.project.ui.OpenProjects;
080:
081: /**
082: * This actions shows the members of the type of the element under the caret
083: * in a popup window.
084: *
085: * @author Sandip Chitale (Sandip.Chitale@Sun.Com)
086: */
087: public final class InspectMembersAtCaretAction extends BaseAction {
088:
089: private static final String INSPECT_MEMBERS_AT_CARET = "inspect-members-at-caret"; // NOI18N
090: private static final String INSPECT_MEMBERS_AT_CARET_POPUP = INSPECT_MEMBERS_AT_CARET
091: + "-popup"; // NOI18N
092:
093: /**
094: *
095: */
096: public InspectMembersAtCaretAction() {
097: super (NbBundle.getMessage(InspectMembersAtCaretAction.class,
098: INSPECT_MEMBERS_AT_CARET), 0);
099:
100: putValue(SHORT_DESCRIPTION, getValue(NAME));
101: putValue(ExtKit.TRIMMED_TEXT, getValue(NAME));
102: putValue(POPUP_MENU_TEXT, NbBundle.getMessage(
103: InspectMembersAtCaretAction.class,
104: INSPECT_MEMBERS_AT_CARET_POPUP));
105:
106: putValue("noIconInMenu", Boolean.TRUE); // NOI18N
107: }
108:
109: @Override
110: public boolean isEnabled() {
111: if (EditorRegistry.lastFocusedComponent() == null
112: || !EditorRegistry.lastFocusedComponent().isShowing()) {
113: return false;
114: }
115: return OpenProjects.getDefault().getOpenProjects().length > 0;
116: }
117:
118: public void actionPerformed(ActionEvent evt,
119: final JTextComponent target) {
120: if (target == null) {
121: Toolkit.getDefaultToolkit().beep();
122: return;
123: }
124:
125: JavaSource javaSource = JavaSource.forDocument(target
126: .getDocument());
127:
128: if (javaSource == null) {
129: Toolkit.getDefaultToolkit().beep();
130: return;
131: }
132:
133: try {
134: javaSource.runUserActionTask(
135: new Task<CompilationController>() {
136:
137: public void run(
138: CompilationController compilationController)
139: throws IOException {
140: // Move to resolved phase
141: compilationController
142: .toPhase(Phase.ELEMENTS_RESOLVED);
143:
144: // Get document if open
145: Document document = compilationController
146: .getDocument();
147:
148: if (document != null) {
149: // Get Caret position
150: int dot = target.getCaret().getDot();
151:
152: // Find the TreePath for the caret position
153: TreePath tp = compilationController
154: .getTreeUtilities()
155: .pathFor(dot);
156:
157: // Get Element
158: Element element = compilationController
159: .getTrees().getElement(tp);
160: FileObject elementFileObject = NbEditorUtilities
161: .getFileObject(document);
162: if (elementFileObject != null) {
163: if (element instanceof TypeElement) {
164: JavaMembers
165: .show(
166: elementFileObject,
167: new Element[] { element },
168: compilationController);
169: } else if (element instanceof VariableElement) {
170: TypeMirror typeMirror = ((VariableElement) element)
171: .asType();
172:
173: if (typeMirror.getKind() == TypeKind.DECLARED) {
174: element = ((DeclaredType) typeMirror)
175: .asElement();
176:
177: if (element != null) {
178: JavaMembers
179: .show(
180: elementFileObject,
181: new Element[] { element },
182: compilationController);
183: }
184: }
185: } else if (element instanceof ExecutableElement) {
186: // Method
187: if (element.getKind() == ElementKind.METHOD) {
188: TypeMirror typeMirror = ((ExecutableElement) element)
189: .getReturnType();
190:
191: if (typeMirror.getKind() == TypeKind.DECLARED) {
192: element = ((DeclaredType) typeMirror)
193: .asElement();
194:
195: if (element != null) {
196: JavaMembers
197: .show(
198: elementFileObject,
199: new Element[] { element },
200: compilationController);
201: }
202: }
203: } else if (element.getKind() == ElementKind.CONSTRUCTOR) {
204: element = element
205: .getEnclosingElement();
206:
207: if (element != null) {
208: JavaMembers
209: .show(
210: elementFileObject,
211: new Element[] { element },
212: compilationController);
213: }
214: }
215: }
216: }
217: }
218: }
219: }, true);
220: } catch (IOException e) {
221: Logger.getLogger(
222: InspectMembersAtCaretAction.class.getName()).log(
223: Level.WARNING, e.getMessage(), e);
224: }
225: }
226: }
|