1 /*******************************************************************************
2 * Copyright (c) 2007, 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.internal.ui.actions;
13 import org.eclipse.core.runtime.CoreException;
15 import org.eclipse.jface.dialogs.MessageDialog;
16 import org.eclipse.jface.viewers.IStructuredSelection;
18 import org.eclipse.jface.text.ITextSelection;
20 import org.eclipse.ui.IWorkbenchSite;
22 import org.eclipse.jdt.core.IJavaElement;
23 import org.eclipse.jdt.core.IMethod;
24 import org.eclipse.jdt.core.JavaModelException;
26 import org.eclipse.jdt.internal.corext.refactoring.RefactoringAvailabilityTester;
27 import org.eclipse.jdt.internal.corext.refactoring.RefactoringExecutionStarter;
28 import org.eclipse.jdt.internal.corext.util.JavaModelUtil;
30 import org.eclipse.jdt.ui.actions.SelectionDispatchAction;
32 import org.eclipse.jdt.internal.ui.JavaPlugin;
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.util.ExceptionHandler;
37 public class IntroduceParameterObjectAction extends SelectionDispatchAction {
39 private JavaEditor fEditor;
42 * Note: This constructor is for internal use only. Clients should not call this constructor.
43 * @param editor the compilation unit editor
45 public IntroduceParameterObjectAction(JavaEditor editor) {
46 this(editor.getEditorSite());
52 * Creates a new <code>IntroduceIndirectionAction</code>.
54 * @param site the site providing context information for this action
56 public IntroduceParameterObjectAction(IWorkbenchSite site) {
58 setText(ActionMessages.IntroduceParameterObjectAction_action_text);
59 setToolTipText(ActionMessages.IntroduceParameterObjectAction_action_tooltip);
60 setDescription(ActionMessages.IntroduceParameterObjectAction_action_description);
63 //---- structured selection --------------------------------------------------
66 * @see SelectionDispatchAction#selectionChanged(IStructuredSelection)
69 public void selectionChanged(IStructuredSelection selection) {
71 setEnabled(RefactoringAvailabilityTester.isIntroduceParameterObjectAvailable(selection));
72 } catch (JavaModelException e) {
73 if (JavaModelUtil.isExceptionToBeLogged(e))
79 * @see SelectionDispatchAction#selectionChanged(ITextSelection)
82 public void selectionChanged(ITextSelection selection) {
87 * @see org.eclipse.jdt.ui.actions.SelectionDispatchAction#selectionChanged(org.eclipse.jdt.internal.ui.javaeditor.JavaTextSelection)
90 public void selectionChanged(JavaTextSelection selection) {
92 setEnabled(RefactoringAvailabilityTester.isIntroduceParameterObjectAvailable(selection));
93 } catch (JavaModelException e) {
94 if (JavaModelUtil.isExceptionToBeLogged(e))
101 * @see SelectionDispatchAction#run(IStructuredSelection)
104 public void run(IStructuredSelection selection) {
106 IMethod singleSelectedMethod= getSingleSelectedMethod(selection);
107 if (!ActionUtil.isEditable(getShell(), singleSelectedMethod))
109 run(singleSelectedMethod);
110 } catch (CoreException e) {
111 ExceptionHandler.handle(e, getShell(), ActionMessages.IntroduceParameterObjectAction_exceptiondialog_title, ActionMessages.IntroduceParameterObjectAction_unexpected_exception);
116 * (non-Javadoc) Method declared on SelectionDispatchAction
119 public void run(ITextSelection selection) {
121 if (!ActionUtil.isEditable(fEditor))
123 run(getSingleSelectedMethod(selection));
124 } catch (CoreException e) {
125 ExceptionHandler.handle(e, getShell(), ActionMessages.IntroduceParameterObjectAction_exceptiondialog_title, ActionMessages.IntroduceParameterObjectAction_unexpected_exception);
130 public void run(JavaTextSelection selection) {
132 IJavaElement[] elements= selection.resolveElementAtOffset();
133 if (elements.length != 1)
136 if (!(elements[0] instanceof IMethod))
139 run((IMethod) elements[0]);
140 } catch (CoreException e) {
141 ExceptionHandler.handle(e, getShell(), ActionMessages.IntroduceParameterObjectAction_exceptiondialog_title, ActionMessages.IntroduceParameterObjectAction_unexpected_exception);
145 private void run(IMethod method) throws CoreException {
146 if (method == null) {
147 MessageDialog.openError(getShell(), ActionMessages.IntroduceParameterObjectAction_exceptiondialog_title, ActionMessages.IntroduceParameterObjectAction_can_not_run_refactoring_message);
148 } else if (fEditor == null || ActionUtil.isEditable(fEditor)) {
149 RefactoringExecutionStarter.startIntroduceParameterObject(method, getShell());
153 private static IMethod getSingleSelectedMethod(IStructuredSelection selection) {
154 if (selection.size() != 1)
157 Object element= selection.getFirstElement();
158 if (!(element instanceof IMethod))
161 return (IMethod)element;
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 IJavaElement[] elements= SelectionConverter.codeResolve(fEditor);
170 if (elements.length > 1)
173 if (elements.length == 1 && elements[0] instanceof IMethod) {
174 return (IMethod) elements[0];
176 IJavaElement elementAt= SelectionConverter.getInputAsCompilationUnit(fEditor).getElementAt(selection.getOffset());
177 if (!(elementAt instanceof IMethod))
180 return (IMethod) elementAt;