KickJava   Java API By Example, From Geeks To Geeks.

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


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.IJavaModelStatus;
16 import org.eclipse.jdt.core.IJavaModelStatusConstants;
17 import org.eclipse.jdt.core.IType;
18 import org.eclipse.jdt.core.JavaModelException;
19 import org.eclipse.jdt.core.dom.ASTNode;
20 import org.eclipse.jdt.core.dom.AbstractTypeDeclaration;
21 import org.eclipse.jdt.core.dom.SimpleName;
22 import org.eclipse.jdt.core.dom.rewrite.ASTRewrite;
23 import org.eclipse.jdt.internal.core.util.Messages;
24 import org.eclipse.jface.text.IDocument;
25
26 /**
27  * <p>This operation creates a class or interface.
28  *
29  * <p>Required Attributes:<ul>
30  * <li>Parent element - must be a compilation unit, or type.
31  * <li>The source code for the type. No verification of the source is
32  * performed.
33  * </ul>
34  */

35 public class CreateTypeOperation extends CreateTypeMemberOperation {
36 /**
37  * When executed, this operation will create a type unit
38  * in the given parent element (a compilation unit, type)
39  */

40 public CreateTypeOperation(IJavaElement parentElement, String JavaDoc source, boolean force) {
41     super(parentElement, source, force);
42 }
43 protected ASTNode generateElementAST(ASTRewrite rewriter, IDocument document, ICompilationUnit cu) throws JavaModelException {
44     ASTNode node = super.generateElementAST(rewriter, document, cu);
45     if (!(node instanceof AbstractTypeDeclaration))
46         throw new JavaModelException(new JavaModelStatus(IJavaModelStatusConstants.INVALID_CONTENTS));
47     return node;
48 }
49
50 /**
51  * @see CreateElementInCUOperation#generateResultHandle()
52  */

53 protected IJavaElement generateResultHandle() {
54     IJavaElement parent= getParentElement();
55     switch (parent.getElementType()) {
56         case IJavaElement.COMPILATION_UNIT:
57             return ((ICompilationUnit)parent).getType(getASTNodeName());
58         case IJavaElement.TYPE:
59             return ((IType)parent).getType(getASTNodeName());
60         // Note: creating local/anonymous type is not supported
61
}
62     return null;
63 }
64 /**
65  * @see CreateElementInCUOperation#getMainTaskName()
66  */

67 public String JavaDoc getMainTaskName(){
68     return Messages.operation_createTypeProgress;
69 }
70 /**
71  * Returns the <code>IType</code> the member is to be created in.
72  */

73 protected IType getType() {
74     IJavaElement parent = getParentElement();
75     if (parent.getElementType() == IJavaElement.TYPE) {
76         return (IType) parent;
77     }
78     return null;
79 }
80 /**
81  * @see CreateTypeMemberOperation#verifyNameCollision
82  */

83 protected IJavaModelStatus verifyNameCollision() {
84     IJavaElement parent = getParentElement();
85     switch (parent.getElementType()) {
86         case IJavaElement.COMPILATION_UNIT:
87             String JavaDoc typeName = getASTNodeName();
88             if (((ICompilationUnit) parent).getType(typeName).exists()) {
89                 return new JavaModelStatus(
90                     IJavaModelStatusConstants.NAME_COLLISION,
91                     Messages.bind(Messages.status_nameCollision, typeName));
92             }
93             break;
94         case IJavaElement.TYPE:
95             typeName = getASTNodeName();
96             if (((IType) parent).getType(typeName).exists()) {
97                 return new JavaModelStatus(
98                     IJavaModelStatusConstants.NAME_COLLISION,
99                     Messages.bind(Messages.status_nameCollision, typeName));
100             }
101             break;
102         // Note: creating local/anonymous type is not supported
103
}
104     return JavaModelStatus.VERIFIED_OK;
105 }
106 private String JavaDoc getASTNodeName() {
107     return ((AbstractTypeDeclaration) this.createdNode).getName().getIdentifier();
108 }
109 protected SimpleName rename(ASTNode node, SimpleName newName) {
110     AbstractTypeDeclaration type = (AbstractTypeDeclaration) node;
111     SimpleName oldName = type.getName();
112     type.setName(newName);
113     return oldName;
114 }
115 }
116
Popular Tags