1 19 package org.netbeans.modules.javacore.jmiimpl.javamodel; 20 21 import org.netbeans.jmi.javamodel.ArrayAccess; 22 import org.netbeans.jmi.javamodel.Expression; 23 import org.netbeans.jmi.javamodel.PrimaryExpression; 24 import org.netbeans.lib.java.parser.ASTree; 25 import org.netbeans.mdr.storagemodel.StorableObject; 26 import org.netbeans.modules.javacore.parser.ASTProvider; 27 28 import java.util.*; 29 import org.netbeans.jmi.javamodel.Element; 30 import org.netbeans.jmi.javamodel.JavaModelPackage; 31 32 36 public abstract class ArrayAccessImpl extends ExpressionImpl implements ArrayAccess { 37 private PrimaryExpression array = null; 38 private Expression index = null; 39 40 41 public ArrayAccessImpl(StorableObject o) { 42 super(o); 43 } 44 45 public void setArray(PrimaryExpression expression) { 46 objectChanged(CHANGED_ARRAY); 47 changeChild(getArray(), expression); 48 this.array = expression; 49 } 50 51 public PrimaryExpression getArray() { 52 if (!childrenInited) { 53 initChildren(); 54 } 55 return array; 56 } 57 58 public void setIndex(Expression expression) { 59 objectChanged(CHANGED_INDEX); 60 changeChild(getIndex(), expression); 61 this.index = expression; 62 } 63 64 public Expression getIndex() { 65 if (!childrenInited) { 66 initChildren(); 67 } 68 return index; 69 } 70 71 public List getChildren() { 72 List list = new ArrayList(); 73 addIfNotNull(list, getArray()); 74 addIfNotNull(list, getIndex()); 75 return list; 76 } 77 78 protected void initChildren() { 79 childrenInited = false; 80 ASTree tree = getASTree(); 81 if (tree != null) { 82 ASTree[] parts = tree.getSubTrees(); 83 array = (PrimaryExpression) initOrCreate(array, parts[0]); 84 index = (Expression) initOrCreate(index, parts[1]); 85 } 86 childrenInited = true; 87 } 88 89 public String getSourceText() { 90 String origElem; 91 if ((origElem = checkChange()) != null) 92 return origElem; 93 StringBuffer buf = new StringBuffer (); 94 StatementImpl array = (StatementImpl) getArray(); 95 StatementImpl index = (StatementImpl) getIndex(); 96 buf.append(array.getSourceText()); 97 formatElementPart(ARRAY_OPEN_BRACKET, buf); 98 buf.append(index.getSourceText()); 99 formatElementPart(ARRAY_CLOSE_BRACKET, buf); 100 return buf.toString(); 101 } 102 103 public void getDiff(List diff) { 104 ASTProvider parser = getParser(); 105 ASTree[] children = getASTree().getSubTrees(); 106 getChildDiff(diff, parser, children[0], (StatementImpl) getArray(), CHANGED_ARRAY); 107 getChildDiff(diff, parser, children[1], (StatementImpl) getIndex(), CHANGED_INDEX); 108 } 109 110 void setData(PrimaryExpression array, Expression index) { 111 changeChild(null, array); 112 this.array = array; 113 changeChild(null, index); 114 this.index = index; 115 } 116 117 protected void _delete() { 118 if (childrenInited) { 120 deleteChild(array); 121 deleteChild(index); 122 } 123 super._delete(); 127 } 128 129 public void replaceChild(Element oldElement,Element newElement) { 130 if (childrenInited) { 131 if (oldElement.equals(array)) { 132 setArray((PrimaryExpression)newElement); 133 } else if (oldElement.equals(index)) { 134 setIndex((Expression)newElement); 135 } 136 } 137 } 138 139 public Element duplicate(JavaModelPackage targetExtent) { 140 return targetExtent.getArrayAccess().createArrayAccess( 141 (PrimaryExpression) duplicateElement(getArray(), targetExtent), 142 (Expression) duplicateElement(getIndex(), targetExtent) 143 ); 144 } 145 } 146 | Popular Tags |