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