KickJava   Java API By Example, From Geeks To Geeks.

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


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 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 /**
35  * <p>This operation adds/replaces a package declaration in an existing compilation unit.
36  * If the compilation unit already includes the specified package declaration,
37  * it is not generated (it does not generate duplicates).
38  *
39  * <p>Required Attributes:<ul>
40  * <li>Compilation unit element
41  * <li>Package name
42  * </ul>
43  */

44 public class CreatePackageDeclarationOperation extends CreateElementInCUOperation {
45     /**
46      * The name of the package declaration being created
47      */

48     protected String JavaDoc name = null;
49 /**
50  * When executed, this operation will add a package declaration to the given compilation unit.
51  */

52 public CreatePackageDeclarationOperation(String JavaDoc 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     //look for an existing package declaration
61
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             //equivalent package declaration already exists
65
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 /**
76  * Creates and returns the handle for the element this operation created.
77  */

78 protected IJavaElement generateResultHandle() {
79     return getCompilationUnit().getPackageDeclaration(this.name);
80 }
81 /**
82  * @see CreateElementInCUOperation#getMainTaskName()
83  */

84 public String JavaDoc getMainTaskName(){
85     return Messages.operation_createPackageProgress;
86 }
87 /**
88  * Sets the correct position for new package declaration:<ul>
89  * <li> before the first import
90  * <li> if no imports, before the first type
91  * <li> if no type - first thing in the CU
92  * <li>
93  */

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         // cu doesn't exist: ignore
109
}
110 }
111 /**
112  * Possible failures: <ul>
113  * <li>NO_ELEMENTS_TO_PROCESS - no compilation unit was supplied to the operation
114  * <li>INVALID_NAME - a name supplied to the operation was not a valid
115  * package declaration name.
116  * </ul>
117  * @see IJavaModelStatus
118  * @see JavaConventions
119  */

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