Merged l10n branch to trunk
[debian/openrocket] / src / net / sf / openrocket / util / Prefs.java
index 215389ab4d1d37109098a133f7a4f83be41582eb..047db50c7ae3c1a65599be088552aacb9a87168b 100644 (file)
@@ -22,6 +22,7 @@ import net.sf.openrocket.arch.SystemInfo;
 import net.sf.openrocket.database.Databases;
 import net.sf.openrocket.document.Simulation;
 import net.sf.openrocket.gui.main.ExceptionHandler;
+import net.sf.openrocket.gui.print.PrintSettings;
 import net.sf.openrocket.l10n.Translator;
 import net.sf.openrocket.logging.LogHelper;
 import net.sf.openrocket.material.Material;
@@ -200,7 +201,7 @@ public class Prefs {
         */
        private static class DefaultMaterialHolder {
                private static final Translator trans = Application.getTranslator();
-
+               
                //// Elastic cord (round 2mm, 1/16 in)
                private static final Material DEFAULT_LINE_MATERIAL =
                                Databases.findMaterial(Material.Type.LINE, trans.get("Databases.materials.Elasticcordround2mm"),
@@ -312,6 +313,47 @@ public class Prefs {
                storeVersion();
        }
        
+       
+       /**
+        * Retrieve an enum value from the user preferences.
+        * 
+        * @param <T>   the enum type
+        * @param key   the key
+        * @param def   the default value, cannot be null
+        * @return              the value in the preferences, or the default value
+        */
+       public static <T extends Enum<T>> T getEnum(String key, T def) {
+               if (def == null) {
+                       throw new BugException("Default value cannot be null");
+               }
+               
+               String value = getString(key, null);
+               if (value == null) {
+                       return def;
+               }
+               
+               try {
+                       return Enum.valueOf(def.getDeclaringClass(), value);
+               } catch (IllegalArgumentException e) {
+                       return def;
+               }
+       }
+       
+       /**
+        * Store an enum value to the user preferences.
+        * 
+        * @param key           the key
+        * @param value         the value to store, or null to remove the value
+        */
+       public static void putEnum(String key, Enum<?> value) {
+               if (value == null) {
+                       putString(key, null);
+               } else {
+                       putString(key, value.name());
+               }
+       }
+       
+       
        /**
         * Return a boolean preference.
         * 
@@ -449,6 +491,26 @@ public class Prefs {
                if (color == null)
                        return Color.BLACK;
                
+               Color clr = parseColor(color);
+               if (clr != null) {
+                       return clr;
+               } else {
+                       return Color.BLACK;
+               }
+       }
+       
+       public static void setDefaultColor(Class<? extends RocketComponent> c, Color color) {
+               if (color == null)
+                       return;
+               set("componentColors", c, stringifyColor(color));
+       }
+       
+       
+       private static Color parseColor(String color) {
+               if (color == null) {
+                       return null;
+               }
+               
                String[] rgb = color.split(",");
                if (rgb.length == 3) {
                        try {
@@ -459,17 +521,17 @@ public class Prefs {
                        } catch (NumberFormatException ignore) {
                        }
                }
-               
-               return Color.BLACK;
+               return null;
        }
        
-       public static void setDefaultColor(Class<? extends RocketComponent> c, Color color) {
-               if (color == null)
-                       return;
+       
+       private static String stringifyColor(Color color) {
                String string = color.getRed() + "," + color.getGreen() + "," + color.getBlue();
-               set("componentColors", c, string);
+               return string;
        }
        
+       
+
        public static Color getMotorBorderColor() {
                // TODO: MEDIUM:  Motor color (settable?)
                return new Color(0, 0, 0, 200);
@@ -629,6 +691,35 @@ public class Prefs {
        }
        
        
+       ////  Printing
+       
+       public static PrintSettings getPrintSettings() {
+               PrintSettings settings = new PrintSettings();
+               Color c;
+               
+               c = parseColor(getString("print.template.fillColor", null));
+               if (c != null) {
+                       settings.setTemplateFillColor(c);
+               }
+               
+               c = parseColor(getString("print.template.borderColor", null));
+               if (c != null) {
+                       settings.setTemplateBorderColor(c);
+               }
+               
+               settings.setPaperSize(getEnum("print.paper.size", settings.getPaperSize()));
+               settings.setPaperOrientation(getEnum("print.paper.orientation", settings.getPaperOrientation()));
+               
+               return settings;
+       }
+       
+       public static void setPrintSettings(PrintSettings settings) {
+               putString("print.template.fillColor", stringifyColor(settings.getTemplateFillColor()));
+               putString("print.template.borderColor", stringifyColor(settings.getTemplateBorderColor()));
+               putEnum("print.paper.size", settings.getPaperSize());
+               putEnum("print.paper.orientation", settings.getPaperOrientation());
+       }
+       
        ////  Background flight data computation
        
        public static boolean computeFlightInBackground() {