KickJava   Java API By Example, From Geeks To Geeks.

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


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

35 public abstract class IfStatementImpl extends ConditionImpl implements IfStatement {
36     private Statement thenPart = null;
37     private Statement elsePart = null;
38     
39     /** Creates a new instance of IfStatementImpl */
40     public IfStatementImpl(StorableObject o) {
41         super(o);
42     }
43     
44     public void setThenPart(Statement body) {
45         objectChanged(CHANGED_THEN_PART);
46         changeChild(getThenPart(), body);
47         this.thenPart = body;
48     }
49
50     public void setElsePart(Statement body) {
51         objectChanged(CHANGED_ELSE_PART);
52         changeChild(getElsePart(), body);
53         this.elsePart = body;
54     }
55
56     public List JavaDoc getChildren() {
57         List JavaDoc list = super.getChildren();
58         addIfNotNull(list, getThenPart());
59         addIfNotNull(list, getElsePart());
60         return list;
61     }
62     
63     protected void initChildren() {
64         childrenInited = false;
65         ASTree tree = getASTree();
66         if (tree != null) {
67             ASTree[] parts = tree.getSubTrees();
68             expression = (Expression) initOrCreate(expression, parts[0]);
69             thenPart = (Statement) initOrCreate(thenPart, parts[1]);
70             elsePart = (Statement) initOrCreate(elsePart, parts[2]);
71         }
72         childrenInited = true;
73     }
74     
75     public Statement getThenPart() {
76         if (!childrenInited) {
77             initChildren();
78         }
79         return thenPart;
80     }
81     
82     public Statement getElsePart() {
83         if (!childrenInited) {
84             initChildren();
85         }
86         return elsePart;
87     }
88
89     String JavaDoc getRawText() {
90         boolean nju = getASTree() == null;
91         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
92         StatementImpl cond = (StatementImpl) getExpression();
93         StatementImpl thenPart = (StatementImpl) getThenPart();
94         StatementImpl elsePart = (StatementImpl) getElsePart();
95         formatElementPart(IF_KEYWORD, buf);
96         formatElementPart(STMT_OPEN_BRACKET, buf);
97         buf.append(cond.getSourceText());
98         formatElementPart(STMT_CLOSE_BRACKET, buf);
99         buf.append(thenPart.getSourceText());
100         if (elsePart != null) {
101             if (!nju)
102                 IndentUtil.reformatHeadGarbage(this, thenPart.getASTree().getLastToken()+1, buf);
103             formatElementPart(ELSE_KEYWORD, buf, nju);
104             buf.append(elsePart.getSourceText());
105         }
106         return buf.toString();
107     }
108         
109     public void getDiff(List JavaDoc diff) {
110         ASTProvider parser = getParser();
111         ASTree tree = getASTree();
112         ASTree[] children = tree.getSubTrees();
113
114         getChildDiff(diff, parser, children[0], (MetadataElement) getExpression(), CHANGED_EXPRESSION);
115         getChildDiff(diff, parser, children[1], (MetadataElement) getThenPart(), CHANGED_THEN_PART);
116         getChildDiff(diff, parser, children[2], (MetadataElement) getElsePart(), CHANGED_ELSE_PART, parser.getToken(children[1].getLastToken()).getEndOffset(), formatElementPart(ELSE_KEYWORD));
117     }
118     
119     void setData(Expression expression, Statement thenPart, Statement elsePart) {
120         changeChild(null, expression);
121         changeChild(null, thenPart);
122         changeChild(null, elsePart);
123         this.expression = expression;
124         this.thenPart = thenPart;
125         this.elsePart = elsePart;
126     }
127
128     protected void _delete() {
129         // --- delete components -------------------------------------------
130
if (childrenInited) {
131             deleteChild(thenPart);
132             deleteChild(elsePart);
133         }
134         // --- delete links -----------------------------------------------
135
// no links to delete
136
// --- call super ---------------------------------------
137
super._delete();
138     }
139     
140     public void replaceChild(Element oldElement,Element newElement) {
141         if (childrenInited) {
142             if (oldElement.equals(thenPart)) {
143                 setThenPart((Statement)newElement);
144             } else if (oldElement.equals(elsePart)) {
145                 setElsePart((Statement)newElement);
146             } else
147                 super.replaceChild(oldElement,newElement);
148         }
149     }
150     
151     public Element duplicate(JavaModelPackage targetExtent) {
152         return targetExtent.getIfStatement().createIfStatement(
153                 (Expression) duplicateElement(getExpression(), targetExtent),
154                 (Statement) duplicateElement(getThenPart(), targetExtent),
155                 (Statement) duplicateElement(getElsePart(), targetExtent)
156                );
157     }
158 }
159
Popular Tags