1 11 package org.eclipse.jdt.ui.refactoring; 12 13 import java.lang.reflect.InvocationTargetException ; 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 75 public class RenameSupport { 76 77 private RenameRefactoring fRefactoring; 78 private RefactoringStatus fPreCheckStatus; 79 80 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); } 104 105 114 public void openDialog(Shell parent) throws CoreException { 115 openDialog(parent, false); 116 } 117 118 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 } 154 }; 155 wizard.setForcePreviewReview(showPreviewOnly); 156 starter.initialize(wizard); 157 } 158 return starter.activate(fRefactoring, parent, getJavaRenameProcessor().getSaveMode()); 159 } 160 161 181 public void perform(Shell parent, IRunnableContext context) throws InterruptedException , InvocationTargetException { 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 (e); 201 } 202 } 203 204 205 public static final int NONE= 0; 206 207 208 public static final int UPDATE_REFERENCES= 1 << 0; 209 210 214 public static final int UPDATE_JAVADOC_COMMENTS= 1 << 1; 215 219 public static final int UPDATE_REGULAR_COMMENTS= 1 << 2; 220 224 public static final int UPDATE_STRING_LITERALS= 1 << 3; 225 226 231 public static final int UPDATE_TEXTUAL_MATCHES= 1 << 6; 232 233 234 public static final int UPDATE_GETTER_METHOD= 1 << 4; 235 236 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 264 public static RenameSupport create(RenameJavaElementDescriptor descriptor) throws CoreException { 265 return new RenameSupport(descriptor); 266 } 267 268 private RenameSupport(JavaRenameProcessor processor, String 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 289 public static RenameSupport create(IJavaProject project, String newName, int flags) throws CoreException { 290 JavaRenameProcessor processor= new RenameJavaProjectProcessor(project); 291 return new RenameSupport(processor, newName, flags); 292 } 293 294 304 public static RenameSupport create(IPackageFragmentRoot root, String newName) throws CoreException { 305 JavaRenameProcessor processor= new RenameSourceFolderProcessor(root); 306 return new RenameSupport(processor, newName, 0); 307 } 308 309 322 public static RenameSupport create(IPackageFragment fragment, String newName, int flags) throws CoreException { 323 JavaRenameProcessor processor= new RenamePackageProcessor(fragment); 324 return new RenameSupport(processor, newName, flags); 325 } 326 327 340 public static RenameSupport create(ICompilationUnit unit, String newName, int flags) throws CoreException { 341 JavaRenameProcessor processor= new RenameCompilationUnitProcessor(unit); 342 return new RenameSupport(processor, newName, flags); 343 } 344 345 358 public static RenameSupport create(IType type, String newName, int flags) throws CoreException { 359 JavaRenameProcessor processor= new RenameTypeProcessor(type); 360 return new RenameSupport(processor, newName, flags); 361 } 362 363 375 public static RenameSupport create(IMethod method, String 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 399 public static RenameSupport create(IField field, String 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 423 public static RenameSupport create(ITypeParameter parameter, String 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 442 public static RenameSupport create(ILocalVariable variable, String 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 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 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 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 [] 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 newElement= nameUpdating.getNewElement(); 510 if (newElement != null) { 511 state.restore(newElement); 512 } 513 } 514 } 515 } 516 | Popular Tags |