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: PathChoice.java 2050 2010-12-09 15:31:45Z dmsmith $
21   */
22  package org.crosswire.common.config;
23  
24  import java.io.File;
25  
26  import org.crosswire.common.util.Convert;
27  
28  /**
29   * A class to convert between strings and objects of a type.
30   * 
31   * @see gnu.lgpl.License for license details.<br>
32   *      The copyright to this program is held by it's authors.
33   * @author Joe Walker [joe at eireneh dot com]
34   */
35  public class PathChoice extends AbstractReflectedChoice {
36      /*
37       * (non-Javadoc)
38       * 
39       * @see org.crosswire.common.config.Choice#getConvertionClass()
40       */
41      public Class<File[]> getConversionClass() {
42          return File[].class;
43      }
44  
45      /*
46       * (non-Javadoc)
47       * 
48       * @see
49       * org.crosswire.common.config.AbstractReflectedChoice#convertToString(java
50       * .lang.Object)
51       */
52      @Override
53      public String convertToString(Object orig) {
54          File[] paths = (File[]) orig;
55          String[] names = new String[paths.length];
56          for (int i = 0; i < paths.length; i++) {
57              names[i] = paths[i].getAbsolutePath();
58          }
59  
60          return Convert.stringArray2String(names, File.pathSeparator);
61      }
62  
63      /*
64       * (non-Javadoc)
65       * 
66       * @see
67       * org.crosswire.common.config.AbstractReflectedChoice#convertToObject(java
68       * .lang.String)
69       */
70      @Override
71      public Object convertToObject(String orig) {
72          String[] names = Convert.string2StringArray(orig, File.pathSeparator);
73          File[] paths = new File[names.length];
74          for (int i = 0; i < names.length; i++) {
75              paths[i] = new File(names[i]);
76          }
77  
78          return paths;
79      }
80  }
81