]> git.uio.no Git - ifi-stolz-refaktor.git/blob - case-study/jdt-after/ui/org/eclipse/jdt/ui/actions/ModifyParametersAction.java
Case Study: adding data and statistics
[ifi-stolz-refaktor.git] / case-study / jdt-after / ui / org / eclipse / jdt / ui / actions / ModifyParametersAction.java
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
7  *
8  * Contributors:
9  *     IBM Corporation - initial API and implementation
10  *******************************************************************************/
11 package org.eclipse.jdt.ui.actions;
12
13 import org.eclipse.jface.dialogs.MessageDialog;
14 import org.eclipse.jface.viewers.IStructuredSelection;
15
16 import org.eclipse.jface.text.ITextSelection;
17
18 import org.eclipse.ui.IWorkbenchSite;
19 import org.eclipse.ui.PlatformUI;
20
21 import org.eclipse.jdt.core.IMethod;
22 import org.eclipse.jdt.core.JavaModelException;
23
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;
27
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;
36
37 /**
38  * Action to start the modify parameters refactoring. The refactoring supports
39  * swapping and renaming of arguments.
40  * <p>
41  * This action is applicable to selections containing a method with one or
42  * more arguments.
43  *
44  * <p>
45  * This class may be instantiated; it is not intended to be subclassed.
46  * </p>
47  *
48  * @since 2.0
49  *
50  * @noextend This class is not intended to be subclassed by clients.
51  */
52 public class ModifyParametersAction extends SelectionDispatchAction {
53
54         public JavaEditor fEditor;
55
56         /**
57          * Note: This constructor is for internal use only. Clients should not call this constructor.
58          * @param editor the java editor
59          *
60          * @noreference This constructor is not intended to be referenced by clients.
61          */
62         public ModifyParametersAction(JavaEditor editor) {
63                 this(editor.getEditorSite());
64                 fEditor= editor;
65                 setEnabled(SelectionConverter.canOperateOn(fEditor));
66         }
67
68         /**
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>.
72          *
73          * @param site the site providing context information for this action
74          */
75         public ModifyParametersAction(IWorkbenchSite site) {
76                 super(site);
77                 setText(RefactoringMessages.RefactoringGroup_modify_Parameters_label);
78                 PlatformUI.getWorkbench().getHelpSystem().setHelp(this, IJavaHelpContextIds.MODIFY_PARAMETERS_ACTION);
79         }
80
81         /*
82          * @see SelectionDispatchAction#selectionChanged(IStructuredSelection)
83          */
84         @Override
85         public void selectionChanged(IStructuredSelection selection) {
86                 try {
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))
91                                 JavaPlugin.log(e);
92                         setEnabled(false);//no UI here - happens on selection changes
93                 }
94         }
95
96     /*
97      * @see SelectionDispatchAction#selectionChanged(ITextSelection)
98      */
99         @Override
100         public void selectionChanged(ITextSelection selection) {
101                 setEnabled(true);
102         }
103
104         /**
105          * Note: This method is for internal use only. Clients should not call this method.
106          * 
107          * @param selection the Java text selection
108          * @noreference This method is not intended to be referenced by clients.
109          */
110         @Override
111         public void selectionChanged(JavaTextSelection selection) {
112                 try {
113                         setEnabled(RefactoringAvailabilityTester.isChangeSignatureAvailable(selection));
114                 } catch (JavaModelException e) {
115                         setEnabled(false);
116                 }
117         }
118
119         /*
120          * @see SelectionDispatchAction#run(IStructuredSelection)
121          */
122         @Override
123         public void run(IStructuredSelection selection) {
124                 try {
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))
129                                         return;
130                                 RefactoringExecutionStarter.startChangeSignatureRefactoring(method, this, getShell());
131                         }
132                 } catch (JavaModelException e) {
133                         ExceptionHandler.handle(e, RefactoringMessages.OpenRefactoringWizardAction_refactoring, RefactoringMessages.OpenRefactoringWizardAction_exception);
134                 }
135         }
136
137     /*
138      * @see SelectionDispatchAction#run(ITextSelection)
139      */
140         @Override
141         public void run(ITextSelection selection) {
142                 try {
143                         if (! ActionUtil.isEditable(fEditor))
144                                 return;
145                         IMethod method= getSingleSelectedMethod(selection);
146                         if (RefactoringAvailabilityTester.isChangeSignatureAvailable(method)){
147                                 RefactoringExecutionStarter.startChangeSignatureRefactoring(method, this, getShell());
148                         } else {
149                                 MessageDialog.openInformation(getShell(), RefactoringMessages.OpenRefactoringWizardAction_unavailable, RefactoringMessages.ModifyParametersAction_unavailable);
150                         }
151                 } catch (JavaModelException e) {
152                         ExceptionHandler.handle(e, RefactoringMessages.OpenRefactoringWizardAction_refactoring, RefactoringMessages.OpenRefactoringWizardAction_exception);
153                 }
154         }
155
156         private static IMethod getSingleSelectedMethod(IStructuredSelection selection){
157                 if (selection.isEmpty() || selection.size() != 1)
158                         return null;
159                 if (selection.getFirstElement() instanceof IMethod)
160                         return (IMethod)selection.getFirstElement();
161                 return null;
162         }
163
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);
170         }
171 }