KickJava   Java API By Example, From Geeks To Geeks.

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


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.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.IProject;
20 import org.eclipse.core.resources.IProjectDescription;
21 import org.eclipse.core.resources.IResource;
22
23 import org.eclipse.ltk.core.refactoring.Change;
24 import org.eclipse.ltk.core.refactoring.RefactoringStatus;
25
26 import org.eclipse.jdt.core.IClasspathEntry;
27 import org.eclipse.jdt.core.IJavaProject;
28 import org.eclipse.jdt.core.JavaCore;
29 import org.eclipse.jdt.core.JavaModelException;
30
31 import org.eclipse.jdt.internal.corext.refactoring.AbstractJavaElementRenameChange;
32 import org.eclipse.jdt.internal.corext.refactoring.RefactoringCoreMessages;
33 import org.eclipse.jdt.internal.corext.util.Messages;
34
35 public final class RenameJavaProjectChange extends AbstractJavaElementRenameChange {
36
37     private boolean fUpdateReferences;
38
39     public RenameJavaProjectChange(IJavaProject project, String JavaDoc newName, boolean updateReferences) {
40         this(project.getPath(), project.getElementName(), newName, IResource.NULL_STAMP, updateReferences);
41         Assert.isTrue(!project.isReadOnly(), "should not be read only"); //$NON-NLS-1$
42
}
43
44     private RenameJavaProjectChange(IPath resourcePath, String JavaDoc oldName, String JavaDoc newName, long stampToRestore, boolean updateReferences) {
45         super(resourcePath, oldName, newName);
46         fUpdateReferences= updateReferences;
47     }
48
49     private IClasspathEntry createModifiedEntry(IClasspathEntry oldEntry) {
50         return JavaCore.newProjectEntry(createNewPath(), oldEntry.getAccessRules(), oldEntry.combineAccessRules(), oldEntry.getExtraAttributes(), oldEntry.isExported());
51     }
52
53     protected IPath createNewPath() {
54         return getResourcePath().removeLastSegments(1).append(getNewName());
55     }
56
57     protected Change createUndoChange(long stampToRestore) throws JavaModelException {
58         return new RenameJavaProjectChange(createNewPath(), getNewName(), getOldName(), stampToRestore, fUpdateReferences);
59     }
60
61     protected void doRename(IProgressMonitor pm) throws CoreException {
62         try {
63             pm.beginTask(getName(), 2);
64             if (fUpdateReferences)
65                 modifyClassPaths(new SubProgressMonitor(pm, 1));
66             IProject project= getProject();
67             if (project != null) {
68                 IProjectDescription description= project.getDescription();
69                 description.setName(getNewName());
70                 project.move(description, IResource.FORCE | IResource.SHALLOW, new SubProgressMonitor(pm, 1));
71             }
72         } finally {
73             pm.done();
74         }
75     }
76
77     private IJavaProject getJavaProject() {
78         return (IJavaProject) getModifiedElement();
79     }
80
81     public String JavaDoc getName() {
82         return Messages.format(RefactoringCoreMessages.RenameJavaProjectChange_rename, new String JavaDoc[] { getOldName(), getNewName()});
83     }
84
85     private IProject getProject() {
86         IJavaProject jp= getJavaProject();
87         if (jp == null)
88             return null;
89         return jp.getProject();
90     }
91
92     private boolean isOurEntry(IClasspathEntry cpe) {
93         if (cpe.getEntryKind() != IClasspathEntry.CPE_PROJECT)
94             return false;
95         if (!cpe.getPath().equals(getResourcePath()))
96             return false;
97         return true;
98     }
99
100     public RefactoringStatus isValid(IProgressMonitor pm) throws CoreException {
101         return isValid(pm, DIRTY);
102     }
103
104     private void modifyClassPath(IJavaProject referencingProject, IProgressMonitor pm) throws JavaModelException {
105         pm.beginTask("", 1); //$NON-NLS-1$
106
IClasspathEntry[] oldEntries= referencingProject.getRawClasspath();
107         IClasspathEntry[] newEntries= new IClasspathEntry[oldEntries.length];
108         for (int i= 0; i < newEntries.length; i++) {
109             if (isOurEntry(oldEntries[i]))
110                 newEntries[i]= createModifiedEntry(oldEntries[i]);
111             else
112                 newEntries[i]= oldEntries[i];
113         }
114         referencingProject.setRawClasspath(newEntries, pm);
115         pm.done();
116     }
117
118     private void modifyClassPaths(IProgressMonitor pm) throws JavaModelException {
119         IProject[] referencing= getProject().getReferencingProjects();
120         pm.beginTask(RefactoringCoreMessages.RenameJavaProjectChange_update, referencing.length);
121         for (int i= 0; i < referencing.length; i++) {
122             IJavaProject jp= JavaCore.create(referencing[i]);
123             if (jp != null && jp.exists()) {
124                 modifyClassPath(jp, new SubProgressMonitor(pm, 1));
125             } else {
126                 pm.worked(1);
127             }
128         }
129         pm.done();
130     }
131 }
132
Popular Tags