001: /*******************************************************************************
002: * Copyright (c) 2000, 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.rename;
011:
012: import org.eclipse.core.runtime.Assert;
013: import org.eclipse.core.runtime.CoreException;
014: import org.eclipse.core.runtime.IProgressMonitor;
015:
016: import org.eclipse.core.resources.IContainer;
017: import org.eclipse.core.resources.IResource;
018: import org.eclipse.core.resources.ResourcesPlugin;
019: import org.eclipse.core.resources.mapping.IResourceChangeDescriptionFactory;
020:
021: import org.eclipse.ltk.core.refactoring.Change;
022: import org.eclipse.ltk.core.refactoring.RefactoringDescriptor;
023: import org.eclipse.ltk.core.refactoring.RefactoringStatus;
024: import org.eclipse.ltk.core.refactoring.participants.CheckConditionsContext;
025: import org.eclipse.ltk.core.refactoring.participants.RefactoringArguments;
026: import org.eclipse.ltk.core.refactoring.participants.RefactoringParticipant;
027: import org.eclipse.ltk.core.refactoring.participants.RenameArguments;
028: import org.eclipse.ltk.core.refactoring.participants.RenameProcessor;
029: import org.eclipse.ltk.core.refactoring.participants.ResourceChangeChecker;
030: import org.eclipse.ltk.core.refactoring.participants.SharableParticipants;
031:
032: import org.eclipse.jdt.core.JavaModelException;
033: import org.eclipse.jdt.core.refactoring.IJavaRefactorings;
034: import org.eclipse.jdt.core.refactoring.descriptors.RenameResourceDescriptor;
035:
036: import org.eclipse.jdt.internal.corext.refactoring.JDTRefactoringDescriptorComment;
037: import org.eclipse.jdt.internal.corext.refactoring.JavaRefactoringArguments;
038: import org.eclipse.jdt.internal.corext.refactoring.JavaRefactoringDescriptorUtil;
039: import org.eclipse.jdt.internal.corext.refactoring.RefactoringAvailabilityTester;
040: import org.eclipse.jdt.internal.corext.refactoring.RefactoringCoreMessages;
041: import org.eclipse.jdt.internal.corext.refactoring.changes.DynamicValidationStateChange;
042: import org.eclipse.jdt.internal.corext.refactoring.changes.RenameResourceChange;
043: import org.eclipse.jdt.internal.corext.refactoring.code.ScriptableRefactoring;
044: import org.eclipse.jdt.internal.corext.refactoring.participants.ResourceProcessors;
045: import org.eclipse.jdt.internal.corext.refactoring.tagging.ICommentProvider;
046: import org.eclipse.jdt.internal.corext.refactoring.tagging.INameUpdating;
047: import org.eclipse.jdt.internal.corext.refactoring.tagging.IScriptableRefactoring;
048: import org.eclipse.jdt.internal.corext.util.Messages;
049: import org.eclipse.jdt.internal.corext.util.Resources;
050:
051: import org.eclipse.jdt.ui.refactoring.IRefactoringProcessorIds;
052:
053: public class RenameResourceProcessor extends RenameProcessor implements
054: IScriptableRefactoring, ICommentProvider, INameUpdating {
055:
056: private String fComment;
057:
058: private String fNewElementName;
059:
060: private RenameModifications fRenameModifications;
061:
062: private IResource fResource;
063:
064: /**
065: * Creates a new rename resource processor.
066: *
067: * @param resource
068: * the resource, or <code>null</code> if invoked by scripting
069: */
070: public RenameResourceProcessor(IResource resource) {
071: fResource = resource;
072: if (resource != null) {
073: setNewElementName(resource.getName());
074: }
075: }
076:
077: public boolean canEnableComment() {
078: return true;
079: }
080:
081: public RefactoringStatus checkFinalConditions(IProgressMonitor pm,
082: CheckConditionsContext context) throws JavaModelException {
083: pm.beginTask("", 1); //$NON-NLS-1$
084: try {
085: fRenameModifications = new RenameModifications();
086: fRenameModifications.rename(fResource, new RenameArguments(
087: getNewElementName(), getUpdateReferences()));
088:
089: ResourceChangeChecker checker = (ResourceChangeChecker) context
090: .getChecker(ResourceChangeChecker.class);
091: IResourceChangeDescriptionFactory deltaFactory = checker
092: .getDeltaFactory();
093: fRenameModifications.buildDelta(deltaFactory);
094:
095: return new RefactoringStatus();
096: } finally {
097: pm.done();
098: }
099: }
100:
101: public RefactoringStatus checkInitialConditions(IProgressMonitor pm)
102: throws CoreException {
103: return RefactoringStatus.create(Resources
104: .checkInSync(fResource));
105: }
106:
107: public RefactoringStatus checkNewElementName(String newName)
108: throws JavaModelException {
109: Assert.isNotNull(newName, "new name"); //$NON-NLS-1$
110: IContainer c = fResource.getParent();
111: if (c == null)
112: return RefactoringStatus
113: .createFatalErrorStatus(RefactoringCoreMessages.RenameResourceRefactoring_Internal_Error);
114:
115: if (c.findMember(newName) != null)
116: return RefactoringStatus
117: .createFatalErrorStatus(RefactoringCoreMessages.RenameResourceRefactoring_alread_exists);
118:
119: if (!c.getFullPath().isValidSegment(newName))
120: return RefactoringStatus
121: .createFatalErrorStatus(RefactoringCoreMessages.RenameResourceRefactoring_invalidName);
122:
123: RefactoringStatus result = RefactoringStatus.create(c
124: .getWorkspace().validateName(newName,
125: fResource.getType()));
126: if (!result.hasFatalError())
127: result.merge(RefactoringStatus.create(c.getWorkspace()
128: .validatePath(createNewPath(newName),
129: fResource.getType())));
130: return result;
131: }
132:
133: public Change createChange(IProgressMonitor pm)
134: throws JavaModelException {
135: pm.beginTask("", 1); //$NON-NLS-1$
136: try {
137: String project = null;
138: if (fResource.getType() != IResource.PROJECT)
139: project = fResource.getProject().getName();
140: final String header = Messages
141: .format(
142: RefactoringCoreMessages.RenameResourceChange_descriptor_description,
143: new String[] {
144: fResource.getFullPath().toString(),
145: getNewElementName() });
146: final String description = Messages
147: .format(
148: RefactoringCoreMessages.RenameResourceChange_descriptor_description_short,
149: fResource.getName());
150: final String comment = new JDTRefactoringDescriptorComment(
151: project, this , header).asString();
152: final int flags = RefactoringDescriptor.STRUCTURAL_CHANGE
153: | RefactoringDescriptor.MULTI_CHANGE
154: | RefactoringDescriptor.BREAKING_CHANGE;
155: final RenameResourceDescriptor descriptor = new RenameResourceDescriptor();
156: descriptor.setProject(project);
157: descriptor.setDescription(description);
158: descriptor.setComment(comment);
159: descriptor.setFlags(flags);
160: descriptor.setResource(fResource);
161: descriptor.setNewName(getNewElementName());
162: return new DynamicValidationStateChange(
163: new RenameResourceChange(descriptor, fResource,
164: getNewElementName(), comment));
165: } finally {
166: pm.done();
167: }
168: }
169:
170: private String createNewPath(String newName) {
171: return fResource.getFullPath().removeLastSegments(1).append(
172: newName).toString();
173: }
174:
175: public String[] getAffectedProjectNatures() throws CoreException {
176: return ResourceProcessors.computeAffectedNatures(fResource);
177: }
178:
179: public String getComment() {
180: return fComment;
181: }
182:
183: public String getCurrentElementName() {
184: return fResource.getName();
185: }
186:
187: public Object[] getElements() {
188: return new Object[] { fResource };
189: }
190:
191: public String getIdentifier() {
192: return IRefactoringProcessorIds.RENAME_RESOURCE_PROCESSOR;
193: }
194:
195: public Object getNewElement() {
196: return ResourcesPlugin.getWorkspace().getRoot().findMember(
197: createNewPath(getNewElementName()));
198: }
199:
200: public String getNewElementName() {
201: return fNewElementName;
202: }
203:
204: public String getProcessorName() {
205: return RefactoringCoreMessages.RenameResourceProcessor_name;
206: }
207:
208: public boolean getUpdateReferences() {
209: return true;
210: }
211:
212: public RefactoringStatus initialize(
213: final RefactoringArguments arguments) {
214: if (arguments instanceof JavaRefactoringArguments) {
215: final JavaRefactoringArguments extended = (JavaRefactoringArguments) arguments;
216: final String handle = extended
217: .getAttribute(JavaRefactoringDescriptorUtil.ATTRIBUTE_INPUT);
218: if (handle != null) {
219: fResource = JavaRefactoringDescriptorUtil
220: .handleToResource(extended.getProject(), handle);
221: if (fResource == null || !fResource.exists())
222: return ScriptableRefactoring
223: .createInputFatalStatus(fResource,
224: getRefactoring().getName(),
225: IJavaRefactorings.RENAME_RESOURCE);
226: } else
227: return RefactoringStatus
228: .createFatalErrorStatus(Messages
229: .format(
230: RefactoringCoreMessages.InitializableRefactoring_argument_not_exist,
231: JavaRefactoringDescriptorUtil.ATTRIBUTE_INPUT));
232: final String name = extended
233: .getAttribute(JavaRefactoringDescriptorUtil.ATTRIBUTE_NAME);
234: if (name != null && !"".equals(name)) //$NON-NLS-1$
235: setNewElementName(name);
236: else
237: return RefactoringStatus
238: .createFatalErrorStatus(Messages
239: .format(
240: RefactoringCoreMessages.InitializableRefactoring_argument_not_exist,
241: JavaRefactoringDescriptorUtil.ATTRIBUTE_NAME));
242: } else
243: return RefactoringStatus
244: .createFatalErrorStatus(RefactoringCoreMessages.InitializableRefactoring_inacceptable_arguments);
245: return new RefactoringStatus();
246: }
247:
248: public boolean isApplicable() throws JavaModelException {
249: return RefactoringAvailabilityTester
250: .isRenameAvailable(fResource);
251: }
252:
253: public RefactoringParticipant[] loadParticipants(
254: RefactoringStatus status, SharableParticipants shared)
255: throws CoreException {
256: return fRenameModifications.loadParticipants(status, this ,
257: getAffectedProjectNatures(), shared);
258: }
259:
260: public void setComment(final String comment) {
261: fComment = comment;
262: }
263:
264: public void setNewElementName(String newName) {
265: Assert.isNotNull(newName);
266: fNewElementName = newName;
267: }
268: }
|