altoslib/altosui: begin moving preferences "backend" into interface
authorMike Beattie <mike@ethernal.org>
Thu, 13 Sep 2012 12:25:49 +0000 (00:25 +1200)
committerMike Beattie <mike@ethernal.org>
Fri, 14 Sep 2012 05:51:18 +0000 (17:51 +1200)
Signed-off-by: Mike Beattie <mike@ethernal.org>
altoslib/AltosPreferences.java
altoslib/AltosPreferencesBackend.java [new file with mode: 0644]
altosui/AltosUIPreferences.java
altosui/AltosUIPreferencesBackend.java [new file with mode: 0644]

index 0e7b2bd3848f3d60e2c48613df337c53f8382742..ef30f8e92fd81549ddb016ef1783c6610725435b 100644 (file)
@@ -19,11 +19,10 @@ package org.altusmetrum.AltosLib;
 
 import java.io.*;
 import java.util.*;
-import java.util.prefs.*;
 import javax.swing.filechooser.FileSystemView;
 
 public class AltosPreferences {
-       public static Preferences preferences;
+       public static AltosPreferencesBackend backend = null;
 
        /* logdir preference name */
        public final static String logdirPreference = "LOGDIR";
@@ -100,13 +99,10 @@ public class AltosPreferences {
        public static AltosFrequency[] load_common_frequencies() {
                AltosFrequency[] frequencies = null;
                boolean existing = false;
-               try {
-                       existing = preferences.nodeExists(common_frequencies_node_name);
-               } catch (BackingStoreException be) {
-                       existing = false;
-               }
+               existing = backend.nodeExists(common_frequencies_node_name);
+
                if (existing) {
-                       Preferences     node = preferences.node(common_frequencies_node_name);
+                       AltosPreferencesBackend node = backend.node(common_frequencies_node_name);
                        int             count = node.getInt(frequency_count, 0);
 
                        frequencies = new AltosFrequency[count];
@@ -115,7 +111,7 @@ public class AltosPreferences {
                                String  description;
 
                                frequency = node.getDouble(String.format(frequency_format, i), 0.0);
-                               description = node.get(String.format(description_format, i), null);
+                               description = node.getString(String.format(description_format, i), null);
                                frequencies[i] = new AltosFrequency(frequency, description);
                        }
                } else {
@@ -129,12 +125,12 @@ public class AltosPreferences {
        }
 
        public static void save_common_frequencies(AltosFrequency[] frequencies) {
-               Preferences     node = preferences.node(common_frequencies_node_name);
+               AltosPreferencesBackend node = backend.node(common_frequencies_node_name);
 
                node.putInt(frequency_count, frequencies.length);
                for (int i = 0; i < frequencies.length; i++) {
                        node.putDouble(String.format(frequency_format, i), frequencies[i].frequency);
-                       node.put(String.format(description_format, i), frequencies[i].description);
+                       node.putString(String.format(description_format, i), frequencies[i].description);
                }
        }
        public static int launcher_serial;
@@ -142,10 +138,10 @@ public class AltosPreferences {
        public static int launcher_channel;
 
        public static void init() {
-               preferences = Preferences.userRoot().node("/org/altusmetrum/altosui");
+               //preferences = Preferences.userRoot().node("/org/altusmetrum/altosui");
 
                /* Initialize logdir from preferences */
-               String logdir_string = preferences.get(logdirPreference, null);
+               String logdir_string = backend.getString(logdirPreference, null);
                if (logdir_string != null)
                        logdir = new File(logdir_string);
                else {
@@ -162,17 +158,17 @@ public class AltosPreferences {
 
                telemetries = new Hashtable<Integer,Integer>();
 
-               voice = preferences.getBoolean(voicePreference, true);
+               voice = backend.getBoolean(voicePreference, true);
 
-               callsign = preferences.get(callsignPreference,"N0CALL");
+               callsign = backend.getString(callsignPreference,"N0CALL");
 
-               scanning_telemetry = preferences.getInt(scanningTelemetryPreference,(1 << AltosLib.ao_telemetry_standard));
+               scanning_telemetry = backend.getInt(scanningTelemetryPreference,(1 << AltosLib.ao_telemetry_standard));
 
-               launcher_serial = preferences.getInt(launcherSerialPreference, 0);
+               launcher_serial = backend.getInt(launcherSerialPreference, 0);
 
-               launcher_channel = preferences.getInt(launcherChannelPreference, 0);
+               launcher_channel = backend.getInt(launcherChannelPreference, 0);
 
-               String firmwaredir_string = preferences.get(firmwaredirPreference, null);
+               String firmwaredir_string = backend.getString(firmwaredirPreference, null);
                if (firmwaredir_string != null)
                        firmwaredir = new File(firmwaredir_string);
                else
@@ -180,65 +176,53 @@ public class AltosPreferences {
 
                common_frequencies = load_common_frequencies();
 
-               AltosConvert.imperial_units = preferences.getBoolean(unitsPreference, false);
+               AltosConvert.imperial_units = backend.getBoolean(unitsPreference, false);
        }
 
        static { init(); }
 
        public static void flush_preferences() {
-               try {
-                       preferences.flush();
-               } catch (BackingStoreException ee) {
-/*
-                       if (component != null)
-                               JOptionPane.showMessageDialog(component,
-                                                             preferences.absolutePath(),
-                                                             "Cannot save prefernces",
-                                                             JOptionPane.ERROR_MESSAGE);
-                       else
-*/
-                               System.err.printf("Cannot save preferences\n");
-               }
+               backend.flush();
        }
 
        public static void set_logdir(File new_logdir) {
-               synchronized (preferences) {
+               synchronized (backend) {
                        logdir = new_logdir;
                        mapdir = new File(logdir, "maps");
                        if (!mapdir.exists())
                                mapdir.mkdirs();
-                       preferences.put(logdirPreference, logdir.getPath());
+                       backend.putString(logdirPreference, logdir.getPath());
                        flush_preferences();
                }
        }
 
        public static File logdir() {
-               synchronized (preferences) {
+               synchronized (backend) {
                        return logdir;
                }
        }
 
        public static File mapdir() {
-               synchronized (preferences) {
+               synchronized (backend) {
                        return mapdir;
                }
        }
 
        public static void set_frequency(int serial, double new_frequency) {
-               synchronized (preferences) {
+               synchronized (backend) {
                        frequencies.put(serial, new_frequency);
-                       preferences.putDouble(String.format(frequencyPreferenceFormat, serial), new_frequency);
+                       backend.putDouble(String.format(frequencyPreferenceFormat, serial), new_frequency);
                        flush_preferences();
                }
        }
 
        public static double frequency(int serial) {
-               synchronized (preferences) {
+               synchronized (backend) {
                        if (frequencies.containsKey(serial))
                                return frequencies.get(serial);
-                       double frequency = preferences.getDouble(String.format(frequencyPreferenceFormat, serial), 0);
+                       double frequency = backend.getDouble(String.format(frequencyPreferenceFormat, serial), 0);
                        if (frequency == 0.0) {
-                               int channel = preferences.getInt(String.format(channelPreferenceFormat, serial), 0);
+                               int channel = backend.getInt(String.format(channelPreferenceFormat, serial), 0);
                                frequency = AltosConvert.radio_channel_to_frequency(channel);
                        }
                        frequencies.put(serial, frequency);
@@ -247,122 +231,122 @@ public class AltosPreferences {
        }
 
        public static void set_telemetry(int serial, int new_telemetry) {
-               synchronized (preferences) {
+               synchronized (backend) {
                        telemetries.put(serial, new_telemetry);
-                       preferences.putInt(String.format(telemetryPreferenceFormat, serial), new_telemetry);
+                       backend.putInt(String.format(telemetryPreferenceFormat, serial), new_telemetry);
                        flush_preferences();
                }
        }
 
        public static int telemetry(int serial) {
-               synchronized (preferences) {
+               synchronized (backend) {
                        if (telemetries.containsKey(serial))
                                return telemetries.get(serial);
-                       int telemetry = preferences.getInt(String.format(telemetryPreferenceFormat, serial),
-                                                          AltosLib.ao_telemetry_standard);
+                       int telemetry = backend.getInt(String.format(telemetryPreferenceFormat, serial),
+                                                  AltosLib.ao_telemetry_standard);
                        telemetries.put(serial, telemetry);
                        return telemetry;
                }
        }
 
        public static void set_scanning_telemetry(int new_scanning_telemetry) {
-               synchronized (preferences) {
+               synchronized (backend) {
                        scanning_telemetry = new_scanning_telemetry;
-                       preferences.putInt(scanningTelemetryPreference, scanning_telemetry);
+                       backend.putInt(scanningTelemetryPreference, scanning_telemetry);
                        flush_preferences();
                }
        }
 
        public static int scanning_telemetry() {
-               synchronized (preferences) {
+               synchronized (backend) {
                        return scanning_telemetry;
                }
        }
 
        public static void set_voice(boolean new_voice) {
-               synchronized (preferences) {
+               synchronized (backend) {
                        voice = new_voice;
-                       preferences.putBoolean(voicePreference, voice);
+                       backend.putBoolean(voicePreference, voice);
                        flush_preferences();
                }
        }
 
        public static boolean voice() {
-               synchronized (preferences) {
+               synchronized (backend) {
                        return voice;
                }
        }
 
        public static void set_callsign(String new_callsign) {
-               synchronized(preferences) {
+               synchronized(backend) {
                        callsign = new_callsign;
-                       preferences.put(callsignPreference, callsign);
+                       backend.putString(callsignPreference, callsign);
                        flush_preferences();
                }
        }
 
        public static String callsign() {
-               synchronized(preferences) {
+               synchronized(backend) {
                        return callsign;
                }
        }
 
        public static void set_firmwaredir(File new_firmwaredir) {
-               synchronized (preferences) {
+               synchronized (backend) {
                        firmwaredir = new_firmwaredir;
-                       preferences.put(firmwaredirPreference, firmwaredir.getPath());
+                       backend.putString(firmwaredirPreference, firmwaredir.getPath());
                        flush_preferences();
                }
        }
 
        public static File firmwaredir() {
-               synchronized (preferences) {
+               synchronized (backend) {
                        return firmwaredir;
                }
        }
 
        public static void set_launcher_serial(int new_launcher_serial) {
-               synchronized (preferences) {
+               synchronized (backend) {
                        launcher_serial = new_launcher_serial;
-                       preferences.putInt(launcherSerialPreference, launcher_serial);
+                       backend.putInt(launcherSerialPreference, launcher_serial);
                        flush_preferences();
                }
        }
 
        public static int launcher_serial() {
-               synchronized (preferences) {
+               synchronized (backend) {
                        return launcher_serial;
                }
        }
 
        public static void set_launcher_channel(int new_launcher_channel) {
-               synchronized (preferences) {
+               synchronized (backend) {
                        launcher_channel = new_launcher_channel;
-                       preferences.putInt(launcherChannelPreference, launcher_channel);
+                       backend.putInt(launcherChannelPreference, launcher_channel);
                        flush_preferences();
                }
        }
 
        public static int launcher_channel() {
-               synchronized (preferences) {
+               synchronized (backend) {
                        return launcher_channel;
                }
        }
        
-       public static Preferences bt_devices() {
-               synchronized (preferences) {
-                       return preferences.node("bt_devices");
+       public static AltosPreferencesBackend bt_devices() {
+               synchronized (backend) {
+                       return backend.node("bt_devices");
                }
        }
 
        public static AltosFrequency[] common_frequencies() {
-               synchronized (preferences) {
+               synchronized (backend) {
                        return common_frequencies;
                }
        }
 
        public static void set_common_frequencies(AltosFrequency[] frequencies) {
-               synchronized(preferences) {
+               synchronized(backend) {
                        common_frequencies = frequencies;
                        save_common_frequencies(frequencies);
                        flush_preferences();
@@ -388,15 +372,15 @@ public class AltosPreferences {
        }
 
        public static boolean imperial_units() {
-               synchronized(preferences) {
+               synchronized(backend) {
                        return AltosConvert.imperial_units;
                }
        }
 
        public static void set_imperial_units(boolean imperial_units) {
-               synchronized (preferences) {
+               synchronized (backend) {
                        AltosConvert.imperial_units = imperial_units;
-                       preferences.putBoolean(unitsPreference, imperial_units);
+                       backend.putBoolean(unitsPreference, imperial_units);
                        flush_preferences();
                }
        }
diff --git a/altoslib/AltosPreferencesBackend.java b/altoslib/AltosPreferencesBackend.java
new file mode 100644 (file)
index 0000000..a8cfb31
--- /dev/null
@@ -0,0 +1,38 @@
+/*
+ * Copyright © 2010 Mike Beattie <mike@ethernal.org>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; version 2 of the License.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
+ */
+
+package org.altusmetrum.AltosLib;
+
+public interface AltosPreferencesBackend {
+
+       public String  getString(String key, String def);
+       public void    putString(String key, String value);
+
+       public int     getInt(String key, int def);
+       public void    putInt(String key, int value);
+
+       public double  getDouble(String key, double def);
+       public void    putDouble(String key, double value);
+
+       public boolean getBoolean(String key, boolean def);
+       public void    putBoolean(String key, boolean value);
+
+       public boolean nodeExists(String key);
+       public AltosPreferencesBackend node(String key);
+
+       public void    flush();
+}
index a026d12302a27af2beba13781f66b7aabb8709a2..03e8e1290936e21de066365f44e1f7a9fe4e4d7f 100644 (file)
@@ -48,12 +48,12 @@ public class AltosUIPreferences extends AltosPreferences {
        public static void init() {
                font_listeners = new LinkedList<AltosFontListener>();
 
-               font_size = preferences.getInt(fontSizePreference, Altos.font_size_medium);
+               font_size = backend.getInt(fontSizePreference, Altos.font_size_medium);
                Altos.set_fonts(font_size);
-               look_and_feel = preferences.get(lookAndFeelPreference, UIManager.getSystemLookAndFeelClassName());
+               look_and_feel = backend.getString(lookAndFeelPreference, UIManager.getSystemLookAndFeelClassName());
 
                ui_listeners = new LinkedList<AltosUIListener>();
-               serial_debug = preferences.getBoolean(serialDebugPreference, false);
+               serial_debug = backend.getBoolean(serialDebugPreference, false);
                AltosLink.set_debug(serial_debug);
        }
 
@@ -107,9 +107,9 @@ public class AltosUIPreferences extends AltosPreferences {
        }
 
        public static void set_font_size(int new_font_size) {
-               synchronized (preferences) {
+               synchronized (backend) {
                        font_size = new_font_size;
-                       preferences.putInt(fontSizePreference, font_size);
+                       backend.putInt(fontSizePreference, font_size);
                        flush_preferences();
                        Altos.set_fonts(font_size);
                        for (AltosFontListener l : font_listeners)
@@ -118,13 +118,13 @@ public class AltosUIPreferences extends AltosPreferences {
        }
 
        public static void register_font_listener(AltosFontListener l) {
-               synchronized (preferences) {
+               synchronized (backend) {
                        font_listeners.add(l);
                }
        }
 
        public static void unregister_font_listener(AltosFontListener l) {
-               synchronized (preferences) {
+               synchronized (backend) {
                        font_listeners.remove(l);
                }
        }
@@ -134,9 +134,9 @@ public class AltosUIPreferences extends AltosPreferences {
                        UIManager.setLookAndFeel(new_look_and_feel);
                } catch (Exception e) {
                }
-               synchronized(preferences) {
+               synchronized(backend) {
                        look_and_feel = new_look_and_feel;
-                       preferences.put(lookAndFeelPreference, look_and_feel);
+                       backend.putString(lookAndFeelPreference, look_and_feel);
                        flush_preferences();
                        for (AltosUIListener l : ui_listeners)
                                l.ui_changed(look_and_feel);
@@ -150,21 +150,21 @@ public class AltosUIPreferences extends AltosPreferences {
        }
 
        public static void register_ui_listener(AltosUIListener l) {
-               synchronized(preferences) {
+               synchronized(backend) {
                        ui_listeners.add(l);
                }
        }
 
        public static void unregister_ui_listener(AltosUIListener l) {
-               synchronized (preferences) {
+               synchronized (backend) {
                        ui_listeners.remove(l);
                }
        }
        public static void set_serial_debug(boolean new_serial_debug) {
-               AltosLink.set_debug(new_serial_debug);
-               synchronized (preferences) {
+               AltosLink.set_debug(serial_debug);
+               synchronized (backend) {
                        serial_debug = new_serial_debug;
-                       preferences.putBoolean(serialDebugPreference, serial_debug);
+                       backend.putBoolean(serialDebugPreference, serial_debug);
                        flush_preferences();
                }
        }
@@ -175,4 +175,4 @@ public class AltosUIPreferences extends AltosPreferences {
                }
        }
 
-}
\ No newline at end of file
+}
diff --git a/altosui/AltosUIPreferencesBackend.java b/altosui/AltosUIPreferencesBackend.java
new file mode 100644 (file)
index 0000000..16844f3
--- /dev/null
@@ -0,0 +1,84 @@
+/*
+ * Copyright © 2012 Mike Beattie <mike@ethernal.org>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; version 2 of the License.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
+ */
+
+package altosui;
+
+import java.text.*;
+import java.util.prefs.*;
+import org.altusmetrum.AltosLib.*;
+
+public class AltosUIPreferencesBackend implements AltosPreferencesBackend {
+
+       private Preferences _preferences = null;
+       
+       public AltosUIPreferencesBackend() {
+               _preferences = Preferences.userRoot().node("/org/altusmetrum/altosui");
+       }
+
+       public AltosUIPreferencesBackend(Preferences in_preferences) {
+               _preferences = in_preferences;
+       }
+
+       public String  getString(String key, String def) {
+               return _preferences.get(key, def)
+       }
+       public void    putString(String key, String value) {
+               _preferences.put(key, value);
+       }
+
+       public int     getInt(String key, int def) {
+               return _preferences.getInt(key, def);
+       }
+       public void    putInt(String key, int value) {
+               _preferences.putInt(key, value);
+       }
+
+       public double  getDouble(String key, double def) {
+               return _preferences.getDouble(key, def);
+       }
+       public void    putDouble(String key, double value) {
+               _preferences.putDouble(key, value);
+       }
+
+       public boolean getBoolean(String key, boolean def) {
+               return _preferences.getBoolean(key, def);
+       }
+       public void    putBoolean(String key, boolean value) {
+               _preferences.putBoolean(key, value);
+       }
+
+       public boolean nodeExists(String key) {
+               try {
+                       return _preferences.nodeExists(key);
+               } catch (BackingStoreException be) {
+                       return false;
+               }
+       }
+
+       public AltosPreferencesBackend node(String key) {
+               return new AltosUIPreferencesBackend(_preferences.node(key));
+       }
+
+       public void    flush() {
+               try {
+                       _preferences.flush();
+               } catch (BackingStoreException ee) {
+                       System.err.printf("Cannot save preferences\n");
+               }
+       }
+
+}
\ No newline at end of file