KickJava   Java API By Example, From Geeks To Geeks.

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


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.lang.reflect.Modifier JavaDoc;
22 import org.netbeans.jmi.javamodel.*;
23 import org.netbeans.lib.java.parser.ASTree;
24 import org.netbeans.mdr.handlers.AttrListWrapper;
25 import org.netbeans.mdr.persistence.StorageException;
26 import org.netbeans.mdr.storagemodel.StorableObject;
27 import org.netbeans.modules.javacore.parser.*;
28 import org.openide.util.Utilities;
29 import org.openide.ErrorManager;
30 import java.util.*;
31
32
33 /**
34  *
35  * @author Pavel Flaska
36  * @author Martin Matula
37  */

38 public abstract class FieldGroupImpl extends FeatureImpl implements FieldGroup {
39     public static final String JavaDoc FIELDS_ATTR = "fields"; // NOI18N
40
private static final ElementInfo DEFAULT_INFO = new FieldGroupInfo(null, FieldGroupInfo.FIELDGROUP_TYPE, 0, null, null, null);
41
42     private TypeReference typeName;
43     private LightAttrList fields;
44
45     private boolean elementsInited = false;
46
47     /** Creates a new instance of FieldGroupImpl */
48     protected FieldGroupImpl(StorableObject s) {
49         super(s);
50     }
51
52     public void setType(Type newValue) {
53         TypeRef tr = typeToTypeRef(newValue);
54         _setTypeName((TypeReference) typeRefToTypeReference(tr, 0), tr);
55     }
56
57     public Type getType() {
58         checkUpToDate();
59         return resolveType(getTypeRef());
60     }
61
62     public TypeReference getTypeName() {
63         checkUpToDate();
64         if (!elementsInited) {
65             initASTElements();
66         }
67         return typeName;
68     }
69
70     private void _setTypeName(TypeReference typeName, TypeRef typeRef) {
71         if (!disableChanges) {
72             objectChanged(CHANGED_TYPE);
73             changeChild(getTypeName(), typeName);
74             this.typeName = typeName;
75         }
76         setTypeRef(typeRef);
77     }
78
79     public void setTypeName(TypeReference typeName) {
80         _setTypeName(typeName, typeReferenceToTypeRef(typeName, 0));
81     }
82
83     protected void resetChildren() {
84         super.resetChildren();
85         if (childrenInited) {
86             resetASTElements();
87             initChildren();
88         }
89     }
90
91     public int getModifiers() {
92         ClassDefinition cd = getDeclaringClass();
93         int mods = super.getModifiers();
94         if (cd instanceof JavaClass && ((JavaClass)cd).isInterface()) {
95             return mods | Modifier.STATIC | Modifier.FINAL;
96         } else {
97             return mods;
98         }
99     }
100     
101     protected void initASTElements() {
102         elementsInited = false;
103         if (!childrenInited) {
104             initChildren();
105         }
106         ElementInfo info = getElementInfo();
107         ASTree tree = info.getTypeAST(this);
108         typeName = (TypeReference) initOrCreate(typeName, tree);
109         elementsInited = true;
110     }
111     
112     protected void matchPersistent(ElementInfo info) {
113         super.matchPersistent(info);
114         FieldGroupInfo fgInfo = (FieldGroupInfo)info;
115         
116         if (!isPersisted()) {
117             setPersisted(true);
118             persist();
119             setTypeRef(fgInfo.type);
120             persistChildren(getPersistentList("annotations", super_getAnnotations()), ((FeatureInfo) info).annotations);
121             persistChildren(getPersistentList(FIELDS_ATTR, super_getFields()), fgInfo.fields);
122         } else {
123             if (!Utilities.compareObjects(fgInfo.type, getTypeRef())) {
124                 setTypeRef(fgInfo.type);
125             }
126             processMembers(getAnnotations(), fgInfo.annotations);
127             processMembers(getFields(), fgInfo.fields);
128         }
129     }
130     
131     protected void matchElementInfo(ElementInfo newInfo) {
132         super.matchElementInfo(newInfo);
133         resetASTElements();
134     }
135     
136     public void replaceChild(Element oldElement, Element newElement) {
137         if (isPersisted()) {
138             if (replaceObject(getFields(), oldElement, newElement)) return;
139         }
140         if (elementsInited && oldElement.equals(typeName)) {
141             setTypeName((MultipartId) newElement);
142             return;
143         }
144         super.replaceChild(oldElement, newElement);
145     }
146     
147     protected void resetASTElements() {
148         if (elementsInited) {
149             if (typeName != null) {
150                 TypeReference temp = typeName;
151                 typeName = null;
152                 changeChild(temp, null);
153                 temp.refDelete();
154             }
155             elementsInited = false;
156         }
157     }
158
159     protected List getInitedChildren() {
160         List list = super.getInitedChildren();
161         if (childrenInited) {
162             list.addAll(fields);
163         }
164         if (elementsInited) {
165             addIfNotNull(list, typeName);
166         }
167         return list;
168     }
169
170     public List getFields() {
171         checkUpToDate();
172         if (fields == null) {
173             fields = createChildrenList(FIELDS_ATTR, (AttrListWrapper) super_getFields(), null, CHANGED_FEATURES);
174         }
175         return fields;
176     }
177     
178     List getPersistentFields() {
179         AttrListWrapper result = (AttrListWrapper) super_getFields();
180         result.setAttrName("fields"); // NOI18N
181
return result;
182     }
183
184     void reinitFields() {
185         if (fields != null) {
186             fields.setInnerList(super_getFields());
187         }
188     }
189     
190     public List getChildren() {
191         List result = super.getChildren();
192         addIfNotNull(result, getTypeName());
193         result.addAll(getFields());
194         return result;
195     }
196
197     /**
198      * Creates text representation of this object, e.g. 'public int a, b;' for
199      * the group of two fields a and b with type int and modifier public.
200      */

201     String JavaDoc getRawText() {
202         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
203         generateNewJavaDoc(buf);
204         generateNewModifiers(buf);
205         if (getTypeName() != null) {
206             buf.append(((MetadataElement) getTypeName()).getSourceText());
207         } else {
208             buf.append(getType().getName());
209         }
210         buf.append(' '); // NOI18N
211

212         for (Iterator fieldIt = getFields().iterator(); fieldIt.hasNext(); ) {
213             FieldImpl f = (FieldImpl) fieldIt.next();
214             buf.append(f.getSourceText());
215             if (fieldIt.hasNext())
216                 formatElementPart(COMMA, buf);
217             else
218                 break;
219         }
220         buf.append(';'); // NOI18N
221
return buf.toString();
222     }
223
224     public void getDiff(List diffList) {
225         ASTProvider parser = getParser();
226         ASTree tree = getASTree();
227         ASTree[] children = tree.getSubTrees();
228
229         // javadoc print
230
replaceJavaDoc(diffList);
231         // modifier print
232
if (isChanged(CHANGED_MODIFIERS) || isChanged(CHANGED_ANNOTATION)) {
233             diffModifiers(diffList, children[TYPE], parser);
234         } else if (children[0] != null) {
235             FieldGroupInfo astInfo=(FieldGroupInfo)getElementInfo();
236
237             getCollectionDiff(diffList, parser, CHANGED_ANNOTATION, astInfo.annotations, getAnnotations(), parser.getToken(children[0].getLastToken()).getEndOffset(), " "); // NOI18N
238
}
239         // type print
240
getChildDiff(diffList, parser, children[TYPE], (MetadataElement) getTypeName(), CHANGED_TYPE);
241         // name print
242

243         getCollectionDiff(diffList, parser, CHANGED_FEATURES, ((FieldGroupInfo) getElementInfo()).fields,
244                 getFields(), parser.getToken(tree.getLastToken()).getStartOffset(), formatElementPart(COMMA));
245     }
246
247     protected String JavaDoc getIndentation() {
248         return ((MetadataElement) refImmediateComposite()).getIndentation().concat(INDENTATION);
249     }
250
251     // useful constants
252
private static final int TYPE = 1;
253
254     protected ElementInfo getDefaultInfo() {
255         return DEFAULT_INFO;
256     }
257
258     private List getNakedFeatures() {
259         try {
260             return (List) ((StorableObject) _getDelegate()).getAttribute(FIELDS_ATTR); // NOI18N
261
} catch (StorageException e) {
262             throw (GeneralException) ErrorManager.getDefault().annotate(new GeneralException(e.getMessage()), e);
263         }
264     }
265
266     protected abstract List super_getFields();
267
268     public List getPersistentFeatures() {
269         AttrListWrapper list = (AttrListWrapper) super_getFields();
270         list.setAttrName(FIELDS_ATTR); // NOI18N
271
return list;
272     }
273
274     protected void initChildren() {
275         // initialization of fields requires writable lock
276
boolean fail = true;
277         _lock(true);
278         try {
279             childrenInited = false;
280             fields = createChildrenList(fields, FIELDS_ATTR, (AttrListWrapper) super_getFields(), ((FieldGroupInfo) getElementInfo()).fields, CHANGED_FEATURES);
281             childrenInited = true;
282
283             if (elementsInited) {
284                 initASTElements();
285             }
286             fail = false;
287         } finally {
288             // I do not need to set featuresInitialized to false in
289
// case fail == true, since rollback will follow, thus the
290
// reset method that already does that will be invoked
291
_unlock(fail);
292         }
293     }
294
295     void setData(TypeReference typeName, List fields) {
296         this.fields = createChildrenList(FIELDS_ATTR, (AttrListWrapper) super_getFields(), fields, CHANGED_FEATURES);
297         changeChild(null, typeName);
298         this.typeName = typeName;
299         elementsInited = true;
300         childrenInited = true;
301     }
302
303     protected void _delete() {
304         deleteChildren(FIELDS_ATTR, (AttrListWrapper) super_getFields());
305         if (elementsInited) {
306             deleteChild(typeName);
307         }
308         super._delete();
309     }
310     
311     public Element duplicate(JavaModelPackage targetExtent) {
312         return targetExtent.getFieldGroup().createFieldGroup(
313                 getName(),
314                 duplicateList(getAnnotations(), targetExtent),
315                 getModifiers(),
316                 null,
317                 (JavaDoc) duplicateElement(getJavadoc(), targetExtent),
318                 (TypeReference) duplicateElement(getTypeName(), targetExtent),
319                 duplicateList(getFields(), targetExtent)
320                );
321                 
322     }
323 }
324
Popular Tags