KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > core > CreateFieldOperation


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.jdt.internal.core;
12
13 import java.util.Iterator JavaDoc;
14
15 import org.eclipse.jdt.core.ICompilationUnit;
16 import org.eclipse.jdt.core.IField;
17 import org.eclipse.jdt.core.IJavaElement;
18 import org.eclipse.jdt.core.IJavaModelStatus;
19 import org.eclipse.jdt.core.IJavaModelStatusConstants;
20 import org.eclipse.jdt.core.IType;
21 import org.eclipse.jdt.core.JavaModelException;
22 import org.eclipse.jdt.core.dom.ASTNode;
23 import org.eclipse.jdt.core.dom.FieldDeclaration;
24 import org.eclipse.jdt.core.dom.SimpleName;
25 import org.eclipse.jdt.core.dom.VariableDeclarationFragment;
26 import org.eclipse.jdt.core.dom.rewrite.ASTRewrite;
27 import org.eclipse.jdt.internal.core.util.Messages;
28 import org.eclipse.jface.text.IDocument;
29
30 /**
31  * <p>This operation creates a field declaration in a type.
32  *
33  * <p>Required Attributes:<ul>
34  * <li>Containing Type
35  * <li>The source code for the declaration. No verification of the source is
36  * performed.
37  * </ul>
38  */

39 public class CreateFieldOperation extends CreateTypeMemberOperation {
40 /**
41  * When executed, this operation will create a field with the given name
42  * in the given type with the specified source.
43  *
44  * <p>By default the new field is positioned after the last existing field
45  * declaration, or as the first member in the type if there are no
46  * field declarations.
47  */

48 public CreateFieldOperation(IType parentElement, String JavaDoc source, boolean force) {
49     super(parentElement, source, force);
50 }
51 protected ASTNode generateElementAST(ASTRewrite rewriter, IDocument document, ICompilationUnit cu) throws JavaModelException {
52     ASTNode node = super.generateElementAST(rewriter, document, cu);
53     if (node.getNodeType() != ASTNode.FIELD_DECLARATION)
54         throw new JavaModelException(new JavaModelStatus(IJavaModelStatusConstants.INVALID_CONTENTS));
55     return node;
56 }
57 /**
58  * @see CreateElementInCUOperation#generateResultHandle
59  */

60 protected IJavaElement generateResultHandle() {
61     return getType().getField(getASTNodeName());
62 }
63 /**
64  * @see CreateElementInCUOperation#getMainTaskName()
65  */

66 public String JavaDoc getMainTaskName(){
67     return Messages.operation_createFieldProgress;
68 }
69 private VariableDeclarationFragment getFragment(ASTNode node) {
70     Iterator JavaDoc fragments = ((FieldDeclaration) node).fragments().iterator();
71     if (this.anchorElement != null) {
72         VariableDeclarationFragment fragment = null;
73         String JavaDoc fragmentName = this.anchorElement.getElementName();
74         while (fragments.hasNext()) {
75             fragment = (VariableDeclarationFragment) fragments.next();
76             if (fragment.getName().getIdentifier().equals(fragmentName)) {
77                 return fragment;
78             }
79         }
80         return fragment;
81     } else {
82         return (VariableDeclarationFragment) fragments.next();
83     }
84 }
85 /**
86  * By default the new field is positioned after the last existing field
87  * declaration, or as the first member in the type if there are no
88  * field declarations.
89  */

90 protected void initializeDefaultPosition() {
91     IType parentElement = getType();
92     try {
93         IField[] fields = parentElement.getFields();
94         if (fields != null && fields.length > 0) {
95             final IField lastField = fields[fields.length - 1];
96             if (parentElement.isEnum()) {
97                 IField field = lastField;
98                 if (!field.isEnumConstant()) {
99                     createAfter(lastField);
100                 }
101             } else {
102                 createAfter(lastField);
103             }
104         } else {
105             IJavaElement[] elements = parentElement.getChildren();
106             if (elements != null && elements.length > 0) {
107                 createBefore(elements[0]);
108             }
109         }
110     } catch (JavaModelException e) {
111         // type doesn't exist: ignore
112
}
113 }
114 /**
115  * @see CreateTypeMemberOperation#verifyNameCollision
116  */

117 protected IJavaModelStatus verifyNameCollision() {
118     if (this.createdNode != null) {
119         IType type= getType();
120         String JavaDoc fieldName = getASTNodeName();
121         if (type.getField(fieldName).exists()) {
122             return new JavaModelStatus(
123                 IJavaModelStatusConstants.NAME_COLLISION,
124                 Messages.bind(Messages.status_nameCollision, fieldName));
125         }
126     }
127     return JavaModelStatus.VERIFIED_OK;
128 }
129 private String JavaDoc getASTNodeName() {
130     if (this.alteredName != null) return this.alteredName;
131     return getFragment(this.createdNode).getName().getIdentifier();
132 }
133 protected SimpleName rename(ASTNode node, SimpleName newName) {
134     VariableDeclarationFragment fragment = getFragment(node);
135     SimpleName oldName = fragment.getName();
136     fragment.setName(newName);
137     return oldName;
138 }
139 }
140
Popular Tags