KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > corext > refactoring > RefactoringExecutionStarter


1 /*******************************************************************************
2  * Copyright (c) 2005, 2007 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.corext.refactoring;
12
13 import java.lang.reflect.InvocationTargetException JavaDoc;
14 import java.util.Arrays JavaDoc;
15 import java.util.HashSet JavaDoc;
16 import java.util.Set JavaDoc;
17
18 import org.eclipse.core.runtime.CoreException;
19 import org.eclipse.core.runtime.IStatus;
20 import org.eclipse.core.runtime.NullProgressMonitor;
21
22 import org.eclipse.core.resources.IResource;
23
24 import org.eclipse.swt.widgets.Shell;
25
26 import org.eclipse.jface.dialogs.MessageDialog;
27 import org.eclipse.jface.dialogs.ProgressMonitorDialog;
28 import org.eclipse.jface.operation.IRunnableContext;
29 import org.eclipse.jface.viewers.IStructuredSelection;
30 import org.eclipse.jface.viewers.StructuredSelection;
31
32 import org.eclipse.jface.text.ITextSelection;
33
34 import org.eclipse.ui.PlatformUI;
35
36 import org.eclipse.ltk.core.refactoring.Refactoring;
37 import org.eclipse.ltk.core.refactoring.RefactoringCore;
38 import org.eclipse.ltk.core.refactoring.RefactoringStatus;
39 import org.eclipse.ltk.core.refactoring.RefactoringStatusEntry;
40 import org.eclipse.ltk.core.refactoring.participants.CopyRefactoring;
41 import org.eclipse.ltk.core.refactoring.participants.DeleteRefactoring;
42 import org.eclipse.ltk.ui.refactoring.RefactoringWizard;
43
44 import org.eclipse.jdt.core.IClassFile;
45 import org.eclipse.jdt.core.ICompilationUnit;
46 import org.eclipse.jdt.core.IField;
47 import org.eclipse.jdt.core.IJavaElement;
48 import org.eclipse.jdt.core.IJavaProject;
49 import org.eclipse.jdt.core.ILocalVariable;
50 import org.eclipse.jdt.core.IMember;
51 import org.eclipse.jdt.core.IMethod;
52 import org.eclipse.jdt.core.IPackageFragment;
53 import org.eclipse.jdt.core.IPackageFragmentRoot;
54 import org.eclipse.jdt.core.IType;
55 import org.eclipse.jdt.core.ITypeParameter;
56 import org.eclipse.jdt.core.ITypeRoot;
57 import org.eclipse.jdt.core.JavaModelException;
58 import org.eclipse.jdt.core.dom.CompilationUnit;
59
60 import org.eclipse.jdt.internal.corext.fix.CleanUpRefactoring;
61 import org.eclipse.jdt.internal.corext.refactoring.base.RefactoringStatusCodes;
62 import org.eclipse.jdt.internal.corext.refactoring.code.ConvertAnonymousToNestedRefactoring;
63 import org.eclipse.jdt.internal.corext.refactoring.code.InlineConstantRefactoring;
64 import org.eclipse.jdt.internal.corext.refactoring.code.InlineMethodRefactoring;
65 import org.eclipse.jdt.internal.corext.refactoring.code.InlineTempRefactoring;
66 import org.eclipse.jdt.internal.corext.refactoring.code.IntroduceFactoryRefactoring;
67 import org.eclipse.jdt.internal.corext.refactoring.code.IntroduceIndirectionRefactoring;
68 import org.eclipse.jdt.internal.corext.refactoring.code.IntroduceParameterRefactoring;
69 import org.eclipse.jdt.internal.corext.refactoring.code.ReplaceInvocationsRefactoring;
70 import org.eclipse.jdt.internal.corext.refactoring.generics.InferTypeArgumentsRefactoring;
71 import org.eclipse.jdt.internal.corext.refactoring.rename.JavaRenameRefactoring;
72 import org.eclipse.jdt.internal.corext.refactoring.rename.RenameResourceProcessor;
73 import org.eclipse.jdt.internal.corext.refactoring.reorg.JavaCopyProcessor;
74 import org.eclipse.jdt.internal.corext.refactoring.reorg.JavaCopyRefactoring;
75 import org.eclipse.jdt.internal.corext.refactoring.reorg.JavaDeleteProcessor;
76 import org.eclipse.jdt.internal.corext.refactoring.reorg.JavaDeleteRefactoring;
77 import org.eclipse.jdt.internal.corext.refactoring.reorg.JavaMoveProcessor;
78 import org.eclipse.jdt.internal.corext.refactoring.reorg.ReorgPolicyFactory;
79 import org.eclipse.jdt.internal.corext.refactoring.reorg.IReorgPolicy.ICopyPolicy;
80 import org.eclipse.jdt.internal.corext.refactoring.reorg.IReorgPolicy.IMovePolicy;
81 import org.eclipse.jdt.internal.corext.refactoring.sef.SelfEncapsulateFieldRefactoring;
82 import org.eclipse.jdt.internal.corext.refactoring.structure.ChangeSignatureRefactoring;
83 import org.eclipse.jdt.internal.corext.refactoring.structure.ChangeTypeRefactoring;
84 import org.eclipse.jdt.internal.corext.refactoring.structure.ExtractInterfaceProcessor;
85 import org.eclipse.jdt.internal.corext.refactoring.structure.ExtractInterfaceRefactoring;
86 import org.eclipse.jdt.internal.corext.refactoring.structure.ExtractSupertypeProcessor;
87 import org.eclipse.jdt.internal.corext.refactoring.structure.ExtractSupertypeRefactoring;
88 import org.eclipse.jdt.internal.corext.refactoring.structure.IntroduceParameterObjectRefactoring;
89 import org.eclipse.jdt.internal.corext.refactoring.structure.JavaMoveRefactoring;
90 import org.eclipse.jdt.internal.corext.refactoring.structure.MoveInnerToTopRefactoring;
91 import org.eclipse.jdt.internal.corext.refactoring.structure.MoveInstanceMethodProcessor;
92 import org.eclipse.jdt.internal.corext.refactoring.structure.MoveInstanceMethodRefactoring;
93 import org.eclipse.jdt.internal.corext.refactoring.structure.MoveStaticMembersProcessor;
94 import org.eclipse.jdt.internal.corext.refactoring.structure.PullUpRefactoring;
95 import org.eclipse.jdt.internal.corext.refactoring.structure.PullUpRefactoringProcessor;
96 import org.eclipse.jdt.internal.corext.refactoring.structure.PushDownRefactoring;
97 import org.eclipse.jdt.internal.corext.refactoring.structure.PushDownRefactoringProcessor;
98 import org.eclipse.jdt.internal.corext.refactoring.structure.UseSuperTypeProcessor;
99 import org.eclipse.jdt.internal.corext.refactoring.structure.UseSuperTypeRefactoring;
100
101 import org.eclipse.jdt.ui.actions.SelectionDispatchAction;
102 import org.eclipse.jdt.ui.refactoring.RenameSupport;
103
104 import org.eclipse.jdt.internal.ui.actions.ActionMessages;
105 import org.eclipse.jdt.internal.ui.fix.CleanUpRefactoringWizard;
106 import org.eclipse.jdt.internal.ui.fix.ICleanUp;
107 import org.eclipse.jdt.internal.ui.preferences.JavaPreferencesSettings;
108 import org.eclipse.jdt.internal.ui.refactoring.ChangeSignatureWizard;
109 import org.eclipse.jdt.internal.ui.refactoring.ChangeTypeWizard;
110 import org.eclipse.jdt.internal.ui.refactoring.ConvertAnonymousToNestedWizard;
111 import org.eclipse.jdt.internal.ui.refactoring.ExtractInterfaceWizard;
112 import org.eclipse.jdt.internal.ui.refactoring.ExtractSupertypeWizard;
113 import org.eclipse.jdt.internal.ui.refactoring.InferTypeArgumentsWizard;
114 import org.eclipse.jdt.internal.ui.refactoring.InlineConstantWizard;
115 import org.eclipse.jdt.internal.ui.refactoring.InlineTempWizard;
116 import org.eclipse.jdt.internal.ui.refactoring.IntroduceFactoryWizard;
117 import org.eclipse.jdt.internal.ui.refactoring.IntroduceIndirectionWizard;
118 import org.eclipse.jdt.internal.ui.refactoring.IntroduceParameterObjectWizard;
119 import org.eclipse.jdt.internal.ui.refactoring.IntroduceParameterWizard;
120 import org.eclipse.jdt.internal.ui.refactoring.MoveInnerToTopWizard;
121 import org.eclipse.jdt.internal.ui.refactoring.MoveInstanceMethodWizard;
122 import org.eclipse.jdt.internal.ui.refactoring.MoveMembersWizard;
123 import org.eclipse.jdt.internal.ui.refactoring.PullUpWizard;
124 import org.eclipse.jdt.internal.ui.refactoring.PushDownWizard;
125 import org.eclipse.jdt.internal.ui.refactoring.RefactoringExecutionHelper;
126 import org.eclipse.jdt.internal.ui.refactoring.RefactoringMessages;
127 import org.eclipse.jdt.internal.ui.refactoring.RefactoringSaveHelper;
128 import org.eclipse.jdt.internal.ui.refactoring.UseSupertypeWizard;
129 import org.eclipse.jdt.internal.ui.refactoring.UserInterfaceStarter;
130 import org.eclipse.jdt.internal.ui.refactoring.actions.RefactoringStarter;
131 import org.eclipse.jdt.internal.ui.refactoring.code.InlineMethodWizard;
132 import org.eclipse.jdt.internal.ui.refactoring.code.ReplaceInvocationsWizard;
133 import org.eclipse.jdt.internal.ui.refactoring.reorg.CreateTargetQueries;
134 import org.eclipse.jdt.internal.ui.refactoring.reorg.DeleteUserInterfaceManager;
135 import org.eclipse.jdt.internal.ui.refactoring.reorg.NewNameQueries;
136 import org.eclipse.jdt.internal.ui.refactoring.reorg.RenameUserInterfaceManager;
137 import org.eclipse.jdt.internal.ui.refactoring.reorg.ReorgCopyWizard;
138 import org.eclipse.jdt.internal.ui.refactoring.reorg.ReorgMoveWizard;
139 import org.eclipse.jdt.internal.ui.refactoring.reorg.ReorgQueries;
140 import org.eclipse.jdt.internal.ui.refactoring.sef.SelfEncapsulateFieldWizard;
141 import org.eclipse.jdt.internal.ui.util.ExceptionHandler;
142
143 /**
144  * Helper class to run refactorings from action code.
145  * <p>
146  * This class has been introduced to decouple actions from the refactoring code,
147  * in order not to eagerly load refactoring classes during action
148  * initialization.
149  * </p>
150  *
151  * @since 3.1
152  */

153 public final class RefactoringExecutionStarter {
154
155     private static RenameSupport createRenameSupport(IJavaElement element, String JavaDoc newName, int flags) throws CoreException {
156         switch (element.getElementType()) {
157             case IJavaElement.JAVA_PROJECT:
158                 return RenameSupport.create((IJavaProject) element, newName, flags);
159             case IJavaElement.PACKAGE_FRAGMENT_ROOT:
160                 return RenameSupport.create((IPackageFragmentRoot) element, newName);
161             case IJavaElement.PACKAGE_FRAGMENT:
162                 return RenameSupport.create((IPackageFragment) element, newName, flags);
163             case IJavaElement.COMPILATION_UNIT:
164                 return RenameSupport.create((ICompilationUnit) element, newName, flags);
165             case IJavaElement.TYPE:
166                 return RenameSupport.create((IType) element, newName, flags);
167             case IJavaElement.METHOD:
168                 final IMethod method= (IMethod) element;
169                 if (method.isConstructor())
170                     return createRenameSupport(method.getDeclaringType(), newName, flags);
171                 else
172                     return RenameSupport.create((IMethod) element, newName, flags);
173             case IJavaElement.FIELD:
174                 return RenameSupport.create((IField) element, newName, flags);
175             case IJavaElement.TYPE_PARAMETER:
176                 return RenameSupport.create((ITypeParameter) element, newName, flags);
177             case IJavaElement.LOCAL_VARIABLE:
178                 return RenameSupport.create((ILocalVariable) element, newName, flags);
179         }
180         return null;
181     }
182
183     public static void startChangeSignatureRefactoring(final IMethod method, final SelectionDispatchAction action, final Shell shell) throws JavaModelException {
184         if (!RefactoringAvailabilityTester.isChangeSignatureAvailable(method))
185             return;
186         final ChangeSignatureRefactoring refactoring= new ChangeSignatureRefactoring(method);
187         final UserInterfaceStarter starter= new UserInterfaceStarter() {
188
189             public final boolean activate(final Refactoring ref, final Shell parent, final int saveMode) throws CoreException {
190                 final RefactoringStatus status= ref.checkInitialConditions(new NullProgressMonitor());
191                 if (status.hasFatalError()) {
192                     final RefactoringStatusEntry entry= status.getEntryMatchingSeverity(RefactoringStatus.FATAL);
193                     if (entry.getCode() == RefactoringStatusCodes.OVERRIDES_ANOTHER_METHOD || entry.getCode() == RefactoringStatusCodes.METHOD_DECLARED_IN_INTERFACE) {
194
195                         String JavaDoc message= entry.getMessage();
196                         final Object JavaDoc element= entry.getData();
197                         message= message + RefactoringMessages.RefactoringErrorDialogUtil_okToPerformQuestion;
198                         if (element != null && MessageDialog.openQuestion(shell, RefactoringMessages.OpenRefactoringWizardAction_refactoring, message)) {
199
200                             final IStructuredSelection selection= new StructuredSelection(element);
201                             // TODO: should not hijack this
202
// ModifiyParametersAction.
203
// The action is set up on an editor, but we use it
204
// as if it were set up on a ViewPart.
205
boolean wasEnabled= action.isEnabled();
206                             action.selectionChanged(selection);
207                             if (action.isEnabled()) {
208                                 action.run(selection);
209                             } else {
210                                 MessageDialog.openInformation(shell, ActionMessages.ModifyParameterAction_problem_title, ActionMessages.ModifyParameterAction_problem_message);
211                             }
212                             action.setEnabled(wasEnabled);
213                         }
214                         return false;
215                     }
216                 }
217                 return super.activate(ref, parent, saveMode);
218             }
219         };
220         starter.initialize(new ChangeSignatureWizard(refactoring));
221         try {
222             starter.activate(refactoring, shell, RefactoringSaveHelper.SAVE_JAVA_ONLY_UPDATES);
223         } catch (CoreException e) {
224             ExceptionHandler.handle(e, RefactoringMessages.OpenRefactoringWizardAction_refactoring, RefactoringMessages.RefactoringStarter_unexpected_exception);
225         }
226     }
227
228     public static void startChangeTypeRefactoring(final ICompilationUnit unit, final Shell shell, final int offset, final int length) throws JavaModelException {
229         final ChangeTypeRefactoring refactoring= new ChangeTypeRefactoring(unit, offset, length);
230         new RefactoringStarter().activate(refactoring, new ChangeTypeWizard(refactoring), shell, RefactoringMessages.ChangeTypeAction_dialog_title, RefactoringSaveHelper.SAVE_JAVA_ONLY_UPDATES);
231     }
232     
233     public static void startCleanupRefactoring(ICompilationUnit[] cus, ICleanUp[] cleanUps, Shell shell, boolean showWizard, String JavaDoc actionName) throws InvocationTargetException JavaDoc, JavaModelException {
234         final CleanUpRefactoring refactoring= new CleanUpRefactoring(actionName);
235         for (int i= 0; i < cus.length; i++) {
236             refactoring.addCompilationUnit(cus[i]);
237         }
238         
239         if (!showWizard) {
240             for (int i= 0; i < cleanUps.length; i++) {
241                 refactoring.addCleanUp(cleanUps[i]);
242             }
243             
244             IRunnableContext context;
245             if (refactoring.getCompilationUnits().length > 1) {
246                 context= new ProgressMonitorDialog(shell);
247             } else {
248                 context= PlatformUI.getWorkbench().getActiveWorkbenchWindow();
249             }
250             
251             RefactoringExecutionHelper helper= new RefactoringExecutionHelper(refactoring, IStatus.INFO, RefactoringSaveHelper.SAVE_JAVA_ONLY_UPDATES, shell, context);
252             try {
253                 helper.perform(true, true);
254             } catch (InterruptedException JavaDoc e) {
255             }
256         } else {
257             CleanUpRefactoringWizard refactoringWizard= new CleanUpRefactoringWizard(refactoring, RefactoringWizard.WIZARD_BASED_USER_INTERFACE);
258             RefactoringStarter starter= new RefactoringStarter();
259             starter.activate(refactoring, refactoringWizard, shell, actionName, RefactoringSaveHelper.SAVE_JAVA_ONLY_UPDATES);
260         }
261     }
262
263     public static void startConvertAnonymousRefactoring(final ICompilationUnit unit, final int offset, final int length, final Shell shell) throws JavaModelException {
264         final ConvertAnonymousToNestedRefactoring refactoring= new ConvertAnonymousToNestedRefactoring(unit, offset, length);
265         new RefactoringStarter().activate(refactoring, new ConvertAnonymousToNestedWizard(refactoring), shell, RefactoringMessages.ConvertAnonymousToNestedAction_dialog_title, RefactoringSaveHelper.SAVE_JAVA_ONLY_UPDATES);
266     }
267
268     public static void startCopyRefactoring(IResource[] resources, IJavaElement[] javaElements, Shell shell) throws JavaModelException {
269         ICopyPolicy copyPolicy= ReorgPolicyFactory.createCopyPolicy(resources, javaElements);
270         if (copyPolicy.canEnable()) {
271             JavaCopyProcessor processor= new JavaCopyProcessor(copyPolicy);
272             CopyRefactoring refactoring= new JavaCopyRefactoring(processor);
273             RefactoringWizard wizard= new ReorgCopyWizard(refactoring);
274             processor.setNewNameQueries(new NewNameQueries(wizard));
275             processor.setReorgQueries(new ReorgQueries(wizard));
276             new RefactoringStarter().activate(refactoring, wizard, shell, RefactoringMessages.OpenRefactoringWizardAction_refactoring, RefactoringSaveHelper.SAVE_NOTHING);
277         }
278     }
279
280     public static void startCutRefactoring(final Object JavaDoc[] elements, final Shell shell) throws CoreException, InterruptedException JavaDoc, InvocationTargetException JavaDoc {
281         final JavaDeleteProcessor processor= new JavaDeleteProcessor(elements);
282         processor.setSuggestGetterSetterDeletion(false);
283         processor.setQueries(new ReorgQueries(shell));
284         new RefactoringExecutionHelper(new JavaDeleteRefactoring(processor), RefactoringCore.getConditionCheckingFailedSeverity(), RefactoringSaveHelper.SAVE_NOTHING, shell, new ProgressMonitorDialog(shell)).perform(false, false);
285     }
286
287     public static void startDeleteRefactoring(final Object JavaDoc[] elements, final Shell shell) throws CoreException {
288         final DeleteRefactoring refactoring= new JavaDeleteRefactoring(new JavaDeleteProcessor(elements));
289         DeleteUserInterfaceManager.getDefault().getStarter(refactoring).activate(refactoring, shell, RefactoringSaveHelper.SAVE_NOTHING);
290     }
291
292     public static void startExtractInterfaceRefactoring(final IType type, final Shell shell) throws JavaModelException {
293         final ExtractInterfaceRefactoring refactoring= new ExtractInterfaceRefactoring(new ExtractInterfaceProcessor(type, JavaPreferencesSettings.getCodeGenerationSettings(type.getJavaProject())));
294         new RefactoringStarter().activate(refactoring, new ExtractInterfaceWizard(refactoring), shell, RefactoringMessages.OpenRefactoringWizardAction_refactoring, RefactoringSaveHelper.SAVE_JAVA_ONLY_UPDATES);
295     }
296
297     public static void startExtractSupertypeRefactoring(final IMember[] members, final Shell shell) throws JavaModelException {
298         if (!RefactoringAvailabilityTester.isExtractSupertypeAvailable(members))
299             return;
300         IJavaProject project= null;
301         if (members != null && members.length > 0)
302             project= members[0].getJavaProject();
303         final ExtractSupertypeRefactoring refactoring= new ExtractSupertypeRefactoring(new ExtractSupertypeProcessor(members, JavaPreferencesSettings.getCodeGenerationSettings(project)));
304         new RefactoringStarter().activate(refactoring, new ExtractSupertypeWizard(refactoring), shell, RefactoringMessages.OpenRefactoringWizardAction_refactoring, RefactoringSaveHelper.SAVE_JAVA_ONLY_UPDATES);
305     }
306
307     public static void startInferTypeArgumentsRefactoring(final IJavaElement[] elements, final Shell shell) {
308         try {
309             if (!RefactoringAvailabilityTester.isInferTypeArgumentsAvailable(elements))
310                 return;
311             final InferTypeArgumentsRefactoring refactoring= new InferTypeArgumentsRefactoring(elements);
312             new RefactoringStarter().activate(refactoring, new InferTypeArgumentsWizard(refactoring), shell, RefactoringMessages.InferTypeArgumentsAction_dialog_title, RefactoringSaveHelper.SAVE_JAVA_ONLY_UPDATES);
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) throws JavaModelException {
319         final InlineConstantRefactoring refactoring= new InlineConstantRefactoring(unit, node, offset, length);
320         if (! refactoring.checkStaticFinalConstantNameSelected().hasFatalError()) {
321             new RefactoringStarter().activate(refactoring, new InlineConstantWizard(refactoring), shell, RefactoringMessages.InlineConstantAction_dialog_title, RefactoringSaveHelper.SAVE_JAVA_ONLY_UPDATES);
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) throws JavaModelException {
328         final InlineMethodRefactoring refactoring= InlineMethodRefactoring.create(typeRoot, node, offset, length);
329         if (refactoring != null) {
330             new RefactoringStarter().activate(refactoring, new InlineMethodWizard(refactoring), shell, RefactoringMessages.InlineMethodAction_dialog_title, RefactoringSaveHelper.SAVE_JAVA_ONLY_UPDATES);
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) throws JavaModelException {
337         final InlineTempRefactoring refactoring= new InlineTempRefactoring(unit, node, selection.getOffset(), selection.getLength());
338         if (! refactoring.checkIfTempSelected().hasFatalError()) {
339             new RefactoringStarter().activate(refactoring, 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) throws JavaModelException {
346         final IntroduceFactoryRefactoring refactoring= new IntroduceFactoryRefactoring(unit, selection.getOffset(), selection.getLength());
347         new RefactoringStarter().activate(refactoring, new IntroduceFactoryWizard(refactoring, RefactoringMessages.IntroduceFactoryAction_use_factory), shell, RefactoringMessages.IntroduceFactoryAction_dialog_title, RefactoringSaveHelper.SAVE_JAVA_ONLY_UPDATES);
348     }
349
350     public static void startIntroduceIndirectionRefactoring(final IClassFile file, final int offset, final int length, final Shell shell) throws JavaModelException {
351         final IntroduceIndirectionRefactoring refactoring= new IntroduceIndirectionRefactoring(file, offset, length);
352         new RefactoringStarter().activate(refactoring, new IntroduceIndirectionWizard(refactoring, RefactoringMessages.IntroduceIndirectionAction_dialog_title), shell, RefactoringMessages.IntroduceIndirectionAction_dialog_title, RefactoringSaveHelper.SAVE_JAVA_ONLY_UPDATES);
353     }
354
355     public static void startIntroduceIndirectionRefactoring(final ICompilationUnit unit, final int offset, final int length, final Shell shell) throws JavaModelException {
356         final IntroduceIndirectionRefactoring refactoring= new IntroduceIndirectionRefactoring(unit, offset, length);
357         new RefactoringStarter().activate(refactoring, new IntroduceIndirectionWizard(refactoring, RefactoringMessages.IntroduceIndirectionAction_dialog_title), shell, RefactoringMessages.IntroduceIndirectionAction_dialog_title, RefactoringSaveHelper.SAVE_JAVA_ONLY_UPDATES);
358     }
359
360     public static void startIntroduceIndirectionRefactoring(final IMethod method, final Shell shell) throws JavaModelException {
361         final IntroduceIndirectionRefactoring refactoring= new IntroduceIndirectionRefactoring(method);
362         new RefactoringStarter().activate(refactoring, new IntroduceIndirectionWizard(refactoring, RefactoringMessages.IntroduceIndirectionAction_dialog_title), shell, RefactoringMessages.IntroduceIndirectionAction_dialog_title, RefactoringSaveHelper.SAVE_JAVA_ONLY_UPDATES);
363     }
364
365     public static void startIntroduceParameter(ICompilationUnit unit, int offset, int length, Shell shell) throws JavaModelException {
366         final IntroduceParameterRefactoring refactoring= new IntroduceParameterRefactoring(unit, offset, length);
367         new RefactoringStarter().activate(refactoring, new IntroduceParameterWizard(refactoring), shell, RefactoringMessages.IntroduceParameterAction_dialog_title, RefactoringSaveHelper.SAVE_JAVA_ONLY_UPDATES);
368     }
369
370     public static void startMoveInnerRefactoring(final IType type, final Shell shell) throws JavaModelException {
371         if (!RefactoringAvailabilityTester.isMoveInnerAvailable(type))
372             return;
373         final MoveInnerToTopRefactoring refactoring= new MoveInnerToTopRefactoring(type, JavaPreferencesSettings.getCodeGenerationSettings(type.getJavaProject()));
374         new RefactoringStarter().activate(refactoring, new MoveInnerToTopWizard(refactoring), shell, RefactoringMessages.OpenRefactoringWizardAction_refactoring, RefactoringSaveHelper.SAVE_JAVA_ONLY_UPDATES);
375     }
376
377     public static void startMoveMethodRefactoring(final IMethod method, final Shell shell) throws JavaModelException {
378         final MoveInstanceMethodRefactoring refactoring= new MoveInstanceMethodRefactoring(new MoveInstanceMethodProcessor(method, JavaPreferencesSettings.getCodeGenerationSettings(method.getJavaProject())));
379         new RefactoringStarter().activate(refactoring, new MoveInstanceMethodWizard(refactoring), shell, RefactoringMessages.MoveInstanceMethodAction_dialog_title, RefactoringSaveHelper.SAVE_JAVA_ONLY_UPDATES);
380     }
381
382     public static void startMoveRefactoring(final IResource[] resources, final IJavaElement[] elements, final Shell shell) throws JavaModelException {
383         IMovePolicy policy= ReorgPolicyFactory.createMovePolicy(resources, elements);
384         if (policy.canEnable()) {
385             final JavaMoveProcessor processor= new JavaMoveProcessor(policy);
386             final JavaMoveRefactoring refactoring= new JavaMoveRefactoring(processor);
387             final RefactoringWizard wizard= new ReorgMoveWizard(refactoring);
388             processor.setCreateTargetQueries(new CreateTargetQueries(wizard));
389             processor.setReorgQueries(new ReorgQueries(wizard));
390             new RefactoringStarter().activate(refactoring, wizard, shell, RefactoringMessages.OpenRefactoringWizardAction_refactoring, RefactoringSaveHelper.SAVE_ALL);
391         }
392     }
393
394     public static void startMoveStaticMembersRefactoring(final IMember[] members, final Shell shell) throws JavaModelException {
395         if (!RefactoringAvailabilityTester.isMoveStaticAvailable(members))
396             return;
397         final Set JavaDoc set= new HashSet JavaDoc();
398         set.addAll(Arrays.asList(members));
399         final IMember[] elements= (IMember[]) set.toArray(new IMember[set.size()]);
400         IJavaProject project= null;
401         if (elements.length > 0)
402             project= elements[0].getJavaProject();
403         final JavaMoveRefactoring refactoring= new JavaMoveRefactoring(new MoveStaticMembersProcessor(elements, JavaPreferencesSettings.getCodeGenerationSettings(project)));
404         new RefactoringStarter().activate(refactoring, new MoveMembersWizard(refactoring), shell, RefactoringMessages.OpenRefactoringWizardAction_refactoring, RefactoringSaveHelper.SAVE_NON_JAVA_UPDATES);
405     }
406
407     public static void startPullUpRefactoring(final IMember[] members, final Shell shell) throws JavaModelException {
408         if (!RefactoringAvailabilityTester.isPullUpAvailable(members))
409             return;
410         IJavaProject project= null;
411         if (members != null && members.length > 0)
412             project= members[0].getJavaProject();
413         final PullUpRefactoring refactoring= new PullUpRefactoring(new PullUpRefactoringProcessor(members, JavaPreferencesSettings.getCodeGenerationSettings(project)));
414         new RefactoringStarter().activate(refactoring, new PullUpWizard(refactoring), shell, RefactoringMessages.OpenRefactoringWizardAction_refactoring, RefactoringSaveHelper.SAVE_JAVA_ONLY_UPDATES);
415     }
416
417     public static void startPushDownRefactoring(final IMember[] members, final Shell shell) throws JavaModelException {
418         if (!RefactoringAvailabilityTester.isPushDownAvailable(members))
419             return;
420         final PushDownRefactoring refactoring= new PushDownRefactoring(new PushDownRefactoringProcessor(members));
421         new RefactoringStarter().activate(refactoring, new PushDownWizard(refactoring), shell, RefactoringMessages.OpenRefactoringWizardAction_refactoring, RefactoringSaveHelper.SAVE_JAVA_ONLY_UPDATES);
422     }
423
424     public static void startRenameRefactoring(final IJavaElement element, final Shell shell) throws CoreException {
425         final RenameSupport support= createRenameSupport(element, null, RenameSupport.UPDATE_REFERENCES);
426         if (support != null && support.preCheck().isOK())
427             support.openDialog(shell);
428     }
429
430     public static void startRenameResourceRefactoring(final IResource resource, final Shell shell) throws CoreException {
431         final JavaRenameRefactoring refactoring= new JavaRenameRefactoring(new RenameResourceProcessor(resource));
432         RenameUserInterfaceManager.getDefault().getStarter(refactoring).activate(refactoring, shell, RefactoringSaveHelper.SAVE_ALL);
433     }
434
435     public static void startReplaceInvocationsRefactoring(final ITypeRoot typeRoot, final int offset, final int length, final Shell shell) throws JavaModelException {
436         final ReplaceInvocationsRefactoring refactoring= new ReplaceInvocationsRefactoring(typeRoot, offset, length);
437         new RefactoringStarter().activate(refactoring, new ReplaceInvocationsWizard(refactoring), shell, RefactoringMessages.ReplaceInvocationsAction_dialog_title, RefactoringSaveHelper.SAVE_JAVA_ONLY_UPDATES);
438     }
439
440     public static void startReplaceInvocationsRefactoring(final IMethod method, final Shell shell) throws JavaModelException {
441         final ReplaceInvocationsRefactoring refactoring= new ReplaceInvocationsRefactoring(method);
442         new RefactoringStarter().activate(refactoring, new ReplaceInvocationsWizard(refactoring), shell, RefactoringMessages.ReplaceInvocationsAction_dialog_title, RefactoringSaveHelper.SAVE_JAVA_ONLY_UPDATES);
443     }
444
445     public static void startSelfEncapsulateRefactoring(final IField field, final Shell shell) {
446         try {
447             if (!RefactoringAvailabilityTester.isSelfEncapsulateAvailable(field))
448                 return;
449             final SelfEncapsulateFieldRefactoring refactoring= new SelfEncapsulateFieldRefactoring(field);
450             new RefactoringStarter().activate(refactoring, new SelfEncapsulateFieldWizard(refactoring), shell, "", RefactoringSaveHelper.SAVE_JAVA_ONLY_UPDATES); //$NON-NLS-1$
451
} catch (JavaModelException e) {
452             ExceptionHandler.handle(e, ActionMessages.SelfEncapsulateFieldAction_dialog_title, ActionMessages.SelfEncapsulateFieldAction_dialog_cannot_perform);
453         }
454     }
455
456     public static void startUseSupertypeRefactoring(final IType type, final Shell shell) throws JavaModelException {
457         final UseSuperTypeRefactoring refactoring= new UseSuperTypeRefactoring(new UseSuperTypeProcessor(type));
458         new RefactoringStarter().activate(refactoring, new UseSupertypeWizard(refactoring), shell, RefactoringMessages.OpenRefactoringWizardAction_refactoring, RefactoringSaveHelper.SAVE_JAVA_ONLY_UPDATES);
459     }
460
461     private RefactoringExecutionStarter() {
462         // Not for instantiation
463
}
464     
465     public static void startIntroduceParameterObject(ICompilationUnit unit, int offset, int length, Shell shell) throws CoreException {
466         IJavaElement javaElement= unit.getElementAt(offset);
467         if (javaElement instanceof IMethod) {
468             IMethod method= (IMethod) javaElement;
469             startIntroduceParameterObject(method, shell);
470         }
471     }
472
473     public static void startIntroduceParameterObject(IMethod method, Shell shell) throws CoreException {
474         RefactoringStatus availability= Checks.checkAvailability(method);
475         if (availability.hasError()){
476             MessageDialog.openError(shell, RefactoringMessages.RefactoringExecutionStarter_IntroduceParameterObject_problem_title, RefactoringMessages.RefactoringExecutionStarter_IntroduceParameterObject_problem_description);
477             return;
478         }
479         IntroduceParameterObjectRefactoring refactoring= new IntroduceParameterObjectRefactoring(method);
480         final RefactoringStatus status= refactoring.checkInitialConditions(new NullProgressMonitor());
481         if (status.hasFatalError()) {
482             final RefactoringStatusEntry entry= status.getEntryMatchingSeverity(RefactoringStatus.FATAL);
483             if (entry.getCode() == RefactoringStatusCodes.OVERRIDES_ANOTHER_METHOD || entry.getCode() == RefactoringStatusCodes.METHOD_DECLARED_IN_INTERFACE) {
484
485                 String JavaDoc message= entry.getMessage();
486                 final Object JavaDoc element= entry.getData();
487                 IMethod superMethod= (IMethod) element;
488                 availability= Checks.checkAvailability(superMethod);
489                 if (availability.hasError()){
490                     MessageDialog.openError(shell, RefactoringMessages.RefactoringExecutionStarter_IntroduceParameterObject_problem_title, RefactoringMessages.RefactoringExecutionStarter_IntroduceParameterObject_problem_description);
491                     return;
492                 }
493                 message= message + RefactoringMessages.RefactoringErrorDialogUtil_okToPerformQuestion;
494                 if (element != null && MessageDialog.openQuestion(shell, RefactoringMessages.OpenRefactoringWizardAction_refactoring, message)) {
495                     refactoring=new IntroduceParameterObjectRefactoring(superMethod);
496                 }
497                 else refactoring=null;
498             }
499         }
500         if (refactoring!=null)
501             new RefactoringStarter().activate(refactoring, new IntroduceParameterObjectWizard(refactoring), shell, RefactoringMessages.OpenRefactoringWizardAction_refactoring, RefactoringSaveHelper.SAVE_JAVA_ONLY_UPDATES);
502     }
503 }
504
Popular Tags