KickJava   Java API By Example, From Geeks To Geeks.

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


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.core;
12
13 import org.eclipse.core.resources.*;
14 import org.eclipse.core.resources.IFile;
15 import org.eclipse.core.resources.IFolder;
16 import org.eclipse.core.resources.IResource;
17 import org.eclipse.core.runtime.CoreException;
18 import org.eclipse.jdt.core.IJavaElement;
19 import org.eclipse.jdt.core.IJavaModelStatusConstants;
20 import org.eclipse.jdt.core.IOpenable;
21 import org.eclipse.jdt.core.IPackageFragment;
22 import org.eclipse.jdt.core.JavaModelException;
23 import org.eclipse.jdt.internal.core.util.Messages;
24
25 /**
26  * This operation deletes a collection of resources and all of their children.
27  * It does not delete resources which do not belong to the Java Model
28  * (eg GIF files).
29  */

30 public class DeleteResourceElementsOperation extends MultiOperation {
31 /**
32  * When executed, this operation will delete the given elements. The elements
33  * to delete cannot be <code>null</code> or empty, and must have a corresponding
34  * resource.
35  */

36 protected DeleteResourceElementsOperation(IJavaElement[] elementsToProcess, boolean force) {
37     super(elementsToProcess, force);
38 }
39 /**
40  * Deletes the direct children of <code>frag</code> corresponding to its kind
41  * (K_SOURCE or K_BINARY), and deletes the corresponding folder if it is then
42  * empty.
43  */

44 private void deletePackageFragment(IPackageFragment frag)
45     throws JavaModelException {
46     IResource res = frag.getResource();
47     if (res != null) {
48         // collect the children to remove
49
IJavaElement[] childrenOfInterest = frag.getChildren();
50         if (childrenOfInterest.length > 0) {
51             IResource[] resources = new IResource[childrenOfInterest.length];
52             // remove the children
53
for (int i = 0; i < childrenOfInterest.length; i++) {
54                 resources[i] = childrenOfInterest[i].getCorrespondingResource();
55             }
56             deleteResources(resources, force);
57         }
58
59         // Discard non-java resources
60
Object JavaDoc[] nonJavaResources = frag.getNonJavaResources();
61         int actualResourceCount = 0;
62         for (int i = 0, max = nonJavaResources.length; i < max; i++){
63             if (nonJavaResources[i] instanceof IResource) actualResourceCount++;
64         }
65         IResource[] actualNonJavaResources = new IResource[actualResourceCount];
66         for (int i = 0, max = nonJavaResources.length, index = 0; i < max; i++){
67             if (nonJavaResources[i] instanceof IResource) actualNonJavaResources[index++] = (IResource)nonJavaResources[i];
68         }
69         deleteResources(actualNonJavaResources, force);
70         
71         // delete remaining files in this package (.class file in the case where Proj=src=bin)
72
IResource[] remainingFiles;
73         try {
74             remainingFiles = ((IContainer) res).members();
75         } catch (CoreException ce) {
76             throw new JavaModelException(ce);
77         }
78         boolean isEmpty = true;
79         for (int i = 0, length = remainingFiles.length; i < length; i++) {
80             IResource file = remainingFiles[i];
81             if (file instanceof IFile && org.eclipse.jdt.internal.compiler.util.Util.isClassFileName(file.getName())) {
82                 this.deleteResource(file, IResource.FORCE | IResource.KEEP_HISTORY);
83             } else {
84                 isEmpty = false;
85             }
86         }
87         if (isEmpty && !frag.isDefaultPackage()/*don't delete default package's folder: see https://bugs.eclipse.org/bugs/show_bug.cgi?id=38450*/) {
88             // delete recursively empty folders
89
IResource fragResource = frag.getResource();
90             if (fragResource != null) {
91                 deleteEmptyPackageFragment(frag, false, fragResource.getParent());
92             }
93         }
94     }
95 }
96 /**
97  * @see MultiOperation
98  */

99 protected String JavaDoc getMainTaskName() {
100     return Messages.operation_deleteResourceProgress;
101 }
102 /**
103  * @see MultiOperation This method delegate to <code>deleteResource</code> or
104  * <code>deletePackageFragment</code> depending on the type of <code>element</code>.
105  */

106 protected void processElement(IJavaElement element) throws JavaModelException {
107     switch (element.getElementType()) {
108         case IJavaElement.CLASS_FILE :
109         case IJavaElement.COMPILATION_UNIT :
110             deleteResource(element.getResource(), force ? IResource.FORCE | IResource.KEEP_HISTORY : IResource.KEEP_HISTORY);
111             break;
112         case IJavaElement.PACKAGE_FRAGMENT :
113             deletePackageFragment((IPackageFragment) element);
114             break;
115         default :
116             throw new JavaModelException(new JavaModelStatus(IJavaModelStatusConstants.INVALID_ELEMENT_TYPES, element));
117     }
118     // ensure the element is closed
119
if (element instanceof IOpenable) {
120         ((IOpenable)element).close();
121     }
122 }
123 /**
124  * @see MultiOperation
125  */

126 protected void verify(IJavaElement element) throws JavaModelException {
127     if (element == null || !element.exists())
128         error(IJavaModelStatusConstants.ELEMENT_DOES_NOT_EXIST, element);
129
130     int type = element.getElementType();
131     if (type <= IJavaElement.PACKAGE_FRAGMENT_ROOT || type > IJavaElement.COMPILATION_UNIT)
132         error(IJavaModelStatusConstants.INVALID_ELEMENT_TYPES, element);
133     else if (type == IJavaElement.PACKAGE_FRAGMENT && element instanceof JarPackageFragment)
134         error(IJavaModelStatusConstants.INVALID_ELEMENT_TYPES, element);
135     IResource resource = element.getResource();
136     if (resource instanceof IFolder) {
137         if (resource.isLinked()) {
138             error(IJavaModelStatusConstants.INVALID_RESOURCE, element);
139         }
140     }
141 }
142 }
143
Popular Tags