KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.util.Iterator JavaDoc;
23 import java.util.List JavaDoc;
24 import javax.jmi.reflect.RefObject;
25 import org.netbeans.jmi.javamodel.Annotation;
26 import org.netbeans.jmi.javamodel.AnnotationType;
27 import org.netbeans.jmi.javamodel.Attribute;
28 import org.netbeans.jmi.javamodel.AttributeValue;
29 import org.netbeans.jmi.javamodel.Element;
30 import org.netbeans.modules.javacore.parser.AnnotationValueInfo;
31 import org.netbeans.jmi.javamodel.ElementReference;
32 import org.netbeans.jmi.javamodel.InitialValue;
33 import org.netbeans.jmi.javamodel.JavaModelPackage;
34 import org.netbeans.jmi.javamodel.NamedElement;
35 import org.netbeans.lib.java.parser.ASTree;
36 import org.netbeans.mdr.storagemodel.StorableObject;
37 import org.netbeans.modules.javacore.JMManager;
38 import org.netbeans.modules.javacore.parser.AnnotationInfo;
39 import org.netbeans.modules.javacore.parser.AnnotationValueInfo;
40 import org.netbeans.modules.javacore.parser.ElementInfo;
41
42
43 /**
44  *
45  * @author Pavel Flaska
46  */

47 public abstract class AttributeValueImpl extends SemiPersistentElement implements AttributeValue, ElementReference {
48     private static final ElementInfo DEFAULT_INFO = new AnnotationValueInfo(null, AnnotationValueInfo.ANNOTATIONVALUE_STRING, null, null);
49     
50     private String JavaDoc attributeValueText;
51     private InitialValue attributeValue;
52     
53     
54     /** Creates a new instance of AnnotationValueImpl */
55     public AttributeValueImpl(StorableObject o) {
56         super(o);
57     }
58
59     public InitialValue getValue() {
60         if (!childrenInited) {
61             initChildren();
62         }
63         return attributeValue;
64     }
65     
66     public void setValue(InitialValue newValue) {
67         objectChanged(CHANGED_INITIAL_VALUE);
68         changeChild(getValue(), newValue);
69         attributeValue = newValue;
70     }
71         
72     public Attribute getDefinition() {
73         String JavaDoc name=getName();
74         
75         if (name!=null) {
76             return (Attribute)getElement();
77         } else {
78             Annotation ann=(Annotation)refImmediateComposite();
79             AnnotationType annType=ann.getType();
80             
81             if (annType!=null) {
82                 int valIndex=ann.getAttributeValues().indexOf(this);
83                 Object JavaDoc[] features=annType.getContents().toArray();
84                 int attrIndex=0;
85                 
86                 for (int i=0;i<features.length;i++) {
87                     Object JavaDoc obj = features[i];
88                     if (obj instanceof Attribute) {
89                         if (valIndex==attrIndex)
90                             return (Attribute)obj;
91                         attrIndex++;
92                     }
93                 }
94             }
95         }
96         return null;
97     }
98     
99     public void setDefinition(Attribute newValue) {
100         throw new UnsupportedOperationException JavaDoc("setDefinition()"); // NOI18N
101
}
102     
103     public List JavaDoc getChildren() {
104         List JavaDoc list = new ArrayList JavaDoc(1);
105         addIfNotNull(list, getValue());
106         return list;
107     }
108     
109     ////////////////////////////////////////////////////////////////////////////
110
public String JavaDoc getSourceText() {
111         String JavaDoc origElem;
112         if ((origElem = checkChange()) != null)
113             return origElem;
114         
115         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
116         String JavaDoc name = getName();
117         if (name != null && name.length() > 0) {
118             buf.append(name);
119             buf.append(formatElementPart(FIELD_EQUALS));
120         }
121         buf.append(((MetadataElement) getValue()).getSourceText());
122         return buf.toString();
123     }
124     
125     public void getDiff(List JavaDoc diff) {
126         if (isChanged(CHANGED_NAME)) {
127             replaceNode(diff, getParser(), getASTree().getSubTrees()[0], getName(), 0, null);
128         }
129         if (isChanged(CHANGED_INITIAL_VALUE)) {
130             getChildDiff(diff, getParser(), getASTree().getSubTrees()[1], (MetadataElement) attributeValue, CHANGED_INITIAL_VALUE);
131         } else if (isChanged(CHANGED_CHILDREN)) {
132             if (attributeValue != null) {
133                 ((MetadataElement) attributeValue).getDiff(diff);
134             }
135         }
136     }
137     
138     void setData(InitialValue attributeValue, String JavaDoc attributeValueText) {
139         changeChild(null, attributeValue);
140         this.attributeValue = attributeValue;
141         this.attributeValueText = attributeValueText;
142     }
143     
144     protected void initChildren() {
145         if (childrenInited) {
146             JMManager.getTransactionMutex().addBFeatureToInitQueue(this);
147         } else {
148             initInitValue();
149         }
150     }
151     
152     public void initInitValue() {
153         childrenInited = false;
154         RefObject parent = (RefObject) refImmediateComposite();
155         while (parent != null && !(parent instanceof AnnotationImpl)) {
156             parent = (RefObject) parent.refImmediateComposite();
157         }
158         if (parent != null) {
159             AnnotationImpl annParent = (AnnotationImpl) parent;
160             AnnotationInfo initValInfo = (AnnotationInfo) annParent.getElementInfo();
161             if (initValInfo != null) {
162                 AnnotationValueInfo info = (AnnotationValueInfo) getElementInfo();
163                 ASTree tree = getASTree();
164                 if (tree != null) {
165                     initValInfo.doAttribution(annParent);
166                 }
167                 if (info.infoType == AnnotationValueInfo.ANNOTATIONVALUE_ANNOTATION) {
168                     InitialValue oldInit = attributeValue;
169                     attributeValue = (Annotation) createElement((AnnotationInfo)info.value);
170                     changeChild(oldInit,attributeValue);
171                     if (oldInit != null) {
172                         oldInit.refDelete();
173                     }
174                 } else {
175                     attributeValue = (InitialValue) initOrCreate(attributeValue, extractAttributeValue());
176                 }
177             }
178         }
179         childrenInited = true;
180     }
181     
182     private ASTree extractAttributeValue() {
183         return getASTree().getSubTrees()[1];
184     }
185     
186     public Element duplicate(JavaModelPackage targetExtent) {
187         return targetExtent.getAttributeValue().createAttributeValue(
188                 getName(), (InitialValue) duplicateElement(getValue(), targetExtent));
189     }
190
191     public NamedElement getElement() {
192         String JavaDoc name=getName();
193         
194         if (name!=null) {
195             AnnotationType annType=((Annotation)refImmediateComposite()).getType();
196         
197             if (annType!=null) {
198                 Iterator JavaDoc iter = annType.getFeatures().iterator();
199                 while (iter.hasNext()) {
200                     Object JavaDoc obj = iter.next ();
201                     if (obj instanceof Attribute) {
202                         Attribute attrib = (Attribute) obj;
203                         if (name.equals(attrib.getName()))
204                             return attrib;
205                     }
206                 }
207             }
208         }
209         return null;
210     }
211
212     protected ElementInfo getDefaultInfo() {
213         return DEFAULT_INFO;
214     }
215
216     protected void matchPersistent(ElementInfo info) {
217         super.matchPersistent(info);
218         if (!isPersisted()) {
219             setPersisted(true);
220             persist();
221             // persist elements
222
} else {
223             // match elements
224
}
225     }
226
227     protected void matchElementInfo(ElementInfo newInfo) {
228         super.matchElementInfo(newInfo);
229         resetChildren();
230     }
231
232     protected void resetChildren() {
233         if (childrenInited) {
234             InitialValue temp = attributeValue;
235             attributeValue = null;
236             changeChild(temp, null);
237             temp.refDelete();
238             childrenInited = false;
239         }
240     }
241
242     public void replaceChild(Element oldElement, Element newElement) {
243         if (childrenInited && oldElement.equals(attributeValue)) {
244             setValue((InitialValue) newElement);
245             return;
246         }
247         super.replaceChild(oldElement, newElement);
248     }
249     
250     public boolean isPersisted() {
251         return _getDelegate().getSlot1() != null;
252     }
253     
254     public void setPersisted(boolean persisted) {
255         _getDelegate().setSlot1(persisted ? "" : null);
256     }
257
258     protected void _delete() {
259         if (childrenInited) {
260             deleteChild(attributeValue);
261         }
262         super._delete();
263     }
264 }
265
Popular Tags