KickJava   Java API By Example, From Geeks To Geeks.

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


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 org.netbeans.jmi.javamodel.Element;
23 import org.netbeans.jmi.javamodel.Expression;
24 import org.netbeans.jmi.javamodel.ForStatement;
25 import org.netbeans.jmi.javamodel.Statement;
26 import org.netbeans.lib.java.parser.ASTree;
27 import org.netbeans.lib.java.parser.ASTreeTypes;
28 import org.netbeans.mdr.storagemodel.StorableObject;
29 import org.netbeans.modules.javacore.parser.ASTProvider;
30 import java.util.Iterator JavaDoc;
31 import java.util.List JavaDoc;
32 import org.netbeans.jmi.javamodel.JavaModelPackage;
33
34 /**
35  * Instances of element represent for loop in java source. They contain
36  * several attributes:
37  * <code>
38  * for (init; condition; steps)
39  * body;
40  * </code>
41  * Init, condition, steps and body represent children of this object. The
42  * expressions are available by calling getInit(), getExpression() from
43  * superclass and getSteps(). Getters can return null. Body is represented by
44  * Statement or by StatementBlock instance.
45  *
46  * @see ForEachStatementImpl
47  *
48  * @author Martin Matula
49  */

50 public abstract class ForStatementImpl extends ConditionImpl implements ForStatement {
51     private Statement body = null;
52     private LightAttrList steps = null;
53     private LightAttrList init = null;
54     
55     /** Creates a new instance of ForStatementImpl */
56     public ForStatementImpl(StorableObject o) {
57         super(o);
58     }
59     
60     public List JavaDoc getSteps() {
61         if (!childrenInited) {
62             initChildren();
63         }
64         return steps;
65     }
66     
67     public List JavaDoc getInit() {
68         if (!childrenInited) {
69             initChildren();
70         }
71         return init;
72     }
73     
74     public void setBody(Statement body) {
75         objectChanged(CHANGED_BODY);
76         changeChild(getBody(), body);
77         this.body = body;
78     }
79
80     protected void initChildren() {
81         childrenInited = false;
82         ASTree tree = getASTree();
83         if (tree != null) {
84             ASTree[] parts = tree.getSubTrees();
85             init = createChildrenList(init, "init", parts[0], ASTreeTypes.STATEMENT_EXPRESSION_LIST, CHANGED_INIT, false); // NOI18N
86
expression = (Expression) initOrCreate(expression, parts[1]);
87             steps = createChildrenList(steps, "steps", parts[2], ASTreeTypes.STATEMENT_EXPRESSION_LIST, CHANGED_STEPS, false); // NOI18N
88
body = (Statement) initOrCreate(body, parts[3]);
89         }
90         childrenInited = true;
91     }
92     
93     public Statement getBody() {
94         if (!childrenInited) {
95             initChildren();
96         }
97         return body;
98     }
99     
100     String JavaDoc getRawText() {
101         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
102         StatementImpl cond = (StatementImpl)getExpression();
103         StatementImpl body = (StatementImpl) getBody();
104         List JavaDoc init = getInit();
105         List JavaDoc steps = getSteps();
106         
107         formatElementPart(FOR_KEYWORD, buf);
108         formatElementPart(STMT_OPEN_BRACKET, buf);
109         Iterator JavaDoc iter = init.iterator();
110         while (iter.hasNext()) {
111             StatementImpl expr = (StatementImpl) iter.next();
112             buf.append(expr.getSourceText());
113             if (iter.hasNext())
114                 formatElementPart(MetadataElement.COMMA, buf);
115         }
116         formatElementPart(FOR_SEMICOLON, buf);
117         if (cond != null) {
118             buf.append(cond.getSourceText());
119         }
120         formatElementPart(FOR_SEMICOLON, buf);
121         iter = getSteps().iterator();
122         while (iter.hasNext()) {
123             StatementImpl expr = (StatementImpl) iter.next();
124             buf.append(expr.getSourceText());
125             if (iter.hasNext()) {
126                 formatElementPart(MetadataElement.COMMA, buf);
127             }
128         }
129         formatElementPart(STMT_CLOSE_BRACKET, buf);
130         buf.append(body.getSourceText());
131         return buf.toString();
132     }
133
134     public void getDiff(List JavaDoc diff) {
135         ASTProvider parser = getParser();
136         ASTree tree = getASTree();
137         ASTree[] children = tree.getSubTrees();
138
139         int endTokens[] = new int[3];
140         for (int i = 3; i > 0; i--) {
141             if (children[i] == null) {
142                 endTokens[i - 1] = endTokens[i] - 1;
143             } else {
144                 endTokens[i - 1] = children[i].getFirstToken() - 1;
145             }
146         }
147         getCollectionDiff(diff, parser, CHANGED_INIT, children[0], ASTreeTypes.STATEMENT_EXPRESSION_LIST, getInit(), parser.getToken(endTokens[0]).getStartOffset(), formatElementPart(MetadataElement.COMMA));
148         getChildDiff(diff, parser, children[1], (MetadataElement) getExpression(), CHANGED_EXPRESSION, parser.getToken(endTokens[1]).getStartOffset(), "");
149         getCollectionDiff(diff, parser, CHANGED_STEPS, children[2], ASTreeTypes.STATEMENT_EXPRESSION_LIST, getSteps(), parser.getToken(endTokens[2]).getStartOffset(), formatElementPart(MetadataElement.COMMA));
150         getChildDiff(diff, parser, children[3], (MetadataElement) getBody(), CHANGED_BODY);
151     }
152         
153     void setData(Expression expression, List JavaDoc steps, Statement body, List JavaDoc init) {
154         // expression
155
changeChild(null, expression);
156         this.expression = expression;
157         // steps
158
this.steps = createChildrenList("steps", steps, CHANGED_STEPS); // NOI18N
159
// body
160
changeChild(null, body);
161         this.body = body;
162         // init
163
this.init = createChildrenList("init", init, CHANGED_INIT); // NOI18N
164
}
165
166     protected void _delete() {
167         // --- delete components -------------------------------------------
168
if (childrenInited) {
169             deleteChildren(steps);
170             deleteChild(body);
171             deleteChildren(init);
172         }
173         // --- delete links -----------------------------------------------
174
// no links to delete
175
// --- call super ---------------------------------------
176
super._delete();
177     }
178     
179     public void replaceChild(Element oldElement,Element newElement) {
180         if (childrenInited) {
181             if (oldElement.equals(body)) {
182                 setBody((Statement)newElement);
183             } else if (replaceObject(getInit(),oldElement,newElement))
184                 ;
185             else if (replaceObject(getSteps(),oldElement,newElement))
186                 ;
187             else
188                 super.replaceChild(oldElement,newElement);
189         }
190     }
191     
192     public List JavaDoc getChildren() {
193         List JavaDoc list = new ArrayList JavaDoc(4);
194         list.addAll(getInit());
195         list.addAll(super.getChildren());
196         list.addAll(getSteps());
197         addIfNotNull(list, getBody());
198         return list;
199     }
200     
201     public Element duplicate(JavaModelPackage targetExtent) {
202         return targetExtent.getForStatement().createForStatement(
203                 (Expression) duplicateElement(getExpression(), targetExtent),
204                 duplicateList(getSteps(), targetExtent),
205                 (Statement) duplicateElement(getBody(), targetExtent),
206                 duplicateList(getInit(), targetExtent)
207                );
208     }
209 }
210
Popular Tags