KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > core > MovePackageFragmentRootOperation


1 /*******************************************************************************
2  * Copyright (c) 2000, 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.jdt.internal.core;
12
13 import org.eclipse.core.resources.*;
14 import org.eclipse.core.runtime.CoreException;
15 import org.eclipse.core.runtime.IPath;
16 import org.eclipse.jdt.core.*;
17
18 public class MovePackageFragmentRootOperation extends CopyPackageFragmentRootOperation {
19     /*
20      * Renames the classpath entries equal to the given path in the given project.
21      * If an entry with the destination path already existed, remove it.
22      */

23     protected void renameEntryInClasspath(IPath rootPath, IJavaProject project) throws JavaModelException {
24             
25         IClasspathEntry[] classpath = project.getRawClasspath();
26         IClasspathEntry[] newClasspath = null;
27         int cpLength = classpath.length;
28         int newCPIndex = -1;
29
30         for (int i = 0; i < cpLength; i++) {
31             IClasspathEntry entry = classpath[i];
32             IPath entryPath = entry.getPath();
33             if (rootPath.equals(entryPath)) {
34                 // rename entry
35
if (newClasspath == null) {
36                     newClasspath = new IClasspathEntry[cpLength];
37                     System.arraycopy(classpath, 0, newClasspath, 0, i);
38                     newCPIndex = i;
39                 }
40                 newClasspath[newCPIndex++] = copy(entry);
41             } else if (this.destination.equals(entryPath)) {
42                 // remove entry equals to destination
43
if (newClasspath == null) {
44                     newClasspath = new IClasspathEntry[cpLength];
45                     System.arraycopy(classpath, 0, newClasspath, 0, i);
46                     newCPIndex = i;
47                 }
48             } else if (entry.getEntryKind() == IClasspathEntry.CPE_SOURCE) {
49                 // update exclusion/inclusion patterns
50
IPath projectRelativePath = rootPath.removeFirstSegments(1);
51                 IPath[] newExclusionPatterns = renamePatterns(projectRelativePath, entry.getExclusionPatterns());
52                 IPath[] newInclusionPatterns = renamePatterns(projectRelativePath, entry.getInclusionPatterns());
53                 if (newExclusionPatterns != null || newInclusionPatterns != null) {
54                     if (newClasspath == null) {
55                         newClasspath = new IClasspathEntry[cpLength];
56                         System.arraycopy(classpath, 0, newClasspath, 0, i);
57                         newCPIndex = i;
58                     }
59                     newClasspath[newCPIndex++] =
60                         JavaCore.newSourceEntry(
61                             entry.getPath(),
62                             newInclusionPatterns == null ? entry.getInclusionPatterns() : newInclusionPatterns,
63                             newExclusionPatterns == null ? entry.getExclusionPatterns() : newExclusionPatterns,
64                             entry.getOutputLocation(),
65                             entry.getExtraAttributes());
66                 } else if (newClasspath != null) {
67                     newClasspath[newCPIndex++] = entry;
68                 }
69             } else if (newClasspath != null) {
70                 newClasspath[newCPIndex++] = entry;
71             }
72         }
73         
74         if (newClasspath != null) {
75             if (newCPIndex < newClasspath.length) {
76                 System.arraycopy(newClasspath, 0, newClasspath = new IClasspathEntry[newCPIndex], 0, newCPIndex);
77             }
78             IJavaModelStatus status = JavaConventions.validateClasspath(project, newClasspath, project.getOutputLocation());
79             if (status.isOK())
80                 project.setRawClasspath(newClasspath, progressMonitor);
81             // don't update classpath if status is not ok to avoid JavaModelException (see https://bugs.eclipse.org/bugs/show_bug.cgi?id=129991)
82
}
83     }
84
85     private IPath[] renamePatterns(IPath rootPath, IPath[] patterns) {
86         IPath[] newPatterns = null;
87         int newPatternsIndex = -1;
88         for (int i = 0, length = patterns.length; i < length; i++) {
89             IPath pattern = patterns[i];
90             if (pattern.equals(rootPath)) {
91                 if (newPatterns == null) {
92                     newPatterns = new IPath[length];
93                     System.arraycopy(patterns, 0, newPatterns, 0, i);
94                     newPatternsIndex = i;
95                 }
96                 IPath newPattern = this.destination.removeFirstSegments(1);
97                 if (pattern.hasTrailingSeparator())
98                     newPattern = newPattern.addTrailingSeparator();
99                 newPatterns[newPatternsIndex++] = newPattern;
100             }
101         }
102         return newPatterns;
103     }
104
105     public MovePackageFragmentRootOperation(
106         IPackageFragmentRoot root,
107         IPath destination,
108         int updateResourceFlags,
109         int updateModelFlags,
110         IClasspathEntry sibling) {
111             
112         super(
113             root,
114             destination,
115             updateResourceFlags,
116             updateModelFlags,
117             sibling);
118     }
119     protected void executeOperation() throws JavaModelException {
120         
121         IPackageFragmentRoot root = (IPackageFragmentRoot)this.getElementToProcess();
122         IClasspathEntry rootEntry = root.getRawClasspathEntry();
123         IWorkspaceRoot workspaceRoot = ResourcesPlugin.getWorkspace().getRoot();
124         
125         // move resource
126
if (!root.isExternal() && (this.updateModelFlags & IPackageFragmentRoot.NO_RESOURCE_MODIFICATION) == 0) {
127             moveResource(root, rootEntry, workspaceRoot);
128         }
129         
130         // update refering projects classpath excluding orignating project
131
IJavaProject originatingProject = root.getJavaProject();
132         if ((this.updateModelFlags & IPackageFragmentRoot.OTHER_REFERRING_PROJECTS_CLASSPATH) != 0) {
133             updateReferringProjectClasspaths(rootEntry.getPath(), originatingProject);
134         }
135
136         boolean isRename = this.destination.segment(0).equals(originatingProject.getElementName());
137         boolean updateOriginating = (this.updateModelFlags & IPackageFragmentRoot.ORIGINATING_PROJECT_CLASSPATH) != 0;
138         boolean updateDestination = (this.updateModelFlags & IPackageFragmentRoot.DESTINATION_PROJECT_CLASSPATH) != 0;
139
140         // update originating classpath
141
if (updateOriginating) {
142             if (isRename && updateDestination) {
143                 renameEntryInClasspath(rootEntry.getPath(), originatingProject);
144             } else {
145                 removeEntryFromClasspath(rootEntry.getPath(), originatingProject);
146             }
147         }
148         
149         // update destination classpath
150
if (updateDestination) {
151             if (!isRename || !updateOriginating) {
152                 addEntryToClasspath(rootEntry, workspaceRoot);
153             } // else reference has been updated when updating originating project classpath
154
}
155     }
156     protected void moveResource(
157         IPackageFragmentRoot root,
158         IClasspathEntry rootEntry,
159         final IWorkspaceRoot workspaceRoot)
160         throws JavaModelException {
161             
162         final char[][] exclusionPatterns = ((ClasspathEntry)rootEntry).fullExclusionPatternChars();
163         IResource rootResource = root.getResource();
164         if (rootEntry.getEntryKind() != IClasspathEntry.CPE_SOURCE || exclusionPatterns == null) {
165             try {
166                 IResource destRes;
167                 if ((this.updateModelFlags & IPackageFragmentRoot.REPLACE) != 0
168                         && (destRes = workspaceRoot.findMember(this.destination)) != null) {
169                     destRes.delete(this.updateResourceFlags, progressMonitor);
170                 }
171                 rootResource.move(this.destination, this.updateResourceFlags, progressMonitor);
172             } catch (CoreException e) {
173                 throw new JavaModelException(e);
174             }
175         } else {
176             final int sourceSegmentCount = rootEntry.getPath().segmentCount();
177             final IFolder destFolder = workspaceRoot.getFolder(this.destination);
178             final IPath[] nestedFolders = getNestedFolders(root);
179             IResourceProxyVisitor visitor = new IResourceProxyVisitor() {
180                 public boolean visit(IResourceProxy proxy) throws CoreException {
181                     if (proxy.getType() == IResource.FOLDER) {
182                         IPath path = proxy.requestFullPath();
183                         if (prefixesOneOf(path, nestedFolders)) {
184                             if (equalsOneOf(path, nestedFolders)) {
185                                 // nested source folder
186
return false;
187                             } else {
188                                 // folder containing nested source folder
189
IFolder folder = destFolder.getFolder(path.removeFirstSegments(sourceSegmentCount));
190                                 if ((updateModelFlags & IPackageFragmentRoot.REPLACE) != 0
191                                         && folder.exists()) {
192                                     return true;
193                                 }
194                                 folder.create(updateResourceFlags, true, progressMonitor);
195                                 return true;
196                             }
197                         } else {
198                             // subtree doesn't contain any nested source folders
199
IPath destPath = destination.append(path.removeFirstSegments(sourceSegmentCount));
200                             IResource destRes;
201                             if ((updateModelFlags & IPackageFragmentRoot.REPLACE) != 0
202                                     && (destRes = workspaceRoot.findMember(destPath)) != null) {
203                                 destRes.delete(updateResourceFlags, progressMonitor);
204                             }
205                             proxy.requestResource().move(destPath, updateResourceFlags, progressMonitor);
206                             return false;
207                         }
208                     } else {
209                         IPath path = proxy.requestFullPath();
210                         IPath destPath = destination.append(path.removeFirstSegments(sourceSegmentCount));
211                         IResource destRes;
212                         if ((updateModelFlags & IPackageFragmentRoot.REPLACE) != 0
213                                 && (destRes = workspaceRoot.findMember(destPath)) != null) {
214                             destRes.delete(updateResourceFlags, progressMonitor);
215                         }
216                         proxy.requestResource().move(destPath, updateResourceFlags, progressMonitor);
217                         return false;
218                     }
219                 }
220             };
221             try {
222                 rootResource.accept(visitor, IResource.NONE);
223             } catch (CoreException e) {
224                 throw new JavaModelException(e);
225             }
226         }
227         setAttribute(HAS_MODIFIED_RESOURCE_ATTR, TRUE);
228     }
229     /*
230      * Renames the classpath entries equal to the given path in all Java projects.
231      */

232     protected void updateReferringProjectClasspaths(IPath rootPath, IJavaProject projectOfRoot) throws JavaModelException {
233         IJavaModel model = this.getJavaModel();
234         IJavaProject[] projects = model.getJavaProjects();
235         for (int i = 0, length = projects.length; i < length; i++) {
236             IJavaProject project = projects[i];
237             if (project.equals(projectOfRoot)) continue;
238             renameEntryInClasspath(rootPath, project);
239         }
240     }
241     /*
242      * Removes the classpath entry equal to the given path from the given project's classpath.
243      */

244     protected void removeEntryFromClasspath(IPath rootPath, IJavaProject project) throws JavaModelException {
245         
246         IClasspathEntry[] classpath = project.getRawClasspath();
247         IClasspathEntry[] newClasspath = null;
248         int cpLength = classpath.length;
249         int newCPIndex = -1;
250         
251         for (int i = 0; i < cpLength; i++) {
252             IClasspathEntry entry = classpath[i];
253             if (rootPath.equals(entry.getPath())) {
254                 if (newClasspath == null) {
255                     newClasspath = new IClasspathEntry[cpLength];
256                     System.arraycopy(classpath, 0, newClasspath, 0, i);
257                     newCPIndex = i;
258                 }
259             } else if (newClasspath != null) {
260                 newClasspath[newCPIndex++] = entry;
261             }
262         }
263         
264         if (newClasspath != null) {
265             if (newCPIndex < newClasspath.length) {
266                 System.arraycopy(newClasspath, 0, newClasspath = new IClasspathEntry[newCPIndex], 0, newCPIndex);
267             }
268             project.setRawClasspath(newClasspath, progressMonitor);
269         }
270     }
271 }
272
Popular Tags