2
package org.crosswire.config.servlet;
4
import java.util.Hashtable;
6
import org.w3c.dom.Document;
7
import org.w3c.dom.Element;
9
import org.crosswire.config.Choice;
10
import org.crosswire.util.Convert;
11
import org.crosswire.util.Logger;
12
import org.crosswire.util.Reporter;
15
* This class provides mapping between Choice types and Fields.
16
* There is an argument that this class should be a properties file
17
* however the practical advantages of compile time type-checking and
18
* make simplicity, overweigh the possible re-use gains of a
21
* <table border='1' cellPadding='3' cellSpacing='0' width="100%">
22
* <tr><td bgColor='white'class='TableRowColor'><font size='-7'>
23
* Distribution Licence:<br />
24
* Project B is free software; you can redistribute it
25
* and/or modify it under the terms of the GNU General Public License,
26
* version 2 as published by the Free Software Foundation.<br />
27
* This program is distributed in the hope that it will be useful,
28
* but WITHOUT ANY WARRANTY; without even the implied warranty of
29
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
30
* General Public License for more details.<br />
31
* The License is available on the internet
32
* <a href='http://www.gnu.org/copyleft/gpl.html'>here</a>, by writing to
33
* <i>Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
34
* MA 02111-1307, USA</i>, Or locally at the Licence link below.<br />
35
* The copyright to this program is held by it's authors.
36
* </font></td></tr></table>
37
* @see <a href='http://www.eireneh.com/servlets/Web'>Project B Home</a>
38
* @see <{docs.Licence}>
44
* Custom code for each of the types
46
public static Element getHTMLElement(Document doc, String key, Choice choice, String value)
48
String type = choice.getType();
51
Class clazz = (Class) hash.get(type);
56
field = (Field) clazz.newInstance();
60
log.warning("field type ("+type+") initialization failed:");
61
Reporter.informUser(choice, ex);
65
log.warning("field type ("+type+") unregistered.");
66
field = new TextField();
72
field = new TextField();
75
field.setHelpText(choice.getHelpText());
77
field.setDocument(doc);
78
field.setOptions(choice.getTypeOptions());
79
field.setValue(value);
81
return field.getHTMLElement();
85
* We configure the FieldMap by access to the Hashtable that holds
86
* the string/Field mapping.
87
* @return The configuration Hashtable
89
public static Hashtable getHashtable()
94
/** The configuration table */
95
private static Hashtable hash = new Hashtable();
98
protected static Logger log = Logger.getLogger(FieldMap.class);
101
* Default hashtable configuration
105
hash.put("boolean", BooleanField.class);
106
hash.put("text", TextField.class);
107
hash.put("action", ActionField.class);
108
hash.put("options", OptionsField.class);
109
hash.put("number", NumberField.class);
110
hash.put("password", PasswordField.class);
111
hash.put("file", FileField.class);
112
hash.put("color", ColorField.class);
116
* A Field is an HTML representation of a Choice.
118
public static abstract class Field
120
public void setKey(String key) { this.key = key; }
121
public void setDocument(Document doc) { this.doc = doc; }
122
public void setHelpText(String help) { this.help = help; }
123
public void setValue(String value) { this.value = value; }
124
public void setOptions(Object data) { this.data = data; }
127
* Get the actual HTML that we can add to a web page.
129
public Element getHTMLElement()
131
Element ele = doc.createElement("input");
133
ele.setAttribute("name", key);
134
ele.setAttribute("size", "30");
135
ele.setAttribute("value", value);
136
ele.setAttribute("title", help);
141
/** The Document used to create Elements */
142
protected Document doc;
144
/** The key to this choice */
145
protected String key = "unset";
147
/** The value to the choice */
148
protected String value = "unset";
150
/** The help text to the choice */
151
protected String help = "";
153
/** The optional data from the choice */
154
protected Object data = null;
158
* A Field is an HTML representation of a Choice.
160
public static class TextField extends Field
165
* A Field is an HTML representation of a Choice.
167
public static class BooleanField extends Field
170
* Get the actual HTML that we can add to a web page.
172
public Element getHTMLElement()
174
Element ele = doc.createElement("input");
176
ele.setAttribute("type", "checkbox");
177
ele.setAttribute("name", key);
178
ele.setAttribute("value", "true");
179
ele.setAttribute("title", help);
181
if (Convert.string2Boolean(value))
182
ele.setAttribute("checked", "true");
189
* A Field is an HTML representation of a Choice.
191
public static class ActionField extends Field
194
* Get the actual HTML that we can add to a web page.
196
public Element getHTMLElement()
198
Element ele = doc.createElement("input");
200
ele.setAttribute("type", "button");
201
ele.setAttribute("value", "Custom actions not supportered in HTML");
202
ele.setAttribute("disabled", "true");
203
ele.setAttribute("title", help);
210
* A Field is an HTML representation of a Choice.
212
public static class OptionsField extends Field
214
/** Get the actual HTML that we can add to a web page. */
215
public Element getHTMLElement()
217
String[] array = (String[]) data;
219
Element ele = doc.createElement("select");
220
ele.setAttribute("name", key);
221
ele.setAttribute("title", help);
223
for (int i=0; i<array.length; i++)
225
Element option = doc.createElement("option");
226
ele.appendChild(option);
227
option.setAttribute("value", array[i]);
229
if (value.equals(array[i]))
230
option.setAttribute("selected", "true");
232
option.appendChild(doc.createTextNode(array[i]));
240
* A Field is an HTML representation of a Choice.
242
public static class NumberField extends Field
245
* Get the actual HTML that we can add to a web page.
247
public Element getHTMLElement()
249
Element ele = doc.createElement("input");
251
ele.setAttribute("name", key);
252
ele.setAttribute("size", "6");
253
ele.setAttribute("value", value);
254
ele.setAttribute("title", help);
261
* A Field is an HTML representation of a Choice.
263
public static class PasswordField extends Field
266
* Get the actual HTML that we can add to a web page.
268
public Element getHTMLElement()
270
Element ele = doc.createElement("input");
272
ele.setAttribute("type", "password");
273
ele.setAttribute("name", key);
274
ele.setAttribute("size", "10");
275
ele.setAttribute("value", value);
276
ele.setAttribute("title", help);
283
* A Field is an HTML representation of a Choice.
285
public static class FileField extends Field
288
* Get the actual HTML that we can add to a web page.
290
public Element getHTMLElement()
292
Element ele = doc.createElement("input");
294
ele.setAttribute("name", key);
295
ele.setAttribute("size", "40");
296
ele.setAttribute("value", value);
297
ele.setAttribute("title", help);
304
* A Field is an HTML representation of a Choice.
306
public static class ColorField extends Field
308
/** Get the actual HTML that we can add to a web page. */
309
public String getHTML()
311
StringBuffer retcode = new StringBuffer();
313
retcode.append("Red: <select name='"+key+"_red' title='"+help+"'>\n");
314
retcode.append("<option value='0'>0</option>\n");
315
retcode.append("<option value='1'>1</option>\n");
316
retcode.append("<option value='2'>2</option>\n");
317
retcode.append("<option value='3'>3</option>\n");
318
retcode.append("<option value='4'>4</option>\n");
319
retcode.append("<option value='5'>5</option>\n");
320
retcode.append("<option value='6'>6</option>\n");
321
retcode.append("<option value='7'>7</option>\n");
322
retcode.append("<option value='8'>8</option>\n");
323
retcode.append("<option value='9'>9</option>\n");
324
retcode.append("<option value='10'>10</option>\n");
325
retcode.append("<option value='11'>11</option>\n");
326
retcode.append("<option value='12'>12</option>\n");
327
retcode.append("<option value='13'>13</option>\n");
328
retcode.append("<option value='14'>14</option>\n");
329
retcode.append("<option value='15'>15</option>\n");
330
retcode.append("</select>\n");
332
retcode.append(" Green: <select name='"+key+"_green' title='"+help+"'>\n");
333
retcode.append("<option value='0'>0</option>\n");
334
retcode.append("<option value='1'>1</option>\n");
335
retcode.append("<option value='2'>2</option>\n");
336
retcode.append("<option value='3'>3</option>\n");
337
retcode.append("<option value='4'>4</option>\n");
338
retcode.append("<option value='5'>5</option>\n");
339
retcode.append("<option value='6'>6</option>\n");
340
retcode.append("<option value='7'>7</option>\n");
341
retcode.append("<option value='8'>8</option>\n");
342
retcode.append("<option value='9'>9</option>\n");
343
retcode.append("<option value='10'>10</option>\n");
344
retcode.append("<option value='11'>11</option>\n");
345
retcode.append("<option value='12'>12</option>\n");
346
retcode.append("<option value='13'>13</option>\n");
347
retcode.append("<option value='14'>14</option>\n");
348
retcode.append("<option value='15'>15</option>\n");
349
retcode.append("</select>\n");
351
retcode.append(" Blue: <select name='"+key+"_blue' title='"+help+"'>\n");
352
retcode.append("<option value='0'>0</option>\n");
353
retcode.append("<option value='1'>1</option>\n");
354
retcode.append("<option value='2'>2</option>\n");
355
retcode.append("<option value='3'>3</option>\n");
356
retcode.append("<option value='4'>4</option>\n");
357
retcode.append("<option value='5'>5</option>\n");
358
retcode.append("<option value='6'>6</option>\n");
359
retcode.append("<option value='7'>7</option>\n");
360
retcode.append("<option value='8'>8</option>\n");
361
retcode.append("<option value='9'>9</option>\n");
362
retcode.append("<option value='10'>10</option>\n");
363
retcode.append("<option value='11'>11</option>\n");
364
retcode.append("<option value='12'>12</option>\n");
365
retcode.append("<option value='13'>13</option>\n");
366
retcode.append("<option value='14'>14</option>\n");
367
retcode.append("<option value='15'>15</option>\n");
368
retcode.append("</select>\n");
370
return retcode.toString();