1 11 package org.eclipse.pde.internal.ui.refactoring; 12 13 import org.eclipse.core.resources.IResource; 14 import org.eclipse.core.resources.ResourcesPlugin; 15 import org.eclipse.core.runtime.CoreException; 16 import org.eclipse.core.runtime.IPath; 17 import org.eclipse.core.runtime.IProgressMonitor; 18 import org.eclipse.ltk.core.refactoring.Change; 19 import org.eclipse.ltk.core.refactoring.ChangeDescriptor; 20 import org.eclipse.ltk.core.refactoring.RefactoringChangeDescriptor; 21 import org.eclipse.ltk.core.refactoring.RefactoringDescriptor; 22 import org.eclipse.ltk.core.refactoring.RefactoringStatus; 23 import org.eclipse.pde.internal.ui.PDEUIMessages; 24 25 import com.ibm.icu.text.MessageFormat; 26 27 public final class RenameProjectChange extends Change { 28 29 public static IPath renamedResourcePath(IPath path, String newName) { 30 return path.removeLastSegments(1).append(newName); 31 } 32 33 private final String fComment; 34 35 private final RefactoringDescriptor fDescriptor; 36 37 private final String fNewName; 38 39 private final IPath fResourcePath; 40 41 private final long fStampToRestore; 42 43 private RenameProjectChange(RefactoringDescriptor descriptor, IPath resourcePath, String newName, String comment, long stampToRestore) { 44 fDescriptor= descriptor; 45 fResourcePath= resourcePath; 46 fNewName= newName; 47 fComment= comment; 48 fStampToRestore= stampToRestore; 49 } 50 51 public RenameProjectChange(RefactoringDescriptor descriptor, IResource resource, String newName, String comment) { 52 this(descriptor, resource.getFullPath(), newName, comment, IResource.NULL_STAMP); 53 } 54 55 public ChangeDescriptor getDescriptor() { 56 if (fDescriptor != null) 57 return new RefactoringChangeDescriptor(fDescriptor); 58 return null; 59 } 60 61 public Object getModifiedElement() { 62 return getResource(); 63 } 64 65 public String getName() { 66 return MessageFormat.format(PDEUIMessages.RenameProjectChange_name, new String [] { fResourcePath.lastSegment(), fNewName}); 67 } 68 69 public String getNewName() { 70 return fNewName; 71 } 72 73 private IResource getResource() { 74 return ResourcesPlugin.getWorkspace().getRoot().findMember(fResourcePath); 75 } 76 77 public RefactoringStatus isValid(IProgressMonitor pm) throws CoreException { 78 IResource resource= getResource(); 79 if (resource == null || !resource.exists()) 80 return RefactoringStatus.createFatalErrorStatus(MessageFormat.format(PDEUIMessages.RenameProjectChange_projectDoesNotExist, new String [] {fResourcePath.toString()})); 81 if (ResourcesPlugin.getWorkspace().getRoot().getProject(fNewName).exists()) 82 return RefactoringStatus.createFatalErrorStatus(MessageFormat.format(PDEUIMessages.RenameProjectChange_destinationExists, new String [] {fNewName})); 83 return new RefactoringStatus(); 84 } 85 86 public Change perform(IProgressMonitor pm) throws CoreException { 87 try { 88 pm.beginTask(PDEUIMessages.RenameProjectChange_taskTitle, 1); 89 90 IResource resource= getResource(); 91 long currentStamp= resource.getModificationStamp(); 92 IPath newPath= renamedResourcePath(fResourcePath, fNewName); 93 resource.move(newPath, IResource.SHALLOW, pm); 94 if (fStampToRestore != IResource.NULL_STAMP) { 95 IResource newResource= ResourcesPlugin.getWorkspace().getRoot().findMember(newPath); 96 newResource.revertModificationStamp(fStampToRestore); 97 } 98 String oldName= fResourcePath.lastSegment(); 99 return new RenameProjectChange(null, newPath, oldName, fComment, currentStamp); 100 } finally { 101 pm.done(); 102 } 103 } 104 105 public void initializeValidationData(IProgressMonitor pm) { 106 } 108 109 } 110 | Popular Tags |