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