001: /*******************************************************************************
002: * Copyright (c) 2000, 2006 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.changes;
011:
012: import org.eclipse.core.runtime.CoreException;
013: import org.eclipse.core.runtime.IPath;
014: import org.eclipse.core.runtime.IProgressMonitor;
015:
016: import org.eclipse.core.resources.IResource;
017: import org.eclipse.core.resources.ResourcesPlugin;
018:
019: import org.eclipse.ltk.core.refactoring.Change;
020: import org.eclipse.ltk.core.refactoring.ChangeDescriptor;
021: import org.eclipse.ltk.core.refactoring.RefactoringChangeDescriptor;
022: import org.eclipse.ltk.core.refactoring.RefactoringDescriptor;
023: import org.eclipse.ltk.core.refactoring.RefactoringStatus;
024:
025: import org.eclipse.jdt.internal.corext.refactoring.RefactoringCoreMessages;
026: import org.eclipse.jdt.internal.corext.refactoring.base.JDTChange;
027: import org.eclipse.jdt.internal.corext.util.Messages;
028:
029: public final class RenameResourceChange extends JDTChange {
030:
031: public static IPath renamedResourcePath(IPath path, String newName) {
032: return path.removeLastSegments(1).append(newName);
033: }
034:
035: private final String fComment;
036:
037: private final RefactoringDescriptor fDescriptor;
038:
039: private final String fNewName;
040:
041: private final IPath fResourcePath;
042:
043: private final long fStampToRestore;
044:
045: private RenameResourceChange(RefactoringDescriptor descriptor,
046: IPath resourcePath, String newName, String comment,
047: long stampToRestore) {
048: fDescriptor = descriptor;
049: fResourcePath = resourcePath;
050: fNewName = newName;
051: fComment = comment;
052: fStampToRestore = stampToRestore;
053: }
054:
055: public RenameResourceChange(RefactoringDescriptor descriptor,
056: IResource resource, String newName, String comment) {
057: this (descriptor, resource.getFullPath(), newName, comment,
058: IResource.NULL_STAMP);
059: }
060:
061: public ChangeDescriptor getDescriptor() {
062: if (fDescriptor != null)
063: return new RefactoringChangeDescriptor(fDescriptor);
064: return null;
065: }
066:
067: public Object getModifiedElement() {
068: return getResource();
069: }
070:
071: public String getName() {
072: return Messages.format(
073: RefactoringCoreMessages.RenameResourceChange_name,
074: new String[] { fResourcePath.toString(), fNewName });
075: }
076:
077: public String getNewName() {
078: return fNewName;
079: }
080:
081: private IResource getResource() {
082: return ResourcesPlugin.getWorkspace().getRoot().findMember(
083: fResourcePath);
084: }
085:
086: public RefactoringStatus isValid(IProgressMonitor pm)
087: throws CoreException {
088: IResource resource = getResource();
089: if (resource == null || !resource.exists()) {
090: return RefactoringStatus
091: .createFatalErrorStatus(Messages
092: .format(
093: RefactoringCoreMessages.RenameResourceChange_does_not_exist,
094: fResourcePath.toString()));
095: } else {
096: return super .isValid(pm, DIRTY);
097: }
098: }
099:
100: public Change perform(IProgressMonitor pm) throws CoreException {
101: try {
102: pm
103: .beginTask(
104: RefactoringCoreMessages.RenameResourceChange_rename_resource,
105: 1);
106:
107: IResource resource = getResource();
108: long currentStamp = resource.getModificationStamp();
109: IPath newPath = renamedResourcePath(fResourcePath, fNewName);
110: resource.move(newPath, IResource.SHALLOW, pm);
111: if (fStampToRestore != IResource.NULL_STAMP) {
112: IResource newResource = ResourcesPlugin.getWorkspace()
113: .getRoot().findMember(newPath);
114: newResource.revertModificationStamp(fStampToRestore);
115: }
116: String oldName = fResourcePath.lastSegment();
117: return new RenameResourceChange(null, newPath, oldName,
118: fComment, currentStamp);
119: } finally {
120: pm.done();
121: }
122: }
123: }
|