1 /*******************************************************************************
2 * Copyright (c) 2000, 2011 IBM Corporation and others.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/epl-v10.html
9 * IBM Corporation - initial API and implementation
10 *******************************************************************************/
11 package org.eclipse.jdt.ui.actions;
13 import java.lang.reflect.InvocationTargetException;
14 import java.util.ArrayList;
16 import org.eclipse.swt.widgets.Shell;
18 import org.eclipse.core.runtime.CoreException;
20 import org.eclipse.core.resources.IWorkspaceRunnable;
22 import org.eclipse.jface.dialogs.MessageDialog;
23 import org.eclipse.jface.operation.IRunnableContext;
24 import org.eclipse.jface.viewers.IStructuredSelection;
25 import org.eclipse.jface.window.Window;
27 import org.eclipse.jface.text.IRewriteTarget;
28 import org.eclipse.jface.text.ITextSelection;
30 import org.eclipse.ui.IEditorPart;
31 import org.eclipse.ui.IWorkbenchSite;
32 import org.eclipse.ui.PlatformUI;
34 import org.eclipse.jdt.core.ICompilationUnit;
35 import org.eclipse.jdt.core.IType;
36 import org.eclipse.jdt.core.JavaModelException;
37 import org.eclipse.jdt.core.dom.ASTParser;
38 import org.eclipse.jdt.core.dom.CompilationUnit;
39 import org.eclipse.jdt.core.dom.IMethodBinding;
40 import org.eclipse.jdt.core.dom.ITypeBinding;
42 import org.eclipse.jdt.internal.corext.codemanipulation.AddUnimplementedMethodsOperation;
43 import org.eclipse.jdt.internal.corext.dom.ASTNodes;
44 import org.eclipse.jdt.internal.corext.util.JavaModelUtil;
46 import org.eclipse.jdt.ui.JavaUI;
48 import org.eclipse.jdt.internal.ui.IJavaHelpContextIds;
49 import org.eclipse.jdt.internal.ui.JavaPlugin;
50 import org.eclipse.jdt.internal.ui.actions.ActionMessages;
51 import org.eclipse.jdt.internal.ui.actions.ActionUtil;
52 import org.eclipse.jdt.internal.ui.actions.SelectionConverter;
53 import org.eclipse.jdt.internal.ui.actions.WorkbenchRunnableAdapter;
54 import org.eclipse.jdt.internal.ui.dialogs.OverrideMethodDialog;
55 import org.eclipse.jdt.internal.ui.javaeditor.CompilationUnitEditor;
56 import org.eclipse.jdt.internal.ui.util.BusyIndicatorRunnableContext;
57 import org.eclipse.jdt.internal.ui.util.ElementValidator;
58 import org.eclipse.jdt.internal.ui.util.ExceptionHandler;
61 * Adds unimplemented methods of a type. The action opens a dialog from which the user can
62 * choose the methods to be added.
64 * Will open the parent compilation unit in a Java editor. The result is unsaved, so the
65 * user can decide if the changes are acceptable.
67 * The action is applicable to structured selections containing elements of type
68 * {@link org.eclipse.jdt.core.IType}.
71 * This class may be instantiated; it is not intended to be subclassed.
76 * @noextend This class is not intended to be subclassed by clients.
78 public class OverrideMethodsAction extends SelectionDispatchAction {
80 /** The dialog title */
81 private static final String DIALOG_TITLE= ActionMessages.OverrideMethodsAction_error_title;
83 /** The compilation unit editor */
84 private CompilationUnitEditor fEditor;
87 * Note: This constructor is for internal use only. Clients should not call this
89 * @param editor the compilation unit editor
91 * @noreference This constructor is not intended to be referenced by clients.
93 public OverrideMethodsAction(final CompilationUnitEditor editor) {
94 this(editor.getEditorSite());
96 setEnabled(checkEnabledEditor());
100 * Creates a new override method action.
102 * The action requires that the selection provided by the site's selection provider is
103 * of type {@link org.eclipse.jface.viewers.IStructuredSelection}.
105 * @param site the workbench site providing context information for this action
107 public OverrideMethodsAction(final IWorkbenchSite site) {
109 setText(ActionMessages.OverrideMethodsAction_label);
110 setDescription(ActionMessages.OverrideMethodsAction_description);
111 setToolTipText(ActionMessages.OverrideMethodsAction_tooltip);
112 PlatformUI.getWorkbench().getHelpSystem().setHelp(this, IJavaHelpContextIds.ADD_UNIMPLEMENTED_METHODS_ACTION);
115 private boolean canEnable(IStructuredSelection selection) throws JavaModelException {
116 if ((selection.size() == 1) && (selection.getFirstElement() instanceof IType)) {
117 final IType type= (IType) selection.getFirstElement();
118 return type.getCompilationUnit() != null && !type.isInterface();
120 if ((selection.size() == 1) && (selection.getFirstElement() instanceof ICompilationUnit))
125 private boolean checkEnabledEditor() {
126 return fEditor != null && SelectionConverter.canOperateOn(fEditor);
129 private String getDialogTitle() {
133 private IType getSelectedType(IStructuredSelection selection) throws JavaModelException {
134 final Object[] elements= selection.toArray();
135 if (elements.length == 1 && (elements[0] instanceof IType)) {
136 final IType type= (IType) elements[0];
137 if (type.getCompilationUnit() != null && !type.isInterface()) {
140 } else if (elements[0] instanceof ICompilationUnit) {
141 final IType type= ((ICompilationUnit) elements[0]).findPrimaryType();
142 if (type != null && !type.isInterface())
149 * @see org.eclipse.jdt.ui.actions.SelectionDispatchAction#run(org.eclipse.jface.viewers.IStructuredSelection)
152 public void run(IStructuredSelection selection) {
154 final IType type= getSelectedType(selection);
156 MessageDialog.openInformation(getShell(), getDialogTitle(), ActionMessages.OverrideMethodsAction_not_applicable);
160 if (!ElementValidator.check(type, getShell(), getDialogTitle(), false) || !ActionUtil.isEditable(getShell(), type)) {
164 run(getShell(), type);
165 } catch (CoreException exception) {
166 ExceptionHandler.handle(exception, getShell(), getDialogTitle(), ActionMessages.OverrideMethodsAction_error_actionfailed);
171 * @see org.eclipse.jdt.ui.actions.SelectionDispatchAction#run(org.eclipse.jface.text.ITextSelection)
174 public void run(ITextSelection selection) {
176 final IType type= SelectionConverter.getTypeAtOffset(fEditor);
178 if (!ElementValidator.check(type, getShell(), getDialogTitle(), false) || !ActionUtil.isEditable(fEditor, getShell(), type)) {
182 if (type.isAnnotation()) {
183 MessageDialog.openInformation(getShell(), getDialogTitle(), ActionMessages.OverrideMethodsAction_annotation_not_applicable);
187 if (type.isInterface()) {
188 MessageDialog.openInformation(getShell(), getDialogTitle(), ActionMessages.OverrideMethodsAction_interface_not_applicable);
192 run(getShell(), type);
194 MessageDialog.openInformation(getShell(), getDialogTitle(), ActionMessages.OverrideMethodsAction_not_applicable);
196 } catch (JavaModelException e) {
197 ExceptionHandler.handle(e, getShell(), getDialogTitle(), null);
198 } catch (CoreException e) {
199 ExceptionHandler.handle(e, getShell(), getDialogTitle(), ActionMessages.OverrideMethodsAction_error_actionfailed);
203 private void run(Shell shell, IType type) throws CoreException {
204 final OverrideMethodDialog dialog= new OverrideMethodDialog(shell, fEditor, type, false);
205 if (!dialog.hasMethodsToOverride()) {
206 MessageDialog.openInformation(shell, getDialogTitle(), ActionMessages.OverrideMethodsAction_error_nothing_found);
210 if (dialog.open() != Window.OK) {
215 final Object[] selected= dialog.getResult();
216 if (selected == null) {
221 ArrayList<IMethodBinding> methods= new ArrayList<IMethodBinding>();
222 for (int i= 0; i < selected.length; i++) {
223 Object elem= selected[i];
224 if (elem instanceof IMethodBinding) {
225 methods.add((IMethodBinding) elem);
228 IMethodBinding[] methodToOverride= methods.toArray(new IMethodBinding[methods.size()]);
231 final IEditorPart editor= JavaUI.openInEditor(type.getCompilationUnit());
232 final IRewriteTarget target= editor != null ? (IRewriteTarget) editor.getAdapter(IRewriteTarget.class) : null;
234 target.beginCompoundChange();
236 CompilationUnit astRoot= dialog.getCompilationUnit();
237 final ITypeBinding typeBinding= ASTNodes.getTypeBinding(astRoot, type);
238 int insertPos= dialog.getInsertOffset();
240 AddUnimplementedMethodsOperation operation= (AddUnimplementedMethodsOperation) createRunnable(astRoot, typeBinding, methodToOverride, insertPos, dialog.getGenerateComment());
241 IRunnableContext context= JavaPlugin.getActiveWorkbenchWindow();
243 context= new BusyIndicatorRunnableContext();
244 PlatformUI.getWorkbench().getProgressService().runInUI(context, new WorkbenchRunnableAdapter(operation, operation.getSchedulingRule()), operation.getSchedulingRule());
245 final String[] created= operation.getCreatedMethods();
246 if (created == null || created.length == 0)
247 MessageDialog.openInformation(shell, getDialogTitle(), ActionMessages.OverrideMethodsAction_error_nothing_found);
248 } catch (InvocationTargetException exception) {
249 ExceptionHandler.handle(exception, shell, getDialogTitle(), null);
250 } catch (InterruptedException exception) {
251 // Do nothing. Operation has been canceled by user.
254 target.endCompoundChange();
260 * Returns a runnable that creates the method stubs for overridden methods.
262 * @param astRoot the AST of the compilation unit to work on. The AST must have been created from a {@link ICompilationUnit}, that
263 * means {@link ASTParser#setSource(ICompilationUnit)} was used.
264 * @param type the binding of the type to add the new methods to. The type binding must correspond to a type declaration in the AST.
265 * @param methodToOverride the bindings of methods to override or <code>null</code> to implement all unimplemented, abstract methods from super types.
266 * @param insertPos a hint for a location in the source where to insert the new methods or <code>-1</code> to use the default behavior.
267 * @param createComments if set, comments will be added to the new methods.
268 * @return returns a runnable that creates the methods stubs.
269 * @throws IllegalArgumentException a {@link IllegalArgumentException} is thrown if the AST passed has not been created from a {@link ICompilationUnit}.
273 public static IWorkspaceRunnable createRunnable(CompilationUnit astRoot, ITypeBinding type, IMethodBinding[] methodToOverride, int insertPos, boolean createComments) {
274 AddUnimplementedMethodsOperation operation= new AddUnimplementedMethodsOperation(astRoot, type, methodToOverride, insertPos, true, true, false);
275 operation.setCreateComments(createComments);
280 * @see org.eclipse.jdt.ui.actions.SelectionDispatchAction#selectionChanged(org.eclipse.jface.viewers.IStructuredSelection)
283 public void selectionChanged(IStructuredSelection selection) {
285 setEnabled(canEnable(selection));
286 } catch (JavaModelException exception) {
287 if (JavaModelUtil.isExceptionToBeLogged(exception))
288 JavaPlugin.log(exception);
294 * @see org.eclipse.jdt.ui.actions.SelectionDispatchAction#run(org.eclipse.jface.text.ITextSelection)
297 public void selectionChanged(ITextSelection selection) {