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;
17 import net.sourceforge.phpdt.internal.core.util.Util;
19 import org.eclipse.core.resources.IFile;
20 import org.eclipse.core.resources.IFolder;
21 import org.eclipse.core.resources.IResource;
22 import org.eclipse.core.runtime.CoreException;
25 * This operation deletes a collection of resources and all of their children.
26 * It does not delete resources which do not belong to the Java Model (eg GIF
29 public class DeleteResourceElementsOperation extends MultiOperation {
31 * When executed, this operation will delete the given elements. The
32 * elements to delete cannot be <code>null</code> or empty, and must have
33 * a corresponding resource.
35 protected DeleteResourceElementsOperation(IJavaElement[] elementsToProcess,
37 super(elementsToProcess, force);
41 * Deletes the direct children of <code>frag</code> corresponding to its
42 * kind (K_SOURCE or K_BINARY), and deletes the corresponding folder if it
45 private void deletePackageFragment(IPackageFragment frag)
46 throws JavaModelException {
47 IResource res = frag.getResource();
48 if (res != null && res.getType() == IResource.FOLDER) {
49 // collect the children to remove
50 IJavaElement[] childrenOfInterest = frag.getChildren();
51 if (childrenOfInterest.length > 0) {
52 IResource[] resources = new IResource[childrenOfInterest.length];
53 // remove the children
54 for (int i = 0; i < childrenOfInterest.length; i++) {
55 resources[i] = childrenOfInterest[i]
56 .getCorrespondingResource();
58 deleteResources(resources, force);
61 // Discard non-java resources
62 // Object[] nonJavaResources = frag.getNonJavaResources();
63 // int actualResourceCount = 0;
64 // for (int i = 0, max = nonJavaResources.length; i < max; i++){
65 // if (nonJavaResources[i] instanceof IResource)
66 // actualResourceCount++;
68 // IResource[] actualNonJavaResources = new
69 // IResource[actualResourceCount];
70 // for (int i = 0, max = nonJavaResources.length, index = 0; i <
72 // if (nonJavaResources[i] instanceof IResource)
73 // actualNonJavaResources[index++] = (IResource)nonJavaResources[i];
75 // deleteResources(actualNonJavaResources, fForce);
77 // delete remaining files in this package (.class file in the case
78 // where Proj=src=bin)
79 IResource[] remainingFiles;
81 remainingFiles = ((IFolder) res).members();
82 } catch (CoreException ce) {
83 throw new JavaModelException(ce);
85 boolean isEmpty = true;
86 for (int i = 0, length = remainingFiles.length; i < length; i++) {
87 IResource file = remainingFiles[i];
88 if (file instanceof IFile) {
89 this.deleteResource(file, IResource.FORCE
90 | IResource.KEEP_HISTORY);
96 // delete recursively empty folders
97 IResource fragResource = frag.getResource();
98 if (fragResource != null) {
99 deleteEmptyPackageFragment(frag, false, fragResource
107 * @see MultiOperation
109 protected String getMainTaskName() {
110 return Util.bind("operation.deleteResourceProgress"); //$NON-NLS-1$
114 * @see MultiOperation. This method delegate to <code>deleteResource</code>
115 * or <code>deletePackageFragment</code> depending on the type of
116 * <code>element</code>.
118 protected void processElement(IJavaElement element)
119 throws JavaModelException {
120 switch (element.getElementType()) {
121 case IJavaElement.CLASS_FILE:
122 case IJavaElement.COMPILATION_UNIT:
123 deleteResource(element.getResource(), force ? IResource.FORCE
124 | IResource.KEEP_HISTORY : IResource.KEEP_HISTORY);
126 case IJavaElement.PACKAGE_FRAGMENT:
127 deletePackageFragment((IPackageFragment) element);
130 throw new JavaModelException(new JavaModelStatus(
131 JavaModelStatus.INVALID_ELEMENT_TYPES, element));
133 // ensure the element is closed
134 if (element instanceof IOpenable) {
135 ((IOpenable) element).close();
140 * @see MultiOperation
142 protected void verify(IJavaElement element) throws JavaModelException {
143 if (element == null || !element.exists())
144 error(JavaModelStatus.ELEMENT_DOES_NOT_EXIST, element);
146 int type = element.getElementType();
147 if (type <= IJavaElement.PACKAGE_FRAGMENT_ROOT
148 || type > IJavaElement.COMPILATION_UNIT)
149 error(JavaModelStatus.INVALID_ELEMENT_TYPES, element);
150 // else if (type == IJavaElement.PACKAGE_FRAGMENT && element instanceof
151 // JarPackageFragment)
152 // error(JavaModelStatus.INVALID_ELEMENT_TYPES, element);
153 IResource resource = element.getResource();
154 if (resource instanceof IFolder) {
155 if (resource.isLinked()) {
156 error(JavaModelStatus.INVALID_RESOURCE, element);