| FieldMap.java |
1 /**
2 * Distribution License:
3 * JSword is free software; you can redistribute it and/or modify it under
4 * the terms of the GNU Lesser General Public License, version 2.1 as published by
5 * the Free Software Foundation. This program is distributed in the hope
6 * that it will be useful, but WITHOUT ANY WARRANTY; without even the
7 * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
8 * See the GNU Lesser General Public License for more details.
9 *
10 * The License is available on the internet at:
11 * http://www.gnu.org/copyleft/lgpl.html
12 * or by writing to:
13 * Free Software Foundation, Inc.
14 * 59 Temple Place - Suite 330
15 * Boston, MA 02111-1307, USA
16 *
17 * Copyright: 2005
18 * The copyright to this program is held by it's authors.
19 *
20 * ID: $Id: FieldMap.java 2091 2011-03-07 04:15:31Z dmsmith $
21 */
22 package org.crosswire.common.config.swing;
23
24 import java.util.Map;
25
26 import org.crosswire.common.config.Choice;
27 import org.crosswire.common.config.MultipleChoice;
28 import org.crosswire.common.util.Logger;
29 import org.crosswire.common.util.PluginUtil;
30 import org.crosswire.common.util.Reporter;
31
32 /**
33 * This class provides mapping between Choice types and Fields. There is an
34 * argument that this class should be a properties file however the practical
35 * advantages of compile time type-checking and make simplicity, outweigh the
36 * possible re-use gains of a properties file.
37 *
38 * @see gnu.lgpl.License for license details.<br>
39 * The copyright to this program is held by it's authors.
40 * @author Joe Walker [joe at eireneh dot com]
41 */
42 public final class FieldMap {
43 /**
44 * Prevent instantiation
45 */
46 private FieldMap() {
47 }
48
49 /**
50 * Get a field from a string
51 *
52 * @param type
53 * the configuration type
54 * @return The best Field that matches
55 */
56 public static Field getField(Choice type) {
57 Field field = null;
58 Exception ex = null;
59 try {
60 // We need to treat instances of MultipleChoice differently
61 // because they are always OptionsFields whatever their underlying
62 // type is.
63 if (type instanceof MultipleChoice) {
64 field = new OptionsField();
65 } else {
66 Class<Field> clazz = map.get(type.getType());
67 if (clazz != null) {
68 field = clazz.newInstance();
69 } else {
70 log.warn("field type (" + type + ") unregistered.");
71 field = new TextField();
72 }
73 }
74 field.setChoice(type);
75 } catch (InstantiationException e) {
76 ex = e;
77 } catch (IllegalAccessException e) {
78 ex = e;
79 }
80
81 if (ex != null) {
82 log.warn("field type (" + type + ") initialization failed:", ex);
83 Reporter.informUser(type, ex);
84
85 log.warn("field type (" + type + ") unregistered.");
86 field = new TextField();
87 field.setChoice(type);
88 }
89
90 return field;
91 }
92
93 /**
94 * The configuration table
95 */
96 private static Map<String, Class<Field>> map;
97
98 /**
99 * Default map configuration
100 */
101 static {
102 map = PluginUtil.getImplementorsMap(Field.class);
103 }
104
105 /**
106 * The log stream
107 */
108 private static final Logger log = Logger.getLogger(FieldMap.class);
109 }
110