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.JavaModelException;
19 import net.sourceforge.phpdt.core.jdom.DOMFactory;
20 import net.sourceforge.phpdt.core.jdom.IDOMNode;
21 import net.sourceforge.phpdt.core.jdom.IDOMPackage;
22 import net.sourceforge.phpdt.internal.core.jdom.DOMNode;
23 import net.sourceforge.phpdt.internal.core.util.Util;
26 * <p>This operation adds/replaces a package declaration in an existing compilation unit.
27 * If the compilation unit already includes the specified package declaration,
28 * it is not generated (it does not generate duplicates).
30 * <p>Required Attributes:<ul>
31 * <li>Compilation unit element
35 public class CreatePackageDeclarationOperation extends CreateElementInCUOperation {
37 * The name of the package declaration being created
39 protected String fName = null;
41 * When executed, this operation will add a package declaration to the given compilation unit.
43 public CreatePackageDeclarationOperation(String name, ICompilationUnit parentElement) {
48 * @see CreateTypeMemberOperation#generateElementDOM
50 protected IDOMNode generateElementDOM() throws JavaModelException {
51 IJavaElement[] children = getCompilationUnit().getChildren();
52 //look for an existing package declaration
53 for (int i = 0; i < children.length; i++) {
54 if (children[i].getElementType() == IJavaElement.PACKAGE_DECLARATION) {
55 IPackageDeclaration pck = (IPackageDeclaration) children[i];
56 IDOMPackage pack = (IDOMPackage) ((JavaElement)pck).findNode(fCUDOM);
57 if (!pack.getName().equals(fName)) {
58 // get the insertion position before setting the name, as this makes it a detailed node
59 // thus the start position is always 0
60 DOMNode node = (DOMNode)pack;
61 fInsertionPosition = node.getStartPosition();
62 fReplacementLength = node.getEndPosition() - fInsertionPosition + 1;
64 fCreatedElement = (net.sourceforge.phpdt.internal.core.jdom.DOMNode)pack;
66 //equivalent package declaration already exists
67 fCreationOccurred= false;
73 IDOMPackage pack = (new DOMFactory()).createPackage();
78 * Creates and returns the handle for the element this operation created.
80 protected IJavaElement generateResultHandle() {
81 return getCompilationUnit().getPackageDeclaration(fName);
84 * @see CreateElementInCUOperation#getMainTaskName()
86 public String getMainTaskName(){
87 return Util.bind("operation.createPackageProgress"); //$NON-NLS-1$
90 * Sets the correct position for new package declaration:<ul>
91 * <li> before the first import
92 * <li> if no imports, before the first type
93 * <li> if no type - first thing in the CU
96 protected void initializeDefaultPosition() {
98 ICompilationUnit cu = getCompilationUnit();
99 // IImportDeclaration[] imports = cu.getImports();
100 // if (imports.length > 0) {
101 // createBefore(imports[0]);
104 IType[] types = cu.getTypes();
105 if (types.length > 0) {
106 createBefore(types[0]);
109 } catch (JavaModelException npe) {
113 * Possible failures: <ul>
114 * <li>NO_ELEMENTS_TO_PROCESS - no compilation unit was supplied to the operation
115 * <li>INVALID_NAME - a name supplied to the operation was not a valid
116 * package declaration name.
118 * @see IJavaModelStatus
119 * @see JavaConventions
121 public IJavaModelStatus verify() {
122 IJavaModelStatus status = super.verify();
123 if (!status.isOK()) {
126 // if (JavaConventions.validatePackageName(fName).getSeverity() == IStatus.ERROR) {
127 // return new JavaModelStatus(IJavaModelStatusConstants.INVALID_NAME, fName);
129 return JavaModelStatus.VERIFIED_OK;