9270628008ed79caa2d6629e0268b5d6597bfb31
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpdt / internal / core / CreatePackageDeclarationOperation.java
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
7  * 
8  * Contributors:
9  *     IBM Corporation - initial API and implementation
10  *******************************************************************************/
11 package net.sourceforge.phpdt.internal.core;
12
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;
24
25 /**
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).
29  *
30  * <p>Required Attributes:<ul>
31  *  <li>Compilation unit element
32  *  <li>Package name
33  * </ul>
34  */
35 public class CreatePackageDeclarationOperation extends CreateElementInCUOperation {
36         /**
37          * The name of the package declaration being created
38          */
39         protected String fName = null;
40 /**
41  * When executed, this operation will add a package declaration to the given compilation unit.
42  */
43 public CreatePackageDeclarationOperation(String name, ICompilationUnit parentElement) {
44         super(parentElement);
45         fName= name;
46 }
47 /**
48  * @see CreateTypeMemberOperation#generateElementDOM
49  */
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;
63                                 pack.setName(fName);
64                                 fCreatedElement = (net.sourceforge.phpdt.internal.core.jdom.DOMNode)pack;
65                         } else {
66                                 //equivalent package declaration already exists
67                                 fCreationOccurred= false;
68                         }
69                         
70                         return null;
71                 }
72         }
73         IDOMPackage pack = (new DOMFactory()).createPackage();
74         pack.setName(fName); 
75         return pack;
76 }
77 /**
78  * Creates and returns the handle for the element this operation created.
79  */
80 protected IJavaElement generateResultHandle() {
81         return getCompilationUnit().getPackageDeclaration(fName);
82 }
83 /**
84  * @see CreateElementInCUOperation#getMainTaskName()
85  */
86 public String getMainTaskName(){
87         return Util.bind("operation.createPackageProgress"); //$NON-NLS-1$
88 }
89 /**
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
94  * <li> 
95  */
96 protected void initializeDefaultPosition() {
97         try {
98                 ICompilationUnit cu = getCompilationUnit();
99 //              IImportDeclaration[] imports = cu.getImports();
100 //              if (imports.length > 0) {
101 //                      createBefore(imports[0]);
102 //                      return;
103 //              }
104                 IType[] types = cu.getTypes();
105                 if (types.length > 0) {
106                         createBefore(types[0]);
107                         return;
108                 }
109         } catch (JavaModelException npe) {
110         }
111 }
112 /**
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.
117  * </ul>
118  * @see IJavaModelStatus
119  * @see JavaConventions
120  */
121 public IJavaModelStatus verify() {
122         IJavaModelStatus status = super.verify();
123         if (!status.isOK()) {
124                 return status;
125         }
126 //      if (JavaConventions.validatePackageName(fName).getSeverity() == IStatus.ERROR) {
127 //              return new JavaModelStatus(IJavaModelStatusConstants.INVALID_NAME, fName);
128 //      }
129         return JavaModelStatus.VERIFIED_OK;
130 }
131 }