1 11 package org.eclipse.jdt.internal.corext.refactoring.rename; 12 13 import org.eclipse.core.runtime.Assert; 14 import org.eclipse.core.runtime.CoreException; 15 import org.eclipse.core.runtime.IProgressMonitor; 16 17 import org.eclipse.core.resources.IContainer; 18 import org.eclipse.core.resources.IResource; 19 import org.eclipse.core.resources.ResourcesPlugin; 20 import org.eclipse.core.resources.mapping.IResourceChangeDescriptionFactory; 21 22 import org.eclipse.ltk.core.refactoring.Change; 23 import org.eclipse.ltk.core.refactoring.RefactoringDescriptor; 24 import org.eclipse.ltk.core.refactoring.RefactoringStatus; 25 import org.eclipse.ltk.core.refactoring.participants.CheckConditionsContext; 26 import org.eclipse.ltk.core.refactoring.participants.RefactoringArguments; 27 import org.eclipse.ltk.core.refactoring.participants.RefactoringParticipant; 28 import org.eclipse.ltk.core.refactoring.participants.RenameArguments; 29 import org.eclipse.ltk.core.refactoring.participants.RenameProcessor; 30 import org.eclipse.ltk.core.refactoring.participants.ResourceChangeChecker; 31 import org.eclipse.ltk.core.refactoring.participants.SharableParticipants; 32 33 import org.eclipse.jdt.core.JavaModelException; 34 import org.eclipse.jdt.core.refactoring.IJavaRefactorings; 35 import org.eclipse.jdt.core.refactoring.descriptors.RenameResourceDescriptor; 36 37 import org.eclipse.jdt.internal.corext.refactoring.JDTRefactoringDescriptor; 38 import org.eclipse.jdt.internal.corext.refactoring.JDTRefactoringDescriptorComment; 39 import org.eclipse.jdt.internal.corext.refactoring.JavaRefactoringArguments; 40 import org.eclipse.jdt.internal.corext.refactoring.RefactoringAvailabilityTester; 41 import org.eclipse.jdt.internal.corext.refactoring.RefactoringCoreMessages; 42 import org.eclipse.jdt.internal.corext.refactoring.changes.DynamicValidationStateChange; 43 import org.eclipse.jdt.internal.corext.refactoring.changes.RenameResourceChange; 44 import org.eclipse.jdt.internal.corext.refactoring.code.ScriptableRefactoring; 45 import org.eclipse.jdt.internal.corext.refactoring.participants.ResourceProcessors; 46 import org.eclipse.jdt.internal.corext.refactoring.tagging.ICommentProvider; 47 import org.eclipse.jdt.internal.corext.refactoring.tagging.INameUpdating; 48 import org.eclipse.jdt.internal.corext.refactoring.tagging.IScriptableRefactoring; 49 import org.eclipse.jdt.internal.corext.util.Messages; 50 import org.eclipse.jdt.internal.corext.util.Resources; 51 52 public class RenameResourceProcessor extends RenameProcessor implements IScriptableRefactoring, ICommentProvider, INameUpdating { 53 54 public static final String IDENTIFIER= "org.eclipse.jdt.ui.renameResourceProcessor"; 56 private String fComment; 57 58 private String fNewElementName; 59 60 private RenameModifications fRenameModifications; 61 62 private IResource fResource; 63 64 70 public RenameResourceProcessor(IResource resource) { 71 fResource= resource; 72 if (resource != null) { 73 setNewElementName(resource.getName()); 74 } 75 } 76 77 public boolean canEnableComment() { 78 return true; 79 } 80 81 public RefactoringStatus checkFinalConditions(IProgressMonitor pm, CheckConditionsContext context) throws JavaModelException { 82 pm.beginTask("", 1); try { 84 fRenameModifications= new RenameModifications(); 85 fRenameModifications.rename(fResource, new RenameArguments(getNewElementName(), getUpdateReferences())); 86 87 ResourceChangeChecker checker= (ResourceChangeChecker) context.getChecker(ResourceChangeChecker.class); 88 IResourceChangeDescriptionFactory deltaFactory= checker.getDeltaFactory(); 89 fRenameModifications.buildDelta(deltaFactory); 90 91 return new RefactoringStatus(); 92 } finally { 93 pm.done(); 94 } 95 } 96 97 public RefactoringStatus checkInitialConditions(IProgressMonitor pm) throws CoreException { 98 return RefactoringStatus.create(Resources.checkInSync(fResource)); 99 } 100 101 public RefactoringStatus checkNewElementName(String newName) throws JavaModelException { 102 Assert.isNotNull(newName, "new name"); IContainer c= fResource.getParent(); 104 if (c == null) 105 return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.RenameResourceRefactoring_Internal_Error); 106 107 if (c.findMember(newName) != null) 108 return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.RenameResourceRefactoring_alread_exists); 109 110 if (!c.getFullPath().isValidSegment(newName)) 111 return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.RenameResourceRefactoring_invalidName); 112 113 RefactoringStatus result= RefactoringStatus.create(c.getWorkspace().validateName(newName, fResource.getType())); 114 if (!result.hasFatalError()) 115 result.merge(RefactoringStatus.create(c.getWorkspace().validatePath(createNewPath(newName), fResource.getType()))); 116 return result; 117 } 118 119 public Change createChange(IProgressMonitor pm) throws JavaModelException { 120 pm.beginTask("", 1); try { 122 String project= null; 123 if (fResource.getType() != IResource.PROJECT) 124 project= fResource.getProject().getName(); 125 final String header= Messages.format(RefactoringCoreMessages.RenameResourceChange_descriptor_description, new String [] { fResource.getFullPath().toString(), getNewElementName()}); 126 final String description= Messages.format(RefactoringCoreMessages.RenameResourceChange_descriptor_description_short, fResource.getName()); 127 final String comment= new JDTRefactoringDescriptorComment(project, this, header).asString(); 128 final int flags= RefactoringDescriptor.STRUCTURAL_CHANGE | RefactoringDescriptor.MULTI_CHANGE | RefactoringDescriptor.BREAKING_CHANGE; 129 final RenameResourceDescriptor descriptor= new RenameResourceDescriptor(); 130 descriptor.setProject(project); 131 descriptor.setDescription(description); 132 descriptor.setComment(comment); 133 descriptor.setFlags(flags); 134 descriptor.setResource(fResource); 135 descriptor.setNewName(getNewElementName()); 136 return new DynamicValidationStateChange(new RenameResourceChange(descriptor, fResource, getNewElementName(), comment)); 137 } finally { 138 pm.done(); 139 } 140 } 141 142 private String createNewPath(String newName) { 143 return fResource.getFullPath().removeLastSegments(1).append(newName).toString(); 144 } 145 146 public String [] getAffectedProjectNatures() throws CoreException { 147 return ResourceProcessors.computeAffectedNatures(fResource); 148 } 149 150 public String getComment() { 151 return fComment; 152 } 153 154 public String getCurrentElementName() { 155 return fResource.getName(); 156 } 157 158 public Object [] getElements() { 159 return new Object [] { fResource}; 160 } 161 162 public String getIdentifier() { 163 return IDENTIFIER; 164 } 165 166 public Object getNewElement() { 167 return ResourcesPlugin.getWorkspace().getRoot().findMember(createNewPath(getNewElementName())); 168 } 169 170 public String getNewElementName() { 171 return fNewElementName; 172 } 173 174 public String getProcessorName() { 175 return RefactoringCoreMessages.RenameResourceProcessor_name; 176 } 177 178 public boolean getUpdateReferences() { 179 return true; 180 } 181 182 public RefactoringStatus initialize(final RefactoringArguments arguments) { 183 if (arguments instanceof JavaRefactoringArguments) { 184 final JavaRefactoringArguments extended= (JavaRefactoringArguments) arguments; 185 final String handle= extended.getAttribute(JDTRefactoringDescriptor.ATTRIBUTE_INPUT); 186 if (handle != null) { 187 fResource= JDTRefactoringDescriptor.handleToResource(extended.getProject(), handle); 188 if (fResource == null || !fResource.exists()) 189 return ScriptableRefactoring.createInputFatalStatus(fResource, getRefactoring().getName(), IJavaRefactorings.RENAME_RESOURCE); 190 } else 191 return RefactoringStatus.createFatalErrorStatus(Messages.format(RefactoringCoreMessages.InitializableRefactoring_argument_not_exist, JDTRefactoringDescriptor.ATTRIBUTE_INPUT)); 192 final String name= extended.getAttribute(JDTRefactoringDescriptor.ATTRIBUTE_NAME); 193 if (name != null && !"".equals(name)) setNewElementName(name); 195 else 196 return RefactoringStatus.createFatalErrorStatus(Messages.format(RefactoringCoreMessages.InitializableRefactoring_argument_not_exist, JDTRefactoringDescriptor.ATTRIBUTE_NAME)); 197 } else 198 return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.InitializableRefactoring_inacceptable_arguments); 199 return new RefactoringStatus(); 200 } 201 202 public boolean isApplicable() throws JavaModelException { 203 return RefactoringAvailabilityTester.isRenameAvailable(fResource); 204 } 205 206 public RefactoringParticipant[] loadParticipants(RefactoringStatus status, SharableParticipants shared) throws CoreException { 207 return fRenameModifications.loadParticipants(status, this, getAffectedProjectNatures(), shared); 208 } 209 210 public void setComment(final String comment) { 211 fComment= comment; 212 } 213 214 public void setNewElementName(String newName) { 215 Assert.isNotNull(newName); 216 fNewElementName= newName; 217 } 218 } | Popular Tags |