KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > percederberg > grammatica > code > csharp > CSharpEnumeration


1 /*
2  * CSharpEnumeration.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) 2003 Per Cederberg. All rights reserved.
32  */

33
34 package net.percederberg.grammatica.code.csharp;
35
36 import java.io.PrintWriter JavaDoc;
37 import net.percederberg.grammatica.code.CodeElement;
38 import net.percederberg.grammatica.code.CodeStyle;
39
40 /**
41  * A class generating a C# enumeration declaration.
42  *
43  * @author Per Cederberg, <per at percederberg dot net>
44  * @version 1.0
45  */

46 public class CSharpEnumeration extends CSharpType {
47
48     /**
49      * The public access modifier constant.
50      */

51     public static final int PUBLIC = CSharpModifier.PUBLIC;
52
53     /**
54      * The protected internal access modifier constant. May only be
55      * used when declared inside another type.
56      */

57     public static final int PROTECTED_INTERNAL =
58         CSharpModifier.PROTECTED_INTERNAL;
59
60     /**
61      * The protected access modifier constant. May only be used when
62      * declared inside another type.
63      */

64     public static final int PROTECTED = CSharpModifier.PROTECTED;
65
66     /**
67      * The internal access modifier constant.
68      */

69     public static final int INTERNAL = CSharpModifier.INTERNAL;
70
71     /**
72      * The private access modifier constant. May only be used when
73      * declared inside another type.
74      */

75     public static final int PRIVATE = CSharpModifier.PRIVATE;
76
77     /**
78      * The new modifier constant. May only be used when declared
79      * inside another type.
80      */

81     public static final int NEW = CSharpModifier.NEW;
82
83     /**
84      * The last enumeration constant added.
85      */

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

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

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

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

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

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

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

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

180     protected void printSeparator(PrintWriter JavaDoc out,
181                                   CodeStyle style,
182                                   CodeElement prev,
183                                   CodeElement next) {
184         // Do nothing
185
}
186
187     /**
188      * A class generating a C# enumeration constant declaration.
189      */

190     private class Constant extends CodeElement {
191
192         /**
193          * The constant name.
194          */

195         private String JavaDoc name;
196
197         /**
198          * The constant value.
199          */

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

205         private CSharpComment comment;
206
207         /**
208          * Creates a new constant.
209          *
210          * @param name the constant name
211          * @param value the constant value
212          */

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

227         public int category() {
228             return 0;
229         }
230
231         /**
232          * Sets the constant comment. This method overwrites any
233          * previous comment.
234          *
235          * @param comment the new constant comment
236          */

237         public void setComment(CSharpComment comment) {
238             this.comment = comment;
239         }
240
241         /**
242          * Prints the code element to the specified output stream.
243          *
244          * @param out the output stream
245          * @param style the code style to use
246          * @param indent the indentation level
247          */

248         public void print(PrintWriter JavaDoc out, CodeStyle style, int indent) {
249             if (comment != null) {
250                 out.println();
251                 comment.print(out, style, indent);
252             }
253             out.print(style.getIndent(indent));
254             out.print(name);
255             if (value != null) {
256                 out.print(" = ");
257                 out.print(value);
258             }
259             if (this != last) {
260                 out.print(",");
261             }
262             out.println();
263         }
264     }
265 }
266
Popular Tags