KickJava   Java API By Example, From Geeks To Geeks.

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


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.*;
22 import org.netbeans.jmi.javamodel.ConditionalExpression;
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.lib.java.parser.ASTree;
27 import org.netbeans.mdr.storagemodel.StorableObject;
28 import org.netbeans.modules.javacore.parser.ASTProvider;
29
30 /**
31  *
32  * @author Martin Matula
33  */

34 public abstract class ConditionalExpressionImpl extends ExpressionImpl implements ConditionalExpression {
35     private Expression condition = null;
36     private Expression truePart = null;
37     private Expression falsePart = null;
38     
39     /** Creates a new instance of ConditionalExpressionImpl */
40     public ConditionalExpressionImpl(StorableObject o) {
41         super(o);
42     }
43     
44     public void setCondition(Expression condition) {
45         objectChanged(CHANGED_CONDITION);
46         changeChild(getCondition(), condition);
47         this.condition = condition;
48     }
49     
50     public Expression getCondition() {
51         if (!childrenInited) {
52             initChildren();
53         }
54         return condition;
55     }
56     
57     public void setTruePart(Expression truePart) {
58         objectChanged(CHANGED_TRUE_PART);
59         changeChild(getTruePart(), truePart);
60         this.truePart = truePart;
61     }
62     
63     public Expression getTruePart() {
64         if (!childrenInited) {
65             initChildren();
66         }
67         return truePart;
68     }
69     
70     public void setFalsePart(Expression falsePart) {
71         objectChanged(CHANGED_FALSE_PART);
72         changeChild(getFalsePart(), falsePart);
73         this.falsePart = falsePart;
74     }
75     
76     public Expression getFalsePart() {
77         if (!childrenInited) {
78             initChildren();
79         }
80         return falsePart;
81     }
82     
83     public List getChildren() {
84         List list = new ArrayList(3);
85         addIfNotNull(list, getCondition());
86         addIfNotNull(list, getTruePart());
87         addIfNotNull(list, getFalsePart());
88         return list;
89     }
90     
91     protected void initChildren() {
92         childrenInited = false;
93         ASTree tree = getASTree();
94         if (tree != null) {
95             ASTree[] parts = tree.getSubTrees();
96             condition = (Expression) initOrCreate(condition, parts[0]);
97             truePart = (Expression) initOrCreate(truePart, parts[1]);
98             falsePart = (Expression) initOrCreate(falsePart, parts[2]);
99         }
100         childrenInited = true;
101     }
102     
103     public String JavaDoc getSourceText() {
104         String JavaDoc origElem;
105         if ((origElem = checkChange()) != null)
106             return origElem;
107         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
108         StatementImpl cond = (StatementImpl) getCondition();
109         StatementImpl falsePart = (StatementImpl) getFalsePart();
110         StatementImpl truePart = (StatementImpl) getTruePart();
111         buf.append(cond.getSourceText());
112         formatElementPart(COND_EXPR_QUESTION, buf);
113         buf.append(truePart.getSourceText());
114         formatElementPart(COND_EXPR_COLON, buf);
115         buf.append(falsePart.getSourceText());
116         return buf.toString();
117     }
118         
119     public void getDiff(List diff) {
120         ASTProvider parser = getParser();
121         ASTree tree = getASTree();
122         ASTree[] children = tree.getSubTrees();
123
124         getChildDiff(diff, parser, children[0], (MetadataElement) getCondition(), CHANGED_CONDITION);
125         getChildDiff(diff, parser, children[1], (MetadataElement) getTruePart(), CHANGED_TRUE_PART);
126         getChildDiff(diff, parser, children[2], (MetadataElement) getFalsePart(), CHANGED_FALSE_PART);
127     }
128     
129     void setData(Expression condition, Expression truePart, Expression falsePart) {
130         changeChild(null, condition);
131         this.condition = condition;
132         changeChild(null, truePart);
133         this.truePart = truePart;
134         changeChild(null, falsePart);
135         this.falsePart = falsePart;
136     }
137
138     protected void _delete() {
139         // --- delete components -------------------------------------------
140
if (childrenInited) {
141             deleteChild(condition);
142             deleteChild(truePart);
143             deleteChild(falsePart);
144         }
145         // --- delete links -----------------------------------------------
146
// no links to delete
147
// --- call super ---------------------------------------
148
super._delete();
149     }
150     
151     public void replaceChild(Element oldElement,Element newElement) {
152         if (childrenInited) {
153             if (oldElement.equals(condition)) {
154                 setCondition((Expression)newElement);
155             } else if (oldElement.equals(truePart)) {
156                 setTruePart((Expression)newElement);
157             } else if (oldElement.equals(falsePart)) {
158                 setFalsePart((Expression)newElement);
159             }
160         }
161     }
162     
163     public Element duplicate(JavaModelPackage targetExtent) {
164         return targetExtent.getConditionalExpression().createConditionalExpression(
165                 (Expression) duplicateElement(getCondition(), targetExtent),
166                 (Expression) duplicateElement(getTruePart(), targetExtent),
167                 (Expression) duplicateElement(getFalsePart(), targetExtent)
168                );
169     }
170 }
171
Popular Tags