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;
26 /** Default config file */
27 private static final File DEFAULT_CONFIG_FILE = new File(".pruneconfig");
29 /** Key for working directory */
30 private static final String KEY_WORKING_DIR = "prune.directory";
31 /** Key for language code */
32 private static final String KEY_LANGUAGE_CODE = "prune.languagecode";
33 /** Key for GPS device */
34 private static final String KEY_GPS_DEVICE = "prune.gpsdevice";
35 /** Key for GPS format */
36 private static final String KEY_GPS_FORMAT = "prune.gpsformat";
37 /** Key for Povray font */
38 private static final String KEY_POVRAY_FONT = "prune.povrayfont";
39 /** Key for metric/imperial */
40 private static final String KEY_METRIC_UNITS = "prune.metricunits";
41 // TODO: Save config file location so save possible
45 * @return working directory for loading and saving
47 public static File getWorkingDirectory()
53 * @param inDirectory working directory to use
55 public static void setWorkingDirectory(File inDirectory)
57 _workingDir = inDirectory;
61 * Load the default configuration file
63 public static void loadDefaultFile()
67 loadFile(DEFAULT_CONFIG_FILE);
69 catch (ConfigException ce) {} // ignore
74 * Load configuration from file
75 * @param inFile file to load
77 public static void loadFile(File inFile) throws ConfigException
79 // Start with default properties
80 Properties props = getDefaultProperties();
81 // Try to load the file into a properties object
82 boolean loadFailed = false;
85 props.load(new FileInputStream(inFile));
91 // Save the properties we know about, ignore the rest
92 _langCode = props.getProperty(KEY_LANGUAGE_CODE);
93 String dir = props.getProperty(KEY_WORKING_DIR);
94 if (dir != null) {setWorkingDirectory(new File(dir));}
95 _gpsDevice = props.getProperty(KEY_GPS_DEVICE);
96 _gpsFormat = props.getProperty(KEY_GPS_FORMAT);
97 _povrayFont = props.getProperty(KEY_POVRAY_FONT);
98 String useMetric = props.getProperty(KEY_METRIC_UNITS);
99 _metricUnits = (useMetric == null || useMetric.equals("") || useMetric.toLowerCase().equals("y"));
101 throw new ConfigException();
106 * @return Properties object containing default values
108 private static Properties getDefaultProperties()
110 Properties props = new Properties();
112 props.put(KEY_GPS_DEVICE, "usb:");
113 props.put(KEY_GPS_FORMAT, "garmin");
114 props.put(KEY_POVRAY_FONT, "crystal.ttf"); // alternative: DejaVuSans-Bold.ttf
118 /** @return language code */
119 public static String getLanguageCode()
124 /** @return gps device */
125 public static String getGpsDevice()
130 /** @return gps format */
131 public static String getGpsFormat()
136 /** @return povray font */
137 public static String getPovrayFont()
142 /** @return true to use metric units */
143 public static boolean getUseMetricUnits()