001: /*******************************************************************************
002: * Copyright (c) 2000, 2006 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.jdt.internal.core;
011:
012: import java.util.Iterator;
013:
014: import org.eclipse.jdt.core.ICompilationUnit;
015: import org.eclipse.jdt.core.IField;
016: import org.eclipse.jdt.core.IJavaElement;
017: import org.eclipse.jdt.core.IJavaModelStatus;
018: import org.eclipse.jdt.core.IJavaModelStatusConstants;
019: import org.eclipse.jdt.core.IType;
020: import org.eclipse.jdt.core.JavaModelException;
021: import org.eclipse.jdt.core.dom.ASTNode;
022: import org.eclipse.jdt.core.dom.FieldDeclaration;
023: import org.eclipse.jdt.core.dom.SimpleName;
024: import org.eclipse.jdt.core.dom.VariableDeclarationFragment;
025: import org.eclipse.jdt.core.dom.rewrite.ASTRewrite;
026: import org.eclipse.jdt.internal.core.util.Messages;
027: import org.eclipse.jface.text.IDocument;
028:
029: /**
030: * <p>This operation creates a field declaration in a type.
031: *
032: * <p>Required Attributes:<ul>
033: * <li>Containing Type
034: * <li>The source code for the declaration. No verification of the source is
035: * performed.
036: * </ul>
037: */
038: public class CreateFieldOperation extends CreateTypeMemberOperation {
039: /**
040: * When executed, this operation will create a field with the given name
041: * in the given type with the specified source.
042: *
043: * <p>By default the new field is positioned after the last existing field
044: * declaration, or as the first member in the type if there are no
045: * field declarations.
046: */
047: public CreateFieldOperation(IType parentElement, String source,
048: boolean force) {
049: super (parentElement, source, force);
050: }
051:
052: protected ASTNode generateElementAST(ASTRewrite rewriter,
053: IDocument document, ICompilationUnit cu)
054: throws JavaModelException {
055: ASTNode node = super .generateElementAST(rewriter, document, cu);
056: if (node.getNodeType() != ASTNode.FIELD_DECLARATION)
057: throw new JavaModelException(new JavaModelStatus(
058: IJavaModelStatusConstants.INVALID_CONTENTS));
059: return node;
060: }
061:
062: /**
063: * @see CreateElementInCUOperation#generateResultHandle
064: */
065: protected IJavaElement generateResultHandle() {
066: return getType().getField(getASTNodeName());
067: }
068:
069: /**
070: * @see CreateElementInCUOperation#getMainTaskName()
071: */
072: public String getMainTaskName() {
073: return Messages.operation_createFieldProgress;
074: }
075:
076: private VariableDeclarationFragment getFragment(ASTNode node) {
077: Iterator fragments = ((FieldDeclaration) node).fragments()
078: .iterator();
079: if (this .anchorElement != null) {
080: VariableDeclarationFragment fragment = null;
081: String fragmentName = this .anchorElement.getElementName();
082: while (fragments.hasNext()) {
083: fragment = (VariableDeclarationFragment) fragments
084: .next();
085: if (fragment.getName().getIdentifier().equals(
086: fragmentName)) {
087: return fragment;
088: }
089: }
090: return fragment;
091: } else {
092: return (VariableDeclarationFragment) fragments.next();
093: }
094: }
095:
096: /**
097: * By default the new field is positioned after the last existing field
098: * declaration, or as the first member in the type if there are no
099: * field declarations.
100: */
101: protected void initializeDefaultPosition() {
102: IType parentElement = getType();
103: try {
104: IField[] fields = parentElement.getFields();
105: if (fields != null && fields.length > 0) {
106: final IField lastField = fields[fields.length - 1];
107: if (parentElement.isEnum()) {
108: IField field = lastField;
109: if (!field.isEnumConstant()) {
110: createAfter(lastField);
111: }
112: } else {
113: createAfter(lastField);
114: }
115: } else {
116: IJavaElement[] elements = parentElement.getChildren();
117: if (elements != null && elements.length > 0) {
118: createBefore(elements[0]);
119: }
120: }
121: } catch (JavaModelException e) {
122: // type doesn't exist: ignore
123: }
124: }
125:
126: /**
127: * @see CreateTypeMemberOperation#verifyNameCollision
128: */
129: protected IJavaModelStatus verifyNameCollision() {
130: if (this .createdNode != null) {
131: IType type = getType();
132: String fieldName = getASTNodeName();
133: if (type.getField(fieldName).exists()) {
134: return new JavaModelStatus(
135: IJavaModelStatusConstants.NAME_COLLISION,
136: Messages.bind(Messages.status_nameCollision,
137: fieldName));
138: }
139: }
140: return JavaModelStatus.VERIFIED_OK;
141: }
142:
143: private String getASTNodeName() {
144: if (this .alteredName != null)
145: return this .alteredName;
146: return getFragment(this .createdNode).getName().getIdentifier();
147: }
148:
149: protected SimpleName rename(ASTNode node, SimpleName newName) {
150: VariableDeclarationFragment fragment = getFragment(node);
151: SimpleName oldName = fragment.getName();
152: fragment.setName(newName);
153: return oldName;
154: }
155: }
|