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.IJavaModelStatusConstants;
17 import net.sourceforge.phpdt.core.IPackageDeclaration;
18 import net.sourceforge.phpdt.core.IType;
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;
26 import org.eclipse.core.runtime.IStatus;
29 * <p>This operation adds/replaces a package declaration in an existing compilation unit.
30 * If the compilation unit already includes the specified package declaration,
31 * it is not generated (it does not generate duplicates).
33 * <p>Required Attributes:<ul>
34 * <li>Compilation unit element
38 public class CreatePackageDeclarationOperation extends CreateElementInCUOperation {
40 * The name of the package declaration being created
42 protected String fName = null;
44 * When executed, this operation will add a package declaration to the given compilation unit.
46 public CreatePackageDeclarationOperation(String name, ICompilationUnit parentElement) {
51 * @see CreateTypeMemberOperation#generateElementDOM
53 protected IDOMNode generateElementDOM() throws JavaModelException {
54 IJavaElement[] children = getCompilationUnit().getChildren();
55 //look for an existing package declaration
56 for (int i = 0; i < children.length; i++) {
57 if (children[i].getElementType() == IJavaElement.PACKAGE_DECLARATION) {
58 IPackageDeclaration pck = (IPackageDeclaration) children[i];
59 IDOMPackage pack = (IDOMPackage) ((JavaElement)pck).findNode(fCUDOM);
60 if (!pack.getName().equals(fName)) {
61 // get the insertion position before setting the name, as this makes it a detailed node
62 // thus the start position is always 0
63 DOMNode node = (DOMNode)pack;
64 fInsertionPosition = node.getStartPosition();
65 fReplacementLength = node.getEndPosition() - fInsertionPosition + 1;
67 fCreatedElement = (net.sourceforge.phpdt.internal.core.jdom.DOMNode)pack;
69 //equivalent package declaration already exists
70 fCreationOccurred= false;
76 IDOMPackage pack = (new DOMFactory()).createPackage();
81 * Creates and returns the handle for the element this operation created.
83 protected IJavaElement generateResultHandle() {
84 return getCompilationUnit().getPackageDeclaration(fName);
87 * @see CreateElementInCUOperation#getMainTaskName()
89 public String getMainTaskName(){
90 return Util.bind("operation.createPackageProgress"); //$NON-NLS-1$
93 * Sets the correct position for new package declaration:<ul>
94 * <li> before the first import
95 * <li> if no imports, before the first type
96 * <li> if no type - first thing in the CU
99 protected void initializeDefaultPosition() {
101 ICompilationUnit cu = getCompilationUnit();
102 // IImportDeclaration[] imports = cu.getImports();
103 // if (imports.length > 0) {
104 // createBefore(imports[0]);
107 IType[] types = cu.getTypes();
108 if (types.length > 0) {
109 createBefore(types[0]);
112 } catch (JavaModelException npe) {
116 * Possible failures: <ul>
117 * <li>NO_ELEMENTS_TO_PROCESS - no compilation unit was supplied to the operation
118 * <li>INVALID_NAME - a name supplied to the operation was not a valid
119 * package declaration name.
121 * @see IJavaModelStatus
122 * @see JavaConventions
124 public IJavaModelStatus verify() {
125 IJavaModelStatus status = super.verify();
126 if (!status.isOK()) {
129 // if (JavaConventions.validatePackageName(fName).getSeverity() == IStatus.ERROR) {
130 // return new JavaModelStatus(IJavaModelStatusConstants.INVALID_NAME, fName);
132 return JavaModelStatus.VERIFIED_OK;