1 12 package org.eclipse.jdt.internal.ui.callhierarchy; 13 14 import java.lang.reflect.InvocationTargetException ; 15 import java.util.ArrayList ; 16 import java.util.Arrays ; 17 import java.util.List ; 18 19 import org.eclipse.core.runtime.IStatus; 20 import org.eclipse.core.runtime.Status; 21 22 import org.eclipse.jface.dialogs.ErrorDialog; 23 import org.eclipse.jface.text.ITextSelection; 24 25 import org.eclipse.jface.viewers.ISelectionProvider; 26 import org.eclipse.jface.viewers.IStructuredSelection; 27 28 import org.eclipse.ui.IWorkbenchSite; 29 import org.eclipse.ui.PlatformUI; 30 31 import org.eclipse.jdt.core.IClassFile; 32 import org.eclipse.jdt.core.ICompilationUnit; 33 import org.eclipse.jdt.core.IJavaElement; 34 import org.eclipse.jdt.core.JavaModelException; 35 36 import org.eclipse.jdt.ui.actions.SelectionDispatchAction; 37 38 import org.eclipse.jdt.internal.ui.IJavaHelpContextIds; 39 import org.eclipse.jdt.internal.ui.IJavaStatusConstants; 40 import org.eclipse.jdt.internal.ui.JavaPlugin; 41 import org.eclipse.jdt.internal.ui.actions.ActionMessages; 42 import org.eclipse.jdt.internal.ui.actions.ActionUtil; 43 import org.eclipse.jdt.internal.ui.actions.SelectionConverter; 44 import org.eclipse.jdt.internal.ui.javaeditor.JavaEditor; 45 import org.eclipse.jdt.internal.ui.util.ExceptionHandler; 46 47 53 public class OpenCallHierarchyAction extends SelectionDispatchAction { 54 55 private JavaEditor fEditor; 56 57 64 public OpenCallHierarchyAction(IWorkbenchSite site) { 65 super(site); 66 setText(CallHierarchyMessages.OpenCallHierarchyAction_label); 67 setToolTipText(CallHierarchyMessages.OpenCallHierarchyAction_tooltip); 68 setDescription(CallHierarchyMessages.OpenCallHierarchyAction_description); 69 PlatformUI.getWorkbench().getHelpSystem().setHelp(this, IJavaHelpContextIds.CALL_HIERARCHY_OPEN_ACTION); 70 71 } 72 73 87 public OpenCallHierarchyAction(IWorkbenchSite site, ISelectionProvider provider) { 88 this(site); 89 setSpecialSelectionProvider(provider); 90 } 91 92 95 public OpenCallHierarchyAction(JavaEditor editor) { 96 this(editor.getEditorSite()); 97 fEditor= editor; 98 setEnabled(SelectionConverter.canOperateOn(fEditor)); 99 } 100 101 104 public void selectionChanged(ITextSelection selection) { 105 } 107 108 111 public void selectionChanged(IStructuredSelection selection) { 112 setEnabled(isEnabled(selection)); 113 } 114 115 private boolean isEnabled(IStructuredSelection selection) { 116 if (selection.size() != 1) 117 return false; 118 Object input= selection.getFirstElement(); 119 if (!(input instanceof IJavaElement)) 120 return false; 121 switch (((IJavaElement)input).getElementType()) { 122 case IJavaElement.METHOD: 123 return true; 124 default: 125 return false; 126 } 127 } 128 129 132 public void run(ITextSelection selection) { 133 IJavaElement input= SelectionConverter.getInput(fEditor); 134 if (!ActionUtil.isProcessable(getShell(), input)) 135 return; 136 137 try { 138 IJavaElement[] elements= SelectionConverter.codeResolveOrInputForked(fEditor); 139 if (elements == null) 140 return; 141 List candidates= new ArrayList (elements.length); 142 for (int i= 0; i < elements.length; i++) { 143 IJavaElement[] resolvedElements= CallHierarchyUI.getCandidates(elements[i]); 144 if (resolvedElements != null) 145 candidates.addAll(Arrays.asList(resolvedElements)); 146 } 147 if (candidates.isEmpty()) { 148 IJavaElement enclosingMethod= getEnclosingMethod(input, selection); 149 if (enclosingMethod != null) { 150 candidates.add(enclosingMethod); 151 } 152 } 153 run((IJavaElement[])candidates.toArray(new IJavaElement[candidates.size()])); 154 } catch (InvocationTargetException e) { 155 ExceptionHandler.handle(e, getShell(), getErrorDialogTitle(), ActionMessages.SelectionConverter_codeResolve_failed); 156 } catch (InterruptedException e) { 157 } 159 } 160 161 private IJavaElement getEnclosingMethod(IJavaElement input, ITextSelection selection) { 162 IJavaElement enclosingElement= null; 163 try { 164 switch (input.getElementType()) { 165 case IJavaElement.CLASS_FILE : 166 IClassFile classFile= (IClassFile) input.getAncestor(IJavaElement.CLASS_FILE); 167 if (classFile != null) { 168 enclosingElement= classFile.getElementAt(selection.getOffset()); 169 } 170 break; 171 case IJavaElement.COMPILATION_UNIT : 172 ICompilationUnit cu= (ICompilationUnit) input.getAncestor(IJavaElement.COMPILATION_UNIT); 173 if (cu != null) { 174 enclosingElement= cu.getElementAt(selection.getOffset()); 175 } 176 break; 177 } 178 if (enclosingElement != null && enclosingElement.getElementType() == IJavaElement.METHOD) { 179 return enclosingElement; 180 } 181 } catch (JavaModelException e) { 182 JavaPlugin.log(e); 183 } 184 185 return null; 186 } 187 188 191 public void run(IStructuredSelection selection) { 192 if (selection.size() != 1) 193 return; 194 Object input= selection.getFirstElement(); 195 196 if (!(input instanceof IJavaElement)) { 197 IStatus status= createStatus(CallHierarchyMessages.OpenCallHierarchyAction_messages_no_java_element); 198 openErrorDialog(status); 199 return; 200 } 201 IJavaElement element= (IJavaElement) input; 202 if (!ActionUtil.isProcessable(getShell(), element)) 203 return; 204 205 List result= new ArrayList (1); 206 IStatus status= compileCandidates(result, element); 207 if (status.isOK()) { 208 run((IJavaElement[]) result.toArray(new IJavaElement[result.size()])); 209 } else { 210 openErrorDialog(status); 211 } 212 } 213 214 private int openErrorDialog(IStatus status) { 215 String message= CallHierarchyMessages.OpenCallHierarchyAction_messages_title; 216 String dialogTitle= getErrorDialogTitle(); 217 return ErrorDialog.openError(getShell(), dialogTitle, message, status); 218 } 219 220 private static String getErrorDialogTitle() { 221 return CallHierarchyMessages.OpenCallHierarchyAction_dialog_title; 222 } 223 224 public void run(IJavaElement[] elements) { 225 if (elements.length == 0) { 226 getShell().getDisplay().beep(); 227 return; 228 } 229 CallHierarchyUI.open(elements, getSite().getWorkbenchWindow()); 230 } 231 232 private static IStatus compileCandidates(List result, IJavaElement elem) { 233 IStatus ok= new Status(IStatus.OK, JavaPlugin.getPluginId(), 0, "", null); switch (elem.getElementType()) { 235 case IJavaElement.METHOD: 236 result.add(elem); 237 return ok; 238 } 239 return createStatus(CallHierarchyMessages.OpenCallHierarchyAction_messages_no_valid_java_element); 240 } 241 242 private static IStatus createStatus(String message) { 243 return new Status(IStatus.INFO, JavaPlugin.getPluginId(), IJavaStatusConstants.INTERNAL_ERROR, message, null); 244 } 245 } 246 | Popular Tags |