KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > corext > refactoring > changes > RenameResourceChange


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.changes;
12
13 import org.eclipse.core.runtime.CoreException;
14 import org.eclipse.core.runtime.IPath;
15 import org.eclipse.core.runtime.IProgressMonitor;
16
17 import org.eclipse.core.resources.IResource;
18 import org.eclipse.core.resources.ResourcesPlugin;
19
20 import org.eclipse.ltk.core.refactoring.Change;
21 import org.eclipse.ltk.core.refactoring.ChangeDescriptor;
22 import org.eclipse.ltk.core.refactoring.RefactoringChangeDescriptor;
23 import org.eclipse.ltk.core.refactoring.RefactoringDescriptor;
24 import org.eclipse.ltk.core.refactoring.RefactoringStatus;
25
26 import org.eclipse.jdt.internal.corext.refactoring.RefactoringCoreMessages;
27 import org.eclipse.jdt.internal.corext.refactoring.base.JDTChange;
28 import org.eclipse.jdt.internal.corext.util.Messages;
29
30 public final class RenameResourceChange extends JDTChange {
31
32     public static IPath renamedResourcePath(IPath path, String JavaDoc newName) {
33         return path.removeLastSegments(1).append(newName);
34     }
35
36     private final String JavaDoc fComment;
37
38     private final RefactoringDescriptor fDescriptor;
39
40     private final String JavaDoc fNewName;
41
42     private final IPath fResourcePath;
43
44     private final long fStampToRestore;
45
46     private RenameResourceChange(RefactoringDescriptor descriptor, IPath resourcePath, String JavaDoc newName, String JavaDoc comment, long stampToRestore) {
47         fDescriptor= descriptor;
48         fResourcePath= resourcePath;
49         fNewName= newName;
50         fComment= comment;
51         fStampToRestore= stampToRestore;
52     }
53
54     public RenameResourceChange(RefactoringDescriptor descriptor, IResource resource, String JavaDoc newName, String JavaDoc comment) {
55         this(descriptor, resource.getFullPath(), newName, comment, IResource.NULL_STAMP);
56     }
57
58     public ChangeDescriptor getDescriptor() {
59         if (fDescriptor != null)
60             return new RefactoringChangeDescriptor(fDescriptor);
61         return null;
62     }
63
64     public Object JavaDoc getModifiedElement() {
65         return getResource();
66     }
67
68     public String JavaDoc getName() {
69         return Messages.format(RefactoringCoreMessages.RenameResourceChange_name, new String JavaDoc[] { fResourcePath.toString(), fNewName});
70     }
71
72     public String JavaDoc getNewName() {
73         return fNewName;
74     }
75
76     private IResource getResource() {
77         return ResourcesPlugin.getWorkspace().getRoot().findMember(fResourcePath);
78     }
79
80     public RefactoringStatus isValid(IProgressMonitor pm) throws CoreException {
81         IResource resource= getResource();
82         if (resource == null || !resource.exists()) {
83             return RefactoringStatus.createFatalErrorStatus(Messages.format(RefactoringCoreMessages.RenameResourceChange_does_not_exist, fResourcePath.toString()));
84         } else {
85             return super.isValid(pm, DIRTY);
86         }
87     }
88
89     public Change perform(IProgressMonitor pm) throws CoreException {
90         try {
91             pm.beginTask(RefactoringCoreMessages.RenameResourceChange_rename_resource, 1);
92
93             IResource resource= getResource();
94             long currentStamp= resource.getModificationStamp();
95             IPath newPath= renamedResourcePath(fResourcePath, fNewName);
96             resource.move(newPath, IResource.SHALLOW, pm);
97             if (fStampToRestore != IResource.NULL_STAMP) {
98                 IResource newResource= ResourcesPlugin.getWorkspace().getRoot().findMember(newPath);
99                 newResource.revertModificationStamp(fStampToRestore);
100             }
101             String JavaDoc oldName= fResourcePath.lastSegment();
102             return new RenameResourceChange(null, newPath, oldName, fComment, currentStamp);
103         } finally {
104             pm.done();
105         }
106     }
107 }
108
Popular Tags