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.Iterator;
23  import java.util.Map;
24  import java.util.ResourceBundle;
25  import java.util.TreeMap;
26  
27  import org.jdom2.Element;
28  
29  /**
30   * A class to convert between strings and objects of a type.
31   * 
32   * @see gnu.lgpl.License The GNU Lesser General Public License for details.
33   * @author Joe Walker
34   * @author DM Smith
35   */
36  public class IntOptionsChoice extends AbstractReflectedChoice implements MappedChoice<Integer, String> {
37      /*
38       * (non-Javadoc)
39       * 
40       * @see org.crosswire.common.config.Choice#init(org.jdom2.Element)
41       */
42      @Override
43      public void init(Element option, ResourceBundle configResources) throws StartupException {
44          assert configResources != null;
45  
46          super.init(option, configResources);
47  
48          String prefix = getKey() + ".alternative.";
49  
50          options = new TreeMap<Integer, String>();
51          Iterator<Element> iter = option.getChildren("alternative").iterator();
52          while (iter.hasNext()) {
53              Element alternative = iter.next();
54              int number = Integer.parseInt(alternative.getAttributeValue("number"));
55              String name = configResources.getString(prefix + number);
56              options.put(Integer.valueOf(number), name);
57          }
58      }
59  
60      /*
61       * (non-Javadoc)
62       * 
63       * @see org.crosswire.common.config.MappedChoice#getOptions()
64       */
65      public Map<Integer, String> getOptions() {
66          return new TreeMap<Integer, String>(options);
67      }
68  
69      /*
70       * (non-Javadoc)
71       * 
72       * @see org.crosswire.common.config.Choice#getConvertionClass()
73       */
74      public Class<Integer> getConversionClass() {
75          return Integer.TYPE;
76      }
77  
78      /*
79       * (non-Javadoc)
80       * 
81       * @see
82       * org.crosswire.common.config.AbstractReflectedChoice#convertToString(java
83       * .lang.Object)
84       */
85      @Override
86      public String convertToString(Object orig) {
87          return orig.toString();
88      }
89  
90      /*
91       * (non-Javadoc)
92       * 
93       * @see
94       * org.crosswire.common.config.AbstractReflectedChoice#convertToObject(java
95       * .lang.String)
96       */
97      @Override
98      public Object convertToObject(String orig) {
99          // First check to see if this is a number
100         try {
101             return Integer.valueOf(orig);
102         } catch (NumberFormatException ex) {
103             for (Map.Entry<Integer, String> mapEntry : options.entrySet()) {
104                 if (mapEntry.getValue().equals(orig)) {
105                     return mapEntry.getKey();
106                 }
107             }
108             return Integer.valueOf(0);
109         }
110     }
111 
112     private Map<Integer, String> options;
113 }
114