3m9 compatible;
[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.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;
25
26 import org.eclipse.core.runtime.IStatus;
27
28 /**
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).
32  *
33  * <p>Required Attributes:<ul>
34  *  <li>Compilation unit element
35  *  <li>Package name
36  * </ul>
37  */
38 public class CreatePackageDeclarationOperation extends CreateElementInCUOperation {
39         /**
40          * The name of the package declaration being created
41          */
42         protected String fName = null;
43 /**
44  * When executed, this operation will add a package declaration to the given compilation unit.
45  */
46 public CreatePackageDeclarationOperation(String name, ICompilationUnit parentElement) {
47         super(parentElement);
48         fName= name;
49 }
50 /**
51  * @see CreateTypeMemberOperation#generateElementDOM
52  */
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;
66                                 pack.setName(fName);
67                                 fCreatedElement = (net.sourceforge.phpdt.internal.core.jdom.DOMNode)pack;
68                         } else {
69                                 //equivalent package declaration already exists
70                                 fCreationOccurred= false;
71                         }
72                         
73                         return null;
74                 }
75         }
76         IDOMPackage pack = (new DOMFactory()).createPackage();
77         pack.setName(fName); 
78         return pack;
79 }
80 /**
81  * Creates and returns the handle for the element this operation created.
82  */
83 protected IJavaElement generateResultHandle() {
84         return getCompilationUnit().getPackageDeclaration(fName);
85 }
86 /**
87  * @see CreateElementInCUOperation#getMainTaskName()
88  */
89 public String getMainTaskName(){
90         return Util.bind("operation.createPackageProgress"); //$NON-NLS-1$
91 }
92 /**
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
97  * <li> 
98  */
99 protected void initializeDefaultPosition() {
100         try {
101                 ICompilationUnit cu = getCompilationUnit();
102 //              IImportDeclaration[] imports = cu.getImports();
103 //              if (imports.length > 0) {
104 //                      createBefore(imports[0]);
105 //                      return;
106 //              }
107                 IType[] types = cu.getTypes();
108                 if (types.length > 0) {
109                         createBefore(types[0]);
110                         return;
111                 }
112         } catch (JavaModelException npe) {
113         }
114 }
115 /**
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.
120  * </ul>
121  * @see IJavaModelStatus
122  * @see JavaConventions
123  */
124 public IJavaModelStatus verify() {
125         IJavaModelStatus status = super.verify();
126         if (!status.isOK()) {
127                 return status;
128         }
129 //      if (JavaConventions.validatePackageName(fName).getSeverity() == IStatus.ERROR) {
130 //              return new JavaModelStatus(IJavaModelStatusConstants.INVALID_NAME, fName);
131 //      }
132         return JavaModelStatus.VERIFIED_OK;
133 }
134 }