~sword-devel/jsword/trunk

« back to all changes in this revision

Viewing changes to jsword/java/common/org/crosswire/common/config/IntOptionsChoice.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.common.config;
 
3
 
 
4
import java.util.ArrayList;
 
5
import java.util.Iterator;
 
6
import java.util.List;
 
7
 
 
8
import org.apache.log4j.Logger;
 
9
import org.crosswire.common.util.Reporter;
 
10
import org.jdom.Element;
 
11
 
 
12
/**
 
13
 * A class to convert between strings and objects of a type
 
14
 * 
 
15
 * <p><table border='1' cellPadding='3' cellSpacing='0'>
 
16
 * <tr><td bgColor='white' class='TableRowColor'><font size='-7'>
 
17
 *
 
18
 * Distribution Licence:<br />
 
19
 * JSword is free software; you can redistribute it
 
20
 * and/or modify it under the terms of the GNU General Public License,
 
21
 * version 2 as published by the Free Software Foundation.<br />
 
22
 * This program is distributed in the hope that it will be useful,
 
23
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
24
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 
25
 * General Public License for more details.<br />
 
26
 * The License is available on the internet
 
27
 * <a href='http://www.gnu.org/copyleft/gpl.html'>here</a>, or by writing to:
 
28
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
 
29
 * MA 02111-1307, USA<br />
 
30
 * The copyright to this program is held by it's authors.
 
31
 * </font></td></tr></table>
 
32
 * @see docs.Licence
 
33
 * @author Joe Walker [joe at eireneh dot com]
 
34
 * @version $Id$
 
35
 */
 
36
public class IntOptionsChoice extends ReflectedChoice implements MultipleChoice
 
37
{
 
38
    public void init(Element option) throws StartupException
 
39
    {
 
40
        super.init(option);
 
41
 
 
42
        List list = new ArrayList();
 
43
        List alts = option.getChildren("alternative");
 
44
        Iterator it = alts.iterator();
 
45
        while (it.hasNext())
 
46
        {
 
47
            Element alternative = (Element) it.next();
 
48
            int number = Integer.parseInt(alternative.getAttributeValue("number"));
 
49
            String name = alternative.getAttributeValue("name");
 
50
            list.add(number, name);
 
51
        }
 
52
 
 
53
        options = (String[]) list.toArray(new String[0]);
 
54
    }
 
55
 
 
56
    public String[] getOptions()
 
57
    {
 
58
        return options;
 
59
    }
 
60
 
 
61
    public Class getConvertionClass()
 
62
    {
 
63
        return Integer.TYPE;
 
64
    }
 
65
 
 
66
    public String convertToString(Object orig)
 
67
    {
 
68
        return options[((Integer) orig).intValue()];
 
69
    }
 
70
 
 
71
    public Object convertToObject(String orig)
 
72
    {
 
73
        // First check to see if this is a number
 
74
        try
 
75
        {
 
76
            return new Integer(orig);
 
77
        }
 
78
        catch (NumberFormatException ex)
 
79
        {
 
80
        }
 
81
 
 
82
        // Then work on the name list
 
83
        for (int i = 0; i < options.length; i++)
 
84
        {
 
85
            String option = (String) options[i];
 
86
            if (option.equalsIgnoreCase(orig))
 
87
                return new Integer(i);
 
88
        }
 
89
        
 
90
        Reporter.informUser("Ignoring invalid option: "+orig);
 
91
        return options[0];
 
92
    }
 
93
 
 
94
    /**
 
95
     * The log stream
 
96
     */
 
97
    protected static Logger log = Logger.getLogger(IntOptionsChoice.class);
 
98
 
 
99
    private String[] options = null;
 
100
}