KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > javacore > jmiimpl > javamodel > AssignmentImpl


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19 package org.netbeans.modules.javacore.jmiimpl.javamodel;
20
21 import java.util.ArrayList JavaDoc;
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 JavaDoc;
27
28 /**
29  *
30  * @author Martin Matula
31  */

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     /** Creates a new instance of AssignmentImpl */
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 JavaDoc getChildren() {
82         List JavaDoc list = new ArrayList JavaDoc(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 JavaDoc getRawText() {
100         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
101         StatementImpl leftSide = (StatementImpl) getLeftSide();
102         StatementImpl rightSide = (StatementImpl) getRightSide();
103         Operator operator = getOperator();
104         buf.append(leftSide.getSourceText());
105         buf.append(" "); // NOI18N
106
buf.append(operatorToString(operator));
107         buf.append(" "); // NOI18N
108
buf.append(rightSide.getSourceText());
109         return buf.toString();
110     }
111     
112     public void getDiff(List JavaDoc 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 JavaDoc operatorToString (Operator op) {
125         if (op == OperatorEnum.PLUSASSIGN) return "+="; // NOI18N
126
if (op == OperatorEnum.MINUSASSIGN) return "-="; // NOI18N
127
if (op == OperatorEnum.PLUSASSIGN) return "*="; // NOI18N
128
if (op == OperatorEnum.DIVASSIGN) return "/="; // NOI18N
129
if (op == OperatorEnum.ANDASSIGN) return "&="; // NOI18N
130
if (op == OperatorEnum.ORASSIGN) return "|="; // NOI18N
131
if (op == OperatorEnum.XORASSIGN) return "^="; // ?? // NOI18N
132
if (op == OperatorEnum.MODASSIGN) return "%="; // NOI18N
133
if (op == OperatorEnum.PLUSASSIGN) return "+="; // NOI18N
134
if (op == OperatorEnum.LSHIFTASSIGN) return "<<="; // NOI18N
135
if (op == OperatorEnum.RSHIFTASSIGN) return ">>="; // NOI18N
136
if (op == OperatorEnum.URSHIFTASSIGN) return ">>>="; // NOI18N
137
if (op == OperatorEnum.ASSIGN) return "="; // NOI18N
138

139         return "???"; // [PENDING] // NOI18N
140
}
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         // --- delete components -------------------------------------------
152
if (childrenInited) {
153             deleteChild(leftSide);
154             deleteChild(rightSide);
155         }
156         // --- delete links -----------------------------------------------
157
// no links to delete
158
// --- call super ---------------------------------------
159
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