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