]>
Commit | Line | Data |
---|---|---|
1b2798f6 EK |
1 | /******************************************************************************* |
2 | * Copyright (c) 2000, 2012 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.rename; | |
12 | ||
13 | import java.net.URI; | |
14 | ||
15 | import org.eclipse.core.filesystem.EFS; | |
16 | import org.eclipse.core.filesystem.IFileStore; | |
17 | ||
18 | import org.eclipse.core.runtime.Assert; | |
19 | import org.eclipse.core.runtime.CoreException; | |
20 | import org.eclipse.core.runtime.IPath; | |
21 | import org.eclipse.core.runtime.IProgressMonitor; | |
22 | ||
23 | import org.eclipse.core.resources.IFile; | |
24 | import org.eclipse.core.resources.IResource; | |
25 | import org.eclipse.core.resources.ResourcesPlugin; | |
26 | ||
27 | import org.eclipse.ltk.core.refactoring.Change; | |
28 | import org.eclipse.ltk.core.refactoring.RefactoringDescriptor; | |
29 | import org.eclipse.ltk.core.refactoring.RefactoringStatus; | |
30 | import org.eclipse.ltk.core.refactoring.participants.CheckConditionsContext; | |
31 | import org.eclipse.ltk.core.refactoring.participants.RenameArguments; | |
32 | ||
33 | import org.eclipse.jdt.core.IJavaElement; | |
34 | import org.eclipse.jdt.core.IJavaProject; | |
35 | import org.eclipse.jdt.core.JavaCore; | |
36 | import org.eclipse.jdt.core.refactoring.IJavaRefactorings; | |
37 | import org.eclipse.jdt.core.refactoring.descriptors.RenameJavaElementDescriptor; | |
38 | ||
39 | import org.eclipse.jdt.internal.core.refactoring.descriptors.RefactoringSignatureDescriptorFactory; | |
40 | import org.eclipse.jdt.internal.corext.refactoring.JDTRefactoringDescriptorComment; | |
41 | import org.eclipse.jdt.internal.corext.refactoring.JavaRefactoringArguments; | |
42 | import org.eclipse.jdt.internal.corext.refactoring.JavaRefactoringDescriptorUtil; | |
43 | import org.eclipse.jdt.internal.corext.refactoring.RefactoringAvailabilityTester; | |
44 | import org.eclipse.jdt.internal.corext.refactoring.RefactoringCoreMessages; | |
45 | import org.eclipse.jdt.internal.corext.refactoring.changes.DynamicValidationRefactoringChange; | |
46 | import org.eclipse.jdt.internal.corext.refactoring.changes.RenameJavaProjectChange; | |
47 | import org.eclipse.jdt.internal.corext.refactoring.participants.JavaProcessors; | |
48 | import org.eclipse.jdt.internal.corext.refactoring.tagging.IReferenceUpdating; | |
49 | import org.eclipse.jdt.internal.corext.util.Messages; | |
50 | import org.eclipse.jdt.internal.corext.util.Resources; | |
51 | ||
52 | import org.eclipse.jdt.ui.refactoring.IRefactoringProcessorIds; | |
53 | import org.eclipse.jdt.ui.refactoring.RefactoringSaveHelper; | |
54 | ||
55 | import org.eclipse.jdt.internal.ui.viewsupport.BasicElementLabels; | |
56 | ||
57 | public final class RenameJavaProjectProcessor extends JavaRenameProcessor implements IReferenceUpdating { | |
58 | ||
59 | private IJavaProject fProject; | |
60 | private boolean fUpdateReferences; | |
61 | ||
62 | /** | |
63 | * Creates a new rename java project processor. | |
64 | * @param project the java project, or <code>null</code> if invoked by scripting | |
65 | */ | |
66 | public RenameJavaProjectProcessor(IJavaProject project) { | |
67 | fProject= project; | |
68 | if (fProject != null) | |
69 | setNewElementName(fProject.getElementName()); | |
70 | fUpdateReferences= true; | |
71 | } | |
72 | ||
73 | public RenameJavaProjectProcessor(JavaRefactoringArguments arguments, RefactoringStatus status) { | |
74 | this(null); | |
75 | RefactoringStatus initializeStatus= initialize(arguments); | |
76 | status.merge(initializeStatus); | |
77 | } | |
78 | ||
79 | @Override | |
80 | public String getIdentifier() { | |
81 | return IRefactoringProcessorIds.RENAME_JAVA_PROJECT_PROCESSOR; | |
82 | } | |
83 | ||
84 | @Override | |
85 | public boolean isApplicable() throws CoreException { | |
86 | return RefactoringAvailabilityTester.isRenameAvailable(fProject); | |
87 | } | |
88 | ||
89 | @Override | |
90 | public String getProcessorName() { | |
91 | return RefactoringCoreMessages.RenameJavaProjectRefactoring_rename; | |
92 | } | |
93 | ||
94 | @Override | |
95 | protected String[] getAffectedProjectNatures() throws CoreException { | |
96 | return JavaProcessors.computeAffectedNatures(fProject); | |
97 | } | |
98 | ||
99 | @Override | |
100 | public Object[] getElements() { | |
101 | return new Object[] {fProject}; | |
102 | } | |
103 | ||
104 | public Object getNewElement() { | |
105 | IPath newPath= fProject.getPath().removeLastSegments(1).append(getNewElementName()); | |
106 | return JavaCore.create(ResourcesPlugin.getWorkspace().getRoot().findMember(newPath)); | |
107 | } | |
108 | ||
109 | @Override | |
110 | protected RenameModifications computeRenameModifications() throws CoreException { | |
111 | RenameModifications result= new RenameModifications(); | |
112 | result.rename(fProject, new RenameArguments(getNewElementName(), getUpdateReferences())); | |
113 | return result; | |
114 | } | |
115 | ||
116 | @Override | |
117 | protected IFile[] getChangedFiles() throws CoreException { | |
118 | IFile projectFile= fProject.getProject().getFile(".project"); //$NON-NLS-1$ | |
119 | if (projectFile != null && projectFile.exists()) { | |
120 | return new IFile[] {projectFile}; | |
121 | } | |
122 | return new IFile[0]; | |
123 | } | |
124 | ||
125 | @Override | |
126 | public int getSaveMode() { | |
127 | return RefactoringSaveHelper.SAVE_ALL; | |
128 | } | |
129 | ||
130 | //---- IReferenceUpdating -------------------------------------- | |
131 | ||
132 | public void setUpdateReferences(boolean update) { | |
133 | fUpdateReferences= update; | |
134 | } | |
135 | ||
136 | public boolean getUpdateReferences() { | |
137 | return fUpdateReferences; | |
138 | } | |
139 | ||
140 | //---- IRenameProcessor ---------------------------------------------- | |
141 | ||
142 | public String getCurrentElementName() { | |
143 | return fProject.getElementName(); | |
144 | } | |
145 | ||
146 | @Override | |
147 | public RefactoringStatus checkInitialConditions(IProgressMonitor pm) throws CoreException { | |
148 | return new RefactoringStatus(); | |
149 | } | |
150 | ||
151 | public RefactoringStatus checkNewElementName(String newName) throws CoreException { | |
152 | Assert.isNotNull(newName, "new name"); //$NON-NLS-1$ | |
153 | RefactoringStatus result= RefactoringStatus.create(ResourcesPlugin.getWorkspace().validateName(newName, IResource.PROJECT)); | |
154 | if (result.hasFatalError()) | |
155 | return result; | |
156 | ||
157 | if (projectNameAlreadyExists(newName)) | |
158 | return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.RenameJavaProjectRefactoring_already_exists); | |
159 | if (projectFolderAlreadyExists(newName)) | |
160 | return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.RenameJavaProjectProcessor_folder_already_exists); | |
161 | ||
162 | return new RefactoringStatus(); | |
163 | } | |
164 | ||
165 | @Override | |
166 | protected RefactoringStatus doCheckFinalConditions(IProgressMonitor pm, CheckConditionsContext context) throws CoreException { | |
167 | pm.beginTask("", 1); //$NON-NLS-1$ | |
168 | try{ | |
169 | if (isReadOnly()){ | |
170 | String message= Messages.format(RefactoringCoreMessages.RenameJavaProjectRefactoring_read_only, | |
171 | BasicElementLabels.getJavaElementName(fProject.getElementName())); | |
172 | return RefactoringStatus.createErrorStatus(message); | |
173 | } | |
174 | return new RefactoringStatus(); | |
175 | } finally{ | |
176 | pm.done(); | |
177 | } | |
178 | } | |
179 | ||
180 | private boolean isReadOnly() { | |
181 | return Resources.isReadOnly(fProject.getResource()); | |
182 | } | |
183 | ||
184 | private boolean projectNameAlreadyExists(String newName){ | |
185 | return ResourcesPlugin.getWorkspace().getRoot().getProject(newName).exists(); | |
186 | } | |
187 | ||
188 | private boolean projectFolderAlreadyExists(String newName) throws CoreException { | |
189 | boolean isNotInWorkpace= fProject.getProject().getDescription().getLocationURI() != null; | |
190 | if (isNotInWorkpace) | |
191 | return false; // projects outside of the workspace are not renamed | |
192 | ||
193 | URI locationURI= fProject.getProject().getLocationURI(); | |
194 | IFileStore projectStore= EFS.getStore(locationURI); | |
195 | ||
196 | if (!projectStore.getFileSystem().isCaseSensitive() && newName.equalsIgnoreCase(fProject.getElementName())) | |
197 | return false; // allow to change case | |
198 | ||
199 | IFileStore newProjectStore= projectStore.getParent().getChild(newName); | |
200 | ||
201 | return newProjectStore.fetchInfo().exists(); | |
202 | } | |
203 | ||
204 | @Override | |
205 | public Change createChange(IProgressMonitor monitor) throws CoreException { | |
206 | try { | |
207 | monitor.beginTask("", 1); //$NON-NLS-1$ | |
208 | final String description= Messages.format(RefactoringCoreMessages.RenameJavaProjectProcessor_descriptor_description_short, BasicElementLabels.getJavaElementName(fProject.getElementName())); | |
209 | final String header= Messages.format(RefactoringCoreMessages.RenameJavaProjectChange_descriptor_description, new String[] { BasicElementLabels.getJavaElementName(fProject.getElementName()), BasicElementLabels.getJavaElementName(getNewElementName())}); | |
210 | final String comment= new JDTRefactoringDescriptorComment(null, this, header).asString(); | |
211 | final int flags= RefactoringDescriptor.STRUCTURAL_CHANGE | RefactoringDescriptor.MULTI_CHANGE | RefactoringDescriptor.BREAKING_CHANGE; | |
212 | final RenameJavaElementDescriptor descriptor= RefactoringSignatureDescriptorFactory.createRenameJavaElementDescriptor(IJavaRefactorings.RENAME_JAVA_PROJECT); | |
213 | descriptor.setProject(null); | |
214 | descriptor.setDescription(description); | |
215 | descriptor.setComment(comment); | |
216 | descriptor.setFlags(flags); | |
217 | descriptor.setJavaElement(fProject); | |
218 | descriptor.setNewName(getNewElementName()); | |
219 | descriptor.setUpdateReferences(fUpdateReferences); | |
220 | return new DynamicValidationRefactoringChange(descriptor, RefactoringCoreMessages.RenameJavaProjectRefactoring_rename, new Change[] { new RenameJavaProjectChange(fProject, getNewElementName(), fUpdateReferences)}); | |
221 | } finally { | |
222 | monitor.done(); | |
223 | } | |
224 | } | |
225 | ||
226 | private RefactoringStatus initialize(JavaRefactoringArguments extended) { | |
227 | final String handle= extended.getAttribute(JavaRefactoringDescriptorUtil.ATTRIBUTE_INPUT); | |
228 | if (handle != null) { | |
229 | final IJavaElement element= JavaRefactoringDescriptorUtil.handleToElement(extended.getProject(), handle, false); | |
230 | if (element == null || !element.exists() || element.getElementType() != IJavaElement.JAVA_PROJECT) | |
231 | return JavaRefactoringDescriptorUtil.createInputFatalStatus(element, getProcessorName(), IJavaRefactorings.RENAME_JAVA_PROJECT); | |
232 | else | |
233 | fProject= (IJavaProject) element; | |
234 | } else | |
235 | return RefactoringStatus.createFatalErrorStatus(Messages.format(RefactoringCoreMessages.InitializableRefactoring_argument_not_exist, JavaRefactoringDescriptorUtil.ATTRIBUTE_INPUT)); | |
236 | final String name= extended.getAttribute(JavaRefactoringDescriptorUtil.ATTRIBUTE_NAME); | |
237 | if (name != null && !"".equals(name)) //$NON-NLS-1$ | |
238 | setNewElementName(name); | |
239 | else | |
240 | return RefactoringStatus.createFatalErrorStatus(Messages.format(RefactoringCoreMessages.InitializableRefactoring_argument_not_exist, JavaRefactoringDescriptorUtil.ATTRIBUTE_NAME)); | |
241 | final String references= extended.getAttribute(JavaRefactoringDescriptorUtil.ATTRIBUTE_REFERENCES); | |
242 | if (references != null) { | |
243 | fUpdateReferences= Boolean.valueOf(references).booleanValue(); | |
244 | } else | |
245 | return RefactoringStatus.createFatalErrorStatus(Messages.format(RefactoringCoreMessages.InitializableRefactoring_argument_not_exist, JavaRefactoringDescriptorUtil.ATTRIBUTE_REFERENCES)); | |
246 | return new RefactoringStatus(); | |
247 | } | |
248 | } |