KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > javacore > jmiimpl > javamodel > TypeCastImpl


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19 package org.netbeans.modules.javacore.jmiimpl.javamodel;
20
21 import org.netbeans.jmi.javamodel.*;
22 import org.netbeans.lib.java.parser.ASTree;
23 import org.netbeans.mdr.storagemodel.StorableObject;
24 import org.netbeans.modules.javacore.parser.ASTProvider;
25 import java.util.*;
26
27 /**
28  *
29  * @author Martin Matula
30  */

31 public abstract class TypeCastImpl extends ExpressionImpl implements TypeCast {
32     private Expression expression;
33     private TypeReference typeName;
34
35     /** Creates a new instance of TypeCastImpl */
36     public TypeCastImpl(StorableObject o) {
37         super(o);
38     }
39     
40     public Expression getExpression() {
41         if (!childrenInited) {
42             initChildren();
43         }
44         return expression;
45     }
46     
47     public void setExpression(Expression expression) {
48         objectChanged(CHANGED_EXPRESSION);
49         changeChild(getExpression(), expression);
50         this.expression = expression;
51     }
52
53     public TypeReference getTypeName() {
54         if (!childrenInited) {
55             initChildren();
56         }
57         return typeName;
58     }
59
60     public void setTypeName(TypeReference typeName) {
61         objectChanged(CHANGED_TYPE);
62         changeChild(getTypeName(), typeName);
63         this.typeName = typeName;
64     }
65
66     public Type getType() {
67         return (Type) getTypeName().getElement();
68     }
69
70     public void setType(Type newValue) {
71         MultipartIdClass proxy = ((JavaModelPackage) refImmediatePackage()).getMultipartId();
72         setTypeName(proxy.createMultipartId(newValue.getName(), null, null));
73     }
74
75     public List getChildren() {
76         List list = new ArrayList(2);
77         addIfNotNull(list, getTypeName());
78         addIfNotNull(list, getExpression());
79         return list;
80     }
81     
82     protected void initChildren() {
83         childrenInited = false;
84         ASTree tree = getASTree();
85         if (tree != null) {
86             ASTree[] parts = tree.getSubTrees();
87             typeName = (TypeReference) initOrCreate(typeName, parts[0]);
88             expression = (Expression) initOrCreate(expression, parts[1]);
89         }
90         childrenInited = true;
91     }
92     
93     public String JavaDoc getSourceText() {
94         String JavaDoc origElem;
95         if ((origElem = checkChange()) != null)
96             return origElem;
97         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
98         StatementImpl expr = (StatementImpl) getExpression();
99         formatElementPart(STMT_OPEN_BRACKET, buf);
100         buf.append(((MetadataElement) getTypeName()).getSourceText());
101         formatElementPart(STMT_CLOSE_BRACKET, buf);
102         buf.append(' ');
103         buf.append(expr.getSourceText());
104         return buf.toString();
105     }
106
107     public void getDiff(List diff) {
108         ASTProvider parser = getParser();
109         ASTree tree = getASTree();
110         ASTree[] children = tree.getSubTrees();
111
112         getChildDiff(diff, parser, children[0], (MetadataElement) getTypeName(), CHANGED_TYPE);
113         getChildDiff(diff, parser, children[1], (MetadataElement) getExpression(), CHANGED_EXPRESSION);
114     }
115
116     void setData(Expression expression, TypeReference typeName) {
117         changeChild(null, expression);
118         this.expression = expression;
119         changeChild(null, typeName);
120         this.typeName = typeName;
121     }
122
123     protected void _delete() {
124         // --- delete components -------------------------------------------
125
if (childrenInited) {
126             deleteChild(expression);
127             deleteChild(typeName);
128         }
129         // --- delete links -----------------------------------------------
130
// no links to delete
131
// --- call super ---------------------------------------
132
super._delete();
133     }
134     
135     public void replaceChild(Element oldElement,Element newElement) {
136         if (childrenInited) {
137             if (oldElement.equals(expression)) {
138                 setExpression((Expression)newElement);
139             }
140             if (oldElement.equals(typeName)) {
141                 setTypeName((TypeReference)newElement);
142             }
143         }
144     }
145     
146     public Element duplicate(JavaModelPackage targetExtent) {
147         return targetExtent.getTypeCast().createTypeCast(
148                 (TypeReference) duplicateElement(getTypeName(), targetExtent),
149                 (Expression) duplicateElement(getExpression(), targetExtent)
150                );
151     }
152 }
153
Popular Tags