~sword-devel/jsword/trunk

« back to all changes in this revision

Viewing changes to jsword/java/historic/org/crosswire/config/servlet/FieldMap.java

  • Committer: joe
  • Date: 2002-10-08 21:36:18 UTC
  • Revision ID: svn-v4:a88caf3b-7e0a-0410-8d0d-cecb45342206:trunk:80
big config and comment update

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
 
2
 
package org.crosswire.config.servlet;
3
 
 
4
 
import java.util.Hashtable;
5
 
 
6
 
import org.w3c.dom.Document;
7
 
import org.w3c.dom.Element;
8
 
 
9
 
import org.crosswire.config.Choice;
10
 
import org.crosswire.util.Convert;
11
 
import org.crosswire.util.Logger;
12
 
import org.crosswire.util.Reporter;
13
 
 
14
 
/**
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
19
 
 * properties file.
20
 
 *
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}>
39
 
 * @author Joe Walker
40
 
 */
41
 
public class FieldMap
42
 
{
43
 
    /**
44
 
     * Custom code for each of the types
45
 
     */
46
 
    public static Element getHTMLElement(Document doc, String key, Choice choice, String value)
47
 
    {
48
 
        String type = choice.getType();
49
 
        Field field = null;
50
 
 
51
 
        Class clazz = (Class) hash.get(type);
52
 
        if (clazz != null)
53
 
        {
54
 
            try
55
 
            {
56
 
                field = (Field) clazz.newInstance();
57
 
            }
58
 
            catch (Exception ex)
59
 
            {
60
 
                log.warning("field type ("+type+") initialization failed:");
61
 
                Reporter.informUser(choice, ex);
62
 
 
63
 
                if (field == null)
64
 
                {
65
 
                    log.warning("field type ("+type+") unregistered.");
66
 
                    field = new TextField();
67
 
                }
68
 
            }
69
 
        }
70
 
        else
71
 
        {
72
 
            field = new TextField();
73
 
        }
74
 
 
75
 
        field.setHelpText(choice.getHelpText());
76
 
        field.setKey(key);
77
 
        field.setDocument(doc);
78
 
        field.setOptions(choice.getTypeOptions());
79
 
        field.setValue(value);
80
 
 
81
 
        return field.getHTMLElement();
82
 
    }
83
 
 
84
 
    /**
85
 
     * We configure the FieldMap by access to the Hashtable that holds
86
 
     * the string/Field mapping.
87
 
     * @return The configuration Hashtable
88
 
     */
89
 
    public static Hashtable getHashtable()
90
 
    {
91
 
        return hash;
92
 
    }
93
 
 
94
 
    /** The configuration table */
95
 
    private static Hashtable hash = new Hashtable();
96
 
 
97
 
    /** The log stream */
98
 
    protected static Logger log = Logger.getLogger(FieldMap.class);
99
 
 
100
 
    /**
101
 
     * Default hashtable configuration
102
 
     */
103
 
    static
104
 
    {
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);
113
 
    }
114
 
 
115
 
    /**
116
 
     * A Field is an HTML representation of a Choice.
117
 
     */
118
 
    public static abstract class Field
119
 
    {
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; }
125
 
 
126
 
        /**
127
 
         * Get the actual HTML that we can add to a web page.
128
 
         */
129
 
        public Element getHTMLElement()
130
 
        {
131
 
            Element ele = doc.createElement("input");
132
 
 
133
 
            ele.setAttribute("name", key);
134
 
            ele.setAttribute("size", "30");
135
 
            ele.setAttribute("value", value);
136
 
            ele.setAttribute("title", help);
137
 
 
138
 
            return ele;
139
 
        }
140
 
 
141
 
        /** The Document used to create Elements */
142
 
        protected Document doc;
143
 
 
144
 
        /** The key to this choice */
145
 
        protected String key = "unset";
146
 
 
147
 
        /** The value to the choice */
148
 
        protected String value = "unset";
149
 
 
150
 
        /** The help text to the choice */
151
 
        protected String help = "";
152
 
 
153
 
        /** The optional data from the choice */
154
 
        protected Object data = null;
155
 
    }
156
 
 
157
 
    /**
158
 
     * A Field is an HTML representation of a Choice.
159
 
     */
160
 
    public static class TextField extends Field
161
 
    {
162
 
    }
163
 
 
164
 
    /**
165
 
     * A Field is an HTML representation of a Choice.
166
 
     */
167
 
    public static class BooleanField extends Field
168
 
    {
169
 
        /**
170
 
         * Get the actual HTML that we can add to a web page.
171
 
         */
172
 
        public Element getHTMLElement()
173
 
        {
174
 
            Element ele = doc.createElement("input");
175
 
 
176
 
            ele.setAttribute("type", "checkbox");
177
 
            ele.setAttribute("name", key);
178
 
            ele.setAttribute("value", "true");
179
 
            ele.setAttribute("title", help);
180
 
 
181
 
            if (Convert.string2Boolean(value))
182
 
                ele.setAttribute("checked", "true");
183
 
 
184
 
            return ele;
185
 
        }
186
 
    }
187
 
 
188
 
    /**
189
 
     * A Field is an HTML representation of a Choice.
190
 
     */
191
 
    public static class ActionField extends Field
192
 
    {
193
 
        /**
194
 
         * Get the actual HTML that we can add to a web page.
195
 
         */
196
 
        public Element getHTMLElement()
197
 
        {
198
 
            Element ele = doc.createElement("input");
199
 
 
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);
204
 
 
205
 
            return ele;
206
 
        }
207
 
    }
208
 
 
209
 
    /**
210
 
     * A Field is an HTML representation of a Choice.
211
 
     */
212
 
    public static class OptionsField extends Field
213
 
    {
214
 
        /** Get the actual HTML that we can add to a web page. */
215
 
        public Element getHTMLElement()
216
 
        {
217
 
            String[] array = (String[]) data;
218
 
 
219
 
            Element ele = doc.createElement("select");
220
 
            ele.setAttribute("name", key);
221
 
            ele.setAttribute("title", help);
222
 
 
223
 
            for (int i=0; i<array.length; i++)
224
 
            {
225
 
                Element option = doc.createElement("option");
226
 
                ele.appendChild(option);
227
 
                option.setAttribute("value", array[i]);
228
 
 
229
 
                if (value.equals(array[i]))
230
 
                    option.setAttribute("selected", "true");
231
 
 
232
 
                option.appendChild(doc.createTextNode(array[i]));
233
 
            }
234
 
 
235
 
            return ele;
236
 
        }
237
 
    }
238
 
 
239
 
    /**
240
 
     * A Field is an HTML representation of a Choice.
241
 
     */
242
 
    public static class NumberField extends Field
243
 
    {
244
 
        /**
245
 
         * Get the actual HTML that we can add to a web page.
246
 
         */
247
 
        public Element getHTMLElement()
248
 
        {
249
 
            Element ele = doc.createElement("input");
250
 
 
251
 
            ele.setAttribute("name", key);
252
 
            ele.setAttribute("size", "6");
253
 
            ele.setAttribute("value", value);
254
 
            ele.setAttribute("title", help);
255
 
 
256
 
            return ele;
257
 
        }
258
 
    }
259
 
 
260
 
    /**
261
 
     * A Field is an HTML representation of a Choice.
262
 
     */
263
 
    public static class PasswordField extends Field
264
 
    {
265
 
        /**
266
 
         * Get the actual HTML that we can add to a web page.
267
 
         */
268
 
        public Element getHTMLElement()
269
 
        {
270
 
            Element ele = doc.createElement("input");
271
 
 
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);
277
 
 
278
 
            return ele;
279
 
        }
280
 
    }
281
 
 
282
 
    /**
283
 
     * A Field is an HTML representation of a Choice.
284
 
     */
285
 
    public static class FileField extends Field
286
 
    {
287
 
        /**
288
 
         * Get the actual HTML that we can add to a web page.
289
 
         */
290
 
        public Element getHTMLElement()
291
 
        {
292
 
            Element ele = doc.createElement("input");
293
 
 
294
 
            ele.setAttribute("name", key);
295
 
            ele.setAttribute("size", "40");
296
 
            ele.setAttribute("value", value);
297
 
            ele.setAttribute("title", help);
298
 
 
299
 
            return ele;
300
 
        }
301
 
    }
302
 
 
303
 
    /**
304
 
     * A Field is an HTML representation of a Choice.
305
 
     */
306
 
    public static class ColorField extends Field
307
 
    {
308
 
        /** Get the actual HTML that we can add to a web page. */
309
 
        public String getHTML()
310
 
        {
311
 
            StringBuffer retcode = new StringBuffer();
312
 
 
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");
331
 
 
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");
350
 
 
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");
369
 
 
370
 
            return retcode.toString();
371
 
        }
372
 
    }
373
 
}
374