e8c5128e663044e597097fc8135b3db8bda864dc
[phpeclipse.git] /
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
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;
22
23 /**
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
26  * (eg GIF files).
27  */
28 public class DeleteResourceElementsOperation extends MultiOperation {
29 /**
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
32  * resource.
33  */
34 protected DeleteResourceElementsOperation(IJavaElement[] elementsToProcess, boolean force) {
35         super(elementsToProcess, force);
36 }
37 /**
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
40  * empty.
41  */
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();
53                         }
54                         deleteResources(resources, fForce);
55                 }
56
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++;
62 //              }
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];
66 //              }
67 //              deleteResources(actualNonJavaResources, fForce);
68                 
69                 // delete remaining files in this package (.class file in the case where Proj=src=bin)
70                 IResource[] remainingFiles;
71                 try {
72                         remainingFiles = ((IFolder) res).members();
73                 } catch (CoreException ce) {
74                         throw new JavaModelException(ce);
75                 }
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);
81                         } else {
82                                 isEmpty = false;
83                         }
84                 }
85                 if (isEmpty) {
86                         // delete recursively empty folders
87                         IResource fragResource =  frag.getResource();
88                         if (fragResource != null) {
89                                 deleteEmptyPackageFragment(frag, false, fragResource.getParent());
90                         }
91                 }
92         }
93 }
94 /**
95  * @see MultiOperation
96  */
97 protected String getMainTaskName() {
98         return Util.bind("operation.deleteResourceProgress"); //$NON-NLS-1$
99 }
100 /**
101  * @see MultiOperation. This method delegate to <code>deleteResource</code> or
102  * <code>deletePackageFragment</code> depending on the type of <code>element</code>.
103  */
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);
109                         break;
110                 case IJavaElement.PACKAGE_FRAGMENT :
111                         deletePackageFragment((IPackageFragment) element);
112                         break;
113                 default :
114                         throw new JavaModelException(new JavaModelStatus(JavaModelStatus.INVALID_ELEMENT_TYPES, element));
115         }
116         // ensure the element is closed
117         if (element instanceof IOpenable) {
118                 ((IOpenable)element).close();
119         }
120 }
121 /**
122  * @see MultiOperation
123  */
124 protected void verify(IJavaElement element) throws JavaModelException {
125         if (element == null || !element.exists())
126                 error(JavaModelStatus.ELEMENT_DOES_NOT_EXIST, element);
127
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);
137                 }
138         }
139 }
140 }