KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > tool > codegen > GeneratorOption


1
2 /*
3  * Enhydra Java Application Server Project
4  *
5  * The contents of this file are subject to the Enhydra Public License
6  * Version 1.1 (the "License"); you may not use this file except in
7  * compliance with the License. You may obtain a copy of the License on
8  * the Enhydra web site ( http://www.enhydra.org/ ).
9  *
10  * Software distributed under the License is distributed on an "AS IS"
11  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
12  * the License for the specific terms governing rights and limitations
13  * under the License.
14  *
15  * The Initial Developer of the Enhydra Application Server is Lutris
16  * Technologies, Inc. The Enhydra Application Server and portions created
17  * by Lutris Technologies, Inc. are Copyright Lutris Technologies, Inc.
18  * All Rights Reserved.
19  *
20  * Contributor(s):
21  *
22  */

23 package org.enhydra.tool.codegen;
24
25 // Toolbox imports
26
import org.enhydra.tool.common.ResUtil;
27 import java.util.ResourceBundle JavaDoc;
28
29 /**
30  * GeneratorOption define a generator specific option for CodeGen. Generation
31  * options include information such as the directory where to generate
32  * an application and name to use for the generated application.
33  */

34 public class GeneratorOption {
35     static ResourceBundle JavaDoc res = ResourceBundle.getBundle("org.enhydra.tool.codegen.Res"); // nores
36

37     private String JavaDoc name = new String JavaDoc();
38     private String JavaDoc value = new String JavaDoc();
39     private String JavaDoc displayName = new String JavaDoc();
40     private String JavaDoc description = new String JavaDoc();
41     private boolean required = false;
42     private boolean persistent = true;
43     private boolean bool = false;
44
45     /**
46      * Create a option for the current generator. Generators
47      * normally instantiate an array of options during
48      * initialization.
49      *
50      * @param name
51      * When passing command line arguments, the name must be prefixed
52      * with a dash.
53      *
54      * @param name
55      * When passing command line arguments, the name must be prefixed
56      * with a dash.
57      *
58      *
59      * @param value
60      * When the name refers to a boolean flag, any value other than 'true'
61      * should be treated as 'false'.
62      *
63      * @param description
64      * The description is what displays when help for the option is
65      * printed out from the command line. The description may also be
66      * used in a wizard panel.
67      *
68      * @param required
69      * True if a value for this option must be specified for the
70      * generation to be successful.
71      *
72      * @throws GeneratorException
73      * Thrown if name is a null or an empty string. Note that no exception
74      * is thrown during construction if required is true and the value is
75      * null or empty.
76      */

77     public GeneratorOption(String JavaDoc name, String JavaDoc value, String JavaDoc displayName,
78                            String JavaDoc description, boolean required,
79                            boolean persistent) throws GeneratorException {
80         if (name == null || name.trim().length() == 0) {
81             throw new GeneratorException(res.getString("Option_name_cannot_be"));
82         } else {
83             this.name = name;
84             this.value = value;
85             this.displayName = displayName;
86             this.description = description;
87             this.required = required;
88             this.persistent = persistent;
89             this.bool = false;
90         }
91     }
92
93     /**
94      * Create a option for the current generator. Generators
95      * normally instantiate an array of options during
96      * initialization.
97      *
98      * @param name
99      * When passing command line arguments, the name must be prefixed
100      * with a dash.
101      *
102      * @param value
103      * When the name refers to a boolean flag, any value other than 'true'
104      * should be treated as 'false'.
105      *
106      * @param description
107      * The description is what displays when help for the option is
108      * printed out from the command line. The description may also be
109      * used in a wizard panel.
110      *
111      *
112      * @throws GeneratorException
113      * Thrown if name is a null or an empty string. Note that no exception
114      * is thrown during construction if required is true and the value is
115      * null or empty.
116      */

117     public GeneratorOption(String JavaDoc n, boolean v, String JavaDoc desc,
118                            boolean persist) throws GeneratorException {
119         this(n, (new Boolean JavaDoc(v)).toString(), new String JavaDoc(), desc, false,
120              persist);
121         this.bool = true;
122     }
123
124     /**
125      * Get the option name. This should be a terse name used for command
126      * line and referencing within generator code. This terse format of
127      * indicating the option should not be displayed in a wizard.
128      *
129      * @return
130      * The option name. Prefix this with a dash for command line usage.
131      */

132     public String JavaDoc getName() {
133         return name;
134     }
135
136     /**
137      * Get the option value.
138      *
139      * @return
140      * For boolean flags, treat any value other than 'true' as 'false'.
141      */

142     public String JavaDoc getValue() {
143         return value;
144     }
145
146     /**
147      * Set the option value. The value can be set when processing
148      * arguments from the command line or during the writeOptions()
149      * method of an CodeGenPanel.
150      *
151      * @param value
152      * String value for the option.
153      *
154      * @throws GeneratorException
155      * Thrown if value is null or empty and required is true.
156      */

157     public void setValue(String JavaDoc value) throws GeneratorException {
158         if (isRequired()) {
159             if (value == null || value.trim().length() == 0) {
160                 throw new GeneratorException(
161                   ResUtil.format(res.getString("Required_value_cannot"),
162                                              getDisplayName()));
163             }
164         }
165         this.value = value;
166     }
167
168     /**
169      * Set the value as a boolean.
170      *
171      * @param value
172      * Boolean value.
173      */

174     public void setValue(boolean value) {
175         Boolean JavaDoc b = new Boolean JavaDoc(value);
176
177         this.value = b.toString();
178     }
179
180     /**
181      * Get a string describing the option. The description
182      * is displayed by command line help and may be used
183      * for wizard panels.
184      *
185      * @return
186      * Descriptive text. String length should not be
187      * longer than 512 characters.
188      */

189     public String JavaDoc getDescription() {
190         return description;
191     }
192
193     /**
194      * Flag indicating if the values needs to be set prior
195      * to generation. Ignore this flag when dealing with
196      * boolean generation options.
197      *
198      * @return
199      * If true, the generator requires that the value not be null
200      * or an empty string.
201      *
202      */

203     public boolean isRequired() {
204         return required;
205     }
206
207     /**
208      * Check if the option value persistent between CodeGen sessions.
209      *
210      * @return
211      * True if the value is persisted between CodeGen sessions.
212      */

213     public boolean isPersistent() {
214         return persistent;
215     }
216
217     /**
218      * Get name to display in Swing components.
219      *
220      * @return
221      * String to display in Swing components such as the generator
222      * selection combobox.
223      *
224      */

225     public String JavaDoc getDisplayName() {
226         return displayName;
227     }
228
229     /**
230      * Get the value as a boolean.
231      *
232      * @return
233      * Returns true if the value is 'true'.
234      */

235     public boolean isValue() {
236         Boolean JavaDoc b = Boolean.valueOf(getValue().toLowerCase());
237
238         return b.booleanValue();
239     }
240
241     /**
242      * Determine if the option is a boolean switch.
243      *
244      * @return
245      * True if the option is a boolean.
246      */

247     public boolean isBoolean() {
248         return bool;
249     }
250
251     public void clearValue() {
252       value = new String JavaDoc();
253     }
254
255     public boolean isEmpty() {
256       return (value == null || value.trim().length() == 0);
257     }
258
259 }
260
Popular Tags