1 /*******************************************************************************
2 * Copyright (c) 2000, 2003 IBM Corporation and others.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Common Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/cpl-v10.html
9 * IBM Corporation - initial API and implementation
10 *******************************************************************************/
11 package net.sourceforge.phpdt.internal.core;
13 import net.sourceforge.phpdt.core.ICompilationUnit;
14 import net.sourceforge.phpdt.core.IJavaElement;
15 import net.sourceforge.phpdt.core.IJavaModelStatus;
16 import net.sourceforge.phpdt.core.IPackageDeclaration;
17 import net.sourceforge.phpdt.core.IType;
18 import net.sourceforge.phpdt.core.JavaConventions;
19 import net.sourceforge.phpdt.core.JavaModelException;
20 import net.sourceforge.phpdt.core.jdom.DOMFactory;
21 import net.sourceforge.phpdt.core.jdom.IDOMNode;
22 import net.sourceforge.phpdt.core.jdom.IDOMPackage;
23 import net.sourceforge.phpdt.internal.core.jdom.DOMNode;
24 import net.sourceforge.phpdt.internal.core.util.Util;
28 * This operation adds/replaces a package declaration in an existing compilation
29 * unit. If the compilation unit already includes the specified package
30 * declaration, it is not generated (it does not generate duplicates).
33 * Required Attributes:
35 * <li>Compilation unit element
39 public class CreatePackageDeclarationOperation extends
40 CreateElementInCUOperation {
42 * The name of the package declaration being created
44 protected String fName = null;
47 * When executed, this operation will add a package declaration to the given
50 public CreatePackageDeclarationOperation(String name,
51 ICompilationUnit parentElement) {
57 * @see CreateTypeMemberOperation#generateElementDOM
59 protected IDOMNode generateElementDOM() throws JavaModelException {
60 IJavaElement[] children = getCompilationUnit().getChildren();
61 // look for an existing package declaration
62 for (int i = 0; i < children.length; i++) {
63 if (children[i].getElementType() == IJavaElement.PACKAGE_DECLARATION) {
64 IPackageDeclaration pck = (IPackageDeclaration) children[i];
65 IDOMPackage pack = (IDOMPackage) ((JavaElement) pck)
67 if (!pack.getName().equals(fName)) {
68 // get the insertion position before setting the name, as
69 // this makes it a detailed node
70 // thus the start position is always 0
71 DOMNode node = (DOMNode) pack;
72 fInsertionPosition = node.getStartPosition();
73 fReplacementLength = node.getEndPosition()
74 - fInsertionPosition + 1;
76 fCreatedElement = (net.sourceforge.phpdt.internal.core.jdom.DOMNode) pack;
78 // equivalent package declaration already exists
79 fCreationOccurred = false;
85 IDOMPackage pack = (new DOMFactory()).createPackage();
91 * Creates and returns the handle for the element this operation created.
93 protected IJavaElement generateResultHandle() {
94 return getCompilationUnit().getPackageDeclaration(fName);
98 * @see CreateElementInCUOperation#getMainTaskName()
100 public String getMainTaskName() {
101 return Util.bind("operation.createPackageProgress"); //$NON-NLS-1$
105 * Sets the correct position for new package declaration:
107 * <li> before the first import
108 * <li> if no imports, before the first type
109 * <li> if no type - first thing in the CU
112 protected void initializeDefaultPosition() {
114 ICompilationUnit cu = getCompilationUnit();
115 // IImportDeclaration[] imports = cu.getImports();
116 // if (imports.length > 0) {
117 // createBefore(imports[0]);
120 IType[] types = cu.getTypes();
121 if (types.length > 0) {
122 createBefore(types[0]);
125 } catch (JavaModelException npe) {
132 * <li>NO_ELEMENTS_TO_PROCESS - no compilation unit was supplied to the
134 * <li>INVALID_NAME - a name supplied to the operation was not a valid
135 * package declaration name.
138 * @see IJavaModelStatus
139 * @see JavaConventions
141 public IJavaModelStatus verify() {
142 IJavaModelStatus status = super.verify();
143 if (!status.isOK()) {
146 // if (JavaConventions.validatePackageName(fName).getSeverity() ==
148 // return new JavaModelStatus(IJavaModelStatusConstants.INVALID_NAME,
151 return JavaModelStatus.VERIFIED_OK;