KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openide > src > FieldElement


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.openide.src;
21
22 import java.io.*;
23 import java.lang.reflect.Modifier JavaDoc;
24
25 import org.openide.util.NbBundle;
26
27 /** Describes a field (variable) in a class.
28 *
29 * @author Petr Hamernik, Jaroslav Tulach
30 */

31 public final class FieldElement extends MemberElement {
32     /** Format for the code generator */
33     private static final ElementFormat FIELD_FORMAT =
34         new ElementFormat("{m,,\" \"}{t} {n}"); // NOI18N
35

36     static final long serialVersionUID =2195820119328567201L;
37
38     /** Create a new field element represented in memory.
39     */

40     public FieldElement() {
41         this(new Memory(), null);
42     }
43
44     /** Create a new field element.
45     * @param impl the pluggable implementation
46     * @param declaringClass declaring class of this field, or <code>null</code>
47     */

48     public FieldElement(FieldElement.Impl impl, ClassElement declaringClass) {
49         super(impl, declaringClass);
50     }
51
52     /** Clone the field element.
53     * @return a new element that has the same values as the original
54     * but is represented in memory
55     */

56     public Object JavaDoc clone () {
57         return new FieldElement (new Memory (this), null);
58     }
59
60     final FieldElement.Impl getFieldImpl() {
61         return (FieldElement.Impl) impl;
62     }
63
64     /** Get the value type of the field.
65     * @return the type
66     */

67     public Type getType() {
68         return getFieldImpl().getType();
69     }
70
71     /** Set the value type of the field.
72     * @param type the type
73     * @throws SourceException if impossible
74     */

75     public void setType(Type type) throws SourceException {
76         // sanity check:
77
if (type == null) {
78         throwSourceException(NbBundle.getMessage(FieldElement.class, "ERR_NullType"));
79         } else if (type.equals(Type.VOID)) {
80             throwSourceException(NbBundle.getMessage(FieldElement.class, "ERR_FieldTypeVoid"));
81         }
82         getFieldImpl().setType(type);
83     }
84
85     /** Get the initial value of the field.
86     * @return the initial value (as source text), or an empty string if uninitialized
87     */

88     public String JavaDoc getInitValue() {
89         return getFieldImpl().getInitValue();
90     }
91
92     /** Set the initial value of the field.
93     * @param value the initial value (as source text), or an empty string if uninitialized
94     * @throws SourceException if impossible
95     */

96     public void setInitValue(String JavaDoc value) throws SourceException {
97         getFieldImpl().setInitValue(value);
98     }
99
100     /* Get the possible modifiers for the field.
101      * @return the mask of possible modifiers for this element. */

102     public int getModifiersMask() {
103         if (isDeclaredInInterface()) {
104             return Modifier.PUBLIC | Modifier.STATIC | Modifier.FINAL;
105         }
106         else {
107             return Modifier.PUBLIC | Modifier.PROTECTED | Modifier.PRIVATE |
108                    Modifier.STATIC | Modifier.FINAL | Modifier.TRANSIENT |
109                    Modifier.VOLATILE;
110         }
111     }
112
113     /** Set the name of this member.
114     * @param name the name
115     * @throws SourceException if impossible
116     */

117     public final void setName(Identifier name) throws SourceException {
118         ClassElement c = getDeclaringClass();
119         if (c != null) {
120             FieldElement f = c.getField(name);
121             if ((f != null) && (f != this)) {
122         String JavaDoc msg = NbBundle.getMessage(ElementFormat.class,
123             "FMT_EXC_RenameField", c.getName().getName(), name ); // NOI18N
124
throwSourceException(msg);
125             }
126         }
127         super.setName(name);
128     }
129
130     /** Get the JavaDoc for the field.
131     * @return the JavaDoc
132     */

133     public JavaDoc.Field getJavaDoc () {
134         return getFieldImpl ().getJavaDoc ();
135     }
136
137     /* Prints the element into the element printer.
138     * @param printer The element printer where to print to
139     * @exception ElementPrinterInterruptException if printer cancel the printing
140     */

141     public void print(ElementPrinter printer) throws ElementPrinterInterruptException {
142         printer.markField(this, printer.ELEMENT_BEGIN);
143
144         JavaDoc doc = getJavaDoc();
145         if ((doc != null) && !doc.isEmpty()) {
146             printer.markField(this, printer.JAVADOC_BEGIN); // JAVADOC begin
147
printJavaDoc(doc, printer);
148             printer.markField(this, printer.JAVADOC_END); // JAVADOC end
149
printer.println(""); // NOI18N
150
}
151
152         printer.markField(this, printer.HEADER_BEGIN);
153         printer.print(FIELD_FORMAT.format(this));
154         printer.markField(this, printer.HEADER_END);
155
156         String JavaDoc init = getInitValue();
157         if (init.length() > 0) {
158             printer.print(" = "); // NOI18N
159
printer.markField(this, printer.BODY_BEGIN);
160             printer.print(init);
161             printer.markField(this, printer.BODY_END);
162         }
163
164         printer.print(";"); // NOI18N
165

166         printer.markField(this, printer.ELEMENT_END);
167     }
168
169     /** Implementation of a field element.
170     * @see FieldElement
171     */

172     public interface Impl extends MemberElement.Impl {
173         /** @deprecated Only public by accident. */
174         /* public static final */ long serialVersionUID = -1612065404733395830L;
175         /** Get the value type of the field.
176          * @return the type
177          */

178         public Type getType();
179
180         /** Set the value type of the field.
181          * @param type the type
182          * @throws SourceException if impossible
183          */

184         public void setType(Type type) throws SourceException;
185
186         /** Get the initial value of the field.
187          * @return the initial value (as source text), or an empty string if uninitialized
188          */

189         public String JavaDoc getInitValue();
190
191         /** Set the initial value of the field.
192          * @param value the initial value (as source text), or an empty string if uninitialized
193          * @throws SourceException if impossible
194          */

195         public void setInitValue(String JavaDoc value) throws SourceException;
196
197         /** Get the JavaDoc for the field.
198          * @return the JavaDoc
199          */

200         public JavaDoc.Field getJavaDoc ();
201     }
202
203     static class Memory extends MemberElement.Memory implements Impl {
204         /** Type of exception */
205         private Type type;
206
207         /** Init value of variable */
208         private String JavaDoc initValue;
209
210         /** java doc */
211         private JavaDoc.Field javaDoc;
212
213         static final long serialVersionUID =1407258001185361107L;
214         Memory() {
215             type = Type.VOID;
216             initValue = ""; // NOI18N
217
javaDoc = JavaDocSupport.createFieldJavaDoc( null );
218         }
219
220         /** Copy constructor.
221         * @param field the object to read values from
222         * @param clazz declaring class to use
223         */

224         Memory (FieldElement field) {
225             super (field);
226             type = field.getType ();
227             initValue = field.getInitValue ();
228             javaDoc = field.getJavaDoc().isEmpty() ?
229                       JavaDocSupport.createFieldJavaDoc( null ) :
230                       JavaDocSupport.createFieldJavaDoc( field.getJavaDoc().getRawText() );
231         }
232
233         /** Type of the variable.
234         * @return the type
235         */

236         public Type getType() {
237             return type;
238         }
239
240         /** Setter for type of the variable.
241         * @param type the variable type
242         */

243         public void setType(Type type) {
244             Type old = this.type;
245             this.type = type;
246             firePropertyChange (PROP_TYPE, old, type);
247         }
248
249         /** Getter for the initial value.
250         * @return initial value for the variable or empty string if it is not initialized
251         */

252         public String JavaDoc getInitValue() {
253             return initValue;
254         }
255
256         /** Setter for the initial value.
257         * @param value initial value for the variable
258         */

259         public void setInitValue(String JavaDoc value) {
260             String JavaDoc old = initValue;
261             initValue = value;
262             firePropertyChange (PROP_INIT_VALUE, old, value);
263         }
264
265         /** @return java doc for the field
266         */

267         public JavaDoc.Field getJavaDoc () {
268             return javaDoc;
269         }
270
271         public Object JavaDoc readResolve() {
272             return new FieldElement(this, null);
273         }
274     }
275 }
276
Popular Tags