1 11 package org.eclipse.jdt.internal.core; 12 13 import java.util.Iterator ; 14 15 import org.eclipse.core.runtime.IStatus; 16 import org.eclipse.jdt.core.Flags; 17 import org.eclipse.jdt.core.ICompilationUnit; 18 import org.eclipse.jdt.core.IImportDeclaration; 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.IType; 24 import org.eclipse.jdt.core.JavaConventions; 25 import org.eclipse.jdt.core.JavaCore; 26 import org.eclipse.jdt.core.JavaModelException; 27 import org.eclipse.jdt.core.compiler.CharOperation; 28 import org.eclipse.jdt.core.dom.AST; 29 import org.eclipse.jdt.core.dom.ASTNode; 30 import org.eclipse.jdt.core.dom.CompilationUnit; 31 import org.eclipse.jdt.core.dom.ImportDeclaration; 32 import org.eclipse.jdt.core.dom.Name; 33 import org.eclipse.jdt.core.dom.StructuralPropertyDescriptor; 34 import org.eclipse.jdt.core.dom.rewrite.ASTRewrite; 35 import org.eclipse.jdt.internal.core.util.Messages; 36 import org.eclipse.jface.text.IDocument; 37 38 56 public class CreateImportOperation extends CreateElementInCUOperation { 57 58 61 protected String importName; 62 63 66 protected int flags; 67 68 71 public CreateImportOperation(String importName, ICompilationUnit parentElement, int flags) { 72 super(parentElement); 73 this.importName = importName; 74 this.flags = flags; 75 } 76 protected StructuralPropertyDescriptor getChildPropertyDescriptor(ASTNode parent) { 77 return CompilationUnit.IMPORTS_PROPERTY; 78 } 79 protected ASTNode generateElementAST(ASTRewrite rewriter, IDocument document, ICompilationUnit cu) throws JavaModelException { 80 Iterator imports = this.cuAST.imports().iterator(); 82 boolean onDemand = this.importName.endsWith(".*"); String importActualName = this.importName; 84 if (onDemand) { 85 importActualName = this.importName.substring(0, this.importName.length() - 2); 86 } 87 while (imports.hasNext()) { 88 ImportDeclaration importDeclaration = (ImportDeclaration) imports.next(); 89 if (importActualName.equals(importDeclaration.getName().getFullyQualifiedName()) 90 && (onDemand == importDeclaration.isOnDemand()) 91 && (Flags.isStatic(this.flags) == importDeclaration.isStatic())) { 92 this.creationOccurred = false; 93 return null; 94 } 95 } 96 97 AST ast = this.cuAST.getAST(); 98 ImportDeclaration importDeclaration = ast.newImportDeclaration(); 99 importDeclaration.setStatic(Flags.isStatic(this.flags)); 100 char[][] charFragments = CharOperation.splitOn('.', importActualName.toCharArray(), 0, importActualName.length()); 102 int length = charFragments.length; 103 String [] strFragments = new String [length]; 104 for (int i = 0; i < length; i++) { 105 strFragments[i] = String.valueOf(charFragments[i]); 106 } 107 Name name = ast.newName(strFragments); 108 importDeclaration.setName(name); 109 if (onDemand) importDeclaration.setOnDemand(true); 110 return importDeclaration; 111 } 112 115 protected IJavaElement generateResultHandle() { 116 return getCompilationUnit().getImport(this.importName); 117 } 118 121 public String getMainTaskName(){ 122 return Messages.operation_createImportsProgress; 123 } 124 131 protected void initializeDefaultPosition() { 132 try { 133 ICompilationUnit cu = getCompilationUnit(); 134 IImportDeclaration[] imports = cu.getImports(); 135 if (imports.length > 0) { 136 createAfter(imports[imports.length - 1]); 137 return; 138 } 139 IType[] types = cu.getTypes(); 140 if (types.length > 0) { 141 createBefore(types[0]); 142 return; 143 } 144 IJavaElement[] children = cu.getChildren(); 145 for (int i = 0; i < children.length; i++) { 147 if (children[i].getElementType() == IJavaElement.PACKAGE_DECLARATION) { 148 createAfter(children[i]); 149 return; 150 } 151 } 152 } catch (JavaModelException e) { 153 } 155 } 156 165 public IJavaModelStatus verify() { 166 IJavaModelStatus status = super.verify(); 167 if (!status.isOK()) { 168 return status; 169 } 170 IJavaProject project = getParentElement().getJavaProject(); 171 if (JavaConventions.validateImportDeclaration(this.importName, project.getOption(JavaCore.COMPILER_SOURCE, true), project.getOption(JavaCore.COMPILER_COMPLIANCE, true)).getSeverity() == IStatus.ERROR) { 172 return new JavaModelStatus(IJavaModelStatusConstants.INVALID_NAME, this.importName); 173 } 174 return JavaModelStatus.VERIFIED_OK; 175 } 176 } 177 | Popular Tags |