KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.util.HashMap JavaDoc;
14 import java.util.Map JavaDoc;
15
16 import org.eclipse.core.resources.*;
17 import org.eclipse.core.runtime.CoreException;
18 import org.eclipse.core.runtime.IPath;
19 import org.eclipse.jdt.core.*;
20
21 public class DeletePackageFragmentRootOperation extends JavaModelOperation {
22
23     int updateResourceFlags;
24     int updateModelFlags;
25
26     public DeletePackageFragmentRootOperation(
27         IPackageFragmentRoot root,
28         int updateResourceFlags,
29         int updateModelFlags) {
30             
31         super(root);
32         this.updateResourceFlags = updateResourceFlags;
33         this.updateModelFlags = updateModelFlags;
34     }
35
36     protected void executeOperation() throws JavaModelException {
37         
38         IPackageFragmentRoot root = (IPackageFragmentRoot)this.getElementToProcess();
39         IClasspathEntry rootEntry = root.getRawClasspathEntry();
40         
41         // remember olds roots
42
DeltaProcessor deltaProcessor = JavaModelManager.getJavaModelManager().getDeltaProcessor();
43         if (deltaProcessor.oldRoots == null)
44             deltaProcessor.oldRoots = new HashMap JavaDoc();
45         
46         // update classpath if needed
47
if ((updateModelFlags & IPackageFragmentRoot.ORIGINATING_PROJECT_CLASSPATH) != 0) {
48             updateProjectClasspath(rootEntry.getPath(), root.getJavaProject(), deltaProcessor.oldRoots);
49         }
50         if ((updateModelFlags & IPackageFragmentRoot.OTHER_REFERRING_PROJECTS_CLASSPATH) != 0) {
51             updateReferringProjectClasspaths(rootEntry.getPath(), root.getJavaProject(), deltaProcessor.oldRoots);
52         }
53         
54         // delete resource
55
if (!root.isExternal() && (this.updateModelFlags & IPackageFragmentRoot.NO_RESOURCE_MODIFICATION) == 0) {
56             deleteResource(root, rootEntry);
57         }
58     }
59
60     protected void deleteResource(
61         IPackageFragmentRoot root,
62         IClasspathEntry rootEntry)
63         throws JavaModelException {
64         final char[][] exclusionPatterns = ((ClasspathEntry)rootEntry).fullExclusionPatternChars();
65         IResource rootResource = root.getResource();
66         if (rootEntry.getEntryKind() != IClasspathEntry.CPE_SOURCE || exclusionPatterns == null) {
67             try {
68                 rootResource.delete(this.updateResourceFlags, progressMonitor);
69             } catch (CoreException e) {
70                 throw new JavaModelException(e);
71             }
72         } else {
73             final IPath[] nestedFolders = getNestedFolders(root);
74             IResourceProxyVisitor visitor = new IResourceProxyVisitor() {
75                 public boolean visit(IResourceProxy proxy) throws CoreException {
76                     if (proxy.getType() == IResource.FOLDER) {
77                         IPath path = proxy.requestFullPath();
78                         if (prefixesOneOf(path, nestedFolders)) {
79                             // equals if nested source folder
80
return !equalsOneOf(path, nestedFolders);
81                         } else {
82                             // subtree doesn't contain any nested source folders
83
proxy.requestResource().delete(updateResourceFlags, progressMonitor);
84                             return false;
85                         }
86                     } else {
87                         proxy.requestResource().delete(updateResourceFlags, progressMonitor);
88                         return false;
89                     }
90                 }
91             };
92             try {
93                 rootResource.accept(visitor, IResource.NONE);
94             } catch (CoreException e) {
95                 throw new JavaModelException(e);
96             }
97         }
98         setAttribute(HAS_MODIFIED_RESOURCE_ATTR, TRUE);
99     }
100
101
102     /*
103      * Deletes the classpath entries equals to the given rootPath from all Java projects.
104      */

105     protected void updateReferringProjectClasspaths(IPath rootPath, IJavaProject projectOfRoot, Map JavaDoc oldRoots) throws JavaModelException {
106         IJavaModel model = this.getJavaModel();
107         IJavaProject[] projects = model.getJavaProjects();
108         for (int i = 0, length = projects.length; i < length; i++) {
109             IJavaProject project = projects[i];
110             if (project.equals(projectOfRoot)) continue;
111             updateProjectClasspath(rootPath, project, oldRoots);
112         }
113     }
114
115     /*
116      * Deletes the classpath entries equals to the given rootPath from the given project.
117      */

118     protected void updateProjectClasspath(IPath rootPath, IJavaProject project, Map JavaDoc oldRoots) throws JavaModelException {
119         // remember old roots
120
oldRoots.put(project, project.getPackageFragmentRoots());
121         
122         IClasspathEntry[] classpath = project.getRawClasspath();
123         IClasspathEntry[] newClasspath = null;
124         int cpLength = classpath.length;
125         int newCPIndex = -1;
126         for (int j = 0; j < cpLength; j++) {
127             IClasspathEntry entry = classpath[j];
128             if (rootPath.equals(entry.getPath())) {
129                 if (newClasspath == null) {
130                     newClasspath = new IClasspathEntry[cpLength-1];
131                     System.arraycopy(classpath, 0, newClasspath, 0, j);
132                     newCPIndex = j;
133                 }
134             } else if (newClasspath != null) {
135                 newClasspath[newCPIndex++] = entry;
136             }
137         }
138         if (newClasspath != null) {
139             if (newCPIndex < newClasspath.length) {
140                 System.arraycopy(newClasspath, 0, newClasspath = new IClasspathEntry[newCPIndex], 0, newCPIndex);
141             }
142             project.setRawClasspath(newClasspath, progressMonitor);
143         }
144     }
145     protected IJavaModelStatus verify() {
146         IJavaModelStatus status = super.verify();
147         if (!status.isOK()) {
148             return status;
149         }
150         IPackageFragmentRoot root = (IPackageFragmentRoot) this.getElementToProcess();
151         if (root == null || !root.exists()) {
152             return new JavaModelStatus(IJavaModelStatusConstants.ELEMENT_DOES_NOT_EXIST, root);
153         }
154
155         IResource resource = root.getResource();
156         if (resource instanceof IFolder) {
157             if (resource.isLinked()) {
158                 return new JavaModelStatus(IJavaModelStatusConstants.INVALID_RESOURCE, root);
159             }
160         }
161         return JavaModelStatus.VERIFIED_OK;
162     }
163
164 }
165
Popular Tags