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