3m9 compatible;
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpdt / internal / core / DeleteResourceElementsOperation.java
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
7  * 
8  * Contributors:
9  *     IBM Corporation - initial API and implementation
10  *******************************************************************************/
11 package net.sourceforge.phpdt.internal.core;
12
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;
18
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;
23
24 /**
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
27  * (eg GIF files).
28  */
29 public class DeleteResourceElementsOperation extends MultiOperation {
30 /**
31  * When executed, this operation will delete the given elements. The elements
32  * to delete cannot be <code>null</code> or empty, and must have a corresponding
33  * resource.
34  */
35 protected DeleteResourceElementsOperation(IJavaElement[] elementsToProcess, boolean force) {
36         super(elementsToProcess, force);
37 }
38 /**
39  * Deletes the direct children of <code>frag</code> corresponding to its kind
40  * (K_SOURCE or K_BINARY), and deletes the corresponding folder if it is then
41  * empty.
42  */
43 private void deletePackageFragment(IPackageFragment frag)
44         throws JavaModelException {
45         IResource res = frag.getResource();
46         if (res != null && res.getType() == IResource.FOLDER) {
47                 // collect the children to remove
48                 IJavaElement[] childrenOfInterest = frag.getChildren();
49                 if (childrenOfInterest.length > 0) {
50                         IResource[] resources = new IResource[childrenOfInterest.length];
51                         // remove the children
52                         for (int i = 0; i < childrenOfInterest.length; i++) {
53                                 resources[i] = childrenOfInterest[i].getCorrespondingResource();
54                         }
55                         deleteResources(resources, force);
56                 }
57
58                 // Discard non-java resources
59 //              Object[] nonJavaResources = frag.getNonJavaResources();
60 //              int actualResourceCount = 0;
61 //              for (int i = 0, max = nonJavaResources.length; i < max; i++){
62 //                      if (nonJavaResources[i] instanceof IResource) actualResourceCount++;
63 //              }
64 //              IResource[] actualNonJavaResources = new IResource[actualResourceCount];
65 //              for (int i = 0, max = nonJavaResources.length, index = 0; i < max; i++){
66 //                      if (nonJavaResources[i] instanceof IResource) actualNonJavaResources[index++] = (IResource)nonJavaResources[i];
67 //              }
68 //              deleteResources(actualNonJavaResources, fForce);
69                 
70                 // delete remaining files in this package (.class file in the case where Proj=src=bin)
71                 IResource[] remainingFiles;
72                 try {
73                         remainingFiles = ((IFolder) res).members();
74                 } catch (CoreException ce) {
75                         throw new JavaModelException(ce);
76                 }
77                 boolean isEmpty = true;
78                 for (int i = 0, length = remainingFiles.length; i < length; i++) {
79                         IResource file = remainingFiles[i];
80                         if (file instanceof IFile) {
81                                 this.deleteResource(file, IResource.FORCE | IResource.KEEP_HISTORY);
82                         } else {
83                                 isEmpty = false;
84                         }
85                 }
86                 if (isEmpty) {
87                         // delete recursively empty folders
88                         IResource fragResource =  frag.getResource();
89                         if (fragResource != null) {
90                                 deleteEmptyPackageFragment(frag, false, fragResource.getParent());
91                         }
92                 }
93         }
94 }
95 /**
96  * @see MultiOperation
97  */
98 protected String getMainTaskName() {
99         return Util.bind("operation.deleteResourceProgress"); //$NON-NLS-1$
100 }
101 /**
102  * @see MultiOperation. This method delegate to <code>deleteResource</code> or
103  * <code>deletePackageFragment</code> depending on the type of <code>element</code>.
104  */
105 protected void processElement(IJavaElement element) throws JavaModelException {
106         switch (element.getElementType()) {
107                 case IJavaElement.CLASS_FILE :
108                 case IJavaElement.COMPILATION_UNIT :
109                         deleteResource(element.getResource(), force ? IResource.FORCE | IResource.KEEP_HISTORY : IResource.KEEP_HISTORY);
110                         break;
111                 case IJavaElement.PACKAGE_FRAGMENT :
112                         deletePackageFragment((IPackageFragment) element);
113                         break;
114                 default :
115                         throw new JavaModelException(new JavaModelStatus(JavaModelStatus.INVALID_ELEMENT_TYPES, element));
116         }
117         // ensure the element is closed
118         if (element instanceof IOpenable) {
119                 ((IOpenable)element).close();
120         }
121 }
122 /**
123  * @see MultiOperation
124  */
125 protected void verify(IJavaElement element) throws JavaModelException {
126         if (element == null || !element.exists())
127                 error(JavaModelStatus.ELEMENT_DOES_NOT_EXIST, element);
128
129         int type = element.getElementType();
130         if (type <= IJavaElement.PACKAGE_FRAGMENT_ROOT || type > IJavaElement.COMPILATION_UNIT)
131                 error(JavaModelStatus.INVALID_ELEMENT_TYPES, element);
132 //      else if (type == IJavaElement.PACKAGE_FRAGMENT && element instanceof JarPackageFragment)
133 //              error(JavaModelStatus.INVALID_ELEMENT_TYPES, element);
134         IResource resource = element.getResource();
135         if (resource instanceof IFolder) {
136                 if (resource.isLinked()) {
137                         error(JavaModelStatus.INVALID_RESOURCE, element);
138                 }
139         }
140 }
141 }