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 org.eclipse.jface.dialogs.MessageDialog;
14 import org.eclipse.jface.viewers.IStructuredSelection;
16 import org.eclipse.jface.text.ITextSelection;
18 import org.eclipse.ui.IWorkbenchSite;
19 import org.eclipse.ui.PlatformUI;
21 import org.eclipse.jdt.core.IMethod;
22 import org.eclipse.jdt.core.JavaModelException;
24 import org.eclipse.jdt.internal.corext.refactoring.RefactoringAvailabilityTester;
25 import org.eclipse.jdt.internal.corext.refactoring.RefactoringExecutionStarter;
26 import org.eclipse.jdt.internal.corext.util.JavaModelUtil;
28 import org.eclipse.jdt.internal.ui.IJavaHelpContextIds;
29 import org.eclipse.jdt.internal.ui.JavaPlugin;
30 import org.eclipse.jdt.internal.ui.actions.ActionUtil;
31 import org.eclipse.jdt.internal.ui.actions.SelectionConverter;
32 import org.eclipse.jdt.internal.ui.javaeditor.JavaEditor;
33 import org.eclipse.jdt.internal.ui.javaeditor.JavaTextSelection;
34 import org.eclipse.jdt.internal.ui.refactoring.RefactoringMessages;
35 import org.eclipse.jdt.internal.ui.util.ExceptionHandler;
38 * Action to start the modify parameters refactoring. The refactoring supports
39 * swapping and renaming of arguments.
41 * This action is applicable to selections containing a method with one or
45 * This class may be instantiated; it is not intended to be subclassed.
50 * @noextend This class is not intended to be subclassed by clients.
52 public class ModifyParametersAction extends SelectionDispatchAction {
54 public JavaEditor fEditor;
57 * Note: This constructor is for internal use only. Clients should not call this constructor.
58 * @param editor the java editor
60 * @noreference This constructor is not intended to be referenced by clients.
62 public ModifyParametersAction(JavaEditor editor) {
63 this(editor.getEditorSite());
65 setEnabled(SelectionConverter.canOperateOn(fEditor));
69 * Creates a new <code>ModifyParametersAction</code>. The action requires
70 * that the selection provided by the site's selection provider is of type <code>
71 * org.eclipse.jface.viewers.IStructuredSelection</code>.
73 * @param site the site providing context information for this action
75 public ModifyParametersAction(IWorkbenchSite site) {
77 setText(RefactoringMessages.RefactoringGroup_modify_Parameters_label);
78 PlatformUI.getWorkbench().getHelpSystem().setHelp(this, IJavaHelpContextIds.MODIFY_PARAMETERS_ACTION);
82 * @see SelectionDispatchAction#selectionChanged(IStructuredSelection)
85 public void selectionChanged(IStructuredSelection selection) {
87 setEnabled(RefactoringAvailabilityTester.isChangeSignatureAvailable(selection));
88 } catch (JavaModelException e) {
89 // http://bugs.eclipse.org/bugs/show_bug.cgi?id=19253
90 if (JavaModelUtil.isExceptionToBeLogged(e))
92 setEnabled(false);//no UI here - happens on selection changes
97 * @see SelectionDispatchAction#selectionChanged(ITextSelection)
100 public void selectionChanged(ITextSelection selection) {
105 * Note: This method is for internal use only. Clients should not call this method.
107 * @param selection the Java text selection
108 * @noreference This method is not intended to be referenced by clients.
111 public void selectionChanged(JavaTextSelection selection) {
113 setEnabled(RefactoringAvailabilityTester.isChangeSignatureAvailable(selection));
114 } catch (JavaModelException e) {
120 * @see SelectionDispatchAction#run(IStructuredSelection)
123 public void run(IStructuredSelection selection) {
125 // we have to call this here - no selection changed event is sent after a refactoring but it may still invalidate enablement
126 if (RefactoringAvailabilityTester.isChangeSignatureAvailable(selection)) {
127 IMethod method= getSingleSelectedMethod(selection);
128 if (! ActionUtil.isEditable(getShell(), method))
130 RefactoringExecutionStarter.startChangeSignatureRefactoring(method, this, getShell());
132 } catch (JavaModelException e) {
133 ExceptionHandler.handle(e, RefactoringMessages.OpenRefactoringWizardAction_refactoring, RefactoringMessages.OpenRefactoringWizardAction_exception);
138 * @see SelectionDispatchAction#run(ITextSelection)
141 public void run(ITextSelection selection) {
143 if (! ActionUtil.isEditable(fEditor))
145 IMethod method= getSingleSelectedMethod(selection);
146 if (RefactoringAvailabilityTester.isChangeSignatureAvailable(method)){
147 RefactoringExecutionStarter.startChangeSignatureRefactoring(method, this, getShell());
149 MessageDialog.openInformation(getShell(), RefactoringMessages.OpenRefactoringWizardAction_unavailable, RefactoringMessages.ModifyParametersAction_unavailable);
151 } catch (JavaModelException e) {
152 ExceptionHandler.handle(e, RefactoringMessages.OpenRefactoringWizardAction_refactoring, RefactoringMessages.OpenRefactoringWizardAction_exception);
156 private static IMethod getSingleSelectedMethod(IStructuredSelection selection){
157 if (selection.isEmpty() || selection.size() != 1)
159 if (selection.getFirstElement() instanceof IMethod)
160 return (IMethod)selection.getFirstElement();
164 private IMethod getSingleSelectedMethod(ITextSelection selection) throws JavaModelException{
165 //- when caret/selection on method name (call or declaration) -> that method
166 //- otherwise: caret position's enclosing method declaration
167 // - when caret inside argument list of method declaration -> enclosing method declaration
168 // - when caret inside argument list of method call -> enclosing method declaration (and NOT method call)
169 return fEditor.generated_3892101521127816329(selection);