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