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