1 19 package org.netbeans.modules.javacore.jmiimpl.javamodel; 20 21 import java.util.ArrayList ; 22 import java.util.List ; 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.jmi.javamodel.Statement; 27 import org.netbeans.jmi.javamodel.WhileStatement; 28 import org.netbeans.lib.java.parser.ASTree; 29 import org.netbeans.mdr.storagemodel.StorableObject; 30 import org.netbeans.modules.javacore.parser.ASTProvider; 31 32 36 public abstract class WhileStatementImpl extends ConditionImpl implements WhileStatement { 37 protected Statement body = null; 38 39 40 public WhileStatementImpl(StorableObject o) { 41 super(o); 42 } 43 44 public void setBody(Statement body) { 45 objectChanged(CHANGED_BODY); 46 changeChild(getBody(), body); 47 this.body = body; 48 } 49 50 public List getChildren() { 51 List list = new ArrayList (4); 52 addIfNotNull(list, getBody()); 53 list.addAll(super.getChildren()); 54 return list; 55 } 56 57 protected void initChildren() { 58 childrenInited = false; 59 ASTree tree = getASTree(); 60 if (tree != null) { 61 ASTree[] parts = tree.getSubTrees(); 62 expression = (Expression) initOrCreate(expression, parts[0]); 63 body = (Statement) initOrCreate(body, parts[1]); 64 } 65 childrenInited = true; 66 } 67 68 public Statement getBody() { 69 if (!childrenInited) { 70 initChildren(); 71 } 72 return body; 73 } 74 75 String getRawText() { 76 StringBuffer buf = new StringBuffer (); 77 StatementImpl cond = (StatementImpl) getExpression(); 78 StatementImpl body = (StatementImpl) getBody(); 79 80 formatElementPart(WHILE_KEYWORD, buf); 81 formatElementPart(STMT_OPEN_BRACKET, buf); 82 buf.append(cond.getSourceText()); 83 formatElementPart(STMT_CLOSE_BRACKET, buf); 84 buf.append(body.getSourceText()); 85 return buf.toString(); 86 } 87 88 public void getDiff(List diff){ 89 ASTProvider parser = getParser(); 90 91 ASTree[] children = getASTree().getSubTrees(); 92 93 getChildDiff(diff, parser, children[0], (MetadataElement) getExpression(), CHANGED_EXPRESSION); 94 getChildDiff(diff, parser, children[1], (MetadataElement) getBody(), CHANGED_BODY); 95 } 96 97 void setData(Expression expression, Statement body) { 98 changeChild(null, expression); 99 changeChild(null, body); 100 this.expression = expression; 101 this.body = body; 102 } 103 104 protected void _delete() { 105 if (childrenInited) { 107 deleteChild(body); 108 } 109 super._delete(); 113 } 114 115 public void replaceChild(Element oldElement,Element newElement) { 116 if (childrenInited) { 117 if (oldElement.equals(body)) { 118 setBody((Statement)newElement); 119 } else 120 super.replaceChild(oldElement,newElement); 121 } 122 } 123 124 public Element duplicate(JavaModelPackage targetExtent) { 125 return targetExtent.getWhileStatement().createWhileStatement( 126 (Expression) duplicateElement(getExpression(), targetExtent), 127 (Statement) duplicateElement(getBody(), targetExtent) 128 ); 129 } 130 } 131 | Popular Tags |