KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > ui > refactoring > RenameSupport


1 /*******************************************************************************
2  * Copyright (c) 2000, 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.ui.refactoring;
12
13 import java.lang.reflect.InvocationTargetException JavaDoc;
14
15 import org.eclipse.core.runtime.CoreException;
16 import org.eclipse.core.runtime.IStatus;
17 import org.eclipse.core.runtime.Status;
18
19 import org.eclipse.swt.widgets.Shell;
20
21 import org.eclipse.jface.dialogs.MessageDialog;
22 import org.eclipse.jface.operation.IRunnableContext;
23
24 import org.eclipse.ltk.core.refactoring.RefactoringCore;
25 import org.eclipse.ltk.core.refactoring.RefactoringStatus;
26 import org.eclipse.ltk.core.refactoring.participants.RenameProcessor;
27 import org.eclipse.ltk.core.refactoring.participants.RenameRefactoring;
28
29 import org.eclipse.jdt.core.ICompilationUnit;
30 import org.eclipse.jdt.core.IField;
31 import org.eclipse.jdt.core.IJavaProject;
32 import org.eclipse.jdt.core.ILocalVariable;
33 import org.eclipse.jdt.core.IMethod;
34 import org.eclipse.jdt.core.IPackageFragment;
35 import org.eclipse.jdt.core.IPackageFragmentRoot;
36 import org.eclipse.jdt.core.IType;
37 import org.eclipse.jdt.core.ITypeParameter;
38 import org.eclipse.jdt.core.refactoring.descriptors.RenameJavaElementDescriptor;
39
40 import org.eclipse.jdt.internal.corext.refactoring.rename.JavaRenameProcessor;
41 import org.eclipse.jdt.internal.corext.refactoring.rename.JavaRenameRefactoring;
42 import org.eclipse.jdt.internal.corext.refactoring.rename.MethodChecks;
43 import org.eclipse.jdt.internal.corext.refactoring.rename.RenameCompilationUnitProcessor;
44 import org.eclipse.jdt.internal.corext.refactoring.rename.RenameEnumConstProcessor;
45 import org.eclipse.jdt.internal.corext.refactoring.rename.RenameFieldProcessor;
46 import org.eclipse.jdt.internal.corext.refactoring.rename.RenameJavaProjectProcessor;
47 import org.eclipse.jdt.internal.corext.refactoring.rename.RenameLocalVariableProcessor;
48 import org.eclipse.jdt.internal.corext.refactoring.rename.RenameNonVirtualMethodProcessor;
49 import org.eclipse.jdt.internal.corext.refactoring.rename.RenamePackageProcessor;
50 import org.eclipse.jdt.internal.corext.refactoring.rename.RenameSourceFolderProcessor;
51 import org.eclipse.jdt.internal.corext.refactoring.rename.RenameTypeParameterProcessor;
52 import org.eclipse.jdt.internal.corext.refactoring.rename.RenameTypeProcessor;
53 import org.eclipse.jdt.internal.corext.refactoring.rename.RenameVirtualMethodProcessor;
54 import org.eclipse.jdt.internal.corext.refactoring.tagging.INameUpdating;
55 import org.eclipse.jdt.internal.corext.refactoring.tagging.IReferenceUpdating;
56 import org.eclipse.jdt.internal.corext.refactoring.tagging.ITextUpdating;
57 import org.eclipse.jdt.internal.corext.util.JdtFlags;
58
59 import org.eclipse.jdt.internal.ui.JavaPlugin;
60 import org.eclipse.jdt.internal.ui.JavaUIMessages;
61 import org.eclipse.jdt.internal.ui.refactoring.RefactoringExecutionHelper;
62 import org.eclipse.jdt.internal.ui.refactoring.UserInterfaceStarter;
63 import org.eclipse.jdt.internal.ui.refactoring.reorg.RenameRefactoringWizard;
64 import org.eclipse.jdt.internal.ui.refactoring.reorg.RenameSelectionState;
65 import org.eclipse.jdt.internal.ui.refactoring.reorg.RenameUserInterfaceManager;
66 import org.eclipse.jdt.internal.ui.refactoring.reorg.RenameUserInterfaceStarter;
67
68 /**
69  * Central access point to execute rename refactorings.
70  * <p>
71  * Note: this class is not intended to be subclassed.
72  * </p>
73  * @since 2.1
74  */

75 public class RenameSupport {
76
77     private RenameRefactoring fRefactoring;
78     private RefactoringStatus fPreCheckStatus;
79     
80     /**
81      * Executes some light weight precondition checking. If the returned status
82      * is an error then the refactoring can't be executed at all. However,
83      * returning an OK status doesn't guarantee that the refactoring can be
84      * executed. It may still fail while performing the exhaustive precondition
85      * checking done inside the methods <code>openDialog</code> or
86      * <code>perform</code>.
87      *
88      * The method is mainly used to determine enable/disablement of actions.
89      *
90      * @return the result of the light weight precondition checking.
91      *
92      * @throws CoreException if an unexpected exception occurs while performing the checking.
93      *
94      * @see #openDialog(Shell)
95      * @see #perform(Shell, IRunnableContext)
96      */

97     public IStatus preCheck() throws CoreException {
98         ensureChecked();
99         if (fPreCheckStatus.hasFatalError())
100             return fPreCheckStatus.getEntryMatchingSeverity(RefactoringStatus.FATAL).toStatus();
101         else
102             return new Status(IStatus.OK, JavaPlugin.getPluginId(), 0, "", null); //$NON-NLS-1$
103
}
104
105     /**
106      * Opens the refactoring dialog for this rename support.
107      *
108      * @param parent a shell used as a parent for the refactoring dialog.
109      * @throws CoreException if an unexpected exception occurs while opening the
110      * dialog.
111      *
112      * @see #openDialog(Shell, boolean)
113      */

114     public void openDialog(Shell parent) throws CoreException {
115         openDialog(parent, false);
116     }
117     
118     /**
119      * Opens the refactoring dialog for this rename support.
120      *
121      * <p>
122      * This method has to be called from within the UI thread.
123      * </p>
124      *
125      * @param parent a shell used as a parent for the refactoring, preview, or error dialog
126      * @param showPreviewOnly if <code>true</code>, the dialog skips all user input pages and
127      * directly shows the preview or error page. Otherwise, shows all pages.
128      * @return <code>true</code> if the refactoring has been executed successfully,
129      * <code>false</code> if it has been canceled or if an error has happened during
130      * initial conditions checking.
131      *
132      * @throws CoreException if an error occurred while executing the
133      * operation.
134      *
135      * @see #openDialog(Shell)
136      * @since 3.3
137      */

138     public boolean openDialog(Shell parent, boolean showPreviewOnly) throws CoreException {
139         ensureChecked();
140         if (fPreCheckStatus.hasFatalError()) {
141             showInformation(parent, fPreCheckStatus);
142             return false;
143         }
144         
145         UserInterfaceStarter starter;
146         if (! showPreviewOnly) {
147             starter= RenameUserInterfaceManager.getDefault().getStarter(fRefactoring);
148         } else {
149             starter= new RenameUserInterfaceStarter();
150             RenameRefactoringWizard wizard= new RenameRefactoringWizard(fRefactoring, fRefactoring.getName(), null, null, null) {
151                 protected void addUserInputPages() {
152                     // nothing to add
153
}
154             };
155             wizard.setForcePreviewReview(showPreviewOnly);
156             starter.initialize(wizard);
157         }
158         return starter.activate(fRefactoring, parent, getJavaRenameProcessor().getSaveMode());
159     }
160
161     /**
162      * Executes the rename refactoring without showing a dialog to gather
163      * additional user input (for example the new name of the <tt>IJavaElement</tt>).
164      * Only an error dialog is shown (if necessary) to present the result
165      * of the refactoring's full precondition checking.
166      * <p>
167      * The method has to be called from within the UI thread.
168      * </p>
169      *
170      * @param parent a shell used as a parent for the error dialog.
171      * @param context a {@link IRunnableContext} to execute the operation.
172      *
173      * @throws InterruptedException if the operation has been canceled by the
174      * user.
175      * @throws InvocationTargetException if an error occurred while executing the
176      * operation.
177      *
178      * @see #openDialog(Shell)
179      * @see IRunnableContext#run(boolean, boolean, org.eclipse.jface.operation.IRunnableWithProgress)
180      */

181     public void perform(Shell parent, IRunnableContext context) throws InterruptedException JavaDoc, InvocationTargetException JavaDoc {
182         try {
183             ensureChecked();
184             if (fPreCheckStatus.hasFatalError()) {
185                 showInformation(parent, fPreCheckStatus);
186                 return;
187             }
188             
189             RenameSelectionState state= createSelectionState();
190         
191             RefactoringExecutionHelper helper= new RefactoringExecutionHelper(fRefactoring,
192                     RefactoringCore.getConditionCheckingFailedSeverity(),
193                     getJavaRenameProcessor().getSaveMode(),
194                     parent,
195                     context);
196             helper.perform(true, true);
197         
198             restoreSelectionState(state);
199         } catch (CoreException e) {
200             throw new InvocationTargetException JavaDoc(e);
201         }
202     }
203
204     /** Flag indication that no additional update is to be performed. */
205     public static final int NONE= 0;
206     
207     /** Flag indicating that references are to be updated as well. */
208     public static final int UPDATE_REFERENCES= 1 << 0;
209     
210     /**
211      * Flag indicating that Javadoc comments are to be updated as well.
212      * @deprecated use UPDATE_REFERENCES or UPDATE_TEXTUAL_MATCHES or both.
213      */

214     public static final int UPDATE_JAVADOC_COMMENTS= 1 << 1;
215     /**
216      * Flag indicating that regular comments are to be updated as well.
217      * @deprecated use UPDATE_TEXTUAL_MATCHES
218      */

219     public static final int UPDATE_REGULAR_COMMENTS= 1 << 2;
220     /**
221      * Flag indicating that string literals are to be updated as well.
222      * @deprecated use UPDATE_TEXTUAL_MATCHES
223      */

224     public static final int UPDATE_STRING_LITERALS= 1 << 3;
225
226     /**
227      * Flag indicating that textual matches in comments and in string literals
228      * are to be updated as well.
229      * @since 3.0
230      */

231     public static final int UPDATE_TEXTUAL_MATCHES= 1 << 6;
232
233     /** Flag indicating that the getter method is to be updated as well. */
234     public static final int UPDATE_GETTER_METHOD= 1 << 4;
235
236     /** Flag indicating that the setter method is to be updated as well. */
237     public static final int UPDATE_SETTER_METHOD= 1 << 5;
238
239
240     private RenameSupport(RenameJavaElementDescriptor descriptor) throws CoreException {
241         RefactoringStatus refactoringStatus= new RefactoringStatus();
242         fRefactoring= (RenameRefactoring) descriptor.createRefactoring(refactoringStatus);
243         if (refactoringStatus.hasFatalError()) {
244             fPreCheckStatus= refactoringStatus;
245         } else {
246             preCheck();
247             refactoringStatus.merge(fPreCheckStatus);
248             fPreCheckStatus= refactoringStatus;
249         }
250     }
251     
252     /**
253      * Creates a new rename support for the given
254      * {@link RenameJavaElementDescriptor}.
255      *
256      * @param descriptor the {@link RenameJavaElementDescriptor} to create a
257      * {@link RenameSupport} for. The caller is responsible for
258      * configuring the descriptor before it is passed.
259      * @return the {@link RenameSupport}.
260      * @throws CoreException if an unexpected error occurred while creating the
261      * {@link RenameSupport}.
262      * @since 3.3
263      */

264     public static RenameSupport create(RenameJavaElementDescriptor descriptor) throws CoreException {
265         return new RenameSupport(descriptor);
266     }
267     
268     private RenameSupport(JavaRenameProcessor processor, String JavaDoc newName, int flags) throws CoreException {
269         fRefactoring= new JavaRenameRefactoring(processor);
270         initialize(fRefactoring, newName, flags);
271     }
272
273     private JavaRenameProcessor getJavaRenameProcessor() {
274         return (JavaRenameProcessor) fRefactoring.getProcessor();
275     }
276
277     /**
278      * Creates a new rename support for the given {@link IJavaProject}.
279      *
280      * @param project the {@link IJavaProject} to be renamed.
281      * @param newName the project's new name. <code>null</code> is a valid
282      * value indicating that no new name is provided.
283      * @param flags flags controlling additional parameters. Valid flags are
284      * <code>UPDATE_REFERENCES</code> or <code>NONE</code>.
285      * @return the {@link RenameSupport}.
286      * @throws CoreException if an unexpected error occurred while creating
287      * the {@link RenameSupport}.
288      */

289     public static RenameSupport create(IJavaProject project, String JavaDoc newName, int flags) throws CoreException {
290         JavaRenameProcessor processor= new RenameJavaProjectProcessor(project);
291         return new RenameSupport(processor, newName, flags);
292     }
293     
294     /**
295      * Creates a new rename support for the given {@link IPackageFragmentRoot}.
296      *
297      * @param root the {@link IPackageFragmentRoot} to be renamed.
298      * @param newName the package fragment root's new name. <code>null</code> is
299      * a valid value indicating that no new name is provided.
300      * @return the {@link RenameSupport}.
301      * @throws CoreException if an unexpected error occurred while creating
302      * the {@link RenameSupport}.
303      */

304     public static RenameSupport create(IPackageFragmentRoot root, String JavaDoc newName) throws CoreException {
305         JavaRenameProcessor processor= new RenameSourceFolderProcessor(root);
306         return new RenameSupport(processor, newName, 0);
307     }
308     
309     /**
310      * Creates a new rename support for the given {@link IPackageFragment}.
311      *
312      * @param fragment the {@link IPackageFragment} to be renamed.
313      * @param newName the package fragment's new name. <code>null</code> is a
314      * valid value indicating that no new name is provided.
315      * @param flags flags controlling additional parameters. Valid flags are
316      * <code>UPDATE_REFERENCES</code>, and <code>UPDATE_TEXTUAL_MATCHES</code>,
317      * or their bitwise OR, or <code>NONE</code>.
318      * @return the {@link RenameSupport}.
319      * @throws CoreException if an unexpected error occurred while creating
320      * the {@link RenameSupport}.
321      */

322     public static RenameSupport create(IPackageFragment fragment, String JavaDoc newName, int flags) throws CoreException {
323         JavaRenameProcessor processor= new RenamePackageProcessor(fragment);
324         return new RenameSupport(processor, newName, flags);
325     }
326     
327     /**
328      * Creates a new rename support for the given {@link ICompilationUnit}.
329      *
330      * @param unit the {@link ICompilationUnit} to be renamed.
331      * @param newName the compilation unit's new name. <code>null</code> is a
332      * valid value indicating that no new name is provided.
333      * @param flags flags controlling additional parameters. Valid flags are
334      * <code>UPDATE_REFERENCES</code>, and <code>UPDATE_TEXTUAL_MATCHES</code>,
335      * or their bitwise OR, or <code>NONE</code>.
336      * @return the {@link RenameSupport}.
337      * @throws CoreException if an unexpected error occurred while creating
338      * the {@link RenameSupport}.
339      */

340     public static RenameSupport create(ICompilationUnit unit, String JavaDoc newName, int flags) throws CoreException {
341         JavaRenameProcessor processor= new RenameCompilationUnitProcessor(unit);
342         return new RenameSupport(processor, newName, flags);
343     }
344     
345     /**
346      * Creates a new rename support for the given {@link IType}.
347      *
348      * @param type the {@link IType} to be renamed.
349      * @param newName the type's new name. <code>null</code> is a valid value
350      * indicating that no new name is provided.
351      * @param flags flags controlling additional parameters. Valid flags are
352      * <code>UPDATE_REFERENCES</code>, and <code>UPDATE_TEXTUAL_MATCHES</code>,
353      * or their bitwise OR, or <code>NONE</code>.
354      * @return the {@link RenameSupport}.
355      * @throws CoreException if an unexpected error occurred while creating
356      * the {@link RenameSupport}.
357      */

358     public static RenameSupport create(IType type, String JavaDoc newName, int flags) throws CoreException {
359         JavaRenameProcessor processor= new RenameTypeProcessor(type);
360         return new RenameSupport(processor, newName, flags);
361     }
362     
363     /**
364      * Creates a new rename support for the given {@link IMethod}.
365      *
366      * @param method the {@link IMethod} to be renamed.
367      * @param newName the method's new name. <code>null</code> is a valid value
368      * indicating that no new name is provided.
369      * @param flags flags controlling additional parameters. Valid flags are
370      * <code>UPDATE_REFERENCES</code> or <code>NONE</code>.
371      * @return the {@link RenameSupport}.
372      * @throws CoreException if an unexpected error occurred while creating
373      * the {@link RenameSupport}.
374      */

375     public static RenameSupport create(IMethod method, String JavaDoc newName, int flags) throws CoreException {
376         JavaRenameProcessor processor;
377         if (MethodChecks.isVirtual(method)) {
378             processor= new RenameVirtualMethodProcessor(method);
379         } else {
380             processor= new RenameNonVirtualMethodProcessor(method);
381         }
382         return new RenameSupport(processor, newName, flags);
383     }
384     
385     /**
386      * Creates a new rename support for the given {@link IField}.
387      *
388      * @param field the {@link IField} to be renamed.
389      * @param newName the field's new name. <code>null</code> is a valid value
390      * indicating that no new name is provided.
391      * @param flags flags controlling additional parameters. Valid flags are
392      * <code>UPDATE_REFERENCES</code>, <code>UPDATE_TEXTUAL_MATCHES</code>,
393      * <code>UPDATE_GETTER_METHOD</code>, and <code>UPDATE_SETTER_METHOD</code>,
394      * or their bitwise OR, or <code>NONE</code>.
395      * @return the {@link RenameSupport}.
396      * @throws CoreException if an unexpected error occurred while creating
397      * the {@link RenameSupport}.
398      */

399     public static RenameSupport create(IField field, String JavaDoc newName, int flags) throws CoreException {
400         if (JdtFlags.isEnum(field))
401             return new RenameSupport(new RenameEnumConstProcessor(field), newName, flags);
402         else {
403             final RenameFieldProcessor processor= new RenameFieldProcessor(field);
404             processor.setRenameGetter(updateGetterMethod(flags));
405             processor.setRenameSetter(updateSetterMethod(flags));
406             return new RenameSupport(processor, newName, flags);
407         }
408     }
409
410     /**
411      * Creates a new rename support for the given {@link ITypeParameter}.
412      *
413      * @param parameter the {@link ITypeParameter} to be renamed.
414      * @param newName the parameter's new name. <code>null</code> is a valid value
415      * indicating that no new name is provided.
416      * @param flags flags controlling additional parameters. Valid flags are
417      * <code>UPDATE_REFERENCES</code>, or <code>NONE</code>.
418      * @return the {@link RenameSupport}.
419      * @throws CoreException if an unexpected error occurred while creating
420      * the {@link RenameSupport}.
421      * @since 3.1
422      */

423     public static RenameSupport create(ITypeParameter parameter, String JavaDoc newName, int flags) throws CoreException {
424         RenameTypeParameterProcessor processor= new RenameTypeParameterProcessor(parameter);
425         processor.setUpdateReferences(updateReferences(flags));
426         return new RenameSupport(processor, newName, flags);
427     }
428
429     /**
430      * Creates a new rename support for the given {@link ILocalVariable}.
431      *
432      * @param variable the {@link ILocalVariable} to be renamed.
433      * @param newName the variable's new name. <code>null</code> is a valid value
434      * indicating that no new name is provided.
435      * @param flags flags controlling additional parameters. Valid flags are
436      * <code>UPDATE_REFERENCES</code>, or <code>NONE</code>.
437      * @return the {@link RenameSupport}.
438      * @throws CoreException if an unexpected error occurred while creating
439      * the {@link RenameSupport}.
440      * @since 3.1
441      */

442     public static RenameSupport create(ILocalVariable variable, String JavaDoc newName, int flags) throws CoreException {
443         RenameLocalVariableProcessor processor= new RenameLocalVariableProcessor(variable);
444         processor.setUpdateReferences(updateReferences(flags));
445         return new RenameSupport(processor, newName, flags);
446     }
447
448     private static void initialize(RenameRefactoring refactoring, String JavaDoc newName, int flags) {
449         if (refactoring.getProcessor() == null)
450             return;
451         setNewName((INameUpdating)refactoring.getAdapter(INameUpdating.class), newName);
452         IReferenceUpdating reference= (IReferenceUpdating)refactoring.getAdapter(IReferenceUpdating.class);
453         if (reference != null) {
454             reference.setUpdateReferences(updateReferences(flags));
455         }
456         ITextUpdating text= (ITextUpdating)refactoring.getAdapter(ITextUpdating.class);
457         if (text != null) {
458             text.setUpdateTextualMatches(updateTextualMatches(flags));
459         }
460     }
461     
462     private static void setNewName(INameUpdating refactoring, String JavaDoc newName) {
463         if (newName != null)
464             refactoring.setNewElementName(newName);
465     }
466     
467     private static boolean updateReferences(int flags) {
468         return (flags & UPDATE_REFERENCES) != 0;
469     }
470     
471     private static boolean updateTextualMatches(int flags) {
472         int TEXT_UPDATES= UPDATE_TEXTUAL_MATCHES | UPDATE_REGULAR_COMMENTS | UPDATE_STRING_LITERALS;
473         return (flags & TEXT_UPDATES) != 0;
474     }
475     
476     private static boolean updateGetterMethod(int flags) {
477         return (flags & UPDATE_GETTER_METHOD) != 0;
478     }
479     
480     private static boolean updateSetterMethod(int flags) {
481         return (flags & UPDATE_SETTER_METHOD) != 0;
482     }
483     
484     private void ensureChecked() throws CoreException {
485         if (fPreCheckStatus == null) {
486             if (!fRefactoring.isApplicable()) {
487                 fPreCheckStatus= RefactoringStatus.createFatalErrorStatus(JavaUIMessages.RenameSupport_not_available);
488             } else {
489                 fPreCheckStatus= new RefactoringStatus();
490             }
491         }
492     }
493     
494     private void showInformation(Shell parent, RefactoringStatus status) {
495         String JavaDoc message= status.getMessageMatchingSeverity(RefactoringStatus.FATAL);
496         MessageDialog.openInformation(parent, JavaUIMessages.RenameSupport_dialog_title, message);
497     }
498     
499     private RenameSelectionState createSelectionState() {
500         RenameProcessor processor= (RenameProcessor) fRefactoring.getAdapter(RenameProcessor.class);
501         Object JavaDoc[] elements= processor.getElements();
502         RenameSelectionState state= elements.length == 1 ? new RenameSelectionState(elements[0]) : null;
503         return state;
504     }
505     
506     private void restoreSelectionState(RenameSelectionState state) throws CoreException {
507         INameUpdating nameUpdating= (INameUpdating) fRefactoring.getAdapter(INameUpdating.class);
508         if (nameUpdating != null && state != null) {
509             Object JavaDoc newElement= nameUpdating.getNewElement();
510             if (newElement != null) {
511                 state.restore(newElement);
512             }
513         }
514     }
515 }
516
Popular Tags