]> git.uio.no Git - ifi-stolz-refaktor.git/blob - case-study/jdt-after/ui refactoring/org/eclipse/jdt/internal/ui/refactoring/actions/MoveInstanceMethodAction.java
Case Study: adding data and statistics
[ifi-stolz-refaktor.git] / case-study / jdt-after / ui refactoring / org / eclipse / jdt / internal / ui / refactoring / actions / MoveInstanceMethodAction.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.internal.ui.refactoring.actions;
12
13 import org.eclipse.core.runtime.Assert;
14 import org.eclipse.core.runtime.CoreException;
15
16 import org.eclipse.jface.dialogs.MessageDialog;
17 import org.eclipse.jface.viewers.IStructuredSelection;
18
19 import org.eclipse.jface.text.ITextSelection;
20
21 import org.eclipse.ui.IWorkbenchSite;
22 import org.eclipse.ui.PlatformUI;
23
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;
28
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;
32
33 import org.eclipse.jdt.ui.actions.SelectionDispatchAction;
34
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;
43
44 public final class MoveInstanceMethodAction extends SelectionDispatchAction {
45
46         private JavaEditor fEditor;
47
48         /**
49          * Note: This constructor is for internal use only. Clients should not call this constructor.
50          * @param editor the java editor
51          */
52         public MoveInstanceMethodAction(JavaEditor editor) {
53                 this(editor.getEditorSite());
54                 fEditor= editor;
55                 setEnabled(SelectionConverter.canOperateOn(fEditor));
56         }
57
58         public MoveInstanceMethodAction(IWorkbenchSite site) {
59                 super(site);
60                 setText(RefactoringMessages.MoveInstanceMethodAction_Move_Method);
61                 PlatformUI.getWorkbench().getHelpSystem().setHelp(this, IJavaHelpContextIds.MOVE_ACTION);
62         }
63
64         /*
65          * @see SelectionDispatchAction#selectionChanged(IStructuredSelection)
66          */
67         @Override
68         public void selectionChanged(IStructuredSelection selection) {
69                 try {
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))
74                                 JavaPlugin.log(e);
75                         setEnabled(false);//no ui
76                 }
77         }
78
79         @Override
80         public void selectionChanged(ITextSelection selection) {
81                 setEnabled(true);
82     }
83
84         /**
85          * Note: This method is for internal use only. Clients should not call this method.
86          */
87         @Override
88         public void selectionChanged(JavaTextSelection selection) {
89                 try {
90                         setEnabled(RefactoringAvailabilityTester.isMoveMethodAvailable(selection));
91                 } catch (CoreException e) {
92                         setEnabled(false);
93                 }
94         }
95
96         private static IMethod getSingleSelectedMethod(IStructuredSelection selection) {
97                 if (selection.isEmpty() || selection.size() != 1)
98                         return null;
99
100                 Object first= selection.getFirstElement();
101                 if (! (first instanceof IMethod))
102                         return null;
103                 return (IMethod) first;
104         }
105         /*
106          * @see SelectionDispatchAction#run(IStructuredSelection)
107          */
108         @Override
109         public void run(IStructuredSelection selection) {
110                 try {
111                         Assert.isTrue(RefactoringAvailabilityTester.isMoveMethodAvailable(selection));
112                         IMethod method= getSingleSelectedMethod(selection);
113                         Assert.isNotNull(method);
114                         if (!ActionUtil.isEditable(fEditor, getShell(), method))
115                                 return;
116                         RefactoringExecutionStarter.startMoveMethodRefactoring(method, getShell());
117                 } catch (JavaModelException e) {
118                         ExceptionHandler.handle(e, getShell(), RefactoringMessages.MoveInstanceMethodAction_dialog_title, RefactoringMessages.MoveInstanceMethodAction_unexpected_exception);
119                 }
120         }
121
122         /* (non-Javadoc)
123          * Method declared on SelectionDispatchAction
124          */
125         @Override
126         public void run(ITextSelection selection) {
127                 try {
128                         run(selection, SelectionConverter.getInputAsCompilationUnit(fEditor));
129                 } catch (JavaModelException e) {
130                         ExceptionHandler.handle(e, getShell(), RefactoringMessages.MoveInstanceMethodAction_dialog_title, RefactoringMessages.MoveInstanceMethodAction_unexpected_exception);
131                 }
132         }
133
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);
138
139                 if (!ActionUtil.isEditable(fEditor, getShell(), cu))
140                         return;
141
142                 IMethod method= getMethod(cu, selection);
143                 if (method != null) {
144                         RefactoringExecutionStarter.startMoveMethodRefactoring(method, getShell());
145                 } else {
146                         MessageDialog.openInformation(getShell(), RefactoringMessages.MoveInstanceMethodAction_dialog_title, RefactoringMessages.MoveInstanceMethodAction_No_reference_or_declaration);
147                 }
148         }
149
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;
154                 return null;
155         }
156 }