1 11 package org.eclipse.jdt.internal.core; 12 13 import org.eclipse.core.resources.*; 14 import org.eclipse.core.resources.IFile; 15 import org.eclipse.core.resources.IFolder; 16 import org.eclipse.core.resources.IResource; 17 import org.eclipse.core.runtime.CoreException; 18 import org.eclipse.jdt.core.IJavaElement; 19 import org.eclipse.jdt.core.IJavaModelStatusConstants; 20 import org.eclipse.jdt.core.IOpenable; 21 import org.eclipse.jdt.core.IPackageFragment; 22 import org.eclipse.jdt.core.JavaModelException; 23 import org.eclipse.jdt.internal.core.util.Messages; 24 25 30 public class DeleteResourceElementsOperation extends MultiOperation { 31 36 protected DeleteResourceElementsOperation(IJavaElement[] elementsToProcess, boolean force) { 37 super(elementsToProcess, force); 38 } 39 44 private void deletePackageFragment(IPackageFragment frag) 45 throws JavaModelException { 46 IResource res = frag.getResource(); 47 if (res != null) { 48 IJavaElement[] childrenOfInterest = frag.getChildren(); 50 if (childrenOfInterest.length > 0) { 51 IResource[] resources = new IResource[childrenOfInterest.length]; 52 for (int i = 0; i < childrenOfInterest.length; i++) { 54 resources[i] = childrenOfInterest[i].getCorrespondingResource(); 55 } 56 deleteResources(resources, force); 57 } 58 59 Object [] nonJavaResources = frag.getNonJavaResources(); 61 int actualResourceCount = 0; 62 for (int i = 0, max = nonJavaResources.length; i < max; i++){ 63 if (nonJavaResources[i] instanceof IResource) actualResourceCount++; 64 } 65 IResource[] actualNonJavaResources = new IResource[actualResourceCount]; 66 for (int i = 0, max = nonJavaResources.length, index = 0; i < max; i++){ 67 if (nonJavaResources[i] instanceof IResource) actualNonJavaResources[index++] = (IResource)nonJavaResources[i]; 68 } 69 deleteResources(actualNonJavaResources, force); 70 71 IResource[] remainingFiles; 73 try { 74 remainingFiles = ((IContainer) res).members(); 75 } catch (CoreException ce) { 76 throw new JavaModelException(ce); 77 } 78 boolean isEmpty = true; 79 for (int i = 0, length = remainingFiles.length; i < length; i++) { 80 IResource file = remainingFiles[i]; 81 if (file instanceof IFile && org.eclipse.jdt.internal.compiler.util.Util.isClassFileName(file.getName())) { 82 this.deleteResource(file, IResource.FORCE | IResource.KEEP_HISTORY); 83 } else { 84 isEmpty = false; 85 } 86 } 87 if (isEmpty && !frag.isDefaultPackage()) { 88 IResource fragResource = frag.getResource(); 90 if (fragResource != null) { 91 deleteEmptyPackageFragment(frag, false, fragResource.getParent()); 92 } 93 } 94 } 95 } 96 99 protected String getMainTaskName() { 100 return Messages.operation_deleteResourceProgress; 101 } 102 106 protected void processElement(IJavaElement element) throws JavaModelException { 107 switch (element.getElementType()) { 108 case IJavaElement.CLASS_FILE : 109 case IJavaElement.COMPILATION_UNIT : 110 deleteResource(element.getResource(), force ? IResource.FORCE | IResource.KEEP_HISTORY : IResource.KEEP_HISTORY); 111 break; 112 case IJavaElement.PACKAGE_FRAGMENT : 113 deletePackageFragment((IPackageFragment) element); 114 break; 115 default : 116 throw new JavaModelException(new JavaModelStatus(IJavaModelStatusConstants.INVALID_ELEMENT_TYPES, element)); 117 } 118 if (element instanceof IOpenable) { 120 ((IOpenable)element).close(); 121 } 122 } 123 126 protected void verify(IJavaElement element) throws JavaModelException { 127 if (element == null || !element.exists()) 128 error(IJavaModelStatusConstants.ELEMENT_DOES_NOT_EXIST, element); 129 130 int type = element.getElementType(); 131 if (type <= IJavaElement.PACKAGE_FRAGMENT_ROOT || type > IJavaElement.COMPILATION_UNIT) 132 error(IJavaModelStatusConstants.INVALID_ELEMENT_TYPES, element); 133 else if (type == IJavaElement.PACKAGE_FRAGMENT && element instanceof JarPackageFragment) 134 error(IJavaModelStatusConstants.INVALID_ELEMENT_TYPES, element); 135 IResource resource = element.getResource(); 136 if (resource instanceof IFolder) { 137 if (resource.isLinked()) { 138 error(IJavaModelStatusConstants.INVALID_RESOURCE, element); 139 } 140 } 141 } 142 } 143 | Popular Tags |