4 import java.io.FileInputStream;
5 import java.util.Properties;
8 * Abstract class to hold application-wide configuration
10 public abstract class Config
12 /** Working directory for loading and saving */
13 private static File _workingDir = null;
14 /** Default language */
15 private static String _langCode = null;
16 /** GPS device name */
17 private static String _gpsDevice = null;
18 /** GPS format name */
19 private static String _gpsFormat = null;
20 /** Font to use for povray */
21 private static String _povrayFont = null;
22 /** True to use metric units */
23 private static boolean _metricUnits = true;
24 /** Path to gnuplot executable */
25 private static String _gnuplotPath = null;
26 /** Index of selected map tile server */
27 private static int _mapTileServerIndex = -1;
28 /** URL for freeform map tile server */
29 private static String _mapTileServerUrl = null;
30 /** File from which Config was loaded */
31 private static File _configFile = null;
33 // TODO: Need setters for all these parameters if want to make the config saveable
35 /** Default config file */
36 private static final File DEFAULT_CONFIG_FILE = new File(".pruneconfig");
38 /** Key for working directory */
39 private static final String KEY_WORKING_DIR = "prune.directory";
40 /** Key for language code */
41 private static final String KEY_LANGUAGE_CODE = "prune.languagecode";
42 /** Key for GPS device */
43 private static final String KEY_GPS_DEVICE = "prune.gpsdevice";
44 /** Key for GPS format */
45 private static final String KEY_GPS_FORMAT = "prune.gpsformat";
46 /** Key for Povray font */
47 private static final String KEY_POVRAY_FONT = "prune.povrayfont";
48 /** Key for metric/imperial */
49 private static final String KEY_METRIC_UNITS = "prune.metricunits";
50 /** Key for gpsbabel path */
51 private static final String KEY_GNUPLOTPATH = "prune.gnuplotpath";
52 /** Key for map server index */
53 private static final String KEY_MAPSERVERINDEX = "prune.mapserverindex";
54 /** Key for map server url */
55 private static final String KEY_MAPSERVERURL = "prune.mapserverurl";
59 * @return working directory for loading and saving
61 public static File getWorkingDirectory()
67 * @param inDirectory working directory to use
69 public static void setWorkingDirectory(File inDirectory)
71 _workingDir = inDirectory;
75 * Load the default configuration file
77 public static void loadDefaultFile()
81 loadFile(DEFAULT_CONFIG_FILE);
83 catch (ConfigException ce) {} // ignore
88 * Load configuration from file
89 * @param inFile file to load
90 * @throws ConfigException if specified file couldn't be read
92 public static void loadFile(File inFile) throws ConfigException
94 // Start with default properties
95 Properties props = getDefaultProperties();
96 // Try to load the file into a properties object
97 boolean loadFailed = false;
98 FileInputStream fis = null;
101 fis = new FileInputStream(inFile);
104 catch (Exception e) {
108 if (fis != null) try {
111 catch (Exception e) {}
113 // Save the properties we know about, ignore the rest
114 _langCode = props.getProperty(KEY_LANGUAGE_CODE);
115 String dir = props.getProperty(KEY_WORKING_DIR);
116 if (dir != null) {setWorkingDirectory(new File(dir));}
117 _gpsDevice = props.getProperty(KEY_GPS_DEVICE);
118 _gpsFormat = props.getProperty(KEY_GPS_FORMAT);
119 _povrayFont = props.getProperty(KEY_POVRAY_FONT);
120 String useMetric = props.getProperty(KEY_METRIC_UNITS);
121 _metricUnits = (useMetric == null || useMetric.equals("") || useMetric.toLowerCase().equals("y"));
122 _gnuplotPath = props.getProperty(KEY_GNUPLOTPATH);
123 if (_gnuplotPath == null || _gnuplotPath.equals("")) {_gnuplotPath = "gnuplot";}
124 _mapTileServerIndex = parseInt(props.getProperty(KEY_MAPSERVERINDEX));
125 _mapTileServerUrl = props.getProperty(KEY_MAPSERVERURL);
127 throw new ConfigException();
129 // Store location of successfully loaded config file
130 _configFile = inFile;
134 * @return Properties object containing default values
136 private static Properties getDefaultProperties()
138 Properties props = new Properties();
140 props.put(KEY_GPS_DEVICE, "usb:");
141 props.put(KEY_GPS_FORMAT, "garmin");
142 props.put(KEY_POVRAY_FONT, "crystal.ttf"); // alternative: DejaVuSans-Bold.ttf
147 * @param inString String to parse
148 * @return int value of String, or 0 if unparseable
150 private static int parseInt(String inString)
154 val = Integer.parseInt(inString);
156 catch (Exception e) {} // ignore, value stays zero
160 /** @return language code */
161 public static String getLanguageCode()
166 /** @return gps device */
167 public static String getGpsDevice()
172 /** @return gps format */
173 public static String getGpsFormat()
178 /** @return povray font */
179 public static String getPovrayFont()
184 /** @return true to use metric units */
185 public static boolean getUseMetricUnits()
190 /** @param inMetric true to use metric units */
191 public static void setUseMetricUnits(boolean inMetric)
193 _metricUnits = inMetric;
196 /** @return path to gnuplot */
197 public static String getGnuplotPath()
202 /** @param inPath path to Gnuplot */
203 public static void setGnuplotPath(String inPath)
205 _gnuplotPath = inPath;
208 /** @return index of map server */
209 public static int getMapServerIndex()
211 return _mapTileServerIndex;
214 /** @param inIndex selected index */
215 public static void setMapServerIndex(int inIndex)
217 _mapTileServerIndex = inIndex;
220 /** @return url of map server */
221 public static String getMapServerUrl()
223 return _mapTileServerUrl;
226 /** @param inUrl url of map server */
227 public static void setMapServerUrl(String inUrl)
229 _mapTileServerUrl = inUrl;
232 /** @return File from which config was loaded (or null) */
233 public static File getConfigFile()