KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.util.ArrayList JavaDoc;
22 import java.util.List JavaDoc;
23 import org.netbeans.jmi.javamodel.Element;
24 import org.netbeans.jmi.javamodel.Expression;
25 import org.netbeans.jmi.javamodel.JavaModelPackage;
26 import org.netbeans.jmi.javamodel.Statement;
27 import org.netbeans.jmi.javamodel.WhileStatement;
28 import org.netbeans.lib.java.parser.ASTree;
29 import org.netbeans.mdr.storagemodel.StorableObject;
30 import org.netbeans.modules.javacore.parser.ASTProvider;
31
32 /**
33  *
34  * @author Martin Matula
35  */

36 public abstract class WhileStatementImpl extends ConditionImpl implements WhileStatement {
37     protected Statement body = null;
38     
39     /** Creates a new instance of WhileStatementImpl */
40     public WhileStatementImpl(StorableObject o) {
41         super(o);
42     }
43     
44     public void setBody(Statement body) {
45         objectChanged(CHANGED_BODY);
46         changeChild(getBody(), body);
47         this.body = body;
48     }
49
50     public List JavaDoc getChildren() {
51         List JavaDoc list = new ArrayList JavaDoc(4);
52         addIfNotNull(list, getBody());
53         list.addAll(super.getChildren());
54         return list;
55     }
56     
57     protected void initChildren() {
58         childrenInited = false;
59         ASTree tree = getASTree();
60         if (tree != null) {
61             ASTree[] parts = tree.getSubTrees();
62             expression = (Expression) initOrCreate(expression, parts[0]);
63             body = (Statement) initOrCreate(body, parts[1]);
64         }
65         childrenInited = true;
66     }
67     
68     public Statement getBody() {
69         if (!childrenInited) {
70             initChildren();
71         }
72         return body;
73     }
74     
75     String JavaDoc getRawText() {
76         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
77         StatementImpl cond = (StatementImpl) getExpression();
78         StatementImpl body = (StatementImpl) getBody();
79         
80         formatElementPart(WHILE_KEYWORD, buf);
81         formatElementPart(STMT_OPEN_BRACKET, buf);
82         buf.append(cond.getSourceText());
83         formatElementPart(STMT_CLOSE_BRACKET, buf);
84         buf.append(body.getSourceText());
85         return buf.toString();
86     }
87     
88     public void getDiff(List JavaDoc diff){
89         ASTProvider parser = getParser();
90         
91         ASTree[] children = getASTree().getSubTrees();
92         
93         getChildDiff(diff, parser, children[0], (MetadataElement) getExpression(), CHANGED_EXPRESSION);
94         getChildDiff(diff, parser, children[1], (MetadataElement) getBody(), CHANGED_BODY);
95     }
96     
97     void setData(Expression expression, Statement body) {
98         changeChild(null, expression);
99         changeChild(null, body);
100         this.expression = expression;
101         this.body = body;
102     }
103
104     protected void _delete() {
105         // --- delete components -------------------------------------------
106
if (childrenInited) {
107             deleteChild(body);
108         }
109         // --- delete links -----------------------------------------------
110
// no links to delete
111
// --- call super ---------------------------------------
112
super._delete();
113     }
114     
115     public void replaceChild(Element oldElement,Element newElement) {
116         if (childrenInited) {
117             if (oldElement.equals(body)) {
118                 setBody((Statement)newElement);
119             } else
120                 super.replaceChild(oldElement,newElement);
121         }
122     }
123     
124     public Element duplicate(JavaModelPackage targetExtent) {
125         return targetExtent.getWhileStatement().createWhileStatement(
126                 (Expression) duplicateElement(getExpression(), targetExtent),
127                 (Statement) duplicateElement(getBody(), targetExtent)
128                );
129     }
130 }
131
Popular Tags