]> gitweb.fperrin.net Git - GpsPrune.git/blob - tim/prune/config/Config.java
d2d49c68d7ebb93485c9a428ab6c2fb3c40f3eec
[GpsPrune.git] / tim / prune / config / Config.java
1 package tim.prune.config;
2
3 import java.io.File;
4 import java.io.FileInputStream;
5 import java.util.Properties;
6
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;
13
14
15 /**
16  * Abstract class to hold application-wide configuration
17  */
18 public abstract class Config
19 {
20         /** File from which Config was loaded */
21         private static File _configFile = null;
22
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);
33
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");
37
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 window position */
67         public static final String KEY_WINDOW_BOUNDS = "prune.windowbounds";
68         /** Key for path to disk cache */
69         public static final String KEY_DISK_CACHE = "prune.diskcache";
70         /** Key for working online flag */
71         public static final String KEY_ONLINE_MODE = "prune.onlinemode";
72         /** Key for width of thumbnails in kmz */
73         public static final String KEY_KMZ_IMAGE_SIZE = "prune.kmzimagewidth";
74         /** Key for gpsbabel path */
75         public static final String KEY_GPSBABEL_PATH = "prune.gpsbabelpath";
76         /** Key for gnuplot path */
77         public static final String KEY_GNUPLOT_PATH = "prune.gnuplotpath";
78         /** Key for exiftool path */
79         public static final String KEY_EXIFTOOL_PATH = "prune.exiftoolpath";
80         /** Key for colour scheme */
81         public static final String KEY_COLOUR_SCHEME = "prune.colourscheme";
82         /** Key for point colourer */
83         public static final String KEY_POINT_COLOURER = "prune.pointcolourer";
84         /** Key for line width used for drawing */
85         public static final String KEY_LINE_WIDTH = "prune.linewidth";
86         /** Key for whether to use antialiasing or not */
87         public static final String KEY_ANTIALIAS = "prune.antialias";
88         /** Key for kml track colour */
89         public static final String KEY_KML_TRACK_COLOUR = "prune.kmltrackcolour";
90         /** Key for autosaving settings */
91         public static final String KEY_AUTOSAVE_SETTINGS = "prune.autosavesettings";
92         /** Key for recently used files */
93         public static final String KEY_RECENT_FILES = "prune.recentfiles";
94         /** Key for estimation parameters */
95         public static final String KEY_ESTIMATION_PARAMS = "prune.estimationparams";
96         /** Key for 3D exaggeration factor */
97         public static final String KEY_HEIGHT_EXAGGERATION = "prune.heightexaggeration";
98         /** Key for terrain grid size */
99         public static final String KEY_TERRAIN_GRID_SIZE = "prune.terraingridsize";
100         /** Key for altitude tolerance */
101         public static final String KEY_ALTITUDE_TOLERANCE = "prune.altitudetolerance";
102
103
104         /** Initialise the default properties */
105         static
106         {
107                 _configValues = getDefaultProperties();
108         }
109
110         /**
111          * Load the default configuration file
112          */
113         public static void loadDefaultFile()
114         {
115                 if (DEFAULT_CONFIG_FILE.exists())
116                 {
117                         try {
118                                 loadFile(DEFAULT_CONFIG_FILE);
119                                 return;
120                         }
121                         catch (ConfigException ce) {} // ignore
122                 }
123                 if (HOME_CONFIG_FILE.exists())
124                 {
125                         try {
126                                 loadFile(HOME_CONFIG_FILE);
127                         }
128                         catch (ConfigException ce) {} // ignore
129                 }
130         }
131
132
133         /**
134          * Load configuration from file
135          * @param inFile file to load
136          * @throws ConfigException if specified file couldn't be read
137          */
138         public static void loadFile(File inFile) throws ConfigException
139         {
140                 // Start with default properties
141                 Properties props = getDefaultProperties();
142                 // Try to load the file into a properties object
143                 boolean loadFailed = false;
144                 FileInputStream fis = null;
145                 try
146                 {
147                         fis = new FileInputStream(inFile);
148                         props.load(fis);
149                 }
150                 catch (Exception e) {
151                         loadFailed = true;
152                 }
153                 finally {
154                         if (fis != null) try {
155                                 fis.close();
156                         }
157                         catch (Exception e) {}
158                 }
159                 // Save all properties from file
160                 _configValues.putAll(props);
161                 _colourScheme.loadFromHex(_configValues.getProperty(KEY_COLOUR_SCHEME));
162                 _pointColourer = ColourerFactory.createColourer(_configValues.getProperty(KEY_POINT_COLOURER));
163                 _recentFiles = new RecentFileList(_configValues.getProperty(KEY_RECENT_FILES));
164                 _unitSet = UnitSetLibrary.getUnitSet(_configValues.getProperty(KEY_UNITSET_KEY));
165                 // Adjust map source index if necessary
166                 adjustSelectedMap();
167
168                 if (loadFailed) {
169                         throw new ConfigException();
170                 }
171                 // Store location of successfully loaded config file
172                 _configFile = inFile;
173         }
174
175         /**
176          * @return Properties object containing default values
177          */
178         private static Properties getDefaultProperties()
179         {
180                 Properties props = new Properties();
181                 // Fill in defaults
182                 props.put(KEY_GPS_DEVICE, "usb:");
183                 props.put(KEY_GPS_FORMAT, "garmin");
184                 props.put(KEY_POVRAY_FONT, "crystal.ttf"); // alternative: DejaVuSans-Bold.ttf
185                 props.put(KEY_SHOW_MAP, "0"); // hide by default
186                 props.put(KEY_EXIFTOOL_PATH, "exiftool");
187                 props.put(KEY_GNUPLOT_PATH, "gnuplot");
188                 props.put(KEY_GPSBABEL_PATH, "gpsbabel");
189                 props.put(KEY_IMPORT_FILE_FORMAT, "-1"); // no file format selected
190                 props.put(KEY_KMZ_IMAGE_SIZE, "240");
191                 props.put(KEY_ANTIALIAS, "1"); // antialias on by default
192                 props.put(KEY_AUTOSAVE_SETTINGS, "0"); // autosave false by default
193                 props.put(KEY_UNITSET_KEY, "unitset.kilometres"); // metric by default
194                 props.put(KEY_HEIGHT_EXAGGERATION, "100"); // 100%, no exaggeration
195                 props.put(KEY_TERRAIN_GRID_SIZE, "50");
196                 props.put(KEY_ALTITUDE_TOLERANCE, "0"); // 0, all exact as before
197                 return props;
198         }
199
200         /**
201          * Adjust the index of the selected map
202          * (only required if config was loaded from a previous version of GpsPrune)
203          */
204         private static void adjustSelectedMap()
205         {
206                 int sourceNum = getConfigInt(Config.KEY_MAPSOURCE_INDEX);
207                 int prevNumFixed = getConfigInt(Config.KEY_NUM_FIXED_MAPS);
208                 // Number of fixed maps not specified in version <=13, default to 6
209                 if (prevNumFixed == 0) prevNumFixed = 6;
210                 int currNumFixed = MapSourceLibrary.getNumFixedSources();
211                 // Only need to do something if the number has changed
212                 if (currNumFixed != prevNumFixed && (sourceNum >= prevNumFixed || sourceNum >= currNumFixed))
213                 {
214                         sourceNum += (currNumFixed - prevNumFixed);
215                         setConfigInt(Config.KEY_MAPSOURCE_INDEX, sourceNum);
216                 }
217                 setConfigInt(Config.KEY_NUM_FIXED_MAPS, currNumFixed);
218         }
219
220         /**
221          * @param inString String to parse
222          * @return int value of String, or 0 if unparseable
223          */
224         private static int parseInt(String inString)
225         {
226                 int val = 0;
227                 try {
228                         val = Integer.parseInt(inString);
229                 }
230                 catch (Exception e) {} // ignore, value stays zero
231                 return val;
232         }
233
234         /** @return File from which config was loaded (or null) */
235         public static File getConfigFile()
236         {
237                 return _configFile;
238         }
239
240         /**
241          * @return config Properties object to allow all config values to be saved
242          */
243         public static Properties getAllConfig()
244         {
245                 // Update recently-used files
246                 _configValues.setProperty(KEY_RECENT_FILES, _recentFiles.getConfigString());
247                 return _configValues;
248         }
249
250         /**
251          * @return the current colour scheme
252          */
253         public static ColourScheme getColourScheme()
254         {
255                 return _colourScheme;
256         }
257
258         /**
259          * @return the current point colourer, if any
260          */
261         public static PointColourer getPointColourer()
262         {
263                 return _pointColourer;
264         }
265
266         /**
267          * @return list of recently used files
268          */
269         public static RecentFileList getRecentFileList()
270         {
271                 return _recentFiles;
272         }
273
274         /**
275          * Store the given configuration setting
276          * @param inKey key (from constants)
277          * @param inValue value as string
278          */
279         public static void setConfigString(String inKey, String inValue)
280         {
281                 if (inValue == null || inValue.equals("")) {
282                         _configValues.remove(inKey);
283                 }
284                 else {
285                         _configValues.put(inKey, inValue);
286                 }
287         }
288
289         /**
290          * Store the given configuration setting
291          * @param inKey key (from constants)
292          * @param inValue value as boolean
293          */
294         public static void setConfigBoolean(String inKey, boolean inValue)
295         {
296                 if (inKey != null && !inKey.equals(""))
297                 {
298                         _configValues.put(inKey, (inValue?"1":"0"));
299                 }
300         }
301
302         /**
303          * Store the given configuration setting
304          * @param inKey key (from constants)
305          * @param inValue value as int
306          */
307         public static void setConfigInt(String inKey, int inValue)
308         {
309                 if (inKey != null && !inKey.equals(""))
310                 {
311                         _configValues.put(inKey, "" + inValue);
312                 }
313         }
314
315         /**
316          * Get the given configuration setting as a String
317          * @param inKey key
318          * @return configuration setting as a String
319          */
320         public static String getConfigString(String inKey)
321         {
322                 return _configValues.getProperty(inKey);
323         }
324
325         /**
326          * Get the given configuration setting as a boolean
327          * @param inKey key
328          * @return configuration setting as a boolean (default to true)
329          */
330         public static boolean getConfigBoolean(String inKey)
331         {
332                 String val = _configValues.getProperty(inKey);
333                 return (val == null || val.equals("1"));
334         }
335
336         /**
337          * Get the given configuration setting as an int
338          * @param inKey key
339          * @return configuration setting as an int
340          */
341         public static int getConfigInt(String inKey)
342         {
343                 return parseInt(_configValues.getProperty(inKey));
344         }
345
346         /**
347          * Check whether the given key corresponds to a boolean property
348          * @param inKey key to check
349          * @return true if corresponding property is boolean
350          */
351         public static boolean isKeyBoolean(String inKey)
352         {
353                 return inKey != null && (
354                         inKey.equals(KEY_SHOW_MAP) || inKey.equals(KEY_AUTOSAVE_SETTINGS) || inKey.equals(KEY_ONLINE_MODE)
355                         || inKey.equals(KEY_ANTIALIAS));
356         }
357
358         /**
359          * Update the colour scheme property from the current settings
360          */
361         public static void updateColourScheme()
362         {
363                 setConfigString(KEY_COLOUR_SCHEME, _colourScheme.toString());
364         }
365
366         /**
367          * Update the point colourer from the given colourer
368          * @param inColourer point colourer object, or null
369          */
370         public static void updatePointColourer(PointColourer inColourer)
371         {
372                 _pointColourer = inColourer;
373                 setConfigString(KEY_POINT_COLOURER, ColourerFactory.PointColourerToString(_pointColourer));
374         }
375
376         /**
377          * @return the current unit set
378          */
379         public static UnitSet getUnitSet() {
380                 return _unitSet;
381         }
382
383         /**
384          * @param inIndex index of unit set to select
385          */
386         public static void selectUnitSet(int inIndex)
387         {
388                 _unitSet = UnitSetLibrary.getUnitSet(inIndex);
389                 // Set name of set in config
390                 setConfigString(KEY_UNITSET_KEY, _unitSet.getNameKey());
391         }
392 }