1 19 20 package org.netbeans.modules.javacore.jmiimpl.javamodel; 21 22 import java.util.ArrayList ; 23 import java.util.List ; 24 import org.netbeans.jmi.javamodel.Element; 25 import org.netbeans.jmi.javamodel.Expression; 26 import org.netbeans.jmi.javamodel.ForEachStatement; 27 import org.netbeans.jmi.javamodel.JavaModelPackage; 28 import org.netbeans.jmi.javamodel.Parameter; 29 import org.netbeans.jmi.javamodel.Statement; 30 import org.netbeans.lib.java.parser.*; 31 import org.netbeans.mdr.storagemodel.StorableObject; 32 import org.netbeans.modules.javacore.parser.ASTProvider; 33 import org.netbeans.modules.javacore.parser.ParameterInfo; 34 35 49 public abstract class ForEachStatementImpl extends StatementImpl implements ForEachStatement { 50 51 private Statement body = null; 53 private Parameter parameter = null; 55 private Expression iterable = null; 58 59 64 public ForEachStatementImpl(StorableObject o) { 65 super(o); 66 } 67 68 73 public Statement getBody() { 74 if (!childrenInited) { 75 initChildren(); 76 } 77 return body; 78 } 79 80 86 public void setBody(Statement body) { 87 objectChanged(CHANGED_BODY); 88 changeChild(getBody(), body); 89 this.body = body; 90 } 91 92 98 public Parameter getParameter() { 99 if (!childrenInited) { 100 initChildren(); 101 } 102 return parameter; 103 } 104 105 111 public void setParameter(Parameter newValue) { 112 objectChanged(CHANGED_PARAMETER); 113 changeChild(getParameter(), parameter); 114 this.parameter = parameter; 115 } 116 117 123 public Expression getIterable() { 124 if (!childrenInited) { 125 initChildren(); 126 } 127 return iterable; 128 } 129 130 136 public void setIterable(Expression newValue) { 137 objectChanged(CHANGED_ITERABLE); 138 changeChild(getIterable(), iterable); 139 this.iterable = iterable; 140 } 141 142 public List getChildren() { 143 List list = new ArrayList (3); 144 list.add(getParameter()); 145 list.add(getIterable()); 146 list.add(getBody()); 147 return list; 148 } 149 150 String getRawText() { 152 StatementImpl body = (StatementImpl) getBody(); 155 ParameterImpl parameter = (ParameterImpl) getParameter(); 156 ExpressionImpl iterable = (ExpressionImpl) getIterable(); 157 158 StringBuffer buf = new StringBuffer (); 159 formatElementPart(FOR_KEYWORD, buf, isNew()); 160 formatElementPart(STMT_OPEN_BRACKET, buf); 161 buf.append(parameter.getSourceText()); 162 formatElementPart(COND_EXPR_COLON, buf); 163 buf.append(iterable.getSourceText()); 164 formatElementPart(STMT_CLOSE_BRACKET, buf); 165 buf.append(body.getSourceText()); 166 167 return buf.toString(); 168 } 169 170 public void getDiff(List diff) { 172 ASTProvider parser = getParser(); 173 ASTree tree = getASTree(); 174 ASTree[] children = tree.getSubTrees(); 175 176 getChildDiff(diff, parser, children[0], (MetadataElement) getParameter(), CHANGED_PARAMETER); 177 getChildDiff(diff, parser, children[1], (MetadataElement) getIterable(), CHANGED_BODY); 178 getChildDiff(diff, parser, children[2], (MetadataElement) getBody(), CHANGED_BODY); 179 } 180 181 protected void _delete() { 182 if (childrenInited) { 184 deleteChild(body); 185 deleteChild(parameter); 186 deleteChild(iterable); 187 } 188 super._delete(); 192 } 193 194 197 protected void initChildren() { 198 childrenInited = false; 199 ASTree tree = getASTree(); 200 if (tree != null) { 201 ASTree[] parts = tree.getSubTrees(); 202 ASTree parameterAST = parts[0]; 204 ParameterInfo paramInfo=(ParameterInfo)getParser().getSemanticInfo(parameterAST, this); 205 if (parameter == null) { 206 parameter = (Parameter)createElement(paramInfo); 207 changeChild(null, parameter); 208 } else { 209 ((SemiPersistentElement)parameter).updatePersistent(paramInfo); 210 ((SemiPersistentElement)parameter).setElementInfo(paramInfo); 211 } 212 iterable = (Expression) initOrCreate(iterable, parts[1]); 214 body = (Statement) initOrCreate(body, parts[2]); 216 } 217 childrenInited = true; 218 } 219 220 void setData(Statement body, Parameter parameter, Expression iterable) { 221 changeChild(null, body); 223 this.body = body; 224 changeChild(null, parameter); 226 this.parameter = parameter; 227 changeChild(null, iterable); 229 this.iterable = iterable; 230 } 231 232 public Element duplicate(JavaModelPackage targetExtent) { 233 return targetExtent.getForEachStatement().createForEachStatement( 234 (Statement) duplicateElement(getBody(), targetExtent), 235 (Parameter) duplicateElement(getParameter(), targetExtent), 236 (Expression) duplicateElement(getIterable(), targetExtent) 237 ); 238 } 239 240 } 241 | Popular Tags |