KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > corext > refactoring > AbstractJavaElementRenameChange


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 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
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.jdt.internal.corext.refactoring;
12
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;
18
19 import org.eclipse.core.resources.IResource;
20 import org.eclipse.core.resources.ResourcesPlugin;
21
22 import org.eclipse.ltk.core.refactoring.Change;
23
24 import org.eclipse.jdt.core.JavaCore;
25
26 import org.eclipse.jdt.internal.corext.refactoring.base.JDTChange;
27
28 public abstract class AbstractJavaElementRenameChange extends JDTChange {
29
30     private final String JavaDoc fNewName;
31
32     private final String JavaDoc fOldName;
33
34     private final IPath fResourcePath;
35
36     private final long fStampToRestore;
37
38     protected AbstractJavaElementRenameChange(IPath resourcePath, String JavaDoc oldName, String JavaDoc newName) {
39         this(resourcePath, oldName, newName, IResource.NULL_STAMP);
40     }
41
42     protected AbstractJavaElementRenameChange(IPath resourcePath, String JavaDoc oldName, String JavaDoc newName, long stampToRestore) {
43         Assert.isNotNull(newName, "new name"); //$NON-NLS-1$
44
Assert.isNotNull(oldName, "old name"); //$NON-NLS-1$
45
fResourcePath= resourcePath;
46         fOldName= oldName;
47         fNewName= newName;
48         fStampToRestore= stampToRestore;
49     }
50
51     protected abstract IPath createNewPath();
52
53     protected abstract Change createUndoChange(long stampToRestore) throws CoreException;
54
55     protected abstract void doRename(IProgressMonitor pm) throws CoreException;
56
57     public Object JavaDoc getModifiedElement() {
58         return JavaCore.create(getResource());
59     }
60
61     public String JavaDoc getNewName() {
62         return fNewName;
63     }
64
65     public String JavaDoc getOldName() {
66         return fOldName;
67     }
68
69     protected final IResource getResource() {
70         return ResourcesPlugin.getWorkspace().getRoot().findMember(fResourcePath);
71     }
72
73     protected IPath getResourcePath() {
74         return fResourcePath;
75     }
76
77     public final Change perform(IProgressMonitor pm) throws CoreException {
78         try {
79             pm.beginTask(RefactoringCoreMessages.AbstractRenameChange_Renaming, 1);
80             IResource resource= getResource();
81             IPath newPath= createNewPath();
82             Change result= createUndoChange(resource.getModificationStamp());
83             doRename(new SubProgressMonitor(pm, 1));
84             if (fStampToRestore != IResource.NULL_STAMP) {
85                 IResource newResource= ResourcesPlugin.getWorkspace().getRoot().findMember(newPath);
86                 newResource.revertModificationStamp(fStampToRestore);
87             }
88             return result;
89         } finally {
90             pm.done();
91         }
92     }
93 }
Popular Tags