1 package tim.prune.config;
4 import java.io.FileInputStream;
5 import java.util.Properties;
7 import tim.prune.data.RecentFileList;
8 import tim.prune.data.UnitSet;
9 import tim.prune.data.UnitSetLibrary;
10 import tim.prune.gui.colour.ColourerFactory;
11 import tim.prune.gui.colour.PointColourer;
12 import tim.prune.gui.map.MapSourceLibrary;
16 * Abstract class to hold application-wide configuration
18 public abstract class Config
20 /** File from which Config was loaded */
21 private static File _configFile = null;
23 /** Hashtable containing all config values */
24 private static Properties _configValues = null;
25 /** Colour scheme object is also part of config */
26 private static ColourScheme _colourScheme = new ColourScheme();
27 /** Point colourer object, if any */
28 private static PointColourer _pointColourer = null;
29 /** Recently-used file list */
30 private static RecentFileList _recentFiles = new RecentFileList();
31 /** Current unit set */
32 private static UnitSet _unitSet = UnitSetLibrary.getUnitSet(null);
34 /** Default config file */
35 public static final File DEFAULT_CONFIG_FILE = new File(".pruneconfig");
36 public static final File HOME_CONFIG_FILE = new File(System.getProperty("user.home"), ".pruneconfig");
38 /** Key for track directory */
39 public static final String KEY_TRACK_DIR = "prune.trackdirectory";
40 /** Key for photo directory */
41 public static final String KEY_PHOTO_DIR = "prune.photodirectory";
42 /** Key for language code */
43 public static final String KEY_LANGUAGE_CODE = "prune.languagecode";
44 /** Key for language file */
45 public static final String KEY_LANGUAGE_FILE = "prune.languagefile";
46 /** Key for GPS device */
47 public static final String KEY_GPS_DEVICE = "prune.gpsdevice";
48 /** Key for GPS format */
49 public static final String KEY_GPS_FORMAT = "prune.gpsformat";
50 /** Key for GPSBabel filter string */
51 public static final String KEY_GPSBABEL_FILTER = "prune.gpsbabelfilter";
52 /** Key for GPSBabel import file format */
53 public static final String KEY_IMPORT_FILE_FORMAT = "prune.lastimportfileformat";
54 /** Key for Povray font */
55 public static final String KEY_POVRAY_FONT = "prune.povrayfont";
56 /** Key for the selected unit set */
57 public static final String KEY_UNITSET_KEY = "prune.unitsetkey";
58 /** Key for index of map source */
59 public static final String KEY_MAPSOURCE_INDEX = "prune.mapsource";
60 /** Key for number of fixed map sources */
61 public static final String KEY_NUM_FIXED_MAPS = "prune.numfixedmapsources";
62 /** Key for String containing custom map sources */
63 public static final String KEY_MAPSOURCE_LIST = "prune.mapsourcelist";
64 /** Key for show map flag */
65 public static final String KEY_SHOW_MAP = "prune.showmap";
66 /** Key for path to disk cache */
67 public static final String KEY_DISK_CACHE = "prune.diskcache";
68 /** Key for working online flag */
69 public static final String KEY_ONLINE_MODE = "prune.onlinemode";
70 /** Key for width of thumbnails in kmz */
71 public static final String KEY_KMZ_IMAGE_SIZE = "prune.kmzimagewidth";
72 /** Key for gpsbabel path */
73 public static final String KEY_GPSBABEL_PATH = "prune.gpsbabelpath";
74 /** Key for gnuplot path */
75 public static final String KEY_GNUPLOT_PATH = "prune.gnuplotpath";
76 /** Key for exiftool path */
77 public static final String KEY_EXIFTOOL_PATH = "prune.exiftoolpath";
78 /** Key for colour scheme */
79 public static final String KEY_COLOUR_SCHEME = "prune.colourscheme";
80 /** Key for point colourer */
81 public static final String KEY_POINT_COLOURER = "prune.pointcolourer";
82 /** Key for line width used for drawing */
83 public static final String KEY_LINE_WIDTH = "prune.linewidth";
84 /** Key for kml track colour */
85 public static final String KEY_KML_TRACK_COLOUR = "prune.kmltrackcolour";
86 /** Key for autosaving settings */
87 public static final String KEY_AUTOSAVE_SETTINGS = "prune.autosavesettings";
88 /** Key for recently used files */
89 public static final String KEY_RECENT_FILES = "prune.recentfiles";
90 /** Key for estimation parameters */
91 public static final String KEY_ESTIMATION_PARAMS = "prune.estimationparams";
92 /** Key for 3D exaggeration factor */
93 public static final String KEY_HEIGHT_EXAGGERATION = "prune.heightexaggeration";
94 /** Key for terrain grid size */
95 public static final String KEY_TERRAIN_GRID_SIZE = "prune.terraingridsize";
96 /** Key for altitude tolerance */
97 public static final String KEY_ALTITUDE_TOLERANCE = "prune.altitudetolerance";
100 /** Initialise the default properties */
103 _configValues = getDefaultProperties();
107 * Load the default configuration file
109 public static void loadDefaultFile()
111 if (DEFAULT_CONFIG_FILE.exists())
114 loadFile(DEFAULT_CONFIG_FILE);
117 catch (ConfigException ce) {} // ignore
119 if (HOME_CONFIG_FILE.exists())
122 loadFile(HOME_CONFIG_FILE);
124 catch (ConfigException ce) {} // ignore
130 * Load configuration from file
131 * @param inFile file to load
132 * @throws ConfigException if specified file couldn't be read
134 public static void loadFile(File inFile) throws ConfigException
136 // Start with default properties
137 Properties props = getDefaultProperties();
138 // Try to load the file into a properties object
139 boolean loadFailed = false;
140 FileInputStream fis = null;
143 fis = new FileInputStream(inFile);
146 catch (Exception e) {
150 if (fis != null) try {
153 catch (Exception e) {}
155 // Save all properties from file
156 _configValues.putAll(props);
157 _colourScheme.loadFromHex(_configValues.getProperty(KEY_COLOUR_SCHEME));
158 _pointColourer = ColourerFactory.createColourer(_configValues.getProperty(KEY_POINT_COLOURER));
159 _recentFiles = new RecentFileList(_configValues.getProperty(KEY_RECENT_FILES));
160 _unitSet = UnitSetLibrary.getUnitSet(_configValues.getProperty(KEY_UNITSET_KEY));
161 // Adjust map source index if necessary
165 throw new ConfigException();
167 // Store location of successfully loaded config file
168 _configFile = inFile;
172 * @return Properties object containing default values
174 private static Properties getDefaultProperties()
176 Properties props = new Properties();
178 props.put(KEY_GPS_DEVICE, "usb:");
179 props.put(KEY_GPS_FORMAT, "garmin");
180 props.put(KEY_POVRAY_FONT, "crystal.ttf"); // alternative: DejaVuSans-Bold.ttf
181 props.put(KEY_SHOW_MAP, "0"); // hide by default
182 props.put(KEY_EXIFTOOL_PATH, "exiftool");
183 props.put(KEY_GNUPLOT_PATH, "gnuplot");
184 props.put(KEY_GPSBABEL_PATH, "gpsbabel");
185 props.put(KEY_IMPORT_FILE_FORMAT, "-1"); // no file format selected
186 props.put(KEY_KMZ_IMAGE_SIZE, "240");
187 props.put(KEY_AUTOSAVE_SETTINGS, "0"); // autosave false by default
188 props.put(KEY_UNITSET_KEY, "unitset.kilometres"); // metric by default
189 props.put(KEY_HEIGHT_EXAGGERATION, "100"); // 100%, no exaggeration
190 props.put(KEY_TERRAIN_GRID_SIZE, "50");
191 props.put(KEY_ALTITUDE_TOLERANCE, "0"); // 0, all exact as before
196 * Adjust the index of the selected map
197 * (only required if config was loaded from a previous version of GpsPrune)
199 private static void adjustSelectedMap()
201 int sourceNum = getConfigInt(Config.KEY_MAPSOURCE_INDEX);
202 int prevNumFixed = getConfigInt(Config.KEY_NUM_FIXED_MAPS);
203 // Number of fixed maps not specified in version <=13, default to 6
204 if (prevNumFixed == 0) prevNumFixed = 6;
205 int currNumFixed = MapSourceLibrary.getNumFixedSources();
206 // Only need to do something if the number has changed
207 if (currNumFixed != prevNumFixed && (sourceNum >= prevNumFixed || sourceNum >= currNumFixed))
209 sourceNum += (currNumFixed - prevNumFixed);
210 setConfigInt(Config.KEY_MAPSOURCE_INDEX, sourceNum);
212 setConfigInt(Config.KEY_NUM_FIXED_MAPS, currNumFixed);
216 * @param inString String to parse
217 * @return int value of String, or 0 if unparseable
219 private static int parseInt(String inString)
223 val = Integer.parseInt(inString);
225 catch (Exception e) {} // ignore, value stays zero
229 /** @return File from which config was loaded (or null) */
230 public static File getConfigFile()
236 * @return config Properties object to allow all config values to be saved
238 public static Properties getAllConfig()
240 // Update recently-used files
241 _configValues.setProperty(KEY_RECENT_FILES, _recentFiles.getConfigString());
242 return _configValues;
246 * @return the current colour scheme
248 public static ColourScheme getColourScheme()
250 return _colourScheme;
254 * @return the current point colourer, if any
256 public static PointColourer getPointColourer()
258 return _pointColourer;
262 * @return list of recently used files
264 public static RecentFileList getRecentFileList()
270 * Store the given configuration setting
271 * @param inKey key (from constants)
272 * @param inValue value as string
274 public static void setConfigString(String inKey, String inValue)
276 if (inValue == null || inValue.equals("")) {
277 _configValues.remove(inKey);
280 _configValues.put(inKey, inValue);
285 * Store the given configuration setting
286 * @param inKey key (from constants)
287 * @param inValue value as boolean
289 public static void setConfigBoolean(String inKey, boolean inValue)
291 if (inKey != null && !inKey.equals(""))
293 _configValues.put(inKey, (inValue?"1":"0"));
298 * Store the given configuration setting
299 * @param inKey key (from constants)
300 * @param inValue value as int
302 public static void setConfigInt(String inKey, int inValue)
304 if (inKey != null && !inKey.equals(""))
306 _configValues.put(inKey, "" + inValue);
311 * Get the given configuration setting as a String
313 * @return configuration setting as a String
315 public static String getConfigString(String inKey)
317 return _configValues.getProperty(inKey);
321 * Get the given configuration setting as a boolean
323 * @return configuration setting as a boolean (default to true)
325 public static boolean getConfigBoolean(String inKey)
327 String val = _configValues.getProperty(inKey);
328 return (val == null || val.equals("1"));
332 * Get the given configuration setting as an int
334 * @return configuration setting as an int
336 public static int getConfigInt(String inKey)
338 return parseInt(_configValues.getProperty(inKey));
342 * Check whether the given key corresponds to a boolean property
343 * @param inKey key to check
344 * @return true if corresponding property is boolean
346 public static boolean isKeyBoolean(String inKey)
348 // Only one boolean key so far (after metric flag was removed)
349 return inKey != null && (
350 inKey.equals(KEY_SHOW_MAP));
354 * Update the colour scheme property from the current settings
356 public static void updateColourScheme()
358 setConfigString(KEY_COLOUR_SCHEME, _colourScheme.toString());
362 * Update the point colourer from the given colourer
363 * @param inColourer point colourer object, or null
365 public static void updatePointColourer(PointColourer inColourer)
367 _pointColourer = inColourer;
368 setConfigString(KEY_POINT_COLOURER, ColourerFactory.PointColourerToString(_pointColourer));
372 * @return the current unit set
374 public static UnitSet getUnitSet() {
379 * @param inIndex index of unit set to select
381 public static void selectUnitSet(int inIndex)
383 _unitSet = UnitSetLibrary.getUnitSet(inIndex);
384 // Set name of set in config
385 setConfigString(KEY_UNITSET_KEY, _unitSet.getNameKey());