1 11 package org.eclipse.jdt.internal.core; 12 13 import org.eclipse.core.runtime.IStatus; 14 import org.eclipse.jdt.core.ICompilationUnit; 15 import org.eclipse.jdt.core.IImportDeclaration; 16 import org.eclipse.jdt.core.IJavaElement; 17 import org.eclipse.jdt.core.IJavaModelStatus; 18 import org.eclipse.jdt.core.IJavaModelStatusConstants; 19 import org.eclipse.jdt.core.IJavaProject; 20 import org.eclipse.jdt.core.IType; 21 import org.eclipse.jdt.core.JavaConventions; 22 import org.eclipse.jdt.core.JavaCore; 23 import org.eclipse.jdt.core.JavaModelException; 24 import org.eclipse.jdt.core.dom.AST; 25 import org.eclipse.jdt.core.dom.ASTNode; 26 import org.eclipse.jdt.core.dom.CompilationUnit; 27 import org.eclipse.jdt.core.dom.Name; 28 import org.eclipse.jdt.core.dom.PackageDeclaration; 29 import org.eclipse.jdt.core.dom.StructuralPropertyDescriptor; 30 import org.eclipse.jdt.core.dom.rewrite.ASTRewrite; 31 import org.eclipse.jdt.internal.core.util.Messages; 32 import org.eclipse.jface.text.IDocument; 33 34 44 public class CreatePackageDeclarationOperation extends CreateElementInCUOperation { 45 48 protected String name = null; 49 52 public CreatePackageDeclarationOperation(String name, ICompilationUnit parentElement) { 53 super(parentElement); 54 this.name= name; 55 } 56 protected StructuralPropertyDescriptor getChildPropertyDescriptor(ASTNode parent) { 57 return CompilationUnit.PACKAGE_PROPERTY; 58 } 59 protected ASTNode generateElementAST(ASTRewrite rewriter, IDocument document, ICompilationUnit cu) throws JavaModelException { 60 IJavaElement[] children = getCompilationUnit().getChildren(); 62 for (int i = 0; i < children.length; i++) { 63 if (children[i].getElementType() == IJavaElement.PACKAGE_DECLARATION && this.name.equals(children[i].getElementName())) { 64 this.creationOccurred = false; 66 return null; 67 } 68 } 69 AST ast = this.cuAST.getAST(); 70 PackageDeclaration pkgDeclaration = ast.newPackageDeclaration(); 71 Name astName = ast.newName(this.name); 72 pkgDeclaration.setName(astName); 73 return pkgDeclaration; 74 } 75 78 protected IJavaElement generateResultHandle() { 79 return getCompilationUnit().getPackageDeclaration(this.name); 80 } 81 84 public String getMainTaskName(){ 85 return Messages.operation_createPackageProgress; 86 } 87 94 protected void initializeDefaultPosition() { 95 try { 96 ICompilationUnit cu = getCompilationUnit(); 97 IImportDeclaration[] imports = cu.getImports(); 98 if (imports.length > 0) { 99 createBefore(imports[0]); 100 return; 101 } 102 IType[] types = cu.getTypes(); 103 if (types.length > 0) { 104 createBefore(types[0]); 105 return; 106 } 107 } catch (JavaModelException e) { 108 } 110 } 111 120 public IJavaModelStatus verify() { 121 IJavaModelStatus status = super.verify(); 122 if (!status.isOK()) { 123 return status; 124 } 125 IJavaProject project = getParentElement().getJavaProject(); 126 if (JavaConventions.validatePackageName(this.name, project.getOption(JavaCore.COMPILER_SOURCE, true), project.getOption(JavaCore.COMPILER_COMPLIANCE, true)).getSeverity() == IStatus.ERROR) { 127 return new JavaModelStatus(IJavaModelStatusConstants.INVALID_NAME, this.name); 128 } 129 return JavaModelStatus.VERIFIED_OK; 130 } 131 } 132 | Popular Tags |