KickJava   Java API By Example, From Geeks To Geeks.

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


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.AssertionFailedException;
15 import org.eclipse.core.runtime.CoreException;
16 import org.eclipse.core.runtime.IPath;
17 import org.eclipse.jdt.core.*;
18 import org.eclipse.jdt.internal.core.util.Messages;
19
20 public class CopyPackageFragmentRootOperation extends JavaModelOperation {
21     IPath destination;
22     int updateResourceFlags;
23     int updateModelFlags;
24     IClasspathEntry sibling;
25
26     public CopyPackageFragmentRootOperation(
27         IPackageFragmentRoot root,
28         IPath destination,
29         int updateResourceFlags,
30         int updateModelFlags,
31         IClasspathEntry sibling) {
32             
33         super(root);
34         this.destination = destination;
35         this.updateResourceFlags = updateResourceFlags;
36         this.updateModelFlags = updateModelFlags;
37         this.sibling = sibling;
38     }
39     protected void executeOperation() throws JavaModelException {
40         
41         IPackageFragmentRoot root = (IPackageFragmentRoot)this.getElementToProcess();
42         IClasspathEntry rootEntry = root.getRawClasspathEntry();
43         IWorkspaceRoot workspaceRoot = ResourcesPlugin.getWorkspace().getRoot();
44
45         // copy resource
46
if (!root.isExternal() && (this.updateModelFlags & IPackageFragmentRoot.NO_RESOURCE_MODIFICATION) == 0) {
47             copyResource(root, rootEntry, workspaceRoot);
48         }
49         
50         // update classpath if needed
51
if ((this.updateModelFlags & IPackageFragmentRoot.DESTINATION_PROJECT_CLASSPATH) != 0) {
52             addEntryToClasspath(rootEntry, workspaceRoot);
53         }
54     }
55     protected void copyResource(
56         IPackageFragmentRoot root,
57         IClasspathEntry rootEntry,
58         final IWorkspaceRoot workspaceRoot)
59         throws JavaModelException {
60         final char[][] exclusionPatterns = ((ClasspathEntry)rootEntry).fullExclusionPatternChars();
61         IResource rootResource = root.getResource();
62         if (root.getKind() == IPackageFragmentRoot.K_BINARY || exclusionPatterns == null) {
63             try {
64                 IResource destRes;
65                 if ((this.updateModelFlags & IPackageFragmentRoot.REPLACE) != 0) {
66                     if (rootEntry.getPath().equals(this.destination)) return;
67                     if ((destRes = workspaceRoot.findMember(this.destination)) != null) {
68                         destRes.delete(this.updateResourceFlags, progressMonitor);
69                     }
70                 }
71                 rootResource.copy(this.destination, this.updateResourceFlags, progressMonitor);
72             } catch (CoreException e) {
73                 throw new JavaModelException(e);
74             }
75         } else {
76             final int sourceSegmentCount = rootEntry.getPath().segmentCount();
77             final IFolder destFolder = workspaceRoot.getFolder(this.destination);
78             final IPath[] nestedFolders = getNestedFolders(root);
79             IResourceProxyVisitor visitor = new IResourceProxyVisitor() {
80                 public boolean visit(IResourceProxy proxy) throws CoreException {
81                     if (proxy.getType() == IResource.FOLDER) {
82                         IPath path = proxy.requestFullPath();
83                         if (prefixesOneOf(path, nestedFolders)) {
84                             if (equalsOneOf(path, nestedFolders)) {
85                                 // nested source folder
86
return false;
87                             } else {
88                                 // folder containing nested source folder
89
IFolder folder = destFolder.getFolder(path.removeFirstSegments(sourceSegmentCount));
90                                 if ((updateModelFlags & IPackageFragmentRoot.REPLACE) != 0
91                                         && folder.exists()) {
92                                     return true;
93                                 }
94                                 folder.create(updateResourceFlags, true, progressMonitor);
95                                 return true;
96                             }
97                         } else {
98                             // subtree doesn't contain any nested source folders
99
IPath destPath = destination.append(path.removeFirstSegments(sourceSegmentCount));
100                             IResource destRes;
101                             if ((updateModelFlags & IPackageFragmentRoot.REPLACE) != 0
102                                     && (destRes = workspaceRoot.findMember(destPath)) != null) {
103                                 destRes.delete(updateResourceFlags, progressMonitor);
104                             }
105                             proxy.requestResource().copy(destPath, updateResourceFlags, progressMonitor);
106                             return false;
107                         }
108                     } else {
109                         IPath path = proxy.requestFullPath();
110                         IPath destPath = destination.append(path.removeFirstSegments(sourceSegmentCount));
111                         IResource destRes;
112                         if ((updateModelFlags & IPackageFragmentRoot.REPLACE) != 0
113                                 && (destRes = workspaceRoot.findMember(destPath)) != null) {
114                             destRes.delete(updateResourceFlags, progressMonitor);
115                         }
116                         proxy.requestResource().copy(destPath, updateResourceFlags, progressMonitor);
117                         return false;
118                     }
119                 }
120             };
121             try {
122                 rootResource.accept(visitor, IResource.NONE);
123             } catch (CoreException e) {
124                 throw new JavaModelException(e);
125             }
126         }
127         setAttribute(HAS_MODIFIED_RESOURCE_ATTR, TRUE);
128     }
129     protected void addEntryToClasspath(IClasspathEntry rootEntry, IWorkspaceRoot workspaceRoot) throws JavaModelException {
130         
131         IProject destProject = workspaceRoot.getProject(this.destination.segment(0));
132         IJavaProject jProject = JavaCore.create(destProject);
133         IClasspathEntry[] classpath = jProject.getRawClasspath();
134         int length = classpath.length;
135         IClasspathEntry[] newClasspath;
136         
137         // case of existing entry and REPLACE was specified
138
if ((this.updateModelFlags & IPackageFragmentRoot.REPLACE) != 0) {
139             // find existing entry
140
for (int i = 0; i < length; i++) {
141                 if (this.destination.equals(classpath[i].getPath())) {
142                     newClasspath = new IClasspathEntry[length];
143                     System.arraycopy(classpath, 0, newClasspath, 0, length);
144                     newClasspath[i] = copy(rootEntry);
145                     jProject.setRawClasspath(newClasspath, progressMonitor);
146                     return;
147                 }
148             }
149         }
150         
151         // other cases
152
int position;
153         if (this.sibling == null) {
154             // insert at the end
155
position = length;
156         } else {
157             // insert before sibling
158
position = -1;
159             for (int i = 0; i < length; i++) {
160                 if (this.sibling.equals(classpath[i])) {
161                     position = i;
162                     break;
163                 }
164             }
165         }
166         if (position == -1) {
167             throw new JavaModelException(new JavaModelStatus(IJavaModelStatusConstants.INVALID_SIBLING, this.sibling.toString()));
168         }
169         newClasspath = new IClasspathEntry[length+1];
170         if (position != 0) {
171             System.arraycopy(classpath, 0, newClasspath, 0, position);
172         }
173         if (position != length) {
174             System.arraycopy(classpath, position, newClasspath, position+1, length-position);
175         }
176         IClasspathEntry newEntry = copy(rootEntry);
177         newClasspath[position] = newEntry;
178         jProject.setRawClasspath(newClasspath, progressMonitor);
179     }
180     /*
181      * Copies the given classpath entry replacing its path with the destination path
182      * if it is a source folder or a library.
183      */

184     protected IClasspathEntry copy(IClasspathEntry entry) throws JavaModelException {
185         switch (entry.getEntryKind()) {
186             case IClasspathEntry.CPE_CONTAINER:
187                 return JavaCore.newContainerEntry(entry.getPath(), entry.getAccessRules(), entry.getExtraAttributes(), entry.isExported());
188             case IClasspathEntry.CPE_LIBRARY:
189                 try {
190                     return JavaCore.newLibraryEntry(this.destination, entry.getSourceAttachmentPath(), entry.getSourceAttachmentRootPath(), entry.getAccessRules(), entry.getExtraAttributes(), entry.isExported());
191                 } catch (AssertionFailedException e) {
192                     IJavaModelStatus status = new JavaModelStatus(IJavaModelStatusConstants.INVALID_PATH, e.getMessage());
193                     throw new JavaModelException(status);
194                 }
195             case IClasspathEntry.CPE_PROJECT:
196                 return JavaCore.newProjectEntry(entry.getPath(), entry.getAccessRules(), entry.combineAccessRules(), entry.getExtraAttributes(), entry.isExported());
197             case IClasspathEntry.CPE_SOURCE:
198                 return JavaCore.newSourceEntry(this.destination, entry.getInclusionPatterns(), entry.getExclusionPatterns(), entry.getOutputLocation(), entry.getExtraAttributes());
199             case IClasspathEntry.CPE_VARIABLE:
200                 try {
201                     return JavaCore.newVariableEntry(entry.getPath(), entry.getSourceAttachmentPath(), entry.getSourceAttachmentRootPath(), entry.getAccessRules(), entry.getExtraAttributes(), entry.isExported());
202                 } catch (AssertionFailedException e) {
203                     IJavaModelStatus status = new JavaModelStatus(IJavaModelStatusConstants.INVALID_PATH, e.getMessage());
204                     throw new JavaModelException(status);
205                 }
206             default:
207                 throw new JavaModelException(new JavaModelStatus(IJavaModelStatusConstants.ELEMENT_DOES_NOT_EXIST, this.getElementToProcess()));
208         }
209     }
210     public IJavaModelStatus verify() {
211         IJavaModelStatus status = super.verify();
212         if (!status.isOK()) {
213             return status;
214         }
215         IPackageFragmentRoot root = (IPackageFragmentRoot)getElementToProcess();
216         if (root == null || !root.exists()) {
217             return new JavaModelStatus(IJavaModelStatusConstants.ELEMENT_DOES_NOT_EXIST, root);
218         }
219
220         IResource resource = root.getResource();
221         if (resource instanceof IFolder) {
222             if (resource.isLinked()) {
223                 return new JavaModelStatus(IJavaModelStatusConstants.INVALID_RESOURCE, root);
224             }
225         }
226
227         if ((this.updateModelFlags & IPackageFragmentRoot.DESTINATION_PROJECT_CLASSPATH) != 0) {
228             String JavaDoc destProjectName = this.destination.segment(0);
229             IProject project = ResourcesPlugin.getWorkspace().getRoot().getProject(destProjectName);
230             if (JavaProject.hasJavaNature(project)) {
231                 try {
232                     IJavaProject destProject = JavaCore.create(project);
233                     IClasspathEntry[] destClasspath = destProject.getRawClasspath();
234                     boolean foundSibling = false;
235                     boolean foundExistingEntry = false;
236                     for (int i = 0, length = destClasspath.length; i < length; i++) {
237                         IClasspathEntry entry = destClasspath[i];
238                         if (entry.equals(this.sibling)) {
239                             foundSibling = true;
240                             break;
241                         }
242                         if (entry.getPath().equals(this.destination)) {
243                             foundExistingEntry = true;
244                         }
245                     }
246                     if (this.sibling != null && !foundSibling) {
247                         return new JavaModelStatus(IJavaModelStatusConstants.INVALID_SIBLING, this.sibling.toString());
248                     }
249                     if (foundExistingEntry && (this.updateModelFlags & IPackageFragmentRoot.REPLACE) == 0) {
250                         return new JavaModelStatus(
251                             IJavaModelStatusConstants.NAME_COLLISION,
252                             Messages.bind(Messages.status_nameCollision, new String JavaDoc[] {this.destination.toString()}));
253                     }
254                 } catch (JavaModelException e) {
255                     return e.getJavaModelStatus();
256                 }
257             }
258         }
259
260         return JavaModelStatus.VERIFIED_OK;
261     }
262 }
263
Popular Tags