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