1 11 package org.eclipse.jdt.ui.actions; 12 13 import org.eclipse.core.runtime.CoreException; 14 15 import org.eclipse.jface.dialogs.MessageDialog; 16 import org.eclipse.jface.viewers.ISelectionProvider; 17 import org.eclipse.jface.viewers.IStructuredSelection; 18 19 import org.eclipse.jface.text.ITextSelection; 20 21 import org.eclipse.ui.IWorkbenchSite; 22 import org.eclipse.ui.PlatformUI; 23 24 import org.eclipse.jdt.core.Flags; 25 import org.eclipse.jdt.core.IJavaElement; 26 import org.eclipse.jdt.core.IMethod; 27 import org.eclipse.jdt.core.IType; 28 import org.eclipse.jdt.core.JavaModelException; 29 30 import org.eclipse.jdt.internal.corext.util.Messages; 31 import org.eclipse.jdt.internal.corext.util.MethodOverrideTester; 32 import org.eclipse.jdt.internal.corext.util.SuperTypeHierarchyCache; 33 34 import org.eclipse.jdt.ui.JavaUI; 35 36 import org.eclipse.jdt.internal.ui.IJavaHelpContextIds; 37 import org.eclipse.jdt.internal.ui.JavaPlugin; 38 import org.eclipse.jdt.internal.ui.actions.ActionMessages; 39 import org.eclipse.jdt.internal.ui.actions.ActionUtil; 40 import org.eclipse.jdt.internal.ui.actions.SelectionConverter; 41 import org.eclipse.jdt.internal.ui.javaeditor.JavaEditor; 42 import org.eclipse.jdt.internal.ui.util.ExceptionHandler; 43 44 56 public class OpenSuperImplementationAction extends SelectionDispatchAction { 57 58 private JavaEditor fEditor; 59 60 67 public OpenSuperImplementationAction(IWorkbenchSite site) { 68 super(site); 69 setText(ActionMessages.OpenSuperImplementationAction_label); 70 setDescription(ActionMessages.OpenSuperImplementationAction_description); 71 setToolTipText(ActionMessages.OpenSuperImplementationAction_tooltip); 72 PlatformUI.getWorkbench().getHelpSystem().setHelp(this, IJavaHelpContextIds.OPEN_SUPER_IMPLEMENTATION_ACTION); 73 } 74 75 89 public OpenSuperImplementationAction(IWorkbenchSite site, ISelectionProvider provider) { 90 this(site); 91 setSpecialSelectionProvider(provider); 92 } 93 94 95 96 100 public OpenSuperImplementationAction(JavaEditor editor) { 101 this(editor.getEditorSite()); 102 fEditor= editor; 103 setEnabled(SelectionConverter.canOperateOn(fEditor)); 104 } 105 106 109 public void selectionChanged(ITextSelection selection) { 110 } 111 112 115 public void selectionChanged(IStructuredSelection selection) { 116 IMethod method= getMethod(selection); 117 118 setEnabled(method != null && checkMethod(method)); 119 } 120 121 124 public void run(ITextSelection selection) { 125 if (!ActionUtil.isProcessable(fEditor)) 126 return; 127 IJavaElement element= elementAtOffset(); 128 if (element == null || !(element instanceof IMethod)) { 129 MessageDialog.openInformation(getShell(), getDialogTitle(), ActionMessages.OpenSuperImplementationAction_not_applicable); 130 return; 131 } 132 run((IMethod) element); 133 } 134 135 138 public void run(IStructuredSelection selection) { 139 run(getMethod(selection)); 140 } 141 142 146 public void run(IMethod method) { 147 if (method == null) 148 return; 149 if (!ActionUtil.isProcessable(getShell(), method)) 150 return; 151 152 if (!checkMethod(method)) { 153 MessageDialog.openInformation(getShell(), getDialogTitle(), 154 Messages.format(ActionMessages.OpenSuperImplementationAction_no_super_implementation, method.getElementName())); 155 return; 156 } 157 158 try { 159 IMethod impl= findSuperImplementation(method); 160 if (impl != null) { 161 JavaUI.openInEditor(impl, true, true); 162 } 163 } catch (CoreException e) { 164 ExceptionHandler.handle(e, getDialogTitle(), ActionMessages.OpenSuperImplementationAction_error_message); 165 } 166 } 167 168 private IMethod findSuperImplementation(IMethod method) throws JavaModelException { 169 MethodOverrideTester tester= SuperTypeHierarchyCache.getMethodOverrideTester(method.getDeclaringType()); 170 return tester.findOverriddenMethod(method, false); 171 } 172 173 174 private IMethod getMethod(IStructuredSelection selection) { 175 if (selection.size() != 1) 176 return null; 177 Object element= selection.getFirstElement(); 178 if (element instanceof IMethod) { 179 return (IMethod) element; 180 } 181 return null; 182 } 183 184 private boolean checkMethod(IMethod method) { 185 try { 186 int flags= method.getFlags(); 187 if (!Flags.isStatic(flags) && !Flags.isPrivate(flags)) { 188 IType declaringType= method.getDeclaringType(); 189 if (SuperTypeHierarchyCache.hasInCache(declaringType)) { 190 if (findSuperImplementation(method) == null) { 191 return false; 192 } 193 } 194 return true; 195 } 196 } catch (JavaModelException e) { 197 if (!e.isDoesNotExist()) { 198 JavaPlugin.log(e); 199 } 200 } 201 return false; 202 } 203 204 private IJavaElement elementAtOffset() { 205 try { 206 return SelectionConverter.getElementAtOffset(fEditor); 207 } catch(JavaModelException e) { 208 } 209 return null; 210 } 211 212 private static String getDialogTitle() { 213 return ActionMessages.OpenSuperImplementationAction_error_title; 214 } 215 } 216 | Popular Tags |