1 11 package org.eclipse.jdt.internal.core; 12 13 import java.util.ArrayList ; 14 15 import org.eclipse.core.resources.IContainer; 16 import org.eclipse.core.resources.IResource; 17 import org.eclipse.core.runtime.IStatus; 18 import org.eclipse.core.runtime.Path; 19 import org.eclipse.jdt.core.IJavaElement; 20 import org.eclipse.jdt.core.IJavaModelStatus; 21 import org.eclipse.jdt.core.IJavaModelStatusConstants; 22 import org.eclipse.jdt.core.IJavaProject; 23 import org.eclipse.jdt.core.IPackageFragment; 24 import org.eclipse.jdt.core.IPackageFragmentRoot; 25 import org.eclipse.jdt.core.JavaConventions; 26 import org.eclipse.jdt.core.JavaCore; 27 import org.eclipse.jdt.core.JavaModelException; 28 import org.eclipse.jdt.core.compiler.CharOperation; 29 import org.eclipse.jdt.internal.core.util.Messages; 30 import org.eclipse.jdt.internal.core.util.Util; 31 32 50 public class CreatePackageFragmentOperation extends JavaModelOperation { 51 54 protected String [] pkgName; 55 61 public CreatePackageFragmentOperation(IPackageFragmentRoot parentElement, String packageName, boolean force) { 62 super(null, new IJavaElement[]{parentElement}, force); 63 this.pkgName = packageName == null ? null : Util.getTrimmedSimpleNames(packageName); 64 } 65 71 protected void executeOperation() throws JavaModelException { 72 try { 73 JavaElementDelta delta = null; 74 PackageFragmentRoot root = (PackageFragmentRoot) getParentElement(); 75 beginTask(Messages.operation_createPackageFragmentProgress, this.pkgName.length); 76 IContainer parentFolder = (IContainer) root.getResource(); 77 String [] sideEffectPackageName = CharOperation.NO_STRINGS; 78 ArrayList results = new ArrayList (this.pkgName.length); 79 char[][] inclusionPatterns = root.fullInclusionPatternChars(); 80 char[][] exclusionPatterns = root.fullExclusionPatternChars(); 81 int i; 82 for (i = 0; i < this.pkgName.length; i++) { 83 String subFolderName = this.pkgName[i]; 84 sideEffectPackageName = Util.arrayConcat(sideEffectPackageName, subFolderName); 85 IResource subFolder = parentFolder.findMember(subFolderName); 86 if (subFolder == null) { 87 createFolder(parentFolder, subFolderName, force); 88 parentFolder = parentFolder.getFolder(new Path(subFolderName)); 89 IPackageFragment addedFrag = root.getPackageFragment(sideEffectPackageName); 90 if (!Util.isExcluded(parentFolder, inclusionPatterns, exclusionPatterns)) { 91 if (delta == null) { 92 delta = newJavaElementDelta(); 93 } 94 delta.added(addedFrag); 95 } 96 results.add(addedFrag); 97 } else { 98 parentFolder = (IContainer) subFolder; 99 } 100 worked(1); 101 } 102 if (results.size() > 0) { 103 this.resultElements = new IJavaElement[results.size()]; 104 results.toArray(this.resultElements); 105 if (delta != null) { 106 addDelta(delta); 107 } 108 } 109 } finally { 110 done(); 111 } 112 } 113 127 public IJavaModelStatus verify() { 128 IJavaElement parentElement = getParentElement(); 129 if (parentElement == null) { 130 return new JavaModelStatus(IJavaModelStatusConstants.NO_ELEMENTS_TO_PROCESS); 131 } 132 133 String packageName = this.pkgName == null ? null : Util.concatWith(this.pkgName, '.'); 134 IJavaProject project = parentElement.getJavaProject(); 135 if (this.pkgName == null || (this.pkgName.length > 0 && JavaConventions.validatePackageName(packageName, project.getOption(JavaCore.COMPILER_SOURCE, true), project.getOption(JavaCore.COMPILER_COMPLIANCE, true)).getSeverity() == IStatus.ERROR)) { 136 return new JavaModelStatus(IJavaModelStatusConstants.INVALID_NAME, packageName); 137 } 138 IPackageFragmentRoot root = (IPackageFragmentRoot) getParentElement(); 139 if (root.isReadOnly()) { 140 return new JavaModelStatus(IJavaModelStatusConstants.READ_ONLY, root); 141 } 142 IContainer parentFolder = (IContainer) root.getResource(); 143 int i; 144 for (i = 0; i < this.pkgName.length; i++) { 145 IResource subFolder = parentFolder.findMember(this.pkgName[i]); 146 if (subFolder != null) { 147 if (subFolder.getType() != IResource.FOLDER) { 148 return new JavaModelStatus( 149 IJavaModelStatusConstants.NAME_COLLISION, 150 Messages.bind(Messages.status_nameCollision, subFolder.getFullPath().toString())); 151 } 152 parentFolder = (IContainer) subFolder; 153 } 154 } 155 return JavaModelStatus.VERIFIED_OK; 156 } 157 } 158 | Popular Tags |