1 19 package org.netbeans.modules.javacore.jmiimpl.javamodel; 20 21 import java.util.ArrayList ; 22 import org.netbeans.jmi.javamodel.Element; 23 import org.netbeans.jmi.javamodel.Expression; 24 import org.netbeans.jmi.javamodel.ForStatement; 25 import org.netbeans.jmi.javamodel.Statement; 26 import org.netbeans.lib.java.parser.ASTree; 27 import org.netbeans.lib.java.parser.ASTreeTypes; 28 import org.netbeans.mdr.storagemodel.StorableObject; 29 import org.netbeans.modules.javacore.parser.ASTProvider; 30 import java.util.Iterator ; 31 import java.util.List ; 32 import org.netbeans.jmi.javamodel.JavaModelPackage; 33 34 50 public abstract class ForStatementImpl extends ConditionImpl implements ForStatement { 51 private Statement body = null; 52 private LightAttrList steps = null; 53 private LightAttrList init = null; 54 55 56 public ForStatementImpl(StorableObject o) { 57 super(o); 58 } 59 60 public List getSteps() { 61 if (!childrenInited) { 62 initChildren(); 63 } 64 return steps; 65 } 66 67 public List getInit() { 68 if (!childrenInited) { 69 initChildren(); 70 } 71 return init; 72 } 73 74 public void setBody(Statement body) { 75 objectChanged(CHANGED_BODY); 76 changeChild(getBody(), body); 77 this.body = body; 78 } 79 80 protected void initChildren() { 81 childrenInited = false; 82 ASTree tree = getASTree(); 83 if (tree != null) { 84 ASTree[] parts = tree.getSubTrees(); 85 init = createChildrenList(init, "init", parts[0], ASTreeTypes.STATEMENT_EXPRESSION_LIST, CHANGED_INIT, false); expression = (Expression) initOrCreate(expression, parts[1]); 87 steps = createChildrenList(steps, "steps", parts[2], ASTreeTypes.STATEMENT_EXPRESSION_LIST, CHANGED_STEPS, false); body = (Statement) initOrCreate(body, parts[3]); 89 } 90 childrenInited = true; 91 } 92 93 public Statement getBody() { 94 if (!childrenInited) { 95 initChildren(); 96 } 97 return body; 98 } 99 100 String getRawText() { 101 StringBuffer buf = new StringBuffer (); 102 StatementImpl cond = (StatementImpl)getExpression(); 103 StatementImpl body = (StatementImpl) getBody(); 104 List init = getInit(); 105 List steps = getSteps(); 106 107 formatElementPart(FOR_KEYWORD, buf); 108 formatElementPart(STMT_OPEN_BRACKET, buf); 109 Iterator iter = init.iterator(); 110 while (iter.hasNext()) { 111 StatementImpl expr = (StatementImpl) iter.next(); 112 buf.append(expr.getSourceText()); 113 if (iter.hasNext()) 114 formatElementPart(MetadataElement.COMMA, buf); 115 } 116 formatElementPart(FOR_SEMICOLON, buf); 117 if (cond != null) { 118 buf.append(cond.getSourceText()); 119 } 120 formatElementPart(FOR_SEMICOLON, buf); 121 iter = getSteps().iterator(); 122 while (iter.hasNext()) { 123 StatementImpl expr = (StatementImpl) iter.next(); 124 buf.append(expr.getSourceText()); 125 if (iter.hasNext()) { 126 formatElementPart(MetadataElement.COMMA, buf); 127 } 128 } 129 formatElementPart(STMT_CLOSE_BRACKET, buf); 130 buf.append(body.getSourceText()); 131 return buf.toString(); 132 } 133 134 public void getDiff(List diff) { 135 ASTProvider parser = getParser(); 136 ASTree tree = getASTree(); 137 ASTree[] children = tree.getSubTrees(); 138 139 int endTokens[] = new int[3]; 140 for (int i = 3; i > 0; i--) { 141 if (children[i] == null) { 142 endTokens[i - 1] = endTokens[i] - 1; 143 } else { 144 endTokens[i - 1] = children[i].getFirstToken() - 1; 145 } 146 } 147 getCollectionDiff(diff, parser, CHANGED_INIT, children[0], ASTreeTypes.STATEMENT_EXPRESSION_LIST, getInit(), parser.getToken(endTokens[0]).getStartOffset(), formatElementPart(MetadataElement.COMMA)); 148 getChildDiff(diff, parser, children[1], (MetadataElement) getExpression(), CHANGED_EXPRESSION, parser.getToken(endTokens[1]).getStartOffset(), ""); 149 getCollectionDiff(diff, parser, CHANGED_STEPS, children[2], ASTreeTypes.STATEMENT_EXPRESSION_LIST, getSteps(), parser.getToken(endTokens[2]).getStartOffset(), formatElementPart(MetadataElement.COMMA)); 150 getChildDiff(diff, parser, children[3], (MetadataElement) getBody(), CHANGED_BODY); 151 } 152 153 void setData(Expression expression, List steps, Statement body, List init) { 154 changeChild(null, expression); 156 this.expression = expression; 157 this.steps = createChildrenList("steps", steps, CHANGED_STEPS); changeChild(null, body); 161 this.body = body; 162 this.init = createChildrenList("init", init, CHANGED_INIT); } 165 166 protected void _delete() { 167 if (childrenInited) { 169 deleteChildren(steps); 170 deleteChild(body); 171 deleteChildren(init); 172 } 173 super._delete(); 177 } 178 179 public void replaceChild(Element oldElement,Element newElement) { 180 if (childrenInited) { 181 if (oldElement.equals(body)) { 182 setBody((Statement)newElement); 183 } else if (replaceObject(getInit(),oldElement,newElement)) 184 ; 185 else if (replaceObject(getSteps(),oldElement,newElement)) 186 ; 187 else 188 super.replaceChild(oldElement,newElement); 189 } 190 } 191 192 public List getChildren() { 193 List list = new ArrayList (4); 194 list.addAll(getInit()); 195 list.addAll(super.getChildren()); 196 list.addAll(getSteps()); 197 addIfNotNull(list, getBody()); 198 return list; 199 } 200 201 public Element duplicate(JavaModelPackage targetExtent) { 202 return targetExtent.getForStatement().createForStatement( 203 (Expression) duplicateElement(getExpression(), targetExtent), 204 duplicateList(getSteps(), targetExtent), 205 (Statement) duplicateElement(getBody(), targetExtent), 206 duplicateList(getInit(), targetExtent) 207 ); 208 } 209 } 210 | Popular Tags |