KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > core > CreateImportOperation


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.jdt.internal.core;
12
13 import java.util.Iterator JavaDoc;
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 /**
39  * <p>This operation adds an import declaration to an existing compilation unit.
40  * If the compilation unit already includes the specified import declaration,
41  * the import is not generated (it does not generate duplicates).
42  * Note that it is valid to specify both a single-type import and an on-demand import
43  * for the same package, for example <code>"java.io.File"</code> and
44  * <code>"java.io.*"</code>, in which case both are preserved since the semantics
45  * of this are not the same as just importing <code>"java.io.*"</code>.
46  * Importing <code>"java.lang.*"</code>, or the package in which the compilation unit
47  * is defined, are not treated as special cases. If they are specified, they are
48  * included in the result.
49  *
50  * <p>Required Attributes:<ul>
51  * <li>Compilation unit
52  * <li>Import name - the name of the import to add to the
53  * compilation unit. For example: <code>"java.io.File"</code> or <code>"java.awt.*"</code>
54  * </ul>
55  */

56 public class CreateImportOperation extends CreateElementInCUOperation {
57
58     /*
59      * The name of the import to be created.
60      */

61     protected String JavaDoc importName;
62     
63     /*
64      * The flags of the import to be created (either Flags#AccDefault or Flags#AccStatic)
65      */

66     protected int flags;
67
68 /**
69  * When executed, this operation will add an import to the given compilation unit.
70  */

71 public CreateImportOperation(String JavaDoc 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     // ensure no duplicate
81
Iterator JavaDoc imports = this.cuAST.imports().iterator();
82     boolean onDemand = this.importName.endsWith(".*"); //$NON-NLS-1$
83
String JavaDoc 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     // split import name into individual fragments, checking for on demand imports
101
char[][] charFragments = CharOperation.splitOn('.', importActualName.toCharArray(), 0, importActualName.length());
102     int length = charFragments.length;
103     String JavaDoc[] strFragments = new String JavaDoc[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 /**
113  * @see CreateElementInCUOperation#generateResultHandle
114  */

115 protected IJavaElement generateResultHandle() {
116     return getCompilationUnit().getImport(this.importName);
117 }
118 /**
119  * @see CreateElementInCUOperation#getMainTaskName()
120  */

121 public String JavaDoc getMainTaskName(){
122     return Messages.operation_createImportsProgress;
123 }
124 /**
125  * Sets the correct position for the new import:<ul>
126  * <li> after the last import
127  * <li> if no imports, before the first type
128  * <li> if no type, after the package statement
129  * <li> and if no package statement - first thing in the CU
130  */

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         //look for the package declaration
146
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         // cu doesn't exit: ignore
154
}
155 }
156 /**
157  * Possible failures: <ul>
158  * <li>NO_ELEMENTS_TO_PROCESS - the compilation unit supplied to the operation is
159  * <code>null</code>.
160  * <li>INVALID_NAME - not a valid import declaration name.
161  * </ul>
162  * @see IJavaModelStatus
163  * @see JavaConventions
164  */

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