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: import org.netbeans.api.editor.EditorRegistry;
048:
049: import org.netbeans.api.java.source.Task;
050: import org.netbeans.api.java.source.CompilationController;
051: import org.netbeans.api.java.source.JavaSource;
052: import org.netbeans.api.java.source.JavaSource.Phase;
053: import org.netbeans.editor.BaseAction;
054:
055: import org.netbeans.editor.ext.ExtKit;
056: import org.netbeans.modules.editor.NbEditorUtilities;
057: import org.netbeans.modules.java.navigation.JavaHierarchy;
058:
059: import org.openide.filesystems.FileObject;
060:
061: import org.openide.util.NbBundle;
062:
063: import java.io.IOException;
064:
065: import java.util.logging.Level;
066: import java.util.logging.Logger;
067:
068: import javax.lang.model.element.Element;
069: import javax.lang.model.element.ElementKind;
070: import javax.lang.model.element.ExecutableElement;
071: import javax.lang.model.element.TypeElement;
072: import javax.lang.model.element.VariableElement;
073: import javax.lang.model.type.DeclaredType;
074: import javax.lang.model.type.TypeKind;
075: import javax.lang.model.type.TypeMirror;
076:
077: import javax.swing.text.Document;
078: import javax.swing.text.JTextComponent;
079: import org.netbeans.api.project.ui.OpenProjects;
080:
081: /**
082: * This actions shows the hierarchy 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 InspectHierarchyAtCaretAction extends BaseAction {
088:
089: private static final String INSPECT_HIERARCHY_AT_CARET = "inspect-hierarchy-at-caret"; // NOI18N
090: private static final String INSPECT_HIERARCHY_AT_CARET_POPUP = INSPECT_HIERARCHY_AT_CARET
091: + "-popup"; // NOI18N
092:
093: /**
094: *
095: */
096: public InspectHierarchyAtCaretAction() {
097: super (NbBundle.getMessage(InspectMembersAtCaretAction.class,
098: INSPECT_HIERARCHY_AT_CARET), 0);
099:
100: putValue(ACTION_COMMAND_KEY, INSPECT_HIERARCHY_AT_CARET);
101: putValue(SHORT_DESCRIPTION, getValue(NAME));
102: putValue(ExtKit.TRIMMED_TEXT, getValue(NAME));
103: putValue(POPUP_MENU_TEXT, NbBundle.getMessage(
104: InspectMembersAtCaretAction.class,
105: INSPECT_HIERARCHY_AT_CARET_POPUP));
106:
107: putValue("noIconInMenu", Boolean.TRUE); // NOI18N
108: }
109:
110: @Override
111: public boolean isEnabled() {
112: if (EditorRegistry.lastFocusedComponent() == null
113: || !EditorRegistry.lastFocusedComponent().isShowing()) {
114: return false;
115: }
116: return OpenProjects.getDefault().getOpenProjects().length > 0;
117: }
118:
119: public void actionPerformed(ActionEvent evt,
120: final JTextComponent target) {
121: if (target == null) {
122: Toolkit.getDefaultToolkit().beep();
123: return;
124: }
125:
126: JavaSource javaSource = JavaSource.forDocument(target
127: .getDocument());
128:
129: if (javaSource == null) {
130: Toolkit.getDefaultToolkit().beep();
131: return;
132: }
133:
134: try {
135: javaSource.runUserActionTask(
136: new Task<CompilationController>() {
137:
138: public void run(
139: CompilationController compilationController)
140: throws IOException {
141: // Move to resolved phase
142: compilationController
143: .toPhase(Phase.ELEMENTS_RESOLVED);
144:
145: // Get document if open
146: Document document = compilationController
147: .getDocument();
148:
149: if (document != null) {
150: // Get Caret position
151: int dot = target.getCaret().getDot();
152:
153: // Find the TreePath for the caret position
154: TreePath tp = compilationController
155: .getTreeUtilities()
156: .pathFor(dot);
157:
158: // Get Element
159: Element element = compilationController
160: .getTrees().getElement(tp);
161:
162: FileObject elementFileObject = NbEditorUtilities
163: .getFileObject(document);
164: if (elementFileObject != null) {
165: if (element instanceof TypeElement) {
166: JavaHierarchy
167: .show(
168: elementFileObject,
169: new Element[] { element },
170: compilationController);
171: } else if (element instanceof VariableElement) {
172: TypeMirror typeMirror = ((VariableElement) element)
173: .asType();
174:
175: if (typeMirror.getKind() == TypeKind.DECLARED) {
176: element = ((DeclaredType) typeMirror)
177: .asElement();
178:
179: if (element != null) {
180: JavaHierarchy
181: .show(
182: elementFileObject,
183: new Element[] { element },
184: compilationController);
185: }
186: }
187: } else if (element instanceof ExecutableElement) {
188: // Method
189: if (element.getKind() == ElementKind.METHOD) {
190: TypeMirror typeMirror = ((ExecutableElement) element)
191: .getReturnType();
192:
193: if (typeMirror.getKind() == TypeKind.DECLARED) {
194: element = ((DeclaredType) typeMirror)
195: .asElement();
196:
197: if (element != null) {
198: JavaHierarchy
199: .show(
200: elementFileObject,
201: new Element[] { element },
202: compilationController);
203: }
204: }
205: } else if (element.getKind() == ElementKind.CONSTRUCTOR) {
206: element = element
207: .getEnclosingElement();
208:
209: if (element != null) {
210: JavaHierarchy
211: .show(
212: elementFileObject,
213: new Element[] { element },
214: compilationController);
215: }
216: }
217: }
218: }
219: }
220: }
221: }, true);
222: } catch (IOException e) {
223: Logger.getLogger(
224: InspectHierarchyAtCaretAction.class.getName()).log(
225: Level.WARNING, e.getMessage(), e);
226: }
227: }
228: }
|