1 11 package org.eclipse.jdt.internal.core; 12 13 import org.eclipse.jdt.core.ICompilationUnit; 14 import org.eclipse.jdt.core.IJavaElement; 15 import org.eclipse.jdt.core.IJavaModelStatusConstants; 16 import org.eclipse.jdt.core.IType; 17 import org.eclipse.jdt.core.JavaModelException; 18 import org.eclipse.jdt.core.dom.ASTNode; 19 import org.eclipse.jdt.core.dom.SimpleName; 20 import org.eclipse.jdt.core.dom.rewrite.ASTRewrite; 21 import org.eclipse.jdt.internal.core.util.Messages; 22 import org.eclipse.jface.text.IDocument; 23 24 33 public class CreateInitializerOperation extends CreateTypeMemberOperation { 34 38 protected int numberOfInitializers= 1; 39 47 public CreateInitializerOperation(IType parentElement, String source) { 48 super(parentElement, source, false); 49 } 50 protected ASTNode generateElementAST(ASTRewrite rewriter, IDocument document, ICompilationUnit cu) throws JavaModelException { 51 ASTNode node = super.generateElementAST(rewriter, document, cu); 52 if (node.getNodeType() != ASTNode.INITIALIZER) 53 throw new JavaModelException(new JavaModelStatus(IJavaModelStatusConstants.INVALID_CONTENTS)); 54 return node; 55 } 56 59 protected IJavaElement generateResultHandle() { 60 try { 61 getType().getCompilationUnit().close(); 63 if (this.anchorElement == null) { 64 return getType().getInitializer(this.numberOfInitializers); 65 } else { 66 IJavaElement[] children = getType().getChildren(); 67 int count = 0; 68 for (int i = 0; i < children.length; i++) { 69 IJavaElement child = children[i]; 70 if (child.equals(this.anchorElement)) { 71 if (child .getElementType() == IJavaElement.INITIALIZER && this.insertionPolicy == CreateElementInCUOperation.INSERT_AFTER) { 72 count++; 73 } 74 return getType().getInitializer(count); 75 } else 76 if (child.getElementType() == IJavaElement.INITIALIZER) { 77 count++; 78 } 79 } 80 } 81 } catch (JavaModelException e) { 82 } 84 return null; 85 } 86 89 public String getMainTaskName(){ 90 return Messages.operation_createInitializerProgress; 91 } 92 protected SimpleName rename(ASTNode node, SimpleName newName) { 93 return null; } 95 100 protected void initializeDefaultPosition() { 101 IType parentElement = getType(); 102 try { 103 IJavaElement[] elements = parentElement.getInitializers(); 104 if (elements != null && elements.length > 0) { 105 this.numberOfInitializers = elements.length; 106 createAfter(elements[elements.length - 1]); 107 } else { 108 elements = parentElement.getChildren(); 109 if (elements != null && elements.length > 0) { 110 createBefore(elements[0]); 111 } 112 } 113 } catch (JavaModelException e) { 114 } 116 } 117 } 118 | Popular Tags |