1 19 package org.netbeans.modules.javacore.jmiimpl.javamodel; 20 21 import java.util.ArrayList ; 22 import org.netbeans.jmi.javamodel.*; 23 import org.netbeans.lib.java.parser.ASTree; 24 import org.netbeans.mdr.storagemodel.StorableObject; 25 import org.netbeans.modules.javacore.parser.ASTProvider; 26 import java.util.List ; 27 28 32 public abstract class AssignmentImpl extends ExpressionImpl implements Assignment { 33 private PrimaryExpression leftSide = null; 34 private Operator operator = null; 35 private Expression rightSide = null; 36 37 38 public AssignmentImpl(StorableObject o) { 39 super(o); 40 } 41 42 public void setLeftSide(PrimaryExpression expression) { 43 objectChanged(CHANGED_LEFT_SIDE); 44 changeChild(getLeftSide(), expression); 45 this.leftSide = expression; 46 } 47 48 public PrimaryExpression getLeftSide() { 49 if (!childrenInited) { 50 initChildren(); 51 } 52 return leftSide; 53 } 54 55 public void setRightSide(Expression expression) { 56 objectChanged(CHANGED_RIGHT_SIDE); 57 changeChild(getRightSide(), expression); 58 this.rightSide = expression; 59 } 60 61 public Expression getRightSide() { 62 if (!childrenInited) { 63 initChildren(); 64 } 65 return rightSide; 66 } 67 68 public void setOperator(Operator operator) { 69 objectChanged(CHANGED_OPERATOR); 70 this.operator = operator; 71 } 72 73 public Operator getOperator() { 74 if (isChanged(CHANGED_OPERATOR)) { 75 return operator; 76 } else { 77 return getOperator(getASTree().getSubTrees()[1].getType()); 78 } 79 } 80 81 public List getChildren() { 82 List list = new ArrayList (2); 83 addIfNotNull(list, getLeftSide()); 84 addIfNotNull(list, getRightSide()); 85 return list; 86 } 87 88 protected void initChildren() { 89 childrenInited = false; 90 ASTree tree = getASTree(); 91 if (tree != null) { 92 ASTree[] parts = tree.getSubTrees(); 93 leftSide = (PrimaryExpression) initOrCreate(leftSide, parts[0]); 94 rightSide = (Expression) initOrCreate(rightSide, parts[2]); 95 } 96 childrenInited = true; 97 } 98 99 String getRawText() { 100 StringBuffer buf = new StringBuffer (); 101 StatementImpl leftSide = (StatementImpl) getLeftSide(); 102 StatementImpl rightSide = (StatementImpl) getRightSide(); 103 Operator operator = getOperator(); 104 buf.append(leftSide.getSourceText()); 105 buf.append(" "); buf.append(operatorToString(operator)); 107 buf.append(" "); buf.append(rightSide.getSourceText()); 109 return buf.toString(); 110 } 111 112 public void getDiff(List diff) { 113 ASTProvider parser = getParser(); 114 ASTree tree = getASTree(); 115 ASTree[] children = tree.getSubTrees(); 116 117 getChildDiff(diff, parser, children[0], (MetadataElement) getLeftSide(), CHANGED_LEFT_SIDE); 118 if (isChanged(CHANGED_OPERATOR)) { 119 replaceNode(diff, parser, children[1], operatorToString(getOperator()), 0, null); 120 } 121 getChildDiff(diff, parser, children[2], (MetadataElement) getRightSide(), CHANGED_RIGHT_SIDE); 122 } 123 124 private String operatorToString (Operator op) { 125 if (op == OperatorEnum.PLUSASSIGN) return "+="; if (op == OperatorEnum.MINUSASSIGN) return "-="; if (op == OperatorEnum.PLUSASSIGN) return "*="; if (op == OperatorEnum.DIVASSIGN) return "/="; if (op == OperatorEnum.ANDASSIGN) return "&="; if (op == OperatorEnum.ORASSIGN) return "|="; if (op == OperatorEnum.XORASSIGN) return "^="; if (op == OperatorEnum.MODASSIGN) return "%="; if (op == OperatorEnum.PLUSASSIGN) return "+="; if (op == OperatorEnum.LSHIFTASSIGN) return "<<="; if (op == OperatorEnum.RSHIFTASSIGN) return ">>="; if (op == OperatorEnum.URSHIFTASSIGN) return ">>>="; if (op == OperatorEnum.ASSIGN) return "="; 139 return "???"; } 141 142 void setData(PrimaryExpression leftSide, Operator operator, Expression rightSide) { 143 changeChild(null, leftSide); 144 this.leftSide = leftSide; 145 this.operator = operator; 146 changeChild(null, rightSide); 147 this.rightSide = rightSide; 148 } 149 150 protected void _delete() { 151 if (childrenInited) { 153 deleteChild(leftSide); 154 deleteChild(rightSide); 155 } 156 super._delete(); 160 } 161 162 public void replaceChild(Element oldElement,Element newElement) { 163 if (childrenInited) { 164 if (oldElement.equals(leftSide)) { 165 setLeftSide((PrimaryExpression)newElement); 166 } 167 if (oldElement.equals(rightSide)) { 168 setRightSide((Expression)newElement); 169 } 170 } 171 } 172 173 public Element duplicate(JavaModelPackage targetExtent) { 174 return targetExtent.getAssignment().createAssignment( 175 (PrimaryExpression) duplicateElement(getLeftSide(), targetExtent), 176 getOperator(), 177 (Expression) duplicateElement(getRightSide(), targetExtent) 178 ); 179 } 180 } 181 | Popular Tags |