]> git.uio.no Git - ifi-stolz-refaktor.git/blame - case-study/jdt-after/core refactoring/org/eclipse/jdt/internal/corext/refactoring/RefactoringExecutionStarter.java
Case Study: adding data and statistics
[ifi-stolz-refaktor.git] / case-study / jdt-after / core refactoring / org / eclipse / jdt / internal / corext / refactoring / RefactoringExecutionStarter.java
CommitLineData
1b2798f6
EK
1/*******************************************************************************
2 * Copyright (c) 2005, 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 *******************************************************************************/
11package org.eclipse.jdt.internal.corext.refactoring;
12
13import java.lang.reflect.InvocationTargetException;
14import java.util.Arrays;
15import java.util.HashSet;
16import java.util.Set;
17
18import org.eclipse.swt.widgets.Shell;
19
20import org.eclipse.core.runtime.CoreException;
21import org.eclipse.core.runtime.IStatus;
22import org.eclipse.core.runtime.NullProgressMonitor;
23
24import org.eclipse.core.resources.IResource;
25
26import org.eclipse.jface.dialogs.MessageDialog;
27import org.eclipse.jface.dialogs.ProgressMonitorDialog;
28import org.eclipse.jface.operation.IRunnableContext;
29import org.eclipse.jface.viewers.IStructuredSelection;
30import org.eclipse.jface.viewers.StructuredSelection;
31
32import org.eclipse.jface.text.ITextSelection;
33
34import org.eclipse.ui.PlatformUI;
35
36import org.eclipse.ltk.core.refactoring.Refactoring;
37import org.eclipse.ltk.core.refactoring.RefactoringCore;
38import org.eclipse.ltk.core.refactoring.RefactoringStatus;
39import org.eclipse.ltk.core.refactoring.RefactoringStatusEntry;
40import org.eclipse.ltk.core.refactoring.participants.CopyRefactoring;
41import org.eclipse.ltk.core.refactoring.participants.DeleteRefactoring;
42import org.eclipse.ltk.core.refactoring.participants.MoveRefactoring;
43import org.eclipse.ltk.core.refactoring.participants.ProcessorBasedRefactoring;
44import org.eclipse.ltk.ui.refactoring.RefactoringWizard;
45import org.eclipse.ltk.ui.refactoring.resource.RenameResourceWizard;
46
47import org.eclipse.jdt.core.IClassFile;
48import org.eclipse.jdt.core.ICompilationUnit;
49import org.eclipse.jdt.core.IField;
50import org.eclipse.jdt.core.IJavaElement;
51import org.eclipse.jdt.core.IJavaProject;
52import org.eclipse.jdt.core.ILocalVariable;
53import org.eclipse.jdt.core.IMember;
54import org.eclipse.jdt.core.IMethod;
55import org.eclipse.jdt.core.IPackageFragment;
56import org.eclipse.jdt.core.IPackageFragmentRoot;
57import org.eclipse.jdt.core.IType;
58import org.eclipse.jdt.core.ITypeParameter;
59import org.eclipse.jdt.core.ITypeRoot;
60import org.eclipse.jdt.core.JavaModelException;
61import org.eclipse.jdt.core.dom.CompilationUnit;
62import org.eclipse.jdt.core.refactoring.descriptors.ExtractClassDescriptor;
63import org.eclipse.jdt.core.refactoring.descriptors.IntroduceParameterObjectDescriptor;
64
65import org.eclipse.jdt.internal.core.refactoring.descriptors.RefactoringSignatureDescriptorFactory;
66import org.eclipse.jdt.internal.corext.fix.CleanUpRefactoring;
67import org.eclipse.jdt.internal.corext.refactoring.base.RefactoringStatusCodes;
68import org.eclipse.jdt.internal.corext.refactoring.code.ConvertAnonymousToNestedRefactoring;
69import org.eclipse.jdt.internal.corext.refactoring.code.InlineConstantRefactoring;
70import org.eclipse.jdt.internal.corext.refactoring.code.InlineMethodRefactoring;
71import org.eclipse.jdt.internal.corext.refactoring.code.InlineTempRefactoring;
72import org.eclipse.jdt.internal.corext.refactoring.code.IntroduceFactoryRefactoring;
73import org.eclipse.jdt.internal.corext.refactoring.code.IntroduceIndirectionRefactoring;
74import org.eclipse.jdt.internal.corext.refactoring.code.IntroduceParameterRefactoring;
75import org.eclipse.jdt.internal.corext.refactoring.code.ReplaceInvocationsRefactoring;
76import org.eclipse.jdt.internal.corext.refactoring.generics.InferTypeArgumentsRefactoring;
77import org.eclipse.jdt.internal.corext.refactoring.reorg.JavaCopyProcessor;
78import org.eclipse.jdt.internal.corext.refactoring.reorg.JavaDeleteProcessor;
79import org.eclipse.jdt.internal.corext.refactoring.reorg.JavaMoveProcessor;
80import org.eclipse.jdt.internal.corext.refactoring.reorg.ReorgPolicyFactory;
81import org.eclipse.jdt.internal.corext.refactoring.reorg.IReorgPolicy.ICopyPolicy;
82import org.eclipse.jdt.internal.corext.refactoring.reorg.IReorgPolicy.IMovePolicy;
83import org.eclipse.jdt.internal.corext.refactoring.sef.SelfEncapsulateFieldRefactoring;
84import org.eclipse.jdt.internal.corext.refactoring.structure.ChangeSignatureProcessor;
85import org.eclipse.jdt.internal.corext.refactoring.structure.ChangeTypeRefactoring;
86import org.eclipse.jdt.internal.corext.refactoring.structure.ExtractClassRefactoring;
87import org.eclipse.jdt.internal.corext.refactoring.structure.ExtractInterfaceProcessor;
88import org.eclipse.jdt.internal.corext.refactoring.structure.ExtractSupertypeProcessor;
89import org.eclipse.jdt.internal.corext.refactoring.structure.IntroduceParameterObjectProcessor;
90import org.eclipse.jdt.internal.corext.refactoring.structure.MoveInnerToTopRefactoring;
91import org.eclipse.jdt.internal.corext.refactoring.structure.MoveInstanceMethodProcessor;
92import org.eclipse.jdt.internal.corext.refactoring.structure.MoveStaticMembersProcessor;
93import org.eclipse.jdt.internal.corext.refactoring.structure.PullUpRefactoringProcessor;
94import org.eclipse.jdt.internal.corext.refactoring.structure.PushDownRefactoringProcessor;
95import org.eclipse.jdt.internal.corext.refactoring.structure.UseSuperTypeProcessor;
96import org.eclipse.jdt.internal.corext.util.Messages;
97
98import org.eclipse.jdt.ui.actions.SelectionDispatchAction;
99import org.eclipse.jdt.ui.cleanup.ICleanUp;
100import org.eclipse.jdt.ui.refactoring.RefactoringSaveHelper;
101import org.eclipse.jdt.ui.refactoring.RenameSupport;
102
103import org.eclipse.jdt.internal.ui.actions.ActionMessages;
104import org.eclipse.jdt.internal.ui.fix.CleanUpRefactoringWizard;
105import org.eclipse.jdt.internal.ui.preferences.JavaPreferencesSettings;
106import org.eclipse.jdt.internal.ui.refactoring.ChangeSignatureWizard;
107import org.eclipse.jdt.internal.ui.refactoring.ChangeTypeWizard;
108import org.eclipse.jdt.internal.ui.refactoring.ConvertAnonymousToNestedWizard;
109import org.eclipse.jdt.internal.ui.refactoring.ExtractClassWizard;
110import org.eclipse.jdt.internal.ui.refactoring.ExtractInterfaceWizard;
111import org.eclipse.jdt.internal.ui.refactoring.ExtractSupertypeWizard;
112import org.eclipse.jdt.internal.ui.refactoring.InferTypeArgumentsWizard;
113import org.eclipse.jdt.internal.ui.refactoring.InlineConstantWizard;
114import org.eclipse.jdt.internal.ui.refactoring.InlineTempWizard;
115import org.eclipse.jdt.internal.ui.refactoring.IntroduceFactoryWizard;
116import org.eclipse.jdt.internal.ui.refactoring.IntroduceIndirectionWizard;
117import org.eclipse.jdt.internal.ui.refactoring.IntroduceParameterObjectWizard;
118import org.eclipse.jdt.internal.ui.refactoring.IntroduceParameterWizard;
119import org.eclipse.jdt.internal.ui.refactoring.MoveInnerToTopWizard;
120import org.eclipse.jdt.internal.ui.refactoring.MoveInstanceMethodWizard;
121import org.eclipse.jdt.internal.ui.refactoring.MoveMembersWizard;
122import org.eclipse.jdt.internal.ui.refactoring.PullUpWizard;
123import org.eclipse.jdt.internal.ui.refactoring.PushDownWizard;
124import org.eclipse.jdt.internal.ui.refactoring.RefactoringExecutionHelper;
125import org.eclipse.jdt.internal.ui.refactoring.RefactoringMessages;
126import org.eclipse.jdt.internal.ui.refactoring.UseSupertypeWizard;
127import org.eclipse.jdt.internal.ui.refactoring.actions.RefactoringStarter;
128import org.eclipse.jdt.internal.ui.refactoring.code.InlineMethodWizard;
129import org.eclipse.jdt.internal.ui.refactoring.code.ReplaceInvocationsWizard;
130import org.eclipse.jdt.internal.ui.refactoring.reorg.CreateTargetQueries;
131import org.eclipse.jdt.internal.ui.refactoring.reorg.DeleteUserInterfaceManager;
132import org.eclipse.jdt.internal.ui.refactoring.reorg.NewNameQueries;
133import org.eclipse.jdt.internal.ui.refactoring.reorg.ReorgCopyWizard;
134import org.eclipse.jdt.internal.ui.refactoring.reorg.ReorgMoveWizard;
135import org.eclipse.jdt.internal.ui.refactoring.reorg.ReorgQueries;
136import org.eclipse.jdt.internal.ui.refactoring.sef.SelfEncapsulateFieldWizard;
137import org.eclipse.jdt.internal.ui.util.ExceptionHandler;
138
139/**
140 * Helper class to run refactorings from action code.
141 * <p>
142 * This class has been introduced to decouple actions from the refactoring code,
143 * in order not to eagerly load refactoring classes during action
144 * initialization.
145 * </p>
146 *
147 * @since 3.1
148 */
149public final class RefactoringExecutionStarter {
150
151 private static RenameSupport createRenameSupport(IJavaElement element, String newName, int flags) throws CoreException {
152 switch (element.getElementType()) {
153 case IJavaElement.JAVA_PROJECT:
154 return RenameSupport.create((IJavaProject) element, newName, flags);
155 case IJavaElement.PACKAGE_FRAGMENT_ROOT:
156 return RenameSupport.create((IPackageFragmentRoot) element, newName);
157 case IJavaElement.PACKAGE_FRAGMENT:
158 return RenameSupport.create((IPackageFragment) element, newName, flags);
159 case IJavaElement.COMPILATION_UNIT:
160 return RenameSupport.create((ICompilationUnit) element, newName, flags);
161 case IJavaElement.TYPE:
162 return RenameSupport.create((IType) element, newName, flags);
163 case IJavaElement.METHOD:
164 final IMethod method= (IMethod) element;
165 if (method.isConstructor())
166 return createRenameSupport(method.getDeclaringType(), newName, flags);
167 else
168 return RenameSupport.create((IMethod) element, newName, flags);
169 case IJavaElement.FIELD:
170 return RenameSupport.create((IField) element, newName, flags);
171 case IJavaElement.TYPE_PARAMETER:
172 return RenameSupport.create((ITypeParameter) element, newName, flags);
173 case IJavaElement.LOCAL_VARIABLE:
174 return RenameSupport.create((ILocalVariable) element, newName, flags);
175 }
176 return null;
177 }
178
179 public static void startChangeSignatureRefactoring(final IMethod method, final SelectionDispatchAction action, final Shell shell) throws JavaModelException {
180 if (!RefactoringAvailabilityTester.isChangeSignatureAvailable(method))
181 return;
182 try {
183 ChangeSignatureProcessor processor= new ChangeSignatureProcessor(method);
184 RefactoringStatus status= processor.checkInitialConditions(new NullProgressMonitor());
185 if (status.hasFatalError()) {
186 final RefactoringStatusEntry entry= status.getEntryMatchingSeverity(RefactoringStatus.FATAL);
187 if (entry.getCode() == RefactoringStatusCodes.OVERRIDES_ANOTHER_METHOD || entry.getCode() == RefactoringStatusCodes.METHOD_DECLARED_IN_INTERFACE) {
188 Object element= entry.getData();
189 if (element != null) {
190 String message= Messages.format(RefactoringMessages.RefactoringErrorDialogUtil_okToPerformQuestion, entry.getMessage());
191 if (MessageDialog.openQuestion(shell, RefactoringMessages.OpenRefactoringWizardAction_refactoring, message)) {
192 IStructuredSelection selection= new StructuredSelection(element);
193 // TODO: should not hijack this
194 // ModifiyParametersAction.
195 // The action is set up on an editor, but we use it
196 // as if it were set up on a ViewPart.
197 boolean wasEnabled= action.isEnabled();
198 action.selectionChanged(selection);
199 if (action.isEnabled()) {
200 action.run(selection);
201 } else {
202 MessageDialog.openInformation(shell, ActionMessages.ModifyParameterAction_problem_title, ActionMessages.ModifyParameterAction_problem_message);
203 }
204 action.setEnabled(wasEnabled);
205 }
206 }
207 return;
208 }
209 }
210
211 Refactoring refactoring= new ProcessorBasedRefactoring(processor);
212 ChangeSignatureWizard wizard= new ChangeSignatureWizard(processor, refactoring);
213 new RefactoringStarter().activate(wizard, shell, wizard.getDefaultPageTitle(), RefactoringSaveHelper.SAVE_REFACTORING);
214 } catch (CoreException e) {
215 ExceptionHandler.handle(e, RefactoringMessages.OpenRefactoringWizardAction_refactoring, RefactoringMessages.RefactoringStarter_unexpected_exception);
216 }
217 }
218
219 public static void startChangeTypeRefactoring(final ICompilationUnit unit, final Shell shell, final int offset, final int length) {
220 final ChangeTypeRefactoring refactoring= new ChangeTypeRefactoring(unit, offset, length);
221 new RefactoringStarter().activate(new ChangeTypeWizard(refactoring), shell, RefactoringMessages.ChangeTypeAction_dialog_title, RefactoringSaveHelper.SAVE_REFACTORING);
222 }
223
224 public static void startCleanupRefactoring(ICompilationUnit[] cus, ICleanUp[] cleanUps, boolean useOptionsFromProfile, Shell shell, boolean showWizard, String actionName) throws InvocationTargetException {
225 final CleanUpRefactoring refactoring= new CleanUpRefactoring(actionName);
226 for (int i= 0; i < cus.length; i++) {
227 refactoring.addCompilationUnit(cus[i]);
228 }
229
230 if (!showWizard) {
231 refactoring.setUseOptionsFromProfile(useOptionsFromProfile);
232 for (int i= 0; i < cleanUps.length; i++) {
233 refactoring.addCleanUp(cleanUps[i]);
234 }
235
236 IRunnableContext context;
237 if (refactoring.getCleanUpTargetsSize() > 1) {
238 context= new ProgressMonitorDialog(shell);
239 } else {
240 context= PlatformUI.getWorkbench().getActiveWorkbenchWindow();
241 }
242
243 RefactoringExecutionHelper helper= new RefactoringExecutionHelper(refactoring, IStatus.INFO, RefactoringSaveHelper.SAVE_REFACTORING, shell, context);
244 try {
245 helper.perform(true, true, true);
246 } catch (InterruptedException e) {
247 }
248 } else {
249 CleanUpRefactoringWizard refactoringWizard= new CleanUpRefactoringWizard(refactoring, RefactoringWizard.WIZARD_BASED_USER_INTERFACE);
250 RefactoringStarter starter= new RefactoringStarter();
251 starter.activate(refactoringWizard, shell, actionName, RefactoringSaveHelper.SAVE_REFACTORING);
252 }
253 }
254
255 public static void startConvertAnonymousRefactoring(final ICompilationUnit unit, final int offset, final int length, final Shell shell) {
256 final ConvertAnonymousToNestedRefactoring refactoring= new ConvertAnonymousToNestedRefactoring(unit, offset, length);
257 new RefactoringStarter().activate(new ConvertAnonymousToNestedWizard(refactoring), shell, RefactoringMessages.ConvertAnonymousToNestedAction_dialog_title,
258 RefactoringSaveHelper.SAVE_REFACTORING);
259 }
260
261 public static void startCopyRefactoring(IResource[] resources, IJavaElement[] javaElements, Shell shell) throws JavaModelException {
262 ICopyPolicy copyPolicy= ReorgPolicyFactory.createCopyPolicy(resources, javaElements);
263 if (copyPolicy.canEnable()) {
264 JavaCopyProcessor processor= new JavaCopyProcessor(copyPolicy);
265 Refactoring refactoring= new CopyRefactoring(processor);
266 RefactoringWizard wizard= new ReorgCopyWizard(processor, refactoring);
267 processor.setNewNameQueries(new NewNameQueries(wizard));
268 processor.setReorgQueries(new ReorgQueries(wizard));
269 new RefactoringStarter().activate(wizard, shell, RefactoringMessages.OpenRefactoringWizardAction_refactoring, processor.getSaveMode());
270 }
271 }
272
273 public static void startCutRefactoring(final Object[] elements, final Shell shell) throws InterruptedException, InvocationTargetException {
274 JavaDeleteProcessor processor= new JavaDeleteProcessor(elements);
275 processor.setSuggestGetterSetterDeletion(false);
276 processor.setQueries(new ReorgQueries(shell));
277 Refactoring refactoring= new DeleteRefactoring(processor);
278 int stopSeverity= RefactoringCore.getConditionCheckingFailedSeverity();
279 new RefactoringExecutionHelper(refactoring, stopSeverity, RefactoringSaveHelper.SAVE_NOTHING, shell, new ProgressMonitorDialog(shell)).perform(false, false);
280 }
281
282 public static void startDeleteRefactoring(final Object[] elements, final Shell shell) throws CoreException {
283 Refactoring refactoring= new DeleteRefactoring(new JavaDeleteProcessor(elements));
284 DeleteUserInterfaceManager.getDefault().getStarter(refactoring).activate(refactoring, shell, RefactoringSaveHelper.SAVE_NOTHING);
285 }
286
287 public static void startExtractInterfaceRefactoring(final IType type, final Shell shell) {
288 ExtractInterfaceProcessor processor= new ExtractInterfaceProcessor(type, JavaPreferencesSettings.getCodeGenerationSettings(type.getJavaProject()));
289 Refactoring refactoring= new ProcessorBasedRefactoring(processor);
290 new RefactoringStarter().activate(new ExtractInterfaceWizard(processor, refactoring), shell, RefactoringMessages.OpenRefactoringWizardAction_refactoring,
291 RefactoringSaveHelper.SAVE_REFACTORING);
292 }
293
294 public static void startExtractSupertypeRefactoring(final IMember[] members, final Shell shell) throws JavaModelException {
295 if (!RefactoringAvailabilityTester.isExtractSupertypeAvailable(members))
296 return;
297 IJavaProject project= null;
298 if (members != null && members.length > 0)
299 project= members[0].getJavaProject();
300 ExtractSupertypeProcessor processor= new ExtractSupertypeProcessor(members, JavaPreferencesSettings.getCodeGenerationSettings(project));
301 Refactoring refactoring= new ProcessorBasedRefactoring(processor);
302 ExtractSupertypeWizard wizard= new ExtractSupertypeWizard(processor, refactoring);
303 new RefactoringStarter().activate(wizard, shell, RefactoringMessages.OpenRefactoringWizardAction_refactoring, RefactoringSaveHelper.SAVE_REFACTORING);
304 }
305
306 public static void startInferTypeArgumentsRefactoring(final IJavaElement[] elements, final Shell shell) {
307 try {
308 if (!RefactoringAvailabilityTester.isInferTypeArgumentsAvailable(elements))
309 return;
310 final InferTypeArgumentsRefactoring refactoring= new InferTypeArgumentsRefactoring(elements);
311 new RefactoringStarter()
312 .activate(new InferTypeArgumentsWizard(refactoring), shell, RefactoringMessages.InferTypeArgumentsAction_dialog_title, RefactoringSaveHelper.SAVE_REFACTORING);
313 } catch (CoreException e) {
314 ExceptionHandler.handle(e, RefactoringMessages.InferTypeArgumentsAction_dialog_title, RefactoringMessages.OpenRefactoringWizardAction_exception);
315 }
316 }
317
318 public static boolean startInlineConstantRefactoring(final ICompilationUnit unit, final CompilationUnit node, final int offset, final int length, final Shell shell) {
319 final InlineConstantRefactoring refactoring= new InlineConstantRefactoring(unit, node, offset, length);
320 if (! refactoring.checkStaticFinalConstantNameSelected().hasFatalError()) {
321 new RefactoringStarter().activate(new InlineConstantWizard(refactoring), shell, RefactoringMessages.InlineConstantAction_dialog_title, RefactoringSaveHelper.SAVE_REFACTORING);
322 return true;
323 }
324 return false;
325 }
326
327 public static boolean startInlineMethodRefactoring(final ITypeRoot typeRoot, final CompilationUnit node, final int offset, final int length, final Shell shell) {
328 final InlineMethodRefactoring refactoring= InlineMethodRefactoring.create(typeRoot, node, offset, length);
329 if (refactoring != null) {
330 new RefactoringStarter().activate(new InlineMethodWizard(refactoring), shell, RefactoringMessages.InlineMethodAction_dialog_title, RefactoringSaveHelper.SAVE_REFACTORING);
331 return true;
332 }
333 return false;
334 }
335
336 public static boolean startInlineTempRefactoring(final ICompilationUnit unit, CompilationUnit node, final ITextSelection selection, final Shell shell) {
337 final InlineTempRefactoring refactoring= new InlineTempRefactoring(unit, node, selection.getOffset(), selection.getLength());
338 if (!refactoring.checkIfTempSelected().hasFatalError()) {
339 new RefactoringStarter().activate(new InlineTempWizard(refactoring), shell, RefactoringMessages.InlineTempAction_inline_temp, RefactoringSaveHelper.SAVE_NOTHING);
340 return true;
341 }
342 return false;
343 }
344
345 public static void startIntroduceFactoryRefactoring(final ICompilationUnit unit, final ITextSelection selection, final Shell shell) {
346 final IntroduceFactoryRefactoring refactoring= new IntroduceFactoryRefactoring(unit, selection.getOffset(), selection.getLength());
347 new RefactoringStarter().activate(new IntroduceFactoryWizard(refactoring, RefactoringMessages.IntroduceFactoryAction_use_factory), shell,
348 RefactoringMessages.IntroduceFactoryAction_dialog_title, RefactoringSaveHelper.SAVE_REFACTORING);
349 }
350
351 public static void startIntroduceIndirectionRefactoring(final IClassFile file, final int offset, final int length, final Shell shell) {
352 final IntroduceIndirectionRefactoring refactoring= new IntroduceIndirectionRefactoring(file, offset, length);
353 new RefactoringStarter().activate(new IntroduceIndirectionWizard(refactoring, RefactoringMessages.IntroduceIndirectionAction_dialog_title), shell,
354 RefactoringMessages.IntroduceIndirectionAction_dialog_title, RefactoringSaveHelper.SAVE_REFACTORING);
355 }
356
357 public static void startIntroduceIndirectionRefactoring(final ICompilationUnit unit, final int offset, final int length, final Shell shell) {
358 final IntroduceIndirectionRefactoring refactoring= new IntroduceIndirectionRefactoring(unit, offset, length);
359 new RefactoringStarter().activate(new IntroduceIndirectionWizard(refactoring, RefactoringMessages.IntroduceIndirectionAction_dialog_title), shell,
360 RefactoringMessages.IntroduceIndirectionAction_dialog_title, RefactoringSaveHelper.SAVE_REFACTORING);
361 }
362
363 public static void startIntroduceIndirectionRefactoring(final IMethod method, final Shell shell) {
364 final IntroduceIndirectionRefactoring refactoring= new IntroduceIndirectionRefactoring(method);
365 new RefactoringStarter().activate(new IntroduceIndirectionWizard(refactoring, RefactoringMessages.IntroduceIndirectionAction_dialog_title), shell,
366 RefactoringMessages.IntroduceIndirectionAction_dialog_title, RefactoringSaveHelper.SAVE_REFACTORING);
367 }
368
369 public static void startIntroduceParameter(ICompilationUnit unit, int offset, int length, Shell shell) {
370 final IntroduceParameterRefactoring refactoring= new IntroduceParameterRefactoring(unit, offset, length);
371 new RefactoringStarter().activate(new IntroduceParameterWizard(refactoring), shell, RefactoringMessages.IntroduceParameterAction_dialog_title, RefactoringSaveHelper.SAVE_REFACTORING);
372 }
373
374 public static void startMoveInnerRefactoring(final IType type, final Shell shell) throws JavaModelException {
375 if (!RefactoringAvailabilityTester.isMoveInnerAvailable(type))
376 return;
377 final MoveInnerToTopRefactoring refactoring= new MoveInnerToTopRefactoring(type, JavaPreferencesSettings.getCodeGenerationSettings(type.getJavaProject()));
378 new RefactoringStarter().activate(new MoveInnerToTopWizard(refactoring), shell, RefactoringMessages.OpenRefactoringWizardAction_refactoring, RefactoringSaveHelper.SAVE_REFACTORING);
379 }
380
381 public static void startMoveMethodRefactoring(final IMethod method, final Shell shell) {
382 MoveInstanceMethodProcessor processor= new MoveInstanceMethodProcessor(method, JavaPreferencesSettings.getCodeGenerationSettings(method.getJavaProject()));
383 Refactoring refactoring= new MoveRefactoring(processor);
384 MoveInstanceMethodWizard wizard= new MoveInstanceMethodWizard(processor, refactoring);
385 new RefactoringStarter().activate(wizard, shell, RefactoringMessages.MoveInstanceMethodAction_dialog_title, RefactoringSaveHelper.SAVE_REFACTORING);
386 }
387
388 public static void startMoveRefactoring(final IResource[] resources, final IJavaElement[] elements, final Shell shell) throws JavaModelException {
389 IMovePolicy policy= ReorgPolicyFactory.createMovePolicy(resources, elements);
390 if (policy.canEnable()) {
391 JavaMoveProcessor processor= new JavaMoveProcessor(policy);
392 Refactoring refactoring= new MoveRefactoring(processor);
393 RefactoringWizard wizard= new ReorgMoveWizard(processor, refactoring);
394 processor.setCreateTargetQueries(new CreateTargetQueries(wizard));
395 processor.setReorgQueries(new ReorgQueries(wizard));
396 new RefactoringStarter().activate(wizard, shell, RefactoringMessages.OpenRefactoringWizardAction_refactoring, processor.getSaveMode());
397 }
398 }
399
400 public static void startMoveStaticMembersRefactoring(final IMember[] members, final Shell shell) throws JavaModelException {
401 if (!RefactoringAvailabilityTester.isMoveStaticAvailable(members))
402 return;
403 final Set<IMember> set= new HashSet<IMember>();
404 set.addAll(Arrays.asList(members));
405 final IMember[] elements= set.toArray(new IMember[set.size()]);
406 IJavaProject project= null;
407 if (elements.length > 0)
408 project= elements[0].getJavaProject();
409 MoveStaticMembersProcessor processor= new MoveStaticMembersProcessor(elements, JavaPreferencesSettings.getCodeGenerationSettings(project));
410 Refactoring refactoring= new MoveRefactoring(processor);
411 MoveMembersWizard wizard= new MoveMembersWizard(processor, refactoring);
412 new RefactoringStarter().activate(wizard, shell, RefactoringMessages.OpenRefactoringWizardAction_refactoring, RefactoringSaveHelper.SAVE_REFACTORING);
413 }
414
415 public static void startPullUpRefactoring(final IMember[] members, final Shell shell) throws JavaModelException {
416 if (!RefactoringAvailabilityTester.isPullUpAvailable(members))
417 return;
418 IJavaProject project= null;
419 if (members != null && members.length > 0)
420 project= members[0].getJavaProject();
421 PullUpRefactoringProcessor processor= new PullUpRefactoringProcessor(members, JavaPreferencesSettings.getCodeGenerationSettings(project));
422 Refactoring refactoring= new ProcessorBasedRefactoring(processor);
423 new RefactoringStarter().activate(new PullUpWizard(processor, refactoring), shell, RefactoringMessages.OpenRefactoringWizardAction_refactoring, RefactoringSaveHelper.SAVE_REFACTORING);
424 }
425
426 public static void startPushDownRefactoring(final IMember[] members, final Shell shell) throws JavaModelException {
427 if (!RefactoringAvailabilityTester.isPushDownAvailable(members))
428 return;
429 PushDownRefactoringProcessor processor= new PushDownRefactoringProcessor(members);
430 Refactoring refactoring= new ProcessorBasedRefactoring(processor);
431 PushDownWizard wizard= new PushDownWizard(processor, refactoring);
432 new RefactoringStarter().activate(wizard, shell, RefactoringMessages.OpenRefactoringWizardAction_refactoring, RefactoringSaveHelper.SAVE_REFACTORING);
433 }
434
435 public static void startRenameRefactoring(final IJavaElement element, final Shell shell) throws CoreException {
436 final RenameSupport support= createRenameSupport(element, null, RenameSupport.UPDATE_REFERENCES);
437 if (support != null && support.preCheck().isOK())
438 support.openDialog(shell);
439 }
440
441 public static void startRenameResourceRefactoring(final IResource resource, final Shell shell) {
442 RenameResourceWizard wizard= new RenameResourceWizard(resource);
443 new RefactoringStarter().activate(wizard, shell, wizard.getWindowTitle(), RefactoringSaveHelper.SAVE_ALL);
444 }
445
446 public static void startReplaceInvocationsRefactoring(final ITypeRoot typeRoot, final int offset, final int length, final Shell shell) {
447 final ReplaceInvocationsRefactoring refactoring= new ReplaceInvocationsRefactoring(typeRoot, offset, length);
448 new RefactoringStarter().activate(new ReplaceInvocationsWizard(refactoring), shell, RefactoringMessages.ReplaceInvocationsAction_dialog_title, RefactoringSaveHelper.SAVE_REFACTORING);
449 }
450
451 public static void startReplaceInvocationsRefactoring(final IMethod method, final Shell shell) {
452 final ReplaceInvocationsRefactoring refactoring= new ReplaceInvocationsRefactoring(method);
453 new RefactoringStarter().activate(new ReplaceInvocationsWizard(refactoring), shell, RefactoringMessages.ReplaceInvocationsAction_dialog_title, RefactoringSaveHelper.SAVE_REFACTORING);
454 }
455
456 public static void startSelfEncapsulateRefactoring(final IField field, final Shell shell) {
457 try {
458 if (!RefactoringAvailabilityTester.isSelfEncapsulateAvailable(field))
459 return;
460 final SelfEncapsulateFieldRefactoring refactoring= new SelfEncapsulateFieldRefactoring(field);
461 new RefactoringStarter().activate(new SelfEncapsulateFieldWizard(refactoring), shell, "", RefactoringSaveHelper.SAVE_REFACTORING); //$NON-NLS-1$
462 } catch (JavaModelException e) {
463 ExceptionHandler.handle(e, ActionMessages.SelfEncapsulateFieldAction_dialog_title, ActionMessages.SelfEncapsulateFieldAction_dialog_cannot_perform);
464 }
465 }
466
467 public static void startUseSupertypeRefactoring(final IType type, final Shell shell) {
468 UseSuperTypeProcessor processor= new UseSuperTypeProcessor(type);
469 Refactoring refactoring= new ProcessorBasedRefactoring(processor);
470 UseSupertypeWizard wizard= new UseSupertypeWizard(processor, refactoring);
471 new RefactoringStarter().activate(wizard, shell, RefactoringMessages.OpenRefactoringWizardAction_refactoring, RefactoringSaveHelper.SAVE_REFACTORING);
472 }
473
474 private RefactoringExecutionStarter() {
475 // Not for instantiation
476 }
477
478 public static void startIntroduceParameterObject(ICompilationUnit unit, int offset, Shell shell) throws CoreException {
479 IJavaElement javaElement= unit.getElementAt(offset);
480 if (javaElement instanceof IMethod) {
481 IMethod method= (IMethod) javaElement;
482 startIntroduceParameterObject(method, shell);
483 }
484 }
485
486 public static void startIntroduceParameterObject(IMethod method, Shell shell) throws CoreException {
487 RefactoringStatus availability= Checks.checkAvailability(method);
488 if (availability.hasError()){
489 MessageDialog.openError(shell, RefactoringMessages.RefactoringExecutionStarter_IntroduceParameterObject_problem_title, RefactoringMessages.RefactoringExecutionStarter_IntroduceParameterObject_problem_description);
490 return;
491 }
492 IntroduceParameterObjectDescriptor ipod= RefactoringSignatureDescriptorFactory.createIntroduceParameterObjectDescriptor();
493 ipod.setMethod(method);
494
495 IntroduceParameterObjectProcessor processor= new IntroduceParameterObjectProcessor(ipod);
496
497 final RefactoringStatus status= processor.checkInitialConditions(new NullProgressMonitor());
498 if (status.hasFatalError()) {
499 final RefactoringStatusEntry entry= status.getEntryMatchingSeverity(RefactoringStatus.FATAL);
500 if (entry.getCode() == RefactoringStatusCodes.OVERRIDES_ANOTHER_METHOD || entry.getCode() == RefactoringStatusCodes.METHOD_DECLARED_IN_INTERFACE) {
501 final Object element= entry.getData();
502 IMethod superMethod= (IMethod) element;
503 availability= Checks.checkAvailability(superMethod);
504 if (availability.hasError()){
505 MessageDialog.openError(shell, RefactoringMessages.RefactoringExecutionStarter_IntroduceParameterObject_problem_title, RefactoringMessages.RefactoringExecutionStarter_IntroduceParameterObject_problem_description);
506 return;
507 }
508 String message= Messages.format(RefactoringMessages.RefactoringErrorDialogUtil_okToPerformQuestion, entry.getMessage());
509 if (element != null && MessageDialog.openQuestion(shell, RefactoringMessages.OpenRefactoringWizardAction_refactoring, message)) {
510 ipod= RefactoringSignatureDescriptorFactory.createIntroduceParameterObjectDescriptor();
511 ipod.setMethod(superMethod);
512 processor= new IntroduceParameterObjectProcessor(ipod);
513 }
514 else processor=null;
515 }
516 }
517 if (processor != null) {
518 Refactoring refactoring= new ProcessorBasedRefactoring(processor);
519 IntroduceParameterObjectWizard wizard= new IntroduceParameterObjectWizard(processor, refactoring);
520 new RefactoringStarter().activate(wizard, shell, RefactoringMessages.OpenRefactoringWizardAction_refactoring, RefactoringSaveHelper.SAVE_REFACTORING);
521 }
522 }
523
524 public static void startExtractClassRefactoring(IType type, Shell shell) {
525 ExtractClassDescriptor descriptor= RefactoringSignatureDescriptorFactory.createExtractClassDescriptor();
526 descriptor.setType(type);
527 ExtractClassRefactoring refactoring= new ExtractClassRefactoring(descriptor);
528 ExtractClassWizard wizard= new ExtractClassWizard(descriptor, refactoring);
529 new RefactoringStarter().activate(wizard, shell, RefactoringMessages.OpenRefactoringWizardAction_refactoring, RefactoringSaveHelper.SAVE_REFACTORING);
530 }
531
532}