1 11 package org.eclipse.jdt.internal.core; 12 13 import java.util.Iterator ; 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 39 public class CreateFieldOperation extends CreateTypeMemberOperation { 40 48 public CreateFieldOperation(IType parentElement, String 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 60 protected IJavaElement generateResultHandle() { 61 return getType().getField(getASTNodeName()); 62 } 63 66 public String getMainTaskName(){ 67 return Messages.operation_createFieldProgress; 68 } 69 private VariableDeclarationFragment getFragment(ASTNode node) { 70 Iterator fragments = ((FieldDeclaration) node).fragments().iterator(); 71 if (this.anchorElement != null) { 72 VariableDeclarationFragment fragment = null; 73 String 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 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 } 113 } 114 117 protected IJavaModelStatus verifyNameCollision() { 118 if (this.createdNode != null) { 119 IType type= getType(); 120 String 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 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 |