KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2000, 2005 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 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 /**
25  * <p>This operation creates a initializer in a type.
26  *
27  * <p>Required Attributes:<ul>
28  * <li>Containing Type
29  * <li>The source code for the initializer. No verification of the source is
30  * performed.
31  * </ul>
32  */

33 public class CreateInitializerOperation extends CreateTypeMemberOperation {
34     /**
35      * The current number of initializers in the parent type.
36      * Used to retrieve the handle of the newly created initializer.
37      */

38     protected int numberOfInitializers= 1;
39 /**
40  * When executed, this operation will create an initializer with the given name
41  * in the given type with the specified source.
42  *
43  * <p>By default the new initializer is positioned after the last existing initializer
44  * declaration, or as the first member in the type if there are no
45  * initializers.
46  */

47 public CreateInitializerOperation(IType parentElement, String JavaDoc 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 /**
57  * @see CreateElementInCUOperation#generateResultHandle
58  */

59 protected IJavaElement generateResultHandle() {
60     try {
61         //update the children to be current
62
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         // type doesn't exist: ignore
83
}
84     return null;
85 }
86 /**
87  * @see CreateElementInCUOperation#getMainTaskName()
88  */

89 public String JavaDoc getMainTaskName(){
90     return Messages.operation_createInitializerProgress;
91 }
92 protected SimpleName rename(ASTNode node, SimpleName newName) {
93     return null; // intializer cannot be renamed
94
}
95 /**
96  * By default the new initializer is positioned after the last existing initializer
97  * declaration, or as the first member in the type if there are no
98  * initializers.
99  */

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         // type doesn't exist: ignore
115
}
116 }
117 }
118
Popular Tags