001: /*******************************************************************************
002: * Copyright (c) 2000, 2007 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.jdt.internal.core;
011:
012: import org.eclipse.core.runtime.IStatus;
013: import org.eclipse.jdt.core.ICompilationUnit;
014: import org.eclipse.jdt.core.IImportDeclaration;
015: import org.eclipse.jdt.core.IJavaElement;
016: import org.eclipse.jdt.core.IJavaModelStatus;
017: import org.eclipse.jdt.core.IJavaModelStatusConstants;
018: import org.eclipse.jdt.core.IJavaProject;
019: import org.eclipse.jdt.core.IType;
020: import org.eclipse.jdt.core.JavaConventions;
021: import org.eclipse.jdt.core.JavaCore;
022: import org.eclipse.jdt.core.JavaModelException;
023: import org.eclipse.jdt.core.dom.AST;
024: import org.eclipse.jdt.core.dom.ASTNode;
025: import org.eclipse.jdt.core.dom.CompilationUnit;
026: import org.eclipse.jdt.core.dom.Name;
027: import org.eclipse.jdt.core.dom.PackageDeclaration;
028: import org.eclipse.jdt.core.dom.StructuralPropertyDescriptor;
029: import org.eclipse.jdt.core.dom.rewrite.ASTRewrite;
030: import org.eclipse.jdt.internal.core.util.Messages;
031: import org.eclipse.jface.text.IDocument;
032:
033: /**
034: * <p>This operation adds/replaces a package declaration in an existing compilation unit.
035: * If the compilation unit already includes the specified package declaration,
036: * it is not generated (it does not generate duplicates).
037: *
038: * <p>Required Attributes:<ul>
039: * <li>Compilation unit element
040: * <li>Package name
041: * </ul>
042: */
043: public class CreatePackageDeclarationOperation extends
044: CreateElementInCUOperation {
045: /**
046: * The name of the package declaration being created
047: */
048: protected String name = null;
049:
050: /**
051: * When executed, this operation will add a package declaration to the given compilation unit.
052: */
053: public CreatePackageDeclarationOperation(String name,
054: ICompilationUnit parentElement) {
055: super (parentElement);
056: this .name = name;
057: }
058:
059: protected StructuralPropertyDescriptor getChildPropertyDescriptor(
060: ASTNode parent) {
061: return CompilationUnit.PACKAGE_PROPERTY;
062: }
063:
064: protected ASTNode generateElementAST(ASTRewrite rewriter,
065: IDocument document, ICompilationUnit cu)
066: throws JavaModelException {
067: //look for an existing package declaration
068: IJavaElement[] children = getCompilationUnit().getChildren();
069: for (int i = 0; i < children.length; i++) {
070: if (children[i].getElementType() == IJavaElement.PACKAGE_DECLARATION
071: && this .name.equals(children[i].getElementName())) {
072: //equivalent package declaration already exists
073: this .creationOccurred = false;
074: return null;
075: }
076: }
077: AST ast = this .cuAST.getAST();
078: PackageDeclaration pkgDeclaration = ast.newPackageDeclaration();
079: Name astName = ast.newName(this .name);
080: pkgDeclaration.setName(astName);
081: return pkgDeclaration;
082: }
083:
084: /**
085: * Creates and returns the handle for the element this operation created.
086: */
087: protected IJavaElement generateResultHandle() {
088: return getCompilationUnit().getPackageDeclaration(this .name);
089: }
090:
091: /**
092: * @see CreateElementInCUOperation#getMainTaskName()
093: */
094: public String getMainTaskName() {
095: return Messages.operation_createPackageProgress;
096: }
097:
098: /**
099: * Sets the correct position for new package declaration:<ul>
100: * <li> before the first import
101: * <li> if no imports, before the first type
102: * <li> if no type - first thing in the CU
103: * <li>
104: */
105: protected void initializeDefaultPosition() {
106: try {
107: ICompilationUnit cu = getCompilationUnit();
108: IImportDeclaration[] imports = cu.getImports();
109: if (imports.length > 0) {
110: createBefore(imports[0]);
111: return;
112: }
113: IType[] types = cu.getTypes();
114: if (types.length > 0) {
115: createBefore(types[0]);
116: return;
117: }
118: } catch (JavaModelException e) {
119: // cu doesn't exist: ignore
120: }
121: }
122:
123: /**
124: * Possible failures: <ul>
125: * <li>NO_ELEMENTS_TO_PROCESS - no compilation unit was supplied to the operation
126: * <li>INVALID_NAME - a name supplied to the operation was not a valid
127: * package declaration name.
128: * </ul>
129: * @see IJavaModelStatus
130: * @see JavaConventions
131: */
132: public IJavaModelStatus verify() {
133: IJavaModelStatus status = super .verify();
134: if (!status.isOK()) {
135: return status;
136: }
137: IJavaProject project = getParentElement().getJavaProject();
138: if (JavaConventions.validatePackageName(this .name,
139: project.getOption(JavaCore.COMPILER_SOURCE, true),
140: project.getOption(JavaCore.COMPILER_COMPLIANCE, true))
141: .getSeverity() == IStatus.ERROR) {
142: return new JavaModelStatus(
143: IJavaModelStatusConstants.INVALID_NAME, this.name);
144: }
145: return JavaModelStatus.VERIFIED_OK;
146: }
147: }
|