1) Added missing strings for italic, underline and strike through.
[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 (eg GIF
27  * files).
28  */
29 public class DeleteResourceElementsOperation extends MultiOperation {
30         /**
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.
34          */
35         protected DeleteResourceElementsOperation(IJavaElement[] elementsToProcess,
36                         boolean force) {
37                 super(elementsToProcess, force);
38         }
39
40         /**
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
43          * is then empty.
44          */
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();
57                                 }
58                                 deleteResources(resources, force);
59                         }
60
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++;
67                         // }
68                         // IResource[] actualNonJavaResources = new
69                         // IResource[actualResourceCount];
70                         // for (int i = 0, max = nonJavaResources.length, index = 0; i <
71                         // max; i++){
72                         // if (nonJavaResources[i] instanceof IResource)
73                         // actualNonJavaResources[index++] = (IResource)nonJavaResources[i];
74                         // }
75                         // deleteResources(actualNonJavaResources, fForce);
76
77                         // delete remaining files in this package (.class file in the case
78                         // where Proj=src=bin)
79                         IResource[] remainingFiles;
80                         try {
81                                 remainingFiles = ((IFolder) res).members();
82                         } catch (CoreException ce) {
83                                 throw new JavaModelException(ce);
84                         }
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);
91                                 } else {
92                                         isEmpty = false;
93                                 }
94                         }
95                         if (isEmpty) {
96                                 // delete recursively empty folders
97                                 IResource fragResource = frag.getResource();
98                                 if (fragResource != null) {
99                                         deleteEmptyPackageFragment(frag, false, fragResource
100                                                         .getParent());
101                                 }
102                         }
103                 }
104         }
105
106         /**
107          * @see MultiOperation
108          */
109         protected String getMainTaskName() {
110                 return Util.bind("operation.deleteResourceProgress"); //$NON-NLS-1$
111         }
112
113         /**
114          * @see MultiOperation. This method delegate to <code>deleteResource</code>
115          *      or <code>deletePackageFragment</code> depending on the type of
116          *      <code>element</code>.
117          */
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);
125                         break;
126                 case IJavaElement.PACKAGE_FRAGMENT:
127                         deletePackageFragment((IPackageFragment) element);
128                         break;
129                 default:
130                         throw new JavaModelException(new JavaModelStatus(
131                                         JavaModelStatus.INVALID_ELEMENT_TYPES, element));
132                 }
133                 // ensure the element is closed
134                 if (element instanceof IOpenable) {
135                         ((IOpenable) element).close();
136                 }
137         }
138
139         /**
140          * @see MultiOperation
141          */
142         protected void verify(IJavaElement element) throws JavaModelException {
143                 if (element == null || !element.exists())
144                         error(JavaModelStatus.ELEMENT_DOES_NOT_EXIST, element);
145
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);
157                         }
158                 }
159         }
160 }