1 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 ; 28 import org.netbeans.jmi.javamodel.Element; 29 import org.netbeans.jmi.javamodel.JavaModelPackage; 30 31 35 public abstract class IfStatementImpl extends ConditionImpl implements IfStatement { 36 private Statement thenPart = null; 37 private Statement elsePart = null; 38 39 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 getChildren() { 57 List 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 getRawText() { 90 boolean nju = getASTree() == null; 91 StringBuffer buf = new StringBuffer (); 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 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 if (childrenInited) { 131 deleteChild(thenPart); 132 deleteChild(elsePart); 133 } 134 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 |