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