]> gitweb.fperrin.net Git - GpsPrune.git/blob - tim/prune/Config.java
Version 6, October 2008
[GpsPrune.git] / tim / prune / Config.java
1 package tim.prune;
2
3 import java.io.File;
4 import java.io.FileInputStream;
5 import java.util.Properties;
6
7 /**
8  * Abstract class to hold application-wide configuration
9  */
10 public abstract class Config
11 {
12         /** Working directory for loading and saving */
13         private static File _workingDir = null;
14         /** Default language */
15         private static String _langCode = null;
16         /** GPS device name */
17         private static String _gpsDevice = null;
18         /** GPS format name */
19         private static String _gpsFormat = null;
20         /** Font to use for povray */
21         private static String _povrayFont = null;
22         /** True to use metric units */
23         private static boolean _metricUnits = true;
24
25
26         /** Default config file */
27         private static final File DEFAULT_CONFIG_FILE = new File(".pruneconfig");
28
29         /** Key for working directory */
30         private static final String KEY_WORKING_DIR = "prune.directory";
31         /** Key for language code */
32         private static final String KEY_LANGUAGE_CODE = "prune.languagecode";
33         /** Key for GPS device */
34         private static final String KEY_GPS_DEVICE = "prune.gpsdevice";
35         /** Key for GPS format */
36         private static final String KEY_GPS_FORMAT = "prune.gpsformat";
37         /** Key for Povray font */
38         private static final String KEY_POVRAY_FONT = "prune.povrayfont";
39         /** Key for metric/imperial */
40         private static final String KEY_METRIC_UNITS = "prune.metricunits";
41         // TODO: Save config file location so save possible
42
43
44         /**
45          * @return working directory for loading and saving
46          */
47         public static File getWorkingDirectory()
48         {
49                 return _workingDir;
50         }
51
52         /**
53          * @param inDirectory working directory to use
54          */
55         public static void setWorkingDirectory(File inDirectory)
56         {
57                 _workingDir = inDirectory;
58         }
59
60         /**
61          * Load the default configuration file
62          */
63         public static void loadDefaultFile()
64         {
65                 try
66                 {
67                         loadFile(DEFAULT_CONFIG_FILE);
68                 }
69                 catch (ConfigException ce) {} // ignore
70         }
71
72
73         /**
74          * Load configuration from file
75          * @param inFile file to load
76          */
77         public static void loadFile(File inFile) throws ConfigException
78         {
79                 // Start with default properties
80                 Properties props = getDefaultProperties();
81                 // Try to load the file into a properties object
82                 boolean loadFailed = false;
83                 try
84                 {
85                         props.load(new FileInputStream(inFile));
86                 }
87                 catch (Exception e)
88                 {
89                         loadFailed = true;
90                 }
91                 // Save the properties we know about, ignore the rest
92                 _langCode = props.getProperty(KEY_LANGUAGE_CODE);
93                 String dir = props.getProperty(KEY_WORKING_DIR);
94                 if (dir != null) {setWorkingDirectory(new File(dir));}
95                 _gpsDevice = props.getProperty(KEY_GPS_DEVICE);
96                 _gpsFormat = props.getProperty(KEY_GPS_FORMAT);
97                 _povrayFont = props.getProperty(KEY_POVRAY_FONT);
98                 String useMetric = props.getProperty(KEY_METRIC_UNITS);
99                 _metricUnits = (useMetric == null || useMetric.equals("") || useMetric.toLowerCase().equals("y"));
100                 if (loadFailed) {
101                         throw new ConfigException();
102                 }
103         }
104
105         /**
106          * @return Properties object containing default values
107          */
108         private static Properties getDefaultProperties()
109         {
110                 Properties props = new Properties();
111                 // Fill in defaults
112                 props.put(KEY_GPS_DEVICE, "usb:");
113                 props.put(KEY_GPS_FORMAT, "garmin");
114                 props.put(KEY_POVRAY_FONT, "crystal.ttf"); // alternative: DejaVuSans-Bold.ttf
115                 return props;
116         }
117
118         /** @return language code */
119         public static String getLanguageCode()
120         {
121                 return _langCode;
122         }
123
124         /** @return gps device */
125         public static String getGpsDevice()
126         {
127                 return _gpsDevice;
128         }
129
130         /** @return gps format */
131         public static String getGpsFormat()
132         {
133                 return _gpsFormat;
134         }
135
136         /** @return povray font */
137         public static String getPovrayFont()
138         {
139                 return _povrayFont;
140         }
141
142         /** @return true to use metric units */
143         public static boolean getUseMetricUnits()
144         {
145                 return _metricUnits;
146         }
147 }