]> gitweb.fperrin.net Git - GpsPrune.git/blobdiff - tim/prune/config/Config.java
Version 14, October 2012
[GpsPrune.git] / tim / prune / config / Config.java
index 14231383fc03d860b4eea332575837e6cede4a0d..5a95bebdebc62d52c6da4f52ec7391026781c206 100644 (file)
@@ -4,6 +4,10 @@ import java.io.File;
 import java.io.FileInputStream;
 import java.util.Properties;
 
+import tim.prune.data.RecentFileList;
+import tim.prune.data.UnitSet;
+import tim.prune.data.UnitSetLibrary;
+
 
 /**
  * Abstract class to hold application-wide configuration
@@ -14,12 +18,17 @@ public abstract class Config
        private static File _configFile = null;
 
        /** Hashtable containing all config values */
-       private static Properties _configValues = new Properties();
+       private static Properties _configValues = null;
        /** Colour scheme object is also part of config */
        private static ColourScheme _colourScheme = new ColourScheme();
+       /** Recently-used file list */
+       private static RecentFileList _recentFiles = new RecentFileList();
+       /** Current unit set */
+       private static UnitSet _unitSet = UnitSetLibrary.getUnitSet(null);
 
        /** Default config file */
-       private static final File DEFAULT_CONFIG_FILE = new File(".pruneconfig");
+       public static final File DEFAULT_CONFIG_FILE = new File(".pruneconfig");
+       public static final File HOME_CONFIG_FILE = new File(System.getProperty("user.home"), ".pruneconfig");
 
        /** Key for track directory */
        public static final String KEY_TRACK_DIR = "prune.trackdirectory";
@@ -35,10 +44,12 @@ public abstract class Config
        public static final String KEY_GPS_FORMAT = "prune.gpsformat";
        /** Key for Povray font */
        public static final String KEY_POVRAY_FONT = "prune.povrayfont";
-       /** Key for metric/imperial */
-       public static final String KEY_METRIC_UNITS = "prune.metricunits";
+       /** Key for the selected unit set */
+       public static final String KEY_UNITSET_KEY  = "prune.unitsetkey";
        /** Key for index of map source */
        public static final String KEY_MAPSOURCE_INDEX = "prune.mapsource";
+       /** Key for number of fixed map sources */
+       public static final String KEY_NUM_FIXED_MAPS = "prune.numfixedmapsources";
        /** Key for String containing custom map sources */
        public static final String KEY_MAPSOURCE_LIST = "prune.mapsourcelist";
        /** Key for show map flag */
@@ -59,20 +70,42 @@ public abstract class Config
        public static final String KEY_EXIFTOOL_PATH = "prune.exiftoolpath";
        /** Key for colour scheme */
        public static final String KEY_COLOUR_SCHEME = "prune.colourscheme";
+       /** Key for line width used for drawing */
+       public static final String KEY_LINE_WIDTH = "prune.linewidth";
        /** Key for kml track colour */
        public static final String KEY_KML_TRACK_COLOUR = "prune.kmltrackcolour";
+       /** Key for autosaving settings */
+       public static final String KEY_AUTOSAVE_SETTINGS = "prune.autosavesettings";
+       /** Key for recently used files */
+       public static final String KEY_RECENT_FILES = "prune.recentfiles";
+
 
+       /** Initialise the default properties */
+       static
+       {
+               _configValues = getDefaultProperties();
+       }
 
        /**
         * Load the default configuration file
         */
        public static void loadDefaultFile()
        {
-               try
+               if (DEFAULT_CONFIG_FILE.exists())
                {
-                       loadFile(DEFAULT_CONFIG_FILE);
+                       try {
+                               loadFile(DEFAULT_CONFIG_FILE);
+                               return;
+                       }
+                       catch (ConfigException ce) {} // ignore
+               }
+               if (HOME_CONFIG_FILE.exists())
+               {
+                       try {
+                               loadFile(HOME_CONFIG_FILE);
+                       }
+                       catch (ConfigException ce) {} // ignore
                }
-               catch (ConfigException ce) {} // ignore
        }
 
 
@@ -105,6 +138,9 @@ public abstract class Config
                // Save all properties from file
                _configValues.putAll(props);
                _colourScheme.loadFromHex(_configValues.getProperty(KEY_COLOUR_SCHEME));
+               _recentFiles = new RecentFileList(_configValues.getProperty(KEY_RECENT_FILES));
+               _unitSet = UnitSetLibrary.getUnitSet(_configValues.getProperty(KEY_UNITSET_KEY));
+
                if (loadFailed) {
                        throw new ConfigException();
                }
@@ -128,6 +164,8 @@ public abstract class Config
                props.put(KEY_GPSBABEL_PATH, "gpsbabel");
                props.put(KEY_KMZ_IMAGE_WIDTH, "240");
                props.put(KEY_KMZ_IMAGE_HEIGHT, "240");
+               props.put(KEY_AUTOSAVE_SETTINGS, "0"); // autosave false by default
+               props.put(KEY_UNITSET_KEY, "unitset.kilometres"); // metric by default
                return props;
        }
 
@@ -156,6 +194,8 @@ public abstract class Config
         */
        public static Properties getAllConfig()
        {
+               // Update recently-used files
+               _configValues.setProperty(KEY_RECENT_FILES, _recentFiles.getConfigString());
                return _configValues;
        }
 
@@ -167,6 +207,14 @@ public abstract class Config
                return _colourScheme;
        }
 
+       /**
+        * @return list of recently used files
+        */
+       public static RecentFileList getRecentFileList()
+       {
+               return _recentFiles;
+       }
+
        /**
         * Store the given configuration setting
         * @param inKey key (from constants)
@@ -246,9 +294,9 @@ public abstract class Config
         */
        public static boolean isKeyBoolean(String inKey)
        {
-               // Only two boolean keys so far
+               // Only one boolean key so far (after metric flag was removed)
                return inKey != null && (
-                       inKey.equals(KEY_METRIC_UNITS) || inKey.equals(KEY_SHOW_MAP));
+                       inKey.equals(KEY_SHOW_MAP));
        }
 
        /**
@@ -258,4 +306,18 @@ public abstract class Config
        {
                setConfigString(KEY_COLOUR_SCHEME, _colourScheme.toString());
        }
+
+       /**
+        * @return the current unit set
+        */
+       public static UnitSet getUnitSet() {
+               return _unitSet;
+       }
+
+       public static void selectUnitSet(int inIndex)
+       {
+               _unitSet = UnitSetLibrary.getUnitSet(inIndex);
+               // Set name of set in config
+               setConfigString(KEY_UNITSET_KEY, _unitSet.getNameKey());
+       }
 }