1 package tim.prune.config;
4 import java.io.FileInputStream;
5 import java.util.Properties;
9 * Abstract class to hold application-wide configuration
11 public abstract class Config
13 /** File from which Config was loaded */
14 private static File _configFile = null;
16 /** Hashtable containing all config values */
17 private static Properties _configValues = new Properties();
18 /** Colour scheme object is also part of config */
19 private static ColourScheme _colourScheme = new ColourScheme();
21 /** Default config file */
22 private static final File DEFAULT_CONFIG_FILE = new File(".pruneconfig");
24 /** Key for track directory */
25 public static final String KEY_TRACK_DIR = "prune.trackdirectory";
26 /** Key for photo directory */
27 public static final String KEY_PHOTO_DIR = "prune.photodirectory";
28 /** Key for language code */
29 public static final String KEY_LANGUAGE_CODE = "prune.languagecode";
30 /** Key for language file */
31 public static final String KEY_LANGUAGE_FILE = "prune.languagefile";
32 /** Key for GPS device */
33 public static final String KEY_GPS_DEVICE = "prune.gpsdevice";
34 /** Key for GPS format */
35 public static final String KEY_GPS_FORMAT = "prune.gpsformat";
36 /** Key for Povray font */
37 public static final String KEY_POVRAY_FONT = "prune.povrayfont";
38 /** Key for metric/imperial */
39 public static final String KEY_METRIC_UNITS = "prune.metricunits";
40 /** Key for map server index */
41 public static final String KEY_MAPSERVERINDEX = "prune.mapserverindex";
42 /** Key for map server url */
43 public static final String KEY_MAPSERVERURL = "prune.mapserverurl";
44 /** Key for show map flag */
45 public static final String KEY_SHOW_MAP = "prune.showmap";
46 /** Key for width of thumbnails in kmz */
47 public static final String KEY_KMZ_IMAGE_WIDTH = "prune.kmzimagewidth";
48 /** Key for height of thumbnails in kmz */
49 public static final String KEY_KMZ_IMAGE_HEIGHT = "prune.kmzimageheight";
50 /** Key for gpsbabel path */
51 public static final String KEY_GPSBABEL_PATH = "prune.gpsbabelpath";
52 /** Key for gnuplot path */
53 public static final String KEY_GNUPLOT_PATH = "prune.gnuplotpath";
54 /** Key for exiftool path */
55 public static final String KEY_EXIFTOOL_PATH = "prune.exiftoolpath";
56 /** Key for colour scheme */
57 public static final String KEY_COLOUR_SCHEME = "prune.colourscheme";
58 /** Key for kml track colour */
59 public static final String KEY_KML_TRACK_COLOUR = "prune.kmltrackcolour";
63 * Load the default configuration file
65 public static void loadDefaultFile()
69 loadFile(DEFAULT_CONFIG_FILE);
71 catch (ConfigException ce) {} // ignore
76 * Load configuration from file
77 * @param inFile file to load
78 * @throws ConfigException if specified file couldn't be read
80 public static void loadFile(File inFile) throws ConfigException
82 // Start with default properties
83 Properties props = getDefaultProperties();
84 // Try to load the file into a properties object
85 boolean loadFailed = false;
86 FileInputStream fis = null;
89 fis = new FileInputStream(inFile);
96 if (fis != null) try {
99 catch (Exception e) {}
101 // Save all properties from file
102 _configValues.putAll(props);
103 _colourScheme.loadFromHex(_configValues.getProperty(KEY_COLOUR_SCHEME));
105 throw new ConfigException();
107 // Store location of successfully loaded config file
108 _configFile = inFile;
112 * @return Properties object containing default values
114 private static Properties getDefaultProperties()
116 Properties props = new Properties();
118 props.put(KEY_GPS_DEVICE, "usb:");
119 props.put(KEY_GPS_FORMAT, "garmin");
120 props.put(KEY_POVRAY_FONT, "crystal.ttf"); // alternative: DejaVuSans-Bold.ttf
121 props.put(KEY_SHOW_MAP, "0"); // hide by default
122 props.put(KEY_EXIFTOOL_PATH, "exiftool");
123 props.put(KEY_GNUPLOT_PATH, "gnuplot");
124 props.put(KEY_GPSBABEL_PATH, "gpsbabel");
125 props.put(KEY_KMZ_IMAGE_WIDTH, "240");
126 props.put(KEY_KMZ_IMAGE_HEIGHT, "240");
131 * @param inString String to parse
132 * @return int value of String, or 0 if unparseable
134 private static int parseInt(String inString)
138 val = Integer.parseInt(inString);
140 catch (Exception e) {} // ignore, value stays zero
144 /** @return File from which config was loaded (or null) */
145 public static File getConfigFile()
151 * @return config Properties object to allow all config values to be saved
153 public static Properties getAllConfig()
155 return _configValues;
159 * @return the current colour scheme
161 public static ColourScheme getColourScheme()
163 return _colourScheme;
167 * Store the given configuration setting
168 * @param inKey key (from constants)
169 * @param inValue value as string
171 public static void setConfigString(String inKey, String inValue)
173 if (inValue == null || inValue.equals("")) {
174 _configValues.remove(inKey);
177 _configValues.put(inKey, inValue);
182 * Store the given configuration setting
183 * @param inKey key (from constants)
184 * @param inValue value as boolean
186 public static void setConfigBoolean(String inKey, boolean inValue)
188 if (inKey != null && !inKey.equals(""))
190 _configValues.put(inKey, (inValue?"1":"0"));
195 * Store the given configuration setting
196 * @param inKey key (from constants)
197 * @param inValue value as int
199 public static void setConfigInt(String inKey, int inValue)
201 if (inKey != null && !inKey.equals(""))
203 _configValues.put(inKey, "" + inValue);
208 * Get the given configuration setting as a String
210 * @return configuration setting as a String
212 public static String getConfigString(String inKey)
214 return _configValues.getProperty(inKey);
218 * Get the given configuration setting as a boolean
220 * @return configuration setting as a boolean
222 public static boolean getConfigBoolean(String inKey)
224 String val = _configValues.getProperty(inKey);
225 return (val == null || val.equals("1"));
229 * Get the given configuration setting as an int
231 * @return configuration setting as an int
233 public static int getConfigInt(String inKey)
235 return parseInt(_configValues.getProperty(inKey));
239 * Check whether the given key corresponds to a boolean property
240 * @param inKey key to check
241 * @return true if corresponding property is boolean
243 public static boolean isKeyBoolean(String inKey)
245 // Only two boolean keys so far
246 return inKey != null && (
247 inKey.equals(KEY_METRIC_UNITS) || inKey.equals(KEY_SHOW_MAP));
251 * Update the colour scheme property from the current settings
253 public static void updateColourScheme()
255 setConfigString(KEY_COLOUR_SCHEME, _colourScheme.toString());