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.internal.ui.refactoring.actions;
13 import org.eclipse.core.runtime.Assert;
14 import org.eclipse.core.runtime.CoreException;
16 import org.eclipse.jface.dialogs.MessageDialog;
17 import org.eclipse.jface.viewers.IStructuredSelection;
19 import org.eclipse.jface.text.ITextSelection;
21 import org.eclipse.ui.IWorkbenchSite;
22 import org.eclipse.ui.PlatformUI;
24 import org.eclipse.jdt.core.ICompilationUnit;
25 import org.eclipse.jdt.core.IJavaElement;
26 import org.eclipse.jdt.core.IMethod;
27 import org.eclipse.jdt.core.JavaModelException;
29 import org.eclipse.jdt.internal.corext.refactoring.RefactoringAvailabilityTester;
30 import org.eclipse.jdt.internal.corext.refactoring.RefactoringExecutionStarter;
31 import org.eclipse.jdt.internal.corext.util.JavaModelUtil;
33 import org.eclipse.jdt.ui.actions.SelectionDispatchAction;
35 import org.eclipse.jdt.internal.ui.IJavaHelpContextIds;
36 import org.eclipse.jdt.internal.ui.JavaPlugin;
37 import org.eclipse.jdt.internal.ui.actions.ActionUtil;
38 import org.eclipse.jdt.internal.ui.actions.SelectionConverter;
39 import org.eclipse.jdt.internal.ui.javaeditor.JavaEditor;
40 import org.eclipse.jdt.internal.ui.javaeditor.JavaTextSelection;
41 import org.eclipse.jdt.internal.ui.refactoring.RefactoringMessages;
42 import org.eclipse.jdt.internal.ui.util.ExceptionHandler;
44 public final class MoveInstanceMethodAction extends SelectionDispatchAction {
46 private JavaEditor fEditor;
49 * Note: This constructor is for internal use only. Clients should not call this constructor.
50 * @param editor the java editor
52 public MoveInstanceMethodAction(JavaEditor editor) {
53 this(editor.getEditorSite());
55 setEnabled(SelectionConverter.canOperateOn(fEditor));
58 public MoveInstanceMethodAction(IWorkbenchSite site) {
60 setText(RefactoringMessages.MoveInstanceMethodAction_Move_Method);
61 PlatformUI.getWorkbench().getHelpSystem().setHelp(this, IJavaHelpContextIds.MOVE_ACTION);
65 * @see SelectionDispatchAction#selectionChanged(IStructuredSelection)
68 public void selectionChanged(IStructuredSelection selection) {
70 setEnabled(RefactoringAvailabilityTester.isMoveMethodAvailable(selection));
71 } catch (JavaModelException e) {
72 // http://bugs.eclipse.org/bugs/show_bug.cgi?id=19253
73 if (JavaModelUtil.isExceptionToBeLogged(e))
75 setEnabled(false);//no ui
80 public void selectionChanged(ITextSelection selection) {
85 * Note: This method is for internal use only. Clients should not call this method.
88 public void selectionChanged(JavaTextSelection selection) {
90 setEnabled(RefactoringAvailabilityTester.isMoveMethodAvailable(selection));
91 } catch (CoreException e) {
96 private static IMethod getSingleSelectedMethod(IStructuredSelection selection) {
97 if (selection.isEmpty() || selection.size() != 1)
100 Object first= selection.getFirstElement();
101 if (! (first instanceof IMethod))
103 return (IMethod) first;
106 * @see SelectionDispatchAction#run(IStructuredSelection)
109 public void run(IStructuredSelection selection) {
111 Assert.isTrue(RefactoringAvailabilityTester.isMoveMethodAvailable(selection));
112 IMethod method= getSingleSelectedMethod(selection);
113 Assert.isNotNull(method);
114 if (!ActionUtil.isEditable(fEditor, getShell(), method))
116 RefactoringExecutionStarter.startMoveMethodRefactoring(method, getShell());
117 } catch (JavaModelException e) {
118 ExceptionHandler.handle(e, getShell(), RefactoringMessages.MoveInstanceMethodAction_dialog_title, RefactoringMessages.MoveInstanceMethodAction_unexpected_exception);
123 * Method declared on SelectionDispatchAction
126 public void run(ITextSelection selection) {
128 run(selection, SelectionConverter.getInputAsCompilationUnit(fEditor));
129 } catch (JavaModelException e) {
130 ExceptionHandler.handle(e, getShell(), RefactoringMessages.MoveInstanceMethodAction_dialog_title, RefactoringMessages.MoveInstanceMethodAction_unexpected_exception);
134 private void run(ITextSelection selection, ICompilationUnit cu) throws JavaModelException {
135 Assert.isNotNull(cu);
136 Assert.isTrue(selection.getOffset() >= 0);
137 Assert.isTrue(selection.getLength() >= 0);
139 if (!ActionUtil.isEditable(fEditor, getShell(), cu))
142 IMethod method= getMethod(cu, selection);
143 if (method != null) {
144 RefactoringExecutionStarter.startMoveMethodRefactoring(method, getShell());
146 MessageDialog.openInformation(getShell(), RefactoringMessages.MoveInstanceMethodAction_dialog_title, RefactoringMessages.MoveInstanceMethodAction_No_reference_or_declaration);
150 private static IMethod getMethod(ICompilationUnit cu, ITextSelection selection) throws JavaModelException {
151 IJavaElement element= SelectionConverter.getElementAtOffset(cu, selection);
152 if (element instanceof IMethod)
153 return (IMethod) element;