KickJava   Java API By Example, From Geeks To Geeks.

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


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.*;
22 import javax.jmi.reflect.RefFeatured;
23 import org.netbeans.jmi.javamodel.ArrayInitialization;
24 import org.netbeans.jmi.javamodel.AttributeValue;
25 import org.netbeans.jmi.javamodel.Element;
26 import org.netbeans.jmi.javamodel.JavaModelPackage;
27 import org.netbeans.lib.java.parser.ASTree;
28 import org.netbeans.lib.java.parser.ASTreeTypes;
29 import org.netbeans.mdr.storagemodel.StorableObject;
30 import org.netbeans.modules.javacore.parser.ASTProvider;
31 import org.netbeans.modules.javacore.parser.AnnotationInfo;
32 import org.netbeans.modules.javacore.parser.AnnotationValueInfo;
33
34 /**
35  *
36  * @author Martin Matula
37  */

38 public abstract class ArrayInitializationImpl extends TransientElement implements ArrayInitialization {
39     private LightAttrList elementValues = null;
40     
41     /** Creates a new instance of ArrayInitializationImpl */
42     public ArrayInitializationImpl(StorableObject o) {
43         super(o);
44     }
45     
46     public List getElementValues() {
47         if (!childrenInited) {
48             initChildren();
49         }
50         return elementValues;
51     }
52     
53     public List getChildren() {
54         return new ArrayList(getElementValues());
55     }
56     
57     protected void initChildren() {
58         childrenInited = false;
59         ASTree tree = getASTree();
60         if (tree != null) {
61             ASTree[] parts = tree.getSubTrees();
62             elementValues = createChildrenList(elementValues, "elementValues", parts[0], ASTreeTypes.VARIABLE_INITIALIZERS, CHANGED_ELEMENT_VALUES, false); // NOI18N
63
}
64         childrenInited = true;
65     }
66     
67     protected MetadataElement createElement(ASTree tree) {
68         if (tree == null) return null;
69         if (tree.getType() == ASTreeTypes.ANNOTATION) {
70             RefFeatured parent = refImmediateComposite();
71             if (parent instanceof AttributeValueImpl) {
72                 AttributeValueImpl attrVal = (AttributeValueImpl)parent;
73                 AnnotationValueInfo info = (AnnotationValueInfo) attrVal.getElementInfo();
74                 Object JavaDoc[] arrEls = (Object JavaDoc[])info.value;
75                 
76                 for (int i=0;i<arrEls.length;i++) {
77                     Object JavaDoc el=arrEls[i];
78                     
79                     if (el instanceof AnnotationInfo) {
80                         AnnotationInfo annInfo = (AnnotationInfo) el;
81                         
82                         Object JavaDoc infoTree = annInfo.getASTree().get();
83                         if (infoTree == null) {
84                             infoTree = annInfo.refreshASTree();
85                         }
86                         if (infoTree == tree) {
87                             return createElement(annInfo);
88                         }
89                     }
90                 }
91                 throw new IllegalArgumentException JavaDoc("Annotation tree not found; resource: " + getResource().getName()); // NOI18N
92
}
93         }
94         return super.createElement(tree);
95     }
96
97     public String JavaDoc getRawText() {
98         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
99         List values = getElementValues();
100         formatElementPart(ARRAY_OPEN_CURLY, buf);
101         boolean isAttributeValue = refImmediateComposite() instanceof AttributeValue;
102         MetadataElement composite = (MetadataElement) refImmediateComposite();
103         if (isAttributeValue) {
104             buf.append('\n');
105             buf.append(composite.getIndentation());
106         }
107         Iterator iter = values.iterator();
108         while (iter.hasNext()) {
109             MetadataElement val = (MetadataElement) iter.next();
110             buf.append(val.getSourceText());
111             if (iter.hasNext()) {
112                 if (isAttributeValue) {
113                     buf.append(",\n");
114                     buf.append(composite.getIndentation());
115                 } else {
116                     formatElementPart(COMMA, buf);
117                 }
118             }
119         }
120         if (isAttributeValue) {
121             buf.append('\n');
122             buf.append(((MetadataElement) composite.refImmediateComposite()).getIndentation());
123         }
124         formatElementPart(ARRAY_CLOSE_CURLY, buf);
125         return buf.toString();
126     }
127     
128     public void getDiff(List diff) {
129         ASTProvider parser = getParser();
130         ASTree tree = getASTree();
131         ASTree[] children = tree.getSubTrees();
132
133         getCollectionDiff(diff, parser, CHANGED_ELEMENT_VALUES, children[0], ASTreeTypes.VARIABLE_INITIALIZERS, getElementValues(), parser.getToken(tree.getLastToken()).getStartOffset(), formatElementPart(COMMA));
134     }
135     
136     void setData(List elementValues) {
137         this.elementValues = createChildrenList("elementValues", elementValues, CHANGED_ELEMENT_VALUES); // NOI18N
138
}
139     
140     protected void _delete() {
141         // --- delete components -------------------------------------------
142
if (childrenInited) {
143             deleteChildren(elementValues);
144         }
145         // --- delete links -----------------------------------------------
146
// no links to delete
147
// --- call super ---------------------------------------
148
super._delete();
149     }
150     
151     public void replaceChild(Element oldElement,Element newElement) {
152         if (childrenInited) {
153             replaceObject(getElementValues(),oldElement,newElement);
154         }
155     }
156     
157     public Element duplicate(JavaModelPackage targetExtent) {
158         return targetExtent.getArrayInitialization().createArrayInitialization(
159                 duplicateList(getElementValues(), targetExtent));
160     }
161 }
162
Popular Tags