KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > java > bridge > FieldElementImpl


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
20 package org.netbeans.modules.java.bridge;
21
22 import java.beans.*;
23 import java.util.*;
24 import java.lang.ref.WeakReference JavaDoc;
25
26 import org.openide.nodes.Node;
27 import org.openide.src.*;
28
29 import javax.jmi.reflect.InvalidObjectException;
30 import javax.jmi.reflect.RefBaseObject;
31
32 import org.netbeans.api.mdr.events.*;
33 import org.netbeans.jmi.javamodel.Field;
34 import org.netbeans.jmi.javamodel.IsOfType;
35 import org.netbeans.jmi.javamodel.TypeReference;
36 import org.netbeans.jmi.javamodel.ArrayReference;
37 import org.netbeans.jmi.javamodel.EnumConstant;
38 import org.netbeans.jmi.javamodel.PrimitiveType;
39
40 final class FieldElementImpl extends MemberElementImpl implements FieldElement.Impl {
41
42     private static final long serialVersionUID = 6799964214013985830L;
43     
44     private ElementImpl.ElementListener fieldListener;
45         
46     FieldElementImpl(DefaultLangModel model, Field fieldElement) {
47         super(model, fieldElement);
48         javadoc = new JavaDocImpl.Field(null, this);
49     }
50             
51     public void connectListener () {
52         FieldListener fieldListener = new FieldListener (this);
53         fieldListener.connect ();
54     }
55     
56     protected void createFromModel(Element el) throws SourceException {
57         super.createFromModel(el);
58         FieldElement f = (FieldElement)el;
59         setType(f.getType());
60         setInitValue(f.getInitValue());
61         setJavaDocText(f.getJavaDoc().getRawText(), true);
62     }
63
64     /** Type of the variable.
65     * @return the type
66     */

67     public Type getType() {
68         repository.beginTrans(false);
69         try {
70             if (javaElement.isValid()) {
71                 setClassPath();
72                 if (javaElement instanceof EnumConstant) {
73                     return descrToType(((Field) javaElement).getType());
74                 }
75                 javaElement.refImmediateComposite(); // ensure that javaElement is valid (getTypeName does not throw InvalidObjectException, see issue #50364)
76
org.netbeans.jmi.javamodel.Type t = ((Field)javaElement).getType();
77                 if (t instanceof PrimitiveType) {
78                     return descrToType(t);
79                 }
80                 return typeReferenceToType (((Field) javaElement).getTypeName ());
81             } else {
82                 if (fieldListener != null)
83                     return ((FieldListener)fieldListener).type;
84                 else
85                     return Type.VOID;
86             }
87         } finally {
88             repository.endTrans(false);
89         }
90     }
91     
92     public JavaDoc.Field getJavaDoc() {
93         updateJavadoc();
94         return (JavaDoc.Field)javadoc;
95     }
96     
97     public Object JavaDoc readResolve() {
98         return null;
99     }
100     
101     /** Setter for type of the variable.
102     * @param type the variable type
103     */

104     public void setType(Type type) throws SourceException {
105         checkWritable(true);
106         checkDocument();
107         boolean failed = true;
108         repository.beginTrans (true);
109         try {
110             if (javaElement.isValid()) {
111                 setClassPath();
112                 Type oldType = getType ();
113                 PropertyChangeEvent evt;
114                 type = resolveType(type);
115                 if (compareSourceTypes(oldType, type)) {
116                     failed = false;
117                     return;
118                 }
119                 evt = new PropertyChangeEvent(getElement(), PROP_TYPE, oldType, type);
120                 checkVetoablePropertyChange(evt);
121                 if (type.isPrimitive()) {
122                     ((Field) javaElement).setType(typeToDescr(type));
123                 } else {
124                     ((Field) javaElement).setTypeName(typeToTypeReference(type));
125                 }
126                 failed = false;
127             } else {
128                 failed = false;
129                 throwIsInvalid ();
130             }
131         } finally {
132             repository.endTrans (failed);
133         }
134     }
135
136     /** Getter for the initial value.
137     * @return initial value for the variable or empty string if it is not initialized
138     */

139     public String JavaDoc getInitValue() {
140         repository.beginTrans(false);
141         try {
142             if (javaElement.isValid()) {
143                 setClassPath();
144                 return ((Field)javaElement).getInitialValueText();
145             } else {
146                 return null;
147             }
148         } finally {
149             repository.endTrans(false);
150         }
151     }
152
153     /** Setter for the initial value.
154     * @param value initial value for the variable
155     */

156     public void setInitValue(String JavaDoc newInitValue) throws SourceException {
157         checkWritable(false);
158         checkDocument();
159         String JavaDoc oldInitValue=getInitValue();
160         boolean failed = true;
161         repository.beginTrans (true);
162         try {
163             if (javaElement.isValid()) {
164                 PropertyChangeEvent evt;
165                 setClassPath();
166                 if ((newInitValue == oldInitValue) ||
167                     ((newInitValue != null) && newInitValue.equals(oldInitValue))) {
168                     return;
169                 }
170                 evt = new PropertyChangeEvent(getElement(), PROP_INIT_VALUE, oldInitValue, newInitValue);
171                 // no constraings on the Initializer... only check vetoable listeners.
172
checkVetoablePropertyChange(evt);
173
174                 ((Field)javaElement).setInitialValueText(newInitValue);
175                 failed = false;
176             } else {
177                 failed = false;
178                 throwIsInvalid ();
179             }
180         } finally {
181             repository.endTrans (failed);
182         }
183     }
184
185     public void fireTypeChange (Type oldType, Type newType) {
186         oldType = resolveType (oldType);
187         newType = resolveType (newType);
188         if (compareSourceTypes(oldType, newType))
189             return;
190         
191         PropertyChangeEvent evt = new PropertyChangeEvent(getElement(), PROP_TYPE, oldType, newType);
192         fireOwnPropertyChange(evt);
193         
194         FieldElement oldField = (FieldElement) cloneSelf ();
195         try {
196             FieldElement.Impl impl = (FieldElement.Impl) oldField.getCookie (Element.Impl.class);
197             impl.setType (oldType);
198         } catch (SourceException e) {
199             e.printStackTrace ();
200         }
201         notifyConnectionChange (oldField);
202     }
203
204     public void fireInitValueChange (String JavaDoc oldVal, String JavaDoc newVal) {
205         PropertyChangeEvent evt = new PropertyChangeEvent(getElement(), PROP_INIT_VALUE, oldVal, newVal);
206         fireOwnPropertyChange(evt);
207     }
208     
209     // Utility methods.
210
//////////////////////////////////////////////////////////////////////////////////
211

212     protected Element createWrapperElement() {
213         return null;
214     }
215     
216     public String JavaDoc toString() {
217         return "FieldElementImpl[" + getName().getSourceName() + "]"; // NOI18N
218
}
219     
220     protected Element cloneSelf() {
221         return (Element)((FieldElement)getElement()).clone();
222     }
223     
224     // ..........................................................................
225

226     static class FieldListener extends MemberElementImpl.MemberElementListener implements MDRChangeListener {
227
228         // [PENDING] this code duplicates MethodListener
229

230         private Type type;
231         
232         FieldListener (FieldElementImpl impl) {
233             super (impl);
234         }
235         
236         public void connect () {
237             if (REGISTER_LISTENER) {
238                 super.connect ();
239                 TypeReference typeRef = ((Field) javaElement).getTypeName();
240                 if (typeRef != null) {
241                     type = ((FieldElementImpl) getImpl ()).typeReferenceToType (typeRef);
242                 } else {
243                     type = null;
244                 }
245             }
246         }
247         
248         public void doChange(MDRChangeEvent event) {
249             super.doChange (event);
250
251             if (event instanceof AttributeEvent) {
252                 AttributeEvent attrEvent = (AttributeEvent) event;
253                 String JavaDoc attrName = attrEvent.getAttributeName();
254                 Object JavaDoc source = event.getSource();
255                 if (attrName.equals("typeName")) { // NOI18N
256
Type oldType = type;
257                     TypeReference typeRef = (TypeReference) attrEvent.getNewElement();
258                     if (typeRef != null) {
259                         type = ((FieldElementImpl) getImpl()).typeReferenceToType(typeRef);
260                     } else {
261                         type = null;
262                     }
263                     ((FieldElementImpl) impl).fireTypeChange (oldType, type);
264                 } else if (attrName.equals("initialValueText")) { // NOI18N
265
((FieldElementImpl) impl).fireInitValueChange ((String JavaDoc)attrEvent.getOldElement(), (String JavaDoc)attrEvent.getNewElement());
266                 }
267             }
268         }
269     
270     }
271     
272 }
273
Popular Tags