diff options
Diffstat (limited to 'org.eclipse.jdt.core/model/org/eclipse/jdt/internal/core/CreatePackageDeclarationOperation.java')
-rw-r--r-- | org.eclipse.jdt.core/model/org/eclipse/jdt/internal/core/CreatePackageDeclarationOperation.java | 74 |
1 files changed, 33 insertions, 41 deletions
diff --git a/org.eclipse.jdt.core/model/org/eclipse/jdt/internal/core/CreatePackageDeclarationOperation.java b/org.eclipse.jdt.core/model/org/eclipse/jdt/internal/core/CreatePackageDeclarationOperation.java index 8c4474df..92658066 100644 --- a/org.eclipse.jdt.core/model/org/eclipse/jdt/internal/core/CreatePackageDeclarationOperation.java +++ b/org.eclipse.jdt.core/model/org/eclipse/jdt/internal/core/CreatePackageDeclarationOperation.java @@ -1,10 +1,10 @@ /******************************************************************************* - * Copyright (c) 2000, 2004 IBM Corporation and others. - * All rights reserved. This program and the accompanying materials - * are made available under the terms of the Common Public License v1.0 + * Copyright (c) 2000, 2005 IBM Corporation and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at - * http://www.eclipse.org/legal/cpl-v10.html - * + * http://www.eclipse.org/legal/epl-v10.html + * * Contributors: * IBM Corporation - initial API and implementation *******************************************************************************/ @@ -16,13 +16,18 @@ import org.eclipse.jdt.core.IImportDeclaration; import org.eclipse.jdt.core.IJavaElement; import org.eclipse.jdt.core.IJavaModelStatus; import org.eclipse.jdt.core.IJavaModelStatusConstants; -import org.eclipse.jdt.core.IPackageDeclaration; import org.eclipse.jdt.core.IType; import org.eclipse.jdt.core.JavaConventions; import org.eclipse.jdt.core.JavaModelException; -import org.eclipse.jdt.core.jdom.*; -import org.eclipse.jdt.internal.core.jdom.*; -import org.eclipse.jdt.internal.core.util.Util; +import org.eclipse.jdt.core.dom.AST; +import org.eclipse.jdt.core.dom.ASTNode; +import org.eclipse.jdt.core.dom.CompilationUnit; +import org.eclipse.jdt.core.dom.Name; +import org.eclipse.jdt.core.dom.PackageDeclaration; +import org.eclipse.jdt.core.dom.StructuralPropertyDescriptor; +import org.eclipse.jdt.core.dom.rewrite.ASTRewrite; +import org.eclipse.jdt.internal.core.util.Messages; +import org.eclipse.jface.text.IDocument; /** * <p>This operation adds/replaces a package declaration in an existing compilation unit. @@ -38,57 +43,44 @@ public class CreatePackageDeclarationOperation extends CreateElementInCUOperatio /** * The name of the package declaration being created */ - protected String fName = null; + protected String name = null; /** * When executed, this operation will add a package declaration to the given compilation unit. */ public CreatePackageDeclarationOperation(String name, ICompilationUnit parentElement) { super(parentElement); - fName= name; + this.name= name; } -/** - * @see CreateTypeMemberOperation#generateSyntaxIncorrectDOM() - * @deprecated JDOM is obsolete - */ -// TODO - JDOM - remove once model ported off of JDOM -protected IDOMNode generateElementDOM() throws JavaModelException { - IJavaElement[] children = getCompilationUnit().getChildren(); +protected StructuralPropertyDescriptor getChildPropertyDescriptor(ASTNode parent) { + return CompilationUnit.PACKAGE_PROPERTY; +} +protected ASTNode generateElementAST(ASTRewrite rewriter, IDocument document, ICompilationUnit cu) throws JavaModelException { //look for an existing package declaration + IJavaElement[] children = getCompilationUnit().getChildren(); for (int i = 0; i < children.length; i++) { - if (children[i].getElementType() == IJavaElement.PACKAGE_DECLARATION) { - IPackageDeclaration pck = (IPackageDeclaration) children[i]; - IDOMPackage pack = (IDOMPackage) ((JavaElement)pck).findNode(fCUDOM); - if (!pack.getName().equals(fName)) { - // get the insertion position before setting the name, as this makes it a detailed node - // thus the start position is always 0 - DOMNode node = (org.eclipse.jdt.internal.core.jdom.DOMNode)pack; - fInsertionPosition = node.getStartPosition(); - fReplacementLength = node.getEndPosition() - fInsertionPosition + 1; - pack.setName(fName); - fCreatedElement = (org.eclipse.jdt.internal.core.jdom.DOMNode)pack; - } else { - //equivalent package declaration already exists - fCreationOccurred= false; - } - + if (children[i].getElementType() == IJavaElement.PACKAGE_DECLARATION && this.name.equals(children[i].getElementName())) { + //equivalent package declaration already exists + this.creationOccurred = false; return null; } } - IDOMPackage pack = (new DOMFactory()).createPackage(); - pack.setName(fName); - return pack; + AST ast = this.cuAST.getAST(); + PackageDeclaration pkgDeclaration = ast.newPackageDeclaration(); + Name astName = ast.newName(this.name); + pkgDeclaration.setName(astName); + return pkgDeclaration; } /** * Creates and returns the handle for the element this operation created. */ protected IJavaElement generateResultHandle() { - return getCompilationUnit().getPackageDeclaration(fName); + return getCompilationUnit().getPackageDeclaration(this.name); } /** * @see CreateElementInCUOperation#getMainTaskName() */ public String getMainTaskName(){ - return Util.bind("operation.createPackageProgress"); //$NON-NLS-1$ + return Messages.operation_createPackageProgress; } /** * Sets the correct position for new package declaration:<ul> @@ -128,8 +120,8 @@ public IJavaModelStatus verify() { if (!status.isOK()) { return status; } - if (JavaConventions.validatePackageName(fName).getSeverity() == IStatus.ERROR) { - return new JavaModelStatus(IJavaModelStatusConstants.INVALID_NAME, fName); + if (JavaConventions.validatePackageName(this.name).getSeverity() == IStatus.ERROR) { + return new JavaModelStatus(IJavaModelStatusConstants.INVALID_NAME, this.name); } return JavaModelStatus.VERIFIED_OK; } |