KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > ui > actions > ModifyParametersAction


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 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.IJavaElement;
22 import org.eclipse.jdt.core.IMethod;
23 import org.eclipse.jdt.core.JavaModelException;
24
25 import org.eclipse.jdt.internal.corext.refactoring.RefactoringAvailabilityTester;
26 import org.eclipse.jdt.internal.corext.refactoring.RefactoringExecutionStarter;
27 import org.eclipse.jdt.internal.corext.util.JavaModelUtil;
28
29 import org.eclipse.jdt.internal.ui.IJavaHelpContextIds;
30 import org.eclipse.jdt.internal.ui.JavaPlugin;
31 import org.eclipse.jdt.internal.ui.actions.ActionUtil;
32 import org.eclipse.jdt.internal.ui.actions.SelectionConverter;
33 import org.eclipse.jdt.internal.ui.javaeditor.JavaEditor;
34 import org.eclipse.jdt.internal.ui.javaeditor.JavaTextSelection;
35 import org.eclipse.jdt.internal.ui.refactoring.RefactoringMessages;
36 import org.eclipse.jdt.internal.ui.util.ExceptionHandler;
37
38 /**
39  * Action to start the modify parameters refactoring. The refactoring supports
40  * swapping and renaming of arguments.
41  * <p>
42  * This action is applicable to selections containing a method with one or
43  * more arguments.
44  *
45  * <p>
46  * This class may be instantiated; it is not intended to be subclassed.
47  * </p>
48  *
49  * @since 2.0
50  */

51 public class ModifyParametersAction extends SelectionDispatchAction {
52     
53     private JavaEditor fEditor;
54     
55     /**
56      * Note: This constructor is for internal use only. Clients should not call this constructor.
57      * @param editor the java editor
58      */

59     public ModifyParametersAction(JavaEditor editor) {
60         this(editor.getEditorSite());
61         fEditor= editor;
62         setEnabled(SelectionConverter.canOperateOn(fEditor));
63     }
64
65     /**
66      * Creates a new <code>ModifyParametersAction</code>. The action requires
67      * that the selection provided by the site's selection provider is of type <code>
68      * org.eclipse.jface.viewers.IStructuredSelection</code>.
69      *
70      * @param site the site providing context information for this action
71      */

72     public ModifyParametersAction(IWorkbenchSite site) {
73         super(site);
74         setText(RefactoringMessages.RefactoringGroup_modify_Parameters_label);
75         PlatformUI.getWorkbench().getHelpSystem().setHelp(this, IJavaHelpContextIds.MODIFY_PARAMETERS_ACTION);
76     }
77     
78     /*
79      * @see SelectionDispatchAction#selectionChanged(IStructuredSelection)
80      */

81     public void selectionChanged(IStructuredSelection selection) {
82         try {
83             setEnabled(RefactoringAvailabilityTester.isChangeSignatureAvailable(selection));
84         } catch (JavaModelException e) {
85             // http://bugs.eclipse.org/bugs/show_bug.cgi?id=19253
86
if (JavaModelUtil.isExceptionToBeLogged(e))
87                 JavaPlugin.log(e);
88             setEnabled(false);//no UI here - happens on selection changes
89
}
90     }
91
92     /*
93      * @see SelectionDispatchAction#selectionChanged(ITextSelection)
94      */

95     public void selectionChanged(ITextSelection selection) {
96         setEnabled(true);
97     }
98
99     /**
100      * Note: This method is for internal use only. Clients should not call this method.
101      */

102     public void selectionChanged(JavaTextSelection selection) {
103         try {
104             setEnabled(RefactoringAvailabilityTester.isChangeSignatureAvailable(selection));
105         } catch (JavaModelException e) {
106             setEnabled(false);
107         }
108     }
109
110     /*
111      * @see SelectionDispatchAction#run(IStructuredSelection)
112      */

113     public void run(IStructuredSelection selection) {
114         try {
115             // we have to call this here - no selection changed event is sent after a refactoring but it may still invalidate enablement
116
if (RefactoringAvailabilityTester.isChangeSignatureAvailable(selection)) {
117                 IMethod method= getSingleSelectedMethod(selection);
118                 if (! ActionUtil.isEditable(getShell(), method))
119                     return;
120                 RefactoringExecutionStarter.startChangeSignatureRefactoring(method, this, getShell());
121             }
122         } catch (JavaModelException e) {
123             ExceptionHandler.handle(e, RefactoringMessages.OpenRefactoringWizardAction_refactoring, RefactoringMessages.OpenRefactoringWizardAction_exception);
124         }
125     }
126
127     /*
128      * @see SelectionDispatchAction#run(ITextSelection)
129      */

130     public void run(ITextSelection selection) {
131         try {
132             if (! ActionUtil.isEditable(fEditor))
133                 return;
134             IMethod method= getSingleSelectedMethod(selection);
135             if (RefactoringAvailabilityTester.isChangeSignatureAvailable(method)){
136                 RefactoringExecutionStarter.startChangeSignatureRefactoring(method, this, getShell());
137             } else {
138                 MessageDialog.openInformation(getShell(), RefactoringMessages.OpenRefactoringWizardAction_unavailable, RefactoringMessages.ModifyParametersAction_unavailable);
139             }
140         } catch (JavaModelException e) {
141             ExceptionHandler.handle(e, RefactoringMessages.OpenRefactoringWizardAction_refactoring, RefactoringMessages.OpenRefactoringWizardAction_exception);
142         }
143     }
144
145     private static IMethod getSingleSelectedMethod(IStructuredSelection selection){
146         if (selection.isEmpty() || selection.size() != 1)
147             return null;
148         if (selection.getFirstElement() instanceof IMethod)
149             return (IMethod)selection.getFirstElement();
150         return null;
151     }
152
153     private IMethod getSingleSelectedMethod(ITextSelection selection) throws JavaModelException{
154         //- when caret/selection on method name (call or declaration) -> that method
155
//- otherwise: caret position's enclosing method declaration
156
// - when caret inside argument list of method declaration -> enclosing method declaration
157
// - when caret inside argument list of method call -> enclosing method declaration (and NOT method call)
158
IJavaElement[] elements= SelectionConverter.codeResolve(fEditor);
159         if (elements.length > 1)
160             return null;
161         if (elements.length == 1 && elements[0] instanceof IMethod)
162             return (IMethod)elements[0];
163         IJavaElement elementAt= SelectionConverter.getInputAsCompilationUnit(fEditor).getElementAt(selection.getOffset());
164         if (elementAt instanceof IMethod)
165             return (IMethod)elementAt;
166         return null;
167     }
168 }
169
Popular Tags