1 19 20 package org.netbeans.modules.java.navigation.actions; 21 22 import com.sun.source.util.TreePath; 23 import java.awt.Toolkit ; 24 import java.awt.event.ActionEvent ; 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 ; 41 42 import java.util.logging.Level ; 43 import java.util.logging.Logger ; 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 ; 55 import javax.swing.text.JTextComponent ; 56 57 63 public final class InspectHierarchyAtCaretAction extends BaseAction { 64 65 private static final String INSPECT_HIERARCHY_AT_CARET = "inspect-hierarchy-at-caret"; 67 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); } 80 81 public void actionPerformed(ActionEvent evt, final JTextComponent 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 { 102 compilationController.toPhase(Phase.ELEMENTS_RESOLVED); 104 105 Document document = compilationController.getDocument(); 107 108 if (document != null) { 109 int dot = target.getCaret().getDot(); 111 112 TreePath tp = compilationController.getTreeUtilities() 114 .pathFor(dot); 115 116 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 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 e) { 180 Logger.global.log(Level.WARNING, e.getMessage(), e); 181 } 182 } 183 } 184 | Popular Tags |