KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > percederberg > grammatica > code > visualbasic > VisualBasicEnumeration


1 /*
2  * VisualBasicEnumeration.java
3  *
4  * This work is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published
6  * by the Free Software Foundation; either version 2 of the License,
7  * or (at your option) any later version.
8  *
9  * This work is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
17  * USA
18  *
19  * As a special exception, the copyright holders of this library give
20  * you permission to link this library with independent modules to
21  * produce an executable, regardless of the license terms of these
22  * independent modules, and to copy and distribute the resulting
23  * executable under terms of your choice, provided that you also meet,
24  * for each linked independent module, the terms and conditions of the
25  * license of that module. An independent module is a module which is
26  * not derived from or based on this library. If you modify this
27  * library, you may extend this exception to your version of the
28  * library, but you are not obligated to do so. If you do not wish to
29  * do so, delete this exception statement from your version.
30  *
31  * Copyright (c) 2004 Adrian Moore. All rights reserved.
32  * Copyright (c) 2004 Per Cederberg. All rights reserved.
33  */

34
35 package net.percederberg.grammatica.code.visualbasic;
36
37 import java.io.PrintWriter JavaDoc;
38 import net.percederberg.grammatica.code.CodeElement;
39 import net.percederberg.grammatica.code.CodeStyle;
40
41 /**
42  * A class generating a Visual Basic enumeration declaration.
43  *
44  * @author Adrian Moore, <adrianrob at hotmail dot com>
45  * @author Per Cederberg, <per at percederberg dot net>
46  * @version 1.5
47  * @since 1.5
48  */

49 public class VisualBasicEnumeration extends VisualBasicType {
50
51     /**
52      * The public access modifier constant.
53      */

54     public static final int PUBLIC = VisualBasicModifier.PUBLIC;
55
56     /**
57      * The protected friend access modifier constant. May only be
58      * used when declared inside another type.
59      */

60     public static final int PROTECTED_FRIEND =
61         VisualBasicModifier.PROTECTED_FRIEND;
62
63     /**
64      * The protected access modifier constant. May only be used when
65      * declared inside another type.
66      */

67     public static final int PROTECTED = VisualBasicModifier.PROTECTED;
68
69     /**
70      * The friend access modifier constant.
71      */

72     public static final int FRIEND = VisualBasicModifier.FRIEND;
73
74     /**
75      * The private access modifier constant. May only be used when
76      * declared inside another type.
77      */

78     public static final int PRIVATE = VisualBasicModifier.PRIVATE;
79
80     /**
81      * The shadows modifier constant. May only be used when declared
82      * inside another type.
83      */

84     public static final int SHADOWS = VisualBasicModifier.SHADOWS;
85
86     /**
87      * The last enumeration constant added.
88      */

89     private Constant last = null;
90
91     /**
92      * Creates a new enumeration code generator with public access.
93      *
94      * @param name the enumeration name
95      */

96     public VisualBasicEnumeration(String JavaDoc name) {
97         this(PUBLIC, name);
98     }
99
100     /**
101      * Creates a new enumeration code generator with the specified
102      * modifiers.
103      *
104      * @param modifiers the modifier flag constants
105      * @param name the enumeration name
106      */

107     public VisualBasicEnumeration(int modifiers, String JavaDoc name) {
108         super(modifiers, name, "");
109     }
110
111     /**
112      * Returns a numeric category number for the code element. A lower
113      * category number implies that the code element should be placed
114      * before code elements with a higher category number within a
115      * declaration.
116      *
117      * @return the category number
118      */

119     public int category() {
120         return 3;
121     }
122
123     /**
124      * Adds a constant to the enumeration.
125      *
126      * @param name the constant name
127      */

128     public void addConstant(String JavaDoc name) {
129         addConstant(name, null);
130     }
131
132     /**
133      * Adds a constant to the enumeration.
134      *
135      * @param name the constant name
136      * @param value the constant value
137      */

138     public void addConstant(String JavaDoc name, String JavaDoc value) {
139         addConstant(name, value, null);
140     }
141
142     /**
143      * Adds a constant to the enumeration.
144      *
145      * @param name the constant name
146      * @param value the constant value, or null
147      * @param comment the constant comment
148      */

149     public void addConstant(String JavaDoc name,
150                             String JavaDoc value,
151                             VisualBasicComment comment) {
152
153         Constant c = new Constant(name, value);
154
155         if (comment != null) {
156             c.setComment(comment);
157         }
158         addElement(c);
159         last = c;
160     }
161
162     /**
163      * Prints the code element to the specified output stream.
164      *
165      * @param out the output stream
166      * @param style the code style to use
167      * @param indent the indentation level
168      */

169     public void print(PrintWriter JavaDoc out, CodeStyle style, int indent) {
170         print(out, style, indent, "Enum");
171     }
172
173     /**
174      * Prints the lines separating two elements. By default this
175      * method prints a newline before the first element, and between
176      * elements with different category numbers.
177      *
178      * @param out the output stream
179      * @param style the code style to use
180      * @param prev the previous element, or null if first
181      * @param next the next element, or null if last
182      */

183     protected void printSeparator(PrintWriter JavaDoc out,
184                                   CodeStyle style,
185                                   CodeElement prev,
186                                   CodeElement next) {
187         // Do nothing
188
}
189
190
191     /**
192      * A class generating a Visual Basic enumeration constant
193      * declaration.
194      */

195     private class Constant extends CodeElement {
196
197         /**
198          * The constant name.
199          */

200         private String JavaDoc name;
201
202         /**
203          * The constant value.
204          */

205         private String JavaDoc value;
206
207         /**
208          * The constant comment.
209          */

210         private VisualBasicComment comment;
211
212         /**
213          * Creates a new constant.
214          *
215          * @param name the constant name
216          * @param value the constant value
217          */

218         public Constant(String JavaDoc name, String JavaDoc value) {
219             this.name = name;
220             this.value = value;
221             this.comment = null;
222         }
223
224         /**
225          * Returns a numeric category number for the code element. A lower
226          * category number implies that the code element should be placed
227          * before code elements with a higher category number within a
228          * declaration.
229          *
230          * @return the category number
231          */

232         public int category() {
233             return 0;
234         }
235
236         /**
237          * Sets the constant comment. This method overwrites any
238          * previous comment.
239          *
240          * @param comment the new constant comment
241          */

242         public void setComment(VisualBasicComment comment) {
243             this.comment = comment;
244         }
245
246         /**
247          * Prints the code element to the specified output stream.
248          *
249          * @param out the output stream
250          * @param style the code style to use
251          * @param indent the indentation level
252          */

253         public void print(PrintWriter JavaDoc out, CodeStyle style, int indent) {
254             if (comment != null) {
255                 out.println();
256                 comment.print(out, style, indent);
257             }
258             out.print(style.getIndent(indent));
259             out.print("[" + name + "]");
260             if (value != null) {
261                 out.print(" = ");
262                 out.print(value);
263             }
264             out.println();
265         }
266     }
267 }
268
Popular Tags