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