]> git.uio.no Git - ifi-stolz-refaktor.git/blob - case-study/jdt-before/core refactoring/org/eclipse/jdt/internal/corext/refactoring/AbstractJavaElementRenameChange.java
b74a59cfa7f150e15551f83b7542b0336b7efd14
[ifi-stolz-refaktor.git] / case-study / jdt-before / core refactoring / org / eclipse / jdt / internal / corext / refactoring / AbstractJavaElementRenameChange.java
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
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 import org.eclipse.ltk.core.refactoring.resource.ResourceChange;
24
25 import org.eclipse.jdt.core.JavaCore;
26
27 public abstract class AbstractJavaElementRenameChange extends ResourceChange {
28
29         private final String fNewName;
30
31         private final String fOldName;
32
33         private final IPath fResourcePath;
34
35         private final long fStampToRestore;
36
37         protected AbstractJavaElementRenameChange(IPath resourcePath, String oldName, String newName) {
38                 this(resourcePath, oldName, newName, IResource.NULL_STAMP);
39         }
40
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;
45                 fOldName= oldName;
46                 fNewName= newName;
47                 fStampToRestore= stampToRestore;
48         }
49
50         protected abstract IPath createNewPath();
51
52         protected abstract Change createUndoChange(long stampToRestore) throws CoreException;
53
54         protected abstract void doRename(IProgressMonitor pm) throws CoreException;
55
56         @Override
57         public Object getModifiedElement() {
58                 return JavaCore.create(getResource());
59         }
60
61         @Override
62         protected IResource getModifiedResource() {
63                 return getResource();
64         }
65
66         public String getNewName() {
67                 return fNewName;
68         }
69
70         public String getOldName() {
71                 return fOldName;
72         }
73
74         protected final IResource getResource() {
75                 return ResourcesPlugin.getWorkspace().getRoot().findMember(fResourcePath);
76         }
77
78         protected IPath getResourcePath() {
79                 return fResourcePath;
80         }
81
82         @Override
83         public final Change perform(IProgressMonitor pm) throws CoreException {
84                 try {
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);
93                         }
94                         return result;
95                 } finally {
96                         pm.done();
97                 }
98         }
99 }