| StringOptionsChoice.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 or later
5 * as published by the Free Software Foundation. This program is distributed
6 * in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
7 * the 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 * © CrossWire Bible Society, 2005 - 2016
18 *
19 */
20 package org.crosswire.common.config;
21
22 import java.util.ResourceBundle;
23
24 import org.crosswire.jsword.JSOtherMsg;
25 import org.jdom2.Element;
26
27 /**
28 * A class to convert between strings and objects of a type.
29 *
30 * @see gnu.lgpl.License The GNU Lesser General Public License for details.
31 * @author Joe Walker
32 */
33 public class StringOptionsChoice extends AbstractReflectedChoice implements MultipleChoice {
34 /*
35 * (non-Javadoc)
36 *
37 * @see org.crosswire.common.config.Choice#init(org.jdom2.Element)
38 */
39 @Override
40 public void init(Element option, ResourceBundle configResources) throws StartupException {
41 super.init(option, configResources);
42 Element map = option.getChild("map");
43 if (map == null) {
44 throw new StartupException(JSOtherMsg.lookupText("Missing {0} element in config.xml", "map"));
45 }
46
47 String name = map.getAttributeValue("name");
48 array = (String[]) ChoiceFactory.getDataMap().get(name);
49 }
50
51 /*
52 * (non-Javadoc)
53 *
54 * @see org.crosswire.common.config.MultipleChoice#getOptions()
55 */
56 public String[] getOptions() {
57 String[] copy = new String[array.length];
58 System.arraycopy(array, 0, copy, 0, array.length);
59 return copy;
60 }
61
62 /*
63 * (non-Javadoc)
64 *
65 * @see org.crosswire.common.config.Choice#getConvertionClass()
66 */
67 public Class<String> getConversionClass() {
68 return String.class;
69 }
70
71 /*
72 * (non-Javadoc)
73 *
74 * @see
75 * org.crosswire.common.config.AbstractReflectedChoice#convertToString(java
76 * .lang.Object)
77 */
78 @Override
79 public String convertToString(Object orig) {
80 return (String) orig;
81 }
82
83 /*
84 * (non-Javadoc)
85 *
86 * @see
87 * org.crosswire.common.config.AbstractReflectedChoice#convertToObject(java
88 * .lang.String)
89 */
90 @Override
91 public Object convertToObject(String orig) {
92 return orig;
93 }
94
95 /**
96 * The options that we are presenting the user with
97 */
98 private String[] array;
99 }
100