]> git.uio.no Git - ifi-stolz-refaktor.git/blob - case-study/jdt-before/ui/org/eclipse/jdt/internal/ui/actions/IntroduceParameterObjectAction.java
Case Study: adding data and statistics
[ifi-stolz-refaktor.git] / case-study / jdt-before / ui / org / eclipse / jdt / internal / ui / actions / IntroduceParameterObjectAction.java
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
7  *
8  * Contributors:
9  *     IBM Corporation - initial API and implementation
10  *******************************************************************************/
11 package org.eclipse.jdt.internal.ui.actions;
12
13 import org.eclipse.core.runtime.CoreException;
14
15 import org.eclipse.jface.dialogs.MessageDialog;
16 import org.eclipse.jface.viewers.IStructuredSelection;
17
18 import org.eclipse.jface.text.ITextSelection;
19
20 import org.eclipse.ui.IWorkbenchSite;
21
22 import org.eclipse.jdt.core.IJavaElement;
23 import org.eclipse.jdt.core.IMethod;
24 import org.eclipse.jdt.core.JavaModelException;
25
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;
29
30 import org.eclipse.jdt.ui.actions.SelectionDispatchAction;
31
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;
36
37 public class IntroduceParameterObjectAction extends SelectionDispatchAction {
38
39         private JavaEditor fEditor;
40
41         /**
42          * Note: This constructor is for internal use only. Clients should not call this constructor.
43          * @param editor the compilation unit editor
44          */
45         public IntroduceParameterObjectAction(JavaEditor editor) {
46                 this(editor.getEditorSite());
47                 fEditor= editor;
48                 setEnabled(true);
49         }
50
51         /**
52          * Creates a new <code>IntroduceIndirectionAction</code>.
53          *
54          * @param site the site providing context information for this action
55          */
56         public IntroduceParameterObjectAction(IWorkbenchSite site) {
57                 super(site);
58                 setText(ActionMessages.IntroduceParameterObjectAction_action_text);
59                 setToolTipText(ActionMessages.IntroduceParameterObjectAction_action_tooltip);
60                 setDescription(ActionMessages.IntroduceParameterObjectAction_action_description);
61         }
62
63         //---- structured selection --------------------------------------------------
64
65         /*
66          * @see SelectionDispatchAction#selectionChanged(IStructuredSelection)
67          */
68         @Override
69         public void selectionChanged(IStructuredSelection selection) {
70                 try {
71                         setEnabled(RefactoringAvailabilityTester.isIntroduceParameterObjectAvailable(selection));
72                 } catch (JavaModelException e) {
73                         if (JavaModelUtil.isExceptionToBeLogged(e))
74                                 JavaPlugin.log(e);
75                 }
76         }
77
78         /*
79          * @see SelectionDispatchAction#selectionChanged(ITextSelection)
80          */
81         @Override
82         public void selectionChanged(ITextSelection selection) {
83                 setEnabled(true);
84         }
85
86         /* (non-Javadoc)
87          * @see org.eclipse.jdt.ui.actions.SelectionDispatchAction#selectionChanged(org.eclipse.jdt.internal.ui.javaeditor.JavaTextSelection)
88          */
89         @Override
90         public void selectionChanged(JavaTextSelection selection) {
91                 try {
92                         setEnabled(RefactoringAvailabilityTester.isIntroduceParameterObjectAvailable(selection));
93                 } catch (JavaModelException e) {
94                         if (JavaModelUtil.isExceptionToBeLogged(e))
95                                 JavaPlugin.log(e);
96                         setEnabled(false);
97                 }
98         }
99
100         /*
101          * @see SelectionDispatchAction#run(IStructuredSelection)
102          */
103         @Override
104         public void run(IStructuredSelection selection) {
105                 try {
106                         IMethod singleSelectedMethod= getSingleSelectedMethod(selection);
107                         if (!ActionUtil.isEditable(getShell(), singleSelectedMethod))
108                                 return;
109                         run(singleSelectedMethod);
110                 } catch (CoreException e) {
111                         ExceptionHandler.handle(e, getShell(), ActionMessages.IntroduceParameterObjectAction_exceptiondialog_title,     ActionMessages.IntroduceParameterObjectAction_unexpected_exception);
112                 }
113         }
114
115         /*
116          * (non-Javadoc) Method declared on SelectionDispatchAction
117          */
118         @Override
119         public void run(ITextSelection selection) {
120                 try {
121                         if (!ActionUtil.isEditable(fEditor))
122                                 return;
123                         run(getSingleSelectedMethod(selection));
124                 } catch (CoreException e) {
125                         ExceptionHandler.handle(e, getShell(), ActionMessages.IntroduceParameterObjectAction_exceptiondialog_title,     ActionMessages.IntroduceParameterObjectAction_unexpected_exception);
126                 }
127         }
128
129         @Override
130         public void run(JavaTextSelection selection) {
131                 try {
132                         IJavaElement[] elements= selection.resolveElementAtOffset();
133                         if (elements.length != 1)
134                                 return;
135
136                         if (!(elements[0] instanceof IMethod))
137                                 return;
138
139                         run((IMethod) elements[0]);
140                 } catch (CoreException e) {
141                         ExceptionHandler.handle(e, getShell(), ActionMessages.IntroduceParameterObjectAction_exceptiondialog_title,     ActionMessages.IntroduceParameterObjectAction_unexpected_exception);
142                 }
143         }
144
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());
150                 }
151         }
152
153         private static IMethod getSingleSelectedMethod(IStructuredSelection selection) {
154                 if (selection.size() != 1)
155                         return null;
156
157                 Object element= selection.getFirstElement();
158                 if (!(element instanceof IMethod))
159                         return null;
160
161                 return (IMethod)element;
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                 IJavaElement[] elements= SelectionConverter.codeResolve(fEditor);
170                 if (elements.length > 1)
171                         return null;
172
173                 if (elements.length == 1 && elements[0] instanceof IMethod) {
174                         return (IMethod) elements[0];
175                 } else {
176                         IJavaElement elementAt= SelectionConverter.getInputAsCompilationUnit(fEditor).getElementAt(selection.getOffset());
177                         if (!(elementAt instanceof IMethod))
178                                 return null;
179
180                         return (IMethod) elementAt;
181                 }
182         }
183 }