updates for 0.9.4
[debian/openrocket] / src / net / sf / openrocket / util / Prefs.java
index d5b98b4dd3dfc4bf0456845ae8d6ec1aa8ca1584..f4f26133d81beb2d3fe35b4e291ae04a6746673b 100644 (file)
@@ -8,14 +8,17 @@ import java.io.File;
 import java.io.IOException;
 import java.io.InputStream;
 import java.util.HashMap;
+import java.util.HashSet;
 import java.util.Map;
 import java.util.MissingResourceException;
 import java.util.Properties;
+import java.util.Set;
 import java.util.prefs.BackingStoreException;
 import java.util.prefs.Preferences;
 
 import net.sf.openrocket.database.Databases;
 import net.sf.openrocket.document.Simulation;
+import net.sf.openrocket.gui.main.ExceptionHandler;
 import net.sf.openrocket.material.Material;
 import net.sf.openrocket.rocketcomponent.BodyComponent;
 import net.sf.openrocket.rocketcomponent.FinSet;
@@ -53,13 +56,16 @@ public class Prefs {
        
        private static final String BUILD_VERSION;
        private static final String BUILD_SOURCE;
+       public static final String DEFAULT_BUILD_SOURCE = "default";
+       private static final boolean DEFAULT_CHECK_UPDATES;
        
        static {
                try {
                        InputStream is = ClassLoader.getSystemResourceAsStream("build.properties");
                        if (is == null) {
                                throw new MissingResourceException(
-                                               "build.properties not found, distribution built wrong",
+                                               "build.properties not found, distribution built wrong" + 
+                                               "   path:"+System.getProperty("java.class.path"),
                                                "build.properties", "build.version");
                        }
                        
@@ -76,6 +82,12 @@ public class Prefs {
                        
                        BUILD_SOURCE = props.getProperty("build.source");
                        
+                       String value = props.getProperty("build.checkupdates");
+                       if (value != null)
+                               DEFAULT_CHECK_UPDATES = Boolean.parseBoolean(value);
+                       else
+                               DEFAULT_CHECK_UPDATES = true;
+                       
                } catch (IOException e) {
                        throw new MissingResourceException(
                                        "Error reading build.properties",
@@ -96,6 +108,9 @@ public class Prefs {
        public static final String EXPORT_EVENT_COMMENTS = "ExportEventComments";
        public static final String EXPORT_COMMENT_CHARACTER = "ExportCommentCharacter";
        
+       public static final String PLOT_SHOW_POINTS = "ShowPlotPoints";
+       
+       private static final String CHECK_UPDATES = "CheckUpdates";
        
        /**
         * Node to this application's preferences.
@@ -146,13 +161,18 @@ public class Prefs {
        }
        
        
-       private static final Material DEFAULT_LINE_MATERIAL = 
-               Databases.findMaterial(Material.Type.LINE, "Elastic cord (round 2mm, 1/16 in)", 0.0018);
-       private static final Material DEFAULT_SURFACE_MATERIAL = 
-               Databases.findMaterial(Material.Type.SURFACE, "Ripstop nylon", 0.067);
-       private static final Material DEFAULT_BULK_MATERIAL = 
-               Databases.findMaterial(Material.Type.BULK, "Cardboard", 680);
-
+       /*
+        * Within a holder class so they will load only when needed.
+        */
+       private static class DefaultMaterialHolder {
+               private static final Material DEFAULT_LINE_MATERIAL = 
+                       Databases.findMaterial(Material.Type.LINE, "Elastic cord (round 2mm, 1/16 in)", 
+                                       0.0018, false);
+               private static final Material DEFAULT_SURFACE_MATERIAL = 
+                       Databases.findMaterial(Material.Type.SURFACE, "Ripstop nylon", 0.067, false);
+               private static final Material DEFAULT_BULK_MATERIAL = 
+                       Databases.findMaterial(Material.Type.BULK, "Cardboard", 680, false);
+       }
        
        //////////////////////
        
@@ -167,6 +187,16 @@ public class Prefs {
        }
        
        
+       public static String getUniqueID() {
+               String id = PREFNODE.get("id", null);
+               if (id == null) {
+                       id = UniqueID.generateHashedID();
+                       PREFNODE.put("id", id);
+               }
+               return id;
+       }
+       
+       
        
        public static void storeVersion() {
                PREFNODE.put("OpenRocketVersion", getVersion());
@@ -225,6 +255,16 @@ public class Prefs {
 
        
        
+       public static boolean getCheckUpdates() {
+               return PREFNODE.getBoolean(CHECK_UPDATES, DEFAULT_CHECK_UPDATES);
+       }
+       
+       public static void setCheckUpdates(boolean check) {
+               PREFNODE.putBoolean(CHECK_UPDATES, check);
+               storeVersion();
+       }
+       
+       
        //////////////////
        
        public static File getDefaultDirectory() {
@@ -335,7 +375,7 @@ public class Prefs {
                String material = get("componentMaterials", componentClass, null);
                if (material != null) {
                        try {
-                               Material m = Material.fromStorableString(material);
+                               Material m = Material.fromStorableString(material, false);
                                if (m.getType() == type)
                                        return m;
                        } catch (IllegalArgumentException ignore) { }
@@ -343,11 +383,11 @@ public class Prefs {
                
                switch (type) {
                case LINE:
-                       return DEFAULT_LINE_MATERIAL;
+                       return DefaultMaterialHolder.DEFAULT_LINE_MATERIAL;
                case SURFACE:
-                       return DEFAULT_SURFACE_MATERIAL;
+                       return DefaultMaterialHolder.DEFAULT_SURFACE_MATERIAL;
                case BULK:
-                       return DEFAULT_BULK_MATERIAL;
+                       return DefaultMaterialHolder.DEFAULT_BULK_MATERIAL;
                }
                throw new IllegalArgumentException("Unknown material type: "+type);
        }
@@ -466,8 +506,7 @@ public class Prefs {
                        }
                        
                } catch (BackingStoreException e) {
-                       System.err.println("BackingStoreException:");
-                       e.printStackTrace();
+                       ExceptionHandler.handleErrorCondition(e);
                }
        }
        
@@ -485,6 +524,101 @@ public class Prefs {
        
        
        
+       ////  Material storage
+
+       
+       /**
+        * Add a user-defined material to the preferences.  The preferences are
+        * first checked for an existing material matching the provided one using
+        * {@link Material#equals(Object)}.
+        * 
+        * @param m             the material to add.
+        */
+       public static void addUserMaterial(Material m) {
+               Preferences prefs = PREFNODE.node("userMaterials");
+               
+               
+               // Check whether material already exists
+               if (getUserMaterials().contains(m)) {
+                       return;
+               }
+
+               // Add material using next free key (key is not used when loading)
+               String mat = m.toStorableString();
+               for (int i = 0; ; i++) {
+                       String key = "material" + i;
+                       if (prefs.get(key, null) == null) {
+                               prefs.put(key, mat);
+                               return;
+                       }
+               }
+       }
+
+       
+       /**
+        * Remove a user-defined material from the preferences.  The matching is performed
+        * using {@link Material#equals(Object)}.
+        * 
+        * @param m             the material to remove.
+        */
+       public static void removeUserMaterial(Material m) {
+               Preferences prefs = PREFNODE.node("userMaterials");
+               
+               try {
+                       
+                       // Iterate through materials and remove all keys with a matching material
+                       for (String key: prefs.keys()) {
+                               String value = prefs.get(key, null);
+                               try {
+                                       
+                                       Material existing = Material.fromStorableString(value, true);
+                                       if (existing.equals(m)) {
+                                               prefs.remove(key);
+                                       }
+                                       
+                               } catch (IllegalArgumentException ignore) { }
+                               
+                       }
+                       
+               } catch (BackingStoreException e) {
+                       throw new IllegalStateException("Cannot read preferences!", e);
+               }
+       }
+       
+       
+       /**
+        * Return a set of all user-defined materials in the preferences.  The materials
+        * are created marked as user-defined.
+        * 
+        * @return      a set of all user-defined materials.
+        */
+       public static Set<Material> getUserMaterials() {
+               Preferences prefs = PREFNODE.node("userMaterials");
+               
+               HashSet<Material> materials = new HashSet<Material>();
+               try {
+                       
+                       for (String key: prefs.keys()) {
+                               String value = prefs.get(key, null);
+                               try {
+                                       
+                                       Material m = Material.fromStorableString(value, true);
+                                       materials.add(m);
+                                       
+                               } catch (IllegalArgumentException e) { 
+                                       System.err.println("Illegal material string " + value);
+                               }
+                               
+                       }
+                       
+               } catch (BackingStoreException e) {
+                       throw new IllegalStateException("Cannot read preferences!", e);
+               }
+               
+               return materials;
+       }
+       
+       
        ////  Helper methods
        
        private static String get(String directory,