KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > corext > refactoring > rename > RenameModifications


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.rename;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.Arrays JavaDoc;
15 import java.util.HashSet JavaDoc;
16 import java.util.Iterator JavaDoc;
17 import java.util.List JavaDoc;
18 import java.util.Set JavaDoc;
19
20 import org.eclipse.core.runtime.Assert;
21 import org.eclipse.core.runtime.CoreException;
22 import org.eclipse.core.runtime.IPath;
23
24 import org.eclipse.core.resources.IContainer;
25 import org.eclipse.core.resources.IFile;
26 import org.eclipse.core.resources.IFolder;
27 import org.eclipse.core.resources.IProject;
28 import org.eclipse.core.resources.IResource;
29 import org.eclipse.core.resources.ResourcesPlugin;
30 import org.eclipse.core.resources.mapping.IResourceChangeDescriptionFactory;
31
32 import org.eclipse.ltk.core.refactoring.RefactoringStatus;
33 import org.eclipse.ltk.core.refactoring.participants.IParticipantDescriptorFilter;
34 import org.eclipse.ltk.core.refactoring.participants.MoveArguments;
35 import org.eclipse.ltk.core.refactoring.participants.ParticipantManager;
36 import org.eclipse.ltk.core.refactoring.participants.RefactoringArguments;
37 import org.eclipse.ltk.core.refactoring.participants.RefactoringParticipant;
38 import org.eclipse.ltk.core.refactoring.participants.RefactoringProcessor;
39 import org.eclipse.ltk.core.refactoring.participants.RenameArguments;
40 import org.eclipse.ltk.core.refactoring.participants.SharableParticipants;
41 import org.eclipse.ltk.core.refactoring.participants.ValidateEditChecker;
42
43 import org.eclipse.jdt.core.ICompilationUnit;
44 import org.eclipse.jdt.core.IField;
45 import org.eclipse.jdt.core.IJavaElement;
46 import org.eclipse.jdt.core.IJavaProject;
47 import org.eclipse.jdt.core.ILocalVariable;
48 import org.eclipse.jdt.core.IMethod;
49 import org.eclipse.jdt.core.IPackageFragment;
50 import org.eclipse.jdt.core.IPackageFragmentRoot;
51 import org.eclipse.jdt.core.IType;
52 import org.eclipse.jdt.core.ITypeParameter;
53 import org.eclipse.jdt.core.refactoring.RenameTypeArguments;
54
55 import org.eclipse.jdt.internal.corext.refactoring.participants.ResourceModifications;
56 import org.eclipse.jdt.internal.corext.refactoring.reorg.RefactoringModifications;
57
58 public class RenameModifications extends RefactoringModifications {
59     
60     private List JavaDoc fRename;
61     private List JavaDoc fRenameArguments;
62     private List JavaDoc fParticipantDescriptorFilter;
63     
64     public RenameModifications() {
65         fRename= new ArrayList JavaDoc();
66         fRenameArguments= new ArrayList JavaDoc();
67         fParticipantDescriptorFilter= new ArrayList JavaDoc();
68     }
69     
70     public void rename(IResource resource, RenameArguments args) {
71         add(resource, args, null);
72     }
73
74     public void rename(IJavaProject project, RenameArguments args) {
75         add(project, args, null);
76         IProject rProject= project.getProject();
77         if (rProject != null) {
78             getResourceModifications().addRename(rProject, args);
79             IProject[] referencingProjects= rProject.getReferencingProjects();
80             for (int i= 0; i < referencingProjects.length; i++) {
81                 IFile classpath= getClasspathFile(referencingProjects[i]);
82                 if (classpath != null) {
83                     getResourceModifications().addChanged(classpath);
84                 }
85             }
86         }
87     }
88
89     public void rename(IPackageFragmentRoot sourceFolder, RenameArguments arguments) {
90         add(sourceFolder, arguments, null);
91         if (sourceFolder.getResource() != null) {
92             getResourceModifications().addRename(sourceFolder.getResource(), arguments);
93         }
94     }
95     
96     public void rename(IPackageFragment rootPackage, RenameArguments args, boolean renameSubPackages) throws CoreException {
97         add(rootPackage, args, null);
98         IPackageFragment[] allSubPackages= null;
99         if (renameSubPackages) {
100             allSubPackages= getSubpackages(rootPackage);
101             for (int i= 0; i < allSubPackages.length; i++) {
102                 IPackageFragment pack= allSubPackages[i];
103                 RenameArguments subArgs= new RenameArguments(
104                     getNewPackageName(rootPackage, args.getNewName(), pack.getElementName()),
105                     args.getUpdateReferences());
106                 add(pack, subArgs, null);
107             }
108         }
109         IContainer container= (IContainer)rootPackage.getResource();
110         if (container == null)
111             return;
112         IContainer target= (IContainer) ((IPackageFragmentRoot)rootPackage.getParent()).
113             getPackageFragment(args.getNewName()).getResource();
114         if ((!rootPackage.hasSubpackages() || renameSubPackages) && canMove(container, target)) {
115             createIncludingParents(target.getParent());
116             if (container.getParent().equals(target.getParent())) {
117                 getResourceModifications().addRename(container, new RenameArguments(target.getName(), args.getUpdateReferences()));
118             } else {
119                 // This is a little tricky. The problem is that the refactoring participants
120
// don't support a generic move like the resource API does. So for the delta
121
// we generate one move, however for the participants we have to generate single
122
// moves and deletes.
123
try {
124                     getResourceModifications().ignoreForDelta();
125                     addAllResourceModifications(rootPackage, args, renameSubPackages, allSubPackages);
126                 } finally {
127                     getResourceModifications().trackForDelta();
128                 }
129                 getResourceModifications().addDelta(new ResourceModifications.MoveDescription(container, target.getFullPath()));
130             }
131         } else {
132             addAllResourceModifications(rootPackage, args, renameSubPackages, allSubPackages);
133         }
134     }
135
136     public void rename(ICompilationUnit unit, RenameArguments args) {
137         add(unit, args, null);
138         if (unit.getResource() != null) {
139             getResourceModifications().addRename(unit.getResource(), new RenameArguments(args.getNewName(), args.getUpdateReferences()));
140         }
141     }
142     
143     public void rename(IType type, RenameTypeArguments args, IParticipantDescriptorFilter filter) {
144         add(type, args, filter);
145     }
146     
147     public void rename(IField field, RenameArguments args) {
148         add(field, args, null);
149     }
150     
151     public void rename(IMethod method, RenameArguments args) {
152         add(method, args, null);
153     }
154     
155     public void rename(ILocalVariable variable, RenameArguments args) {
156         add(variable, args, null);
157     }
158     
159     public void rename(ITypeParameter typeParameter, RenameArguments arguments) {
160         add(typeParameter, arguments, null);
161     }
162     
163     public void buildDelta(IResourceChangeDescriptionFactory builder) {
164         for (int i= 0; i < fRename.size(); i++) {
165             Object JavaDoc element= fRename.get(i);
166             if (element instanceof IResource) {
167                 ResourceModifications.buildMoveDelta(builder, (IResource) element, (RenameArguments) fRenameArguments.get(i));
168             }
169         }
170         getResourceModifications().buildDelta(builder);
171     }
172     
173     public void buildValidateEdits(ValidateEditChecker checker) {
174         for (Iterator JavaDoc iter= fRename.iterator(); iter.hasNext();) {
175             Object JavaDoc element= iter.next();
176             if (element instanceof ICompilationUnit) {
177                 ICompilationUnit unit= (ICompilationUnit)element;
178                 IResource resource= unit.getResource();
179                 if (resource != null && resource.getType() == IResource.FILE) {
180                     checker.addFile((IFile)resource);
181                 }
182             }
183         }
184     }
185
186     public RefactoringParticipant[] loadParticipants(RefactoringStatus status, RefactoringProcessor owner, String JavaDoc[] natures, SharableParticipants shared) {
187         List JavaDoc result= new ArrayList JavaDoc();
188         for (int i= 0; i < fRename.size(); i++) {
189             result.addAll(Arrays.asList(ParticipantManager.loadRenameParticipants(status,
190                 owner, fRename.get(i),
191                 (RenameArguments) fRenameArguments.get(i),
192                 (IParticipantDescriptorFilter) fParticipantDescriptorFilter.get(i),
193                 natures, shared)));
194         }
195         result.addAll(Arrays.asList(getResourceModifications().getParticipants(status, owner, natures, shared)));
196         return (RefactoringParticipant[]) result.toArray(new RefactoringParticipant[result.size()]);
197     }
198     
199     private void add(Object JavaDoc element, RefactoringArguments args, IParticipantDescriptorFilter filter) {
200         Assert.isNotNull(element);
201         Assert.isNotNull(args);
202         fRename.add(element);
203         fRenameArguments.add(args);
204         fParticipantDescriptorFilter.add(filter);
205     }
206     
207     private void addAllResourceModifications(IPackageFragment rootPackage, RenameArguments args, boolean renameSubPackages, IPackageFragment[] allSubPackages) throws CoreException {
208         IFolder target= addResourceModifications(rootPackage, args, rootPackage, renameSubPackages);
209         if (renameSubPackages) {
210             IContainer container= (IContainer) rootPackage.getResource();
211             if (container == null)
212                 return;
213             boolean removeContainer= ! container.contains(target);
214             for (int i= 0; i < allSubPackages.length; i++) {
215                 IPackageFragment pack= allSubPackages[i];
216                 IFolder subTarget= addResourceModifications(rootPackage, args, pack, renameSubPackages);
217                 if (container.contains(subTarget))
218                     removeContainer= false;
219             }
220             if (removeContainer) {
221                 getResourceModifications().addDelete(container);
222             }
223         }
224     }
225     
226     private IFolder addResourceModifications(IPackageFragment rootPackage, RenameArguments args, IPackageFragment pack, boolean renameSubPackages) throws CoreException {
227         IContainer container= (IContainer)pack.getResource();
228         if (container == null)
229             return null;
230         IFolder target= computeTargetFolder(rootPackage, args, pack);
231         createIncludingParents(target);
232         MoveArguments arguments= new MoveArguments(target, args.getUpdateReferences());
233         IResource[] resourcesToMove= collectResourcesOfInterest(pack);
234         Set JavaDoc allMembers= new HashSet JavaDoc(Arrays.asList(container.members()));
235         for (int i= 0; i < resourcesToMove.length; i++) {
236             IResource toMove= resourcesToMove[i];
237             getResourceModifications().addMove(toMove, arguments);
238             allMembers.remove(toMove);
239         }
240         for (Iterator JavaDoc iter= allMembers.iterator(); iter.hasNext();) {
241             IResource element= (IResource) iter.next();
242             if (element instanceof IFile) {
243                 getResourceModifications().addDelete(element);
244                 iter.remove();
245             }
246         }
247         if (! renameSubPackages && allMembers.isEmpty()) {
248             getResourceModifications().addDelete(container);
249         }
250         return target;
251     }
252
253     private boolean canMove(IContainer source, IContainer target) {
254         return !target.exists() && !source.getFullPath().isPrefixOf(target.getFullPath());
255     }
256
257     private IPackageFragment[] getSubpackages(IPackageFragment pack) throws CoreException {
258         IPackageFragmentRoot root= (IPackageFragmentRoot) pack.getParent();
259         IJavaElement[] allPackages= root.getChildren();
260         if (pack.isDefaultPackage())
261             return new IPackageFragment[0];
262         ArrayList JavaDoc result= new ArrayList JavaDoc();
263         String JavaDoc prefix= pack.getElementName() + '.';
264         for (int i= 0; i < allPackages.length; i++) {
265             IPackageFragment currentPackage= (IPackageFragment) allPackages[i];
266             if (currentPackage.getElementName().startsWith(prefix))
267                 result.add(currentPackage);
268         }
269         return (IPackageFragment[]) result.toArray(new IPackageFragment[result.size()]);
270     }
271     
272     private IFolder computeTargetFolder(IPackageFragment rootPackage, RenameArguments args, IPackageFragment pack) {
273         IPath path= pack.getParent().getPath();
274         path= path.append(getNewPackageName(rootPackage, args.getNewName(), pack.getElementName()).replace('.', IPath.SEPARATOR));
275         IFolder target= ResourcesPlugin.getWorkspace().getRoot().getFolder(path);
276         return target;
277     }
278     
279     private String JavaDoc getNewPackageName(IPackageFragment rootPackage, String JavaDoc newPackageName, String JavaDoc oldSubPackageName) {
280         String JavaDoc oldPackageName= rootPackage.getElementName();
281         return newPackageName + oldSubPackageName.substring(oldPackageName.length());
282     }
283 }
284
Popular Tags