1 /*******************************************************************************
2 * Copyright (c) 2000, 2003 IBM Corporation and others.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Common Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/cpl-v10.html
9 * IBM Corporation - initial API and implementation
10 *******************************************************************************/
11 package net.sourceforge.phpdt.internal.core;
13 import net.sourceforge.phpdt.core.IJavaElement;
14 import net.sourceforge.phpdt.core.IOpenable;
15 import net.sourceforge.phpdt.core.IPackageFragment;
16 import net.sourceforge.phpdt.core.JavaModelException;
18 import org.eclipse.core.resources.IFile;
19 import org.eclipse.core.resources.IFolder;
20 import org.eclipse.core.resources.IResource;
21 import org.eclipse.core.runtime.CoreException;
24 * This operation deletes a collection of resources and all of their children.
25 * It does not delete resources which do not belong to the Java Model
28 public class DeleteResourceElementsOperation extends MultiOperation {
30 * When executed, this operation will delete the given elements. The elements
31 * to delete cannot be <code>null</code> or empty, and must have a corresponding
34 protected DeleteResourceElementsOperation(IJavaElement[] elementsToProcess, boolean force) {
35 super(elementsToProcess, force);
38 * Deletes the direct children of <code>frag</code> corresponding to its kind
39 * (K_SOURCE or K_BINARY), and deletes the corresponding folder if it is then
42 private void deletePackageFragment(IPackageFragment frag)
43 throws JavaModelException {
44 IResource res = frag.getResource();
45 if (res != null && res.getType() == IResource.FOLDER) {
46 // collect the children to remove
47 IJavaElement[] childrenOfInterest = frag.getChildren();
48 if (childrenOfInterest.length > 0) {
49 IResource[] resources = new IResource[childrenOfInterest.length];
50 // remove the children
51 for (int i = 0; i < childrenOfInterest.length; i++) {
52 resources[i] = childrenOfInterest[i].getCorrespondingResource();
54 deleteResources(resources, fForce);
57 // Discard non-java resources
58 // Object[] nonJavaResources = frag.getNonJavaResources();
59 // int actualResourceCount = 0;
60 // for (int i = 0, max = nonJavaResources.length; i < max; i++){
61 // if (nonJavaResources[i] instanceof IResource) actualResourceCount++;
63 // IResource[] actualNonJavaResources = new IResource[actualResourceCount];
64 // for (int i = 0, max = nonJavaResources.length, index = 0; i < max; i++){
65 // if (nonJavaResources[i] instanceof IResource) actualNonJavaResources[index++] = (IResource)nonJavaResources[i];
67 // deleteResources(actualNonJavaResources, fForce);
69 // delete remaining files in this package (.class file in the case where Proj=src=bin)
70 IResource[] remainingFiles;
72 remainingFiles = ((IFolder) res).members();
73 } catch (CoreException ce) {
74 throw new JavaModelException(ce);
76 boolean isEmpty = true;
77 for (int i = 0, length = remainingFiles.length; i < length; i++) {
78 IResource file = remainingFiles[i];
79 if (file instanceof IFile) {
80 this.deleteResource(file, IResource.FORCE | IResource.KEEP_HISTORY);
86 // delete recursively empty folders
87 IResource fragResource = frag.getResource();
88 if (fragResource != null) {
89 deleteEmptyPackageFragment(frag, false, fragResource.getParent());
97 protected String getMainTaskName() {
98 return Util.bind("operation.deleteResourceProgress"); //$NON-NLS-1$
101 * @see MultiOperation. This method delegate to <code>deleteResource</code> or
102 * <code>deletePackageFragment</code> depending on the type of <code>element</code>.
104 protected void processElement(IJavaElement element) throws JavaModelException {
105 switch (element.getElementType()) {
106 case IJavaElement.CLASS_FILE :
107 case IJavaElement.COMPILATION_UNIT :
108 deleteResource(element.getResource(), fForce ? IResource.FORCE | IResource.KEEP_HISTORY : IResource.KEEP_HISTORY);
110 case IJavaElement.PACKAGE_FRAGMENT :
111 deletePackageFragment((IPackageFragment) element);
114 throw new JavaModelException(new JavaModelStatus(JavaModelStatus.INVALID_ELEMENT_TYPES, element));
116 // ensure the element is closed
117 if (element instanceof IOpenable) {
118 ((IOpenable)element).close();
122 * @see MultiOperation
124 protected void verify(IJavaElement element) throws JavaModelException {
125 if (element == null || !element.exists())
126 error(JavaModelStatus.ELEMENT_DOES_NOT_EXIST, element);
128 int type = element.getElementType();
129 if (type <= IJavaElement.PACKAGE_FRAGMENT_ROOT || type > IJavaElement.COMPILATION_UNIT)
130 error(JavaModelStatus.INVALID_ELEMENT_TYPES, element);
131 // else if (type == IJavaElement.PACKAGE_FRAGMENT && element instanceof JarPackageFragment)
132 // error(JavaModelStatus.INVALID_ELEMENT_TYPES, element);
133 IResource resource = element.getResource();
134 if (resource instanceof IFolder) {
135 if (resource.isLinked()) {
136 error(JavaModelStatus.INVALID_RESOURCE, element);