001: /*******************************************************************************
002: * Copyright (c) 2005, 2007 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.jdt.internal.corext.refactoring;
011:
012: import java.lang.reflect.InvocationTargetException;
013: import java.util.Arrays;
014: import java.util.HashSet;
015: import java.util.Set;
016:
017: import org.eclipse.core.runtime.CoreException;
018: import org.eclipse.core.runtime.IStatus;
019: import org.eclipse.core.runtime.NullProgressMonitor;
020:
021: import org.eclipse.core.resources.IResource;
022:
023: import org.eclipse.swt.widgets.Shell;
024:
025: import org.eclipse.jface.dialogs.MessageDialog;
026: import org.eclipse.jface.dialogs.ProgressMonitorDialog;
027: import org.eclipse.jface.operation.IRunnableContext;
028: import org.eclipse.jface.viewers.IStructuredSelection;
029: import org.eclipse.jface.viewers.StructuredSelection;
030:
031: import org.eclipse.jface.text.ITextSelection;
032:
033: import org.eclipse.ui.PlatformUI;
034:
035: import org.eclipse.ltk.core.refactoring.Refactoring;
036: import org.eclipse.ltk.core.refactoring.RefactoringCore;
037: import org.eclipse.ltk.core.refactoring.RefactoringStatus;
038: import org.eclipse.ltk.core.refactoring.RefactoringStatusEntry;
039: import org.eclipse.ltk.core.refactoring.participants.CopyRefactoring;
040: import org.eclipse.ltk.core.refactoring.participants.DeleteRefactoring;
041: import org.eclipse.ltk.ui.refactoring.RefactoringWizard;
042:
043: import org.eclipse.jdt.core.IClassFile;
044: import org.eclipse.jdt.core.ICompilationUnit;
045: import org.eclipse.jdt.core.IField;
046: import org.eclipse.jdt.core.IJavaElement;
047: import org.eclipse.jdt.core.IJavaProject;
048: import org.eclipse.jdt.core.ILocalVariable;
049: import org.eclipse.jdt.core.IMember;
050: import org.eclipse.jdt.core.IMethod;
051: import org.eclipse.jdt.core.IPackageFragment;
052: import org.eclipse.jdt.core.IPackageFragmentRoot;
053: import org.eclipse.jdt.core.IType;
054: import org.eclipse.jdt.core.ITypeParameter;
055: import org.eclipse.jdt.core.ITypeRoot;
056: import org.eclipse.jdt.core.JavaModelException;
057: import org.eclipse.jdt.core.dom.CompilationUnit;
058: import org.eclipse.jdt.core.refactoring.descriptors.ExtractClassDescriptor;
059: import org.eclipse.jdt.core.refactoring.descriptors.IntroduceParameterObjectDescriptor;
060:
061: import org.eclipse.jdt.internal.corext.fix.CleanUpRefactoring;
062: import org.eclipse.jdt.internal.corext.refactoring.base.RefactoringStatusCodes;
063: import org.eclipse.jdt.internal.corext.refactoring.code.ConvertAnonymousToNestedRefactoring;
064: import org.eclipse.jdt.internal.corext.refactoring.code.InlineConstantRefactoring;
065: import org.eclipse.jdt.internal.corext.refactoring.code.InlineMethodRefactoring;
066: import org.eclipse.jdt.internal.corext.refactoring.code.InlineTempRefactoring;
067: import org.eclipse.jdt.internal.corext.refactoring.code.IntroduceFactoryRefactoring;
068: import org.eclipse.jdt.internal.corext.refactoring.code.IntroduceIndirectionRefactoring;
069: import org.eclipse.jdt.internal.corext.refactoring.code.IntroduceParameterRefactoring;
070: import org.eclipse.jdt.internal.corext.refactoring.code.ReplaceInvocationsRefactoring;
071: import org.eclipse.jdt.internal.corext.refactoring.generics.InferTypeArgumentsRefactoring;
072: import org.eclipse.jdt.internal.corext.refactoring.rename.JavaRenameRefactoring;
073: import org.eclipse.jdt.internal.corext.refactoring.rename.RenameResourceProcessor;
074: import org.eclipse.jdt.internal.corext.refactoring.reorg.JavaCopyProcessor;
075: import org.eclipse.jdt.internal.corext.refactoring.reorg.JavaCopyRefactoring;
076: import org.eclipse.jdt.internal.corext.refactoring.reorg.JavaDeleteProcessor;
077: import org.eclipse.jdt.internal.corext.refactoring.reorg.JavaDeleteRefactoring;
078: import org.eclipse.jdt.internal.corext.refactoring.reorg.JavaMoveProcessor;
079: import org.eclipse.jdt.internal.corext.refactoring.reorg.ReorgPolicyFactory;
080: import org.eclipse.jdt.internal.corext.refactoring.reorg.IReorgPolicy.ICopyPolicy;
081: import org.eclipse.jdt.internal.corext.refactoring.reorg.IReorgPolicy.IMovePolicy;
082: import org.eclipse.jdt.internal.corext.refactoring.sef.SelfEncapsulateFieldRefactoring;
083: import org.eclipse.jdt.internal.corext.refactoring.structure.ChangeSignatureRefactoring;
084: import org.eclipse.jdt.internal.corext.refactoring.structure.ChangeTypeRefactoring;
085: import org.eclipse.jdt.internal.corext.refactoring.structure.ExtractClassRefactoring;
086: import org.eclipse.jdt.internal.corext.refactoring.structure.ExtractInterfaceProcessor;
087: import org.eclipse.jdt.internal.corext.refactoring.structure.ExtractInterfaceRefactoring;
088: import org.eclipse.jdt.internal.corext.refactoring.structure.ExtractSupertypeProcessor;
089: import org.eclipse.jdt.internal.corext.refactoring.structure.ExtractSupertypeRefactoring;
090: import org.eclipse.jdt.internal.corext.refactoring.structure.IntroduceParameterObjectRefactoring;
091: import org.eclipse.jdt.internal.corext.refactoring.structure.JavaMoveRefactoring;
092: import org.eclipse.jdt.internal.corext.refactoring.structure.MoveInnerToTopRefactoring;
093: import org.eclipse.jdt.internal.corext.refactoring.structure.MoveInstanceMethodProcessor;
094: import org.eclipse.jdt.internal.corext.refactoring.structure.MoveInstanceMethodRefactoring;
095: import org.eclipse.jdt.internal.corext.refactoring.structure.MoveStaticMembersProcessor;
096: import org.eclipse.jdt.internal.corext.refactoring.structure.PullUpRefactoring;
097: import org.eclipse.jdt.internal.corext.refactoring.structure.PullUpRefactoringProcessor;
098: import org.eclipse.jdt.internal.corext.refactoring.structure.PushDownRefactoring;
099: import org.eclipse.jdt.internal.corext.refactoring.structure.PushDownRefactoringProcessor;
100: import org.eclipse.jdt.internal.corext.refactoring.structure.UseSuperTypeProcessor;
101: import org.eclipse.jdt.internal.corext.refactoring.structure.UseSuperTypeRefactoring;
102:
103: import org.eclipse.jdt.ui.actions.SelectionDispatchAction;
104: import org.eclipse.jdt.ui.refactoring.RenameSupport;
105:
106: import org.eclipse.jdt.internal.ui.actions.ActionMessages;
107: import org.eclipse.jdt.internal.ui.fix.CleanUpRefactoringWizard;
108: import org.eclipse.jdt.internal.ui.fix.ICleanUp;
109: import org.eclipse.jdt.internal.ui.preferences.JavaPreferencesSettings;
110: import org.eclipse.jdt.internal.ui.refactoring.ChangeSignatureWizard;
111: import org.eclipse.jdt.internal.ui.refactoring.ChangeTypeWizard;
112: import org.eclipse.jdt.internal.ui.refactoring.ConvertAnonymousToNestedWizard;
113: import org.eclipse.jdt.internal.ui.refactoring.ExtractClassWizard;
114: import org.eclipse.jdt.internal.ui.refactoring.ExtractInterfaceWizard;
115: import org.eclipse.jdt.internal.ui.refactoring.ExtractSupertypeWizard;
116: import org.eclipse.jdt.internal.ui.refactoring.InferTypeArgumentsWizard;
117: import org.eclipse.jdt.internal.ui.refactoring.InlineConstantWizard;
118: import org.eclipse.jdt.internal.ui.refactoring.InlineTempWizard;
119: import org.eclipse.jdt.internal.ui.refactoring.IntroduceFactoryWizard;
120: import org.eclipse.jdt.internal.ui.refactoring.IntroduceIndirectionWizard;
121: import org.eclipse.jdt.internal.ui.refactoring.IntroduceParameterObjectWizard;
122: import org.eclipse.jdt.internal.ui.refactoring.IntroduceParameterWizard;
123: import org.eclipse.jdt.internal.ui.refactoring.MoveInnerToTopWizard;
124: import org.eclipse.jdt.internal.ui.refactoring.MoveInstanceMethodWizard;
125: import org.eclipse.jdt.internal.ui.refactoring.MoveMembersWizard;
126: import org.eclipse.jdt.internal.ui.refactoring.PullUpWizard;
127: import org.eclipse.jdt.internal.ui.refactoring.PushDownWizard;
128: import org.eclipse.jdt.internal.ui.refactoring.RefactoringExecutionHelper;
129: import org.eclipse.jdt.internal.ui.refactoring.RefactoringMessages;
130: import org.eclipse.jdt.internal.ui.refactoring.RefactoringSaveHelper;
131: import org.eclipse.jdt.internal.ui.refactoring.UseSupertypeWizard;
132: import org.eclipse.jdt.internal.ui.refactoring.UserInterfaceStarter;
133: import org.eclipse.jdt.internal.ui.refactoring.actions.RefactoringStarter;
134: import org.eclipse.jdt.internal.ui.refactoring.code.InlineMethodWizard;
135: import org.eclipse.jdt.internal.ui.refactoring.code.ReplaceInvocationsWizard;
136: import org.eclipse.jdt.internal.ui.refactoring.reorg.CreateTargetQueries;
137: import org.eclipse.jdt.internal.ui.refactoring.reorg.DeleteUserInterfaceManager;
138: import org.eclipse.jdt.internal.ui.refactoring.reorg.NewNameQueries;
139: import org.eclipse.jdt.internal.ui.refactoring.reorg.RenameUserInterfaceManager;
140: import org.eclipse.jdt.internal.ui.refactoring.reorg.ReorgCopyWizard;
141: import org.eclipse.jdt.internal.ui.refactoring.reorg.ReorgMoveWizard;
142: import org.eclipse.jdt.internal.ui.refactoring.reorg.ReorgQueries;
143: import org.eclipse.jdt.internal.ui.refactoring.sef.SelfEncapsulateFieldWizard;
144: import org.eclipse.jdt.internal.ui.util.ExceptionHandler;
145:
146: /**
147: * Helper class to run refactorings from action code.
148: * <p>
149: * This class has been introduced to decouple actions from the refactoring code,
150: * in order not to eagerly load refactoring classes during action
151: * initialization.
152: * </p>
153: *
154: * @since 3.1
155: */
156: public final class RefactoringExecutionStarter {
157:
158: private static RenameSupport createRenameSupport(
159: IJavaElement element, String newName, int flags)
160: throws CoreException {
161: switch (element.getElementType()) {
162: case IJavaElement.JAVA_PROJECT:
163: return RenameSupport.create((IJavaProject) element,
164: newName, flags);
165: case IJavaElement.PACKAGE_FRAGMENT_ROOT:
166: return RenameSupport.create((IPackageFragmentRoot) element,
167: newName);
168: case IJavaElement.PACKAGE_FRAGMENT:
169: return RenameSupport.create((IPackageFragment) element,
170: newName, flags);
171: case IJavaElement.COMPILATION_UNIT:
172: return RenameSupport.create((ICompilationUnit) element,
173: newName, flags);
174: case IJavaElement.TYPE:
175: return RenameSupport
176: .create((IType) element, newName, flags);
177: case IJavaElement.METHOD:
178: final IMethod method = (IMethod) element;
179: if (method.isConstructor())
180: return createRenameSupport(method.getDeclaringType(),
181: newName, flags);
182: else
183: return RenameSupport.create((IMethod) element, newName,
184: flags);
185: case IJavaElement.FIELD:
186: return RenameSupport.create((IField) element, newName,
187: flags);
188: case IJavaElement.TYPE_PARAMETER:
189: return RenameSupport.create((ITypeParameter) element,
190: newName, flags);
191: case IJavaElement.LOCAL_VARIABLE:
192: return RenameSupport.create((ILocalVariable) element,
193: newName, flags);
194: }
195: return null;
196: }
197:
198: public static void startChangeSignatureRefactoring(
199: final IMethod method, final SelectionDispatchAction action,
200: final Shell shell) throws JavaModelException {
201: if (!RefactoringAvailabilityTester
202: .isChangeSignatureAvailable(method))
203: return;
204: final ChangeSignatureRefactoring refactoring = new ChangeSignatureRefactoring(
205: method);
206: final UserInterfaceStarter starter = new UserInterfaceStarter() {
207:
208: public final boolean activate(final Refactoring ref,
209: final Shell parent, final int saveMode)
210: throws CoreException {
211: final RefactoringStatus status = ref
212: .checkInitialConditions(new NullProgressMonitor());
213: if (status.hasFatalError()) {
214: final RefactoringStatusEntry entry = status
215: .getEntryMatchingSeverity(RefactoringStatus.FATAL);
216: if (entry.getCode() == RefactoringStatusCodes.OVERRIDES_ANOTHER_METHOD
217: || entry.getCode() == RefactoringStatusCodes.METHOD_DECLARED_IN_INTERFACE) {
218:
219: String message = entry.getMessage();
220: final Object element = entry.getData();
221: message = message
222: + RefactoringMessages.RefactoringErrorDialogUtil_okToPerformQuestion;
223: if (element != null
224: && MessageDialog
225: .openQuestion(
226: shell,
227: RefactoringMessages.OpenRefactoringWizardAction_refactoring,
228: message)) {
229:
230: final IStructuredSelection selection = new StructuredSelection(
231: element);
232: // TODO: should not hijack this
233: // ModifiyParametersAction.
234: // The action is set up on an editor, but we use it
235: // as if it were set up on a ViewPart.
236: boolean wasEnabled = action.isEnabled();
237: action.selectionChanged(selection);
238: if (action.isEnabled()) {
239: action.run(selection);
240: } else {
241: MessageDialog
242: .openInformation(
243: shell,
244: ActionMessages.ModifyParameterAction_problem_title,
245: ActionMessages.ModifyParameterAction_problem_message);
246: }
247: action.setEnabled(wasEnabled);
248: }
249: return false;
250: }
251: }
252: return super .activate(ref, parent, saveMode);
253: }
254: };
255: starter.initialize(new ChangeSignatureWizard(refactoring));
256: try {
257: starter.activate(refactoring, shell,
258: RefactoringSaveHelper.SAVE_JAVA_ONLY_UPDATES);
259: } catch (CoreException e) {
260: ExceptionHandler
261: .handle(
262: e,
263: RefactoringMessages.OpenRefactoringWizardAction_refactoring,
264: RefactoringMessages.RefactoringStarter_unexpected_exception);
265: }
266: }
267:
268: public static void startChangeTypeRefactoring(
269: final ICompilationUnit unit, final Shell shell,
270: final int offset, final int length)
271: throws JavaModelException {
272: final ChangeTypeRefactoring refactoring = new ChangeTypeRefactoring(
273: unit, offset, length);
274: new RefactoringStarter().activate(refactoring,
275: new ChangeTypeWizard(refactoring), shell,
276: RefactoringMessages.ChangeTypeAction_dialog_title,
277: RefactoringSaveHelper.SAVE_JAVA_ONLY_UPDATES);
278: }
279:
280: public static void startCleanupRefactoring(ICompilationUnit[] cus,
281: ICleanUp[] cleanUps, Shell shell, boolean showWizard,
282: String actionName) throws InvocationTargetException,
283: JavaModelException {
284: final CleanUpRefactoring refactoring = new CleanUpRefactoring(
285: actionName);
286: for (int i = 0; i < cus.length; i++) {
287: refactoring.addCompilationUnit(cus[i]);
288: }
289:
290: if (!showWizard) {
291: for (int i = 0; i < cleanUps.length; i++) {
292: refactoring.addCleanUp(cleanUps[i]);
293: }
294:
295: IRunnableContext context;
296: if (refactoring.getCleanUpTargetsSize() > 1) {
297: context = new ProgressMonitorDialog(shell);
298: } else {
299: context = PlatformUI.getWorkbench()
300: .getActiveWorkbenchWindow();
301: }
302:
303: RefactoringExecutionHelper helper = new RefactoringExecutionHelper(
304: refactoring, IStatus.INFO,
305: RefactoringSaveHelper.SAVE_JAVA_ONLY_UPDATES,
306: shell, context);
307: try {
308: helper.perform(true, true);
309: } catch (InterruptedException e) {
310: }
311: } else {
312: CleanUpRefactoringWizard refactoringWizard = new CleanUpRefactoringWizard(
313: refactoring,
314: RefactoringWizard.WIZARD_BASED_USER_INTERFACE);
315: RefactoringStarter starter = new RefactoringStarter();
316: starter.activate(refactoring, refactoringWizard, shell,
317: actionName,
318: RefactoringSaveHelper.SAVE_JAVA_ONLY_UPDATES);
319: }
320: }
321:
322: public static void startConvertAnonymousRefactoring(
323: final ICompilationUnit unit, final int offset,
324: final int length, final Shell shell)
325: throws JavaModelException {
326: final ConvertAnonymousToNestedRefactoring refactoring = new ConvertAnonymousToNestedRefactoring(
327: unit, offset, length);
328: new RefactoringStarter()
329: .activate(
330: refactoring,
331: new ConvertAnonymousToNestedWizard(refactoring),
332: shell,
333: RefactoringMessages.ConvertAnonymousToNestedAction_dialog_title,
334: RefactoringSaveHelper.SAVE_JAVA_ONLY_UPDATES);
335: }
336:
337: public static void startCopyRefactoring(IResource[] resources,
338: IJavaElement[] javaElements, Shell shell)
339: throws JavaModelException {
340: ICopyPolicy copyPolicy = ReorgPolicyFactory.createCopyPolicy(
341: resources, javaElements);
342: if (copyPolicy.canEnable()) {
343: JavaCopyProcessor processor = new JavaCopyProcessor(
344: copyPolicy);
345: CopyRefactoring refactoring = new JavaCopyRefactoring(
346: processor);
347: RefactoringWizard wizard = new ReorgCopyWizard(refactoring);
348: processor.setNewNameQueries(new NewNameQueries(wizard));
349: processor.setReorgQueries(new ReorgQueries(wizard));
350: new RefactoringStarter()
351: .activate(
352: refactoring,
353: wizard,
354: shell,
355: RefactoringMessages.OpenRefactoringWizardAction_refactoring,
356: processor.getSaveMode());
357: }
358: }
359:
360: public static void startCutRefactoring(final Object[] elements,
361: final Shell shell) throws CoreException,
362: InterruptedException, InvocationTargetException {
363: final JavaDeleteProcessor processor = new JavaDeleteProcessor(
364: elements);
365: processor.setSuggestGetterSetterDeletion(false);
366: processor.setQueries(new ReorgQueries(shell));
367: new RefactoringExecutionHelper(new JavaDeleteRefactoring(
368: processor), RefactoringCore
369: .getConditionCheckingFailedSeverity(),
370: RefactoringSaveHelper.SAVE_NOTHING, shell,
371: new ProgressMonitorDialog(shell)).perform(false, false);
372: }
373:
374: public static void startDeleteRefactoring(final Object[] elements,
375: final Shell shell) throws CoreException {
376: final DeleteRefactoring refactoring = new JavaDeleteRefactoring(
377: new JavaDeleteProcessor(elements));
378: DeleteUserInterfaceManager.getDefault().getStarter(refactoring)
379: .activate(refactoring, shell,
380: RefactoringSaveHelper.SAVE_NOTHING);
381: }
382:
383: public static void startExtractInterfaceRefactoring(
384: final IType type, final Shell shell)
385: throws JavaModelException {
386: final ExtractInterfaceRefactoring refactoring = new ExtractInterfaceRefactoring(
387: new ExtractInterfaceProcessor(type,
388: JavaPreferencesSettings
389: .getCodeGenerationSettings(type
390: .getJavaProject())));
391: new RefactoringStarter()
392: .activate(
393: refactoring,
394: new ExtractInterfaceWizard(refactoring),
395: shell,
396: RefactoringMessages.OpenRefactoringWizardAction_refactoring,
397: RefactoringSaveHelper.SAVE_JAVA_ONLY_UPDATES);
398: }
399:
400: public static void startExtractSupertypeRefactoring(
401: final IMember[] members, final Shell shell)
402: throws JavaModelException {
403: if (!RefactoringAvailabilityTester
404: .isExtractSupertypeAvailable(members))
405: return;
406: IJavaProject project = null;
407: if (members != null && members.length > 0)
408: project = members[0].getJavaProject();
409: final ExtractSupertypeRefactoring refactoring = new ExtractSupertypeRefactoring(
410: new ExtractSupertypeProcessor(members,
411: JavaPreferencesSettings
412: .getCodeGenerationSettings(project)));
413: new RefactoringStarter()
414: .activate(
415: refactoring,
416: new ExtractSupertypeWizard(refactoring),
417: shell,
418: RefactoringMessages.OpenRefactoringWizardAction_refactoring,
419: RefactoringSaveHelper.SAVE_JAVA_ONLY_UPDATES);
420: }
421:
422: public static void startInferTypeArgumentsRefactoring(
423: final IJavaElement[] elements, final Shell shell) {
424: try {
425: if (!RefactoringAvailabilityTester
426: .isInferTypeArgumentsAvailable(elements))
427: return;
428: final InferTypeArgumentsRefactoring refactoring = new InferTypeArgumentsRefactoring(
429: elements);
430: new RefactoringStarter()
431: .activate(
432: refactoring,
433: new InferTypeArgumentsWizard(refactoring),
434: shell,
435: RefactoringMessages.InferTypeArgumentsAction_dialog_title,
436: RefactoringSaveHelper.SAVE_JAVA_ONLY_UPDATES);
437: } catch (CoreException e) {
438: ExceptionHandler
439: .handle(
440: e,
441: RefactoringMessages.InferTypeArgumentsAction_dialog_title,
442: RefactoringMessages.OpenRefactoringWizardAction_exception);
443: }
444: }
445:
446: public static boolean startInlineConstantRefactoring(
447: final ICompilationUnit unit, final CompilationUnit node,
448: final int offset, final int length, final Shell shell)
449: throws JavaModelException {
450: final InlineConstantRefactoring refactoring = new InlineConstantRefactoring(
451: unit, node, offset, length);
452: if (!refactoring.checkStaticFinalConstantNameSelected()
453: .hasFatalError()) {
454: new RefactoringStarter()
455: .activate(
456: refactoring,
457: new InlineConstantWizard(refactoring),
458: shell,
459: RefactoringMessages.InlineConstantAction_dialog_title,
460: RefactoringSaveHelper.SAVE_JAVA_ONLY_UPDATES);
461: return true;
462: }
463: return false;
464: }
465:
466: public static boolean startInlineMethodRefactoring(
467: final ITypeRoot typeRoot, final CompilationUnit node,
468: final int offset, final int length, final Shell shell)
469: throws JavaModelException {
470: final InlineMethodRefactoring refactoring = InlineMethodRefactoring
471: .create(typeRoot, node, offset, length);
472: if (refactoring != null) {
473: new RefactoringStarter()
474: .activate(
475: refactoring,
476: new InlineMethodWizard(refactoring),
477: shell,
478: RefactoringMessages.InlineMethodAction_dialog_title,
479: RefactoringSaveHelper.SAVE_JAVA_ONLY_UPDATES);
480: return true;
481: }
482: return false;
483: }
484:
485: public static boolean startInlineTempRefactoring(
486: final ICompilationUnit unit, CompilationUnit node,
487: final ITextSelection selection, final Shell shell)
488: throws JavaModelException {
489: final InlineTempRefactoring refactoring = new InlineTempRefactoring(
490: unit, node, selection.getOffset(), selection
491: .getLength());
492: if (!refactoring.checkIfTempSelected().hasFatalError()) {
493: new RefactoringStarter().activate(refactoring,
494: new InlineTempWizard(refactoring), shell,
495: RefactoringMessages.InlineTempAction_inline_temp,
496: RefactoringSaveHelper.SAVE_NOTHING);
497: return true;
498: }
499: return false;
500: }
501:
502: public static void startIntroduceFactoryRefactoring(
503: final ICompilationUnit unit,
504: final ITextSelection selection, final Shell shell)
505: throws JavaModelException {
506: final IntroduceFactoryRefactoring refactoring = new IntroduceFactoryRefactoring(
507: unit, selection.getOffset(), selection.getLength());
508: new RefactoringStarter()
509: .activate(
510: refactoring,
511: new IntroduceFactoryWizard(
512: refactoring,
513: RefactoringMessages.IntroduceFactoryAction_use_factory),
514: shell,
515: RefactoringMessages.IntroduceFactoryAction_dialog_title,
516: RefactoringSaveHelper.SAVE_JAVA_ONLY_UPDATES);
517: }
518:
519: public static void startIntroduceIndirectionRefactoring(
520: final IClassFile file, final int offset, final int length,
521: final Shell shell) throws JavaModelException {
522: final IntroduceIndirectionRefactoring refactoring = new IntroduceIndirectionRefactoring(
523: file, offset, length);
524: new RefactoringStarter()
525: .activate(
526: refactoring,
527: new IntroduceIndirectionWizard(
528: refactoring,
529: RefactoringMessages.IntroduceIndirectionAction_dialog_title),
530: shell,
531: RefactoringMessages.IntroduceIndirectionAction_dialog_title,
532: RefactoringSaveHelper.SAVE_JAVA_ONLY_UPDATES);
533: }
534:
535: public static void startIntroduceIndirectionRefactoring(
536: final ICompilationUnit unit, final int offset,
537: final int length, final Shell shell)
538: throws JavaModelException {
539: final IntroduceIndirectionRefactoring refactoring = new IntroduceIndirectionRefactoring(
540: unit, offset, length);
541: new RefactoringStarter()
542: .activate(
543: refactoring,
544: new IntroduceIndirectionWizard(
545: refactoring,
546: RefactoringMessages.IntroduceIndirectionAction_dialog_title),
547: shell,
548: RefactoringMessages.IntroduceIndirectionAction_dialog_title,
549: RefactoringSaveHelper.SAVE_JAVA_ONLY_UPDATES);
550: }
551:
552: public static void startIntroduceIndirectionRefactoring(
553: final IMethod method, final Shell shell)
554: throws JavaModelException {
555: final IntroduceIndirectionRefactoring refactoring = new IntroduceIndirectionRefactoring(
556: method);
557: new RefactoringStarter()
558: .activate(
559: refactoring,
560: new IntroduceIndirectionWizard(
561: refactoring,
562: RefactoringMessages.IntroduceIndirectionAction_dialog_title),
563: shell,
564: RefactoringMessages.IntroduceIndirectionAction_dialog_title,
565: RefactoringSaveHelper.SAVE_JAVA_ONLY_UPDATES);
566: }
567:
568: public static void startIntroduceParameter(ICompilationUnit unit,
569: int offset, int length, Shell shell)
570: throws JavaModelException {
571: final IntroduceParameterRefactoring refactoring = new IntroduceParameterRefactoring(
572: unit, offset, length);
573: new RefactoringStarter()
574: .activate(
575: refactoring,
576: new IntroduceParameterWizard(refactoring),
577: shell,
578: RefactoringMessages.IntroduceParameterAction_dialog_title,
579: RefactoringSaveHelper.SAVE_JAVA_ONLY_UPDATES);
580: }
581:
582: public static void startMoveInnerRefactoring(final IType type,
583: final Shell shell) throws JavaModelException {
584: if (!RefactoringAvailabilityTester.isMoveInnerAvailable(type))
585: return;
586: final MoveInnerToTopRefactoring refactoring = new MoveInnerToTopRefactoring(
587: type, JavaPreferencesSettings
588: .getCodeGenerationSettings(type
589: .getJavaProject()));
590: new RefactoringStarter()
591: .activate(
592: refactoring,
593: new MoveInnerToTopWizard(refactoring),
594: shell,
595: RefactoringMessages.OpenRefactoringWizardAction_refactoring,
596: RefactoringSaveHelper.SAVE_JAVA_ONLY_UPDATES);
597: }
598:
599: public static void startMoveMethodRefactoring(final IMethod method,
600: final Shell shell) throws JavaModelException {
601: final MoveInstanceMethodRefactoring refactoring = new MoveInstanceMethodRefactoring(
602: new MoveInstanceMethodProcessor(method,
603: JavaPreferencesSettings
604: .getCodeGenerationSettings(method
605: .getJavaProject())));
606: new RefactoringStarter()
607: .activate(
608: refactoring,
609: new MoveInstanceMethodWizard(refactoring),
610: shell,
611: RefactoringMessages.MoveInstanceMethodAction_dialog_title,
612: RefactoringSaveHelper.SAVE_JAVA_ONLY_UPDATES);
613: }
614:
615: public static void startMoveRefactoring(
616: final IResource[] resources, final IJavaElement[] elements,
617: final Shell shell) throws JavaModelException {
618: IMovePolicy policy = ReorgPolicyFactory.createMovePolicy(
619: resources, elements);
620: if (policy.canEnable()) {
621: final JavaMoveProcessor processor = new JavaMoveProcessor(
622: policy);
623: final JavaMoveRefactoring refactoring = new JavaMoveRefactoring(
624: processor);
625: final RefactoringWizard wizard = new ReorgMoveWizard(
626: refactoring);
627: processor.setCreateTargetQueries(new CreateTargetQueries(
628: wizard));
629: processor.setReorgQueries(new ReorgQueries(wizard));
630: new RefactoringStarter()
631: .activate(
632: refactoring,
633: wizard,
634: shell,
635: RefactoringMessages.OpenRefactoringWizardAction_refactoring,
636: processor.getSaveMode());
637: }
638: }
639:
640: public static void startMoveStaticMembersRefactoring(
641: final IMember[] members, final Shell shell)
642: throws JavaModelException {
643: if (!RefactoringAvailabilityTester
644: .isMoveStaticAvailable(members))
645: return;
646: final Set set = new HashSet();
647: set.addAll(Arrays.asList(members));
648: final IMember[] elements = (IMember[]) set
649: .toArray(new IMember[set.size()]);
650: IJavaProject project = null;
651: if (elements.length > 0)
652: project = elements[0].getJavaProject();
653: final JavaMoveRefactoring refactoring = new JavaMoveRefactoring(
654: new MoveStaticMembersProcessor(elements,
655: JavaPreferencesSettings
656: .getCodeGenerationSettings(project)));
657: new RefactoringStarter()
658: .activate(
659: refactoring,
660: new MoveMembersWizard(refactoring),
661: shell,
662: RefactoringMessages.OpenRefactoringWizardAction_refactoring,
663: RefactoringSaveHelper.SAVE_NON_JAVA_UPDATES);
664: }
665:
666: public static void startPullUpRefactoring(final IMember[] members,
667: final Shell shell) throws JavaModelException {
668: if (!RefactoringAvailabilityTester.isPullUpAvailable(members))
669: return;
670: IJavaProject project = null;
671: if (members != null && members.length > 0)
672: project = members[0].getJavaProject();
673: final PullUpRefactoring refactoring = new PullUpRefactoring(
674: new PullUpRefactoringProcessor(members,
675: JavaPreferencesSettings
676: .getCodeGenerationSettings(project)));
677: new RefactoringStarter()
678: .activate(
679: refactoring,
680: new PullUpWizard(refactoring),
681: shell,
682: RefactoringMessages.OpenRefactoringWizardAction_refactoring,
683: RefactoringSaveHelper.SAVE_JAVA_ONLY_UPDATES);
684: }
685:
686: public static void startPushDownRefactoring(
687: final IMember[] members, final Shell shell)
688: throws JavaModelException {
689: if (!RefactoringAvailabilityTester.isPushDownAvailable(members))
690: return;
691: final PushDownRefactoring refactoring = new PushDownRefactoring(
692: new PushDownRefactoringProcessor(members));
693: new RefactoringStarter()
694: .activate(
695: refactoring,
696: new PushDownWizard(refactoring),
697: shell,
698: RefactoringMessages.OpenRefactoringWizardAction_refactoring,
699: RefactoringSaveHelper.SAVE_JAVA_ONLY_UPDATES);
700: }
701:
702: public static void startRenameRefactoring(
703: final IJavaElement element, final Shell shell)
704: throws CoreException {
705: final RenameSupport support = createRenameSupport(element,
706: null, RenameSupport.UPDATE_REFERENCES);
707: if (support != null && support.preCheck().isOK())
708: support.openDialog(shell);
709: }
710:
711: public static void startRenameResourceRefactoring(
712: final IResource resource, final Shell shell)
713: throws CoreException {
714: final JavaRenameRefactoring refactoring = new JavaRenameRefactoring(
715: new RenameResourceProcessor(resource));
716: RenameUserInterfaceManager.getDefault().getStarter(refactoring)
717: .activate(refactoring, shell,
718: RefactoringSaveHelper.SAVE_ALL);
719: }
720:
721: public static void startReplaceInvocationsRefactoring(
722: final ITypeRoot typeRoot, final int offset,
723: final int length, final Shell shell)
724: throws JavaModelException {
725: final ReplaceInvocationsRefactoring refactoring = new ReplaceInvocationsRefactoring(
726: typeRoot, offset, length);
727: new RefactoringStarter()
728: .activate(
729: refactoring,
730: new ReplaceInvocationsWizard(refactoring),
731: shell,
732: RefactoringMessages.ReplaceInvocationsAction_dialog_title,
733: RefactoringSaveHelper.SAVE_JAVA_ONLY_UPDATES);
734: }
735:
736: public static void startReplaceInvocationsRefactoring(
737: final IMethod method, final Shell shell)
738: throws JavaModelException {
739: final ReplaceInvocationsRefactoring refactoring = new ReplaceInvocationsRefactoring(
740: method);
741: new RefactoringStarter()
742: .activate(
743: refactoring,
744: new ReplaceInvocationsWizard(refactoring),
745: shell,
746: RefactoringMessages.ReplaceInvocationsAction_dialog_title,
747: RefactoringSaveHelper.SAVE_JAVA_ONLY_UPDATES);
748: }
749:
750: public static void startSelfEncapsulateRefactoring(
751: final IField field, final Shell shell) {
752: try {
753: if (!RefactoringAvailabilityTester
754: .isSelfEncapsulateAvailable(field))
755: return;
756: final SelfEncapsulateFieldRefactoring refactoring = new SelfEncapsulateFieldRefactoring(
757: field);
758: new RefactoringStarter().activate(refactoring,
759: new SelfEncapsulateFieldWizard(refactoring), shell,
760: "", RefactoringSaveHelper.SAVE_JAVA_ONLY_UPDATES); //$NON-NLS-1$
761: } catch (JavaModelException e) {
762: ExceptionHandler
763: .handle(
764: e,
765: ActionMessages.SelfEncapsulateFieldAction_dialog_title,
766: ActionMessages.SelfEncapsulateFieldAction_dialog_cannot_perform);
767: }
768: }
769:
770: public static void startUseSupertypeRefactoring(final IType type,
771: final Shell shell) throws JavaModelException {
772: final UseSuperTypeRefactoring refactoring = new UseSuperTypeRefactoring(
773: new UseSuperTypeProcessor(type));
774: new RefactoringStarter()
775: .activate(
776: refactoring,
777: new UseSupertypeWizard(refactoring),
778: shell,
779: RefactoringMessages.OpenRefactoringWizardAction_refactoring,
780: RefactoringSaveHelper.SAVE_JAVA_ONLY_UPDATES);
781: }
782:
783: private RefactoringExecutionStarter() {
784: // Not for instantiation
785: }
786:
787: public static void startIntroduceParameterObject(
788: ICompilationUnit unit, int offset, Shell shell)
789: throws CoreException {
790: IJavaElement javaElement = unit.getElementAt(offset);
791: if (javaElement instanceof IMethod) {
792: IMethod method = (IMethod) javaElement;
793: startIntroduceParameterObject(method, shell);
794: }
795: }
796:
797: public static void startIntroduceParameterObject(IMethod method,
798: Shell shell) throws CoreException {
799: RefactoringStatus availability = Checks
800: .checkAvailability(method);
801: if (availability.hasError()) {
802: MessageDialog
803: .openError(
804: shell,
805: RefactoringMessages.RefactoringExecutionStarter_IntroduceParameterObject_problem_title,
806: RefactoringMessages.RefactoringExecutionStarter_IntroduceParameterObject_problem_description);
807: return;
808: }
809: IntroduceParameterObjectDescriptor ipod = new IntroduceParameterObjectDescriptor();
810: ipod.setMethod(method);
811: IntroduceParameterObjectRefactoring refactoring = new IntroduceParameterObjectRefactoring(
812: ipod);
813: final RefactoringStatus status = refactoring
814: .checkInitialConditions(new NullProgressMonitor());
815: if (status.hasFatalError()) {
816: final RefactoringStatusEntry entry = status
817: .getEntryMatchingSeverity(RefactoringStatus.FATAL);
818: if (entry.getCode() == RefactoringStatusCodes.OVERRIDES_ANOTHER_METHOD
819: || entry.getCode() == RefactoringStatusCodes.METHOD_DECLARED_IN_INTERFACE) {
820:
821: String message = entry.getMessage();
822: final Object element = entry.getData();
823: IMethod super Method = (IMethod) element;
824: availability = Checks.checkAvailability(super Method);
825: if (availability.hasError()) {
826: MessageDialog
827: .openError(
828: shell,
829: RefactoringMessages.RefactoringExecutionStarter_IntroduceParameterObject_problem_title,
830: RefactoringMessages.RefactoringExecutionStarter_IntroduceParameterObject_problem_description);
831: return;
832: }
833: message = message
834: + RefactoringMessages.RefactoringErrorDialogUtil_okToPerformQuestion;
835: if (element != null
836: && MessageDialog
837: .openQuestion(
838: shell,
839: RefactoringMessages.OpenRefactoringWizardAction_refactoring,
840: message)) {
841: ipod = new IntroduceParameterObjectDescriptor();
842: ipod.setMethod(super Method);
843: refactoring = new IntroduceParameterObjectRefactoring(
844: ipod);
845: } else
846: refactoring = null;
847: }
848: }
849: if (refactoring != null)
850: new RefactoringStarter()
851: .activate(
852: refactoring,
853: new IntroduceParameterObjectWizard(
854: refactoring),
855: shell,
856: RefactoringMessages.OpenRefactoringWizardAction_refactoring,
857: RefactoringSaveHelper.SAVE_JAVA_ONLY_UPDATES);
858: }
859:
860: public static void startExtractClassRefactoring(IType type,
861: Shell shell) throws CoreException {
862: ExtractClassDescriptor descriptor = new ExtractClassDescriptor();
863: descriptor.setType(type);
864: ExtractClassRefactoring refactoring = new ExtractClassRefactoring(
865: descriptor);
866: new RefactoringStarter()
867: .activate(
868: refactoring,
869: new ExtractClassWizard(descriptor, refactoring),
870: shell,
871: RefactoringMessages.OpenRefactoringWizardAction_refactoring,
872: RefactoringSaveHelper.SAVE_JAVA_ONLY_UPDATES);
873: }
874: }
|