KickJava   Java API By Example, From Geeks To Geeks.

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


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 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 /**
33  *
34  * @author Martin Matula
35  */

36 public abstract class ArrayAccessImpl extends ExpressionImpl implements ArrayAccess {
37     private PrimaryExpression array = null;
38     private Expression index = null;
39     
40     /** Creates a new instance of ArrayAccessImpl */
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 JavaDoc getSourceText() {
90         String JavaDoc origElem;
91         if ((origElem = checkChange()) != null)
92             return origElem;
93         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
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         // --- delete components -------------------------------------------
119
if (childrenInited) {
120             deleteChild(array);
121             deleteChild(index);
122         }
123         // --- delete links -----------------------------------------------
124
// no links to delete
125
// --- call super ---------------------------------------
126
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