KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > ui > refactoring > RenameProjectChange


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