1 /*******************************************************************************
2 * Copyright (c) 2000, 2011 IBM Corporation and others.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/epl-v10.html
9 * IBM Corporation - initial API and implementation
10 *******************************************************************************/
11 package org.eclipse.jdt.internal.corext.refactoring;
13 import org.eclipse.core.runtime.Assert;
14 import org.eclipse.core.runtime.CoreException;
15 import org.eclipse.core.runtime.IPath;
16 import org.eclipse.core.runtime.IProgressMonitor;
17 import org.eclipse.core.runtime.SubProgressMonitor;
19 import org.eclipse.core.resources.IResource;
20 import org.eclipse.core.resources.ResourcesPlugin;
22 import org.eclipse.ltk.core.refactoring.Change;
23 import org.eclipse.ltk.core.refactoring.resource.ResourceChange;
25 import org.eclipse.jdt.core.JavaCore;
27 public abstract class AbstractJavaElementRenameChange extends ResourceChange {
29 private final String fNewName;
31 private final String fOldName;
33 private final IPath fResourcePath;
35 private final long fStampToRestore;
37 protected AbstractJavaElementRenameChange(IPath resourcePath, String oldName, String newName) {
38 this(resourcePath, oldName, newName, IResource.NULL_STAMP);
41 protected AbstractJavaElementRenameChange(IPath resourcePath, String oldName, String newName, long stampToRestore) {
42 Assert.isNotNull(newName, "new name"); //$NON-NLS-1$
43 Assert.isNotNull(oldName, "old name"); //$NON-NLS-1$
44 fResourcePath= resourcePath;
47 fStampToRestore= stampToRestore;
50 protected abstract IPath createNewPath();
52 protected abstract Change createUndoChange(long stampToRestore) throws CoreException;
54 protected abstract void doRename(IProgressMonitor pm) throws CoreException;
57 public Object getModifiedElement() {
58 return JavaCore.create(getResource());
62 protected IResource getModifiedResource() {
66 public String getNewName() {
70 public String getOldName() {
74 protected final IResource getResource() {
75 return ResourcesPlugin.getWorkspace().getRoot().findMember(fResourcePath);
78 protected IPath getResourcePath() {
83 public final Change perform(IProgressMonitor pm) throws CoreException {
85 pm.beginTask(RefactoringCoreMessages.AbstractRenameChange_Renaming, 1);
86 IResource resource= getResource();
87 IPath newPath= createNewPath();
88 Change result= createUndoChange(resource.getModificationStamp());
89 doRename(new SubProgressMonitor(pm, 1));
90 if (fStampToRestore != IResource.NULL_STAMP) {
91 IResource newResource= ResourcesPlugin.getWorkspace().getRoot().findMember(newPath);
92 newResource.revertModificationStamp(fStampToRestore);