1 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 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 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 getSourceText() { 104 String origElem; 105 if ((origElem = checkChange()) != null) 106 return origElem; 107 StringBuffer buf = new StringBuffer (); 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 if (childrenInited) { 141 deleteChild(condition); 142 deleteChild(truePart); 143 deleteChild(falsePart); 144 } 145 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 |