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.JavaMembers; 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 InspectMembersAtCaretAction extends BaseAction { 64 65 private static final String INSPECT_MEMBERS_AT_CARET = "inspect-members-at-caret"; 67 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); } 79 80 public void actionPerformed(ActionEvent evt, final JTextComponent 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 { 101 compilationController.toPhase(Phase.ELEMENTS_RESOLVED); 103 104 Document document = compilationController.getDocument(); 106 107 if (document != null) { 108 int dot = target.getCaret().getDot(); 110 111 TreePath tp = compilationController.getTreeUtilities() 113 .pathFor(dot); 114 115 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 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 e) { 179 Logger.global.log(Level.WARNING, e.getMessage(), e); 180 } 181 } 182 } 183 | Popular Tags |