Merge remote-tracking branch 'mjb/prefs_interface'
authorKeith Packard <keithp@keithp.com>
Thu, 20 Sep 2012 09:30:11 +0000 (11:30 +0200)
committerKeith Packard <keithp@keithp.com>
Thu, 20 Sep 2012 09:30:11 +0000 (11:30 +0200)
altoslib/AltosPreferences.java
altoslib/AltosPreferencesBackend.java [new file with mode: 0644]
altoslib/Makefile.am
altosui/AltosBTKnown.java
altosui/AltosUIPreferences.java
altosui/AltosUIPreferencesBackend.java [new file with mode: 0644]
altosui/Makefile.am

index 0e7b2bd..a82ea3f 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,23 +125,23 @@ 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;
 
        public static int launcher_channel;
 
-       public static void init() {
-               preferences = Preferences.userRoot().node("/org/altusmetrum/altosui");
+       public static void init(AltosPreferencesBackend in_backend) {
+               backend = in_backend;
 
                /* 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,51 @@ 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 +229,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 +370,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..3fc4b0a
--- /dev/null
@@ -0,0 +1,41 @@
+/*
+ * 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 String[] keys();
+       public void    remove(String key);
+
+       public void    flush();
+}
index a9f810f..b56d8af 100644 (file)
@@ -42,6 +42,7 @@ AltosLib_JAVA = \
        $(SRC)/AltosOrderedMegaRecord.java \
        $(SRC)/AltosParse.java \
        $(SRC)/AltosPreferences.java \
+       $(SRC)/AltosPreferencesBackend.java \
        $(SRC)/AltosRecordCompanion.java \
        $(SRC)/AltosRecordIterable.java \
        $(SRC)/AltosRecord.java \
index 74cc683..ae04ac8 100644 (file)
 
 package altosui;
 import java.util.*;
-import java.util.prefs.*;
+import org.altusmetrum.AltosLib.*;
 
 public class AltosBTKnown implements Iterable<AltosBTDevice> {
        LinkedList<AltosBTDevice>       devices = new LinkedList<AltosBTDevice>();
-       Preferences                     bt_pref = AltosUIPreferences.bt_devices();
+       AltosPreferencesBackend         bt_pref = AltosUIPreferences.bt_devices();
 
        private String get_address(String name) {
-               return bt_pref.get(name, "");
+               return bt_pref.getString(name, "");
        }
 
        private void set_address(String name, String addr) {
-               bt_pref.put(name, addr);
+               bt_pref.putString(name, addr);
        }
 
        private void remove(String name) {
@@ -43,7 +43,6 @@ public class AltosBTKnown implements Iterable<AltosBTDevice> {
                                String  addr = get_address(name);
                                devices.add(new AltosBTDevice(name, addr));
                        }
-               } catch (BackingStoreException be) {
                } catch (IllegalStateException ie) {
                }
        }
index a026d12..a28c07b 100644 (file)
@@ -46,14 +46,16 @@ public class AltosUIPreferences extends AltosPreferences {
        static boolean serial_debug;
 
        public static void init() {
+               AltosPreferences.init(new AltosUIPreferencesBackend());
+
                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);
        }
 
@@ -98,7 +100,7 @@ public class AltosUIPreferences extends AltosPreferences {
                }
        }
        public static int font_size() {
-               synchronized (preferences) {
+               synchronized (backend) {
                        return font_size;
                }
        }
@@ -107,9 +109,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 +120,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 +136,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);
@@ -144,35 +146,35 @@ public class AltosUIPreferences extends AltosPreferences {
        }
 
        public static String look_and_feel() {
-               synchronized (preferences) {
+               synchronized (backend) {
                        return look_and_feel;
                }
        }
 
        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();
                }
        }
 
        public static boolean serial_debug() {
-               synchronized (preferences) {
+               synchronized (backend) {
                        return serial_debug;
                }
        }
 
-}
\ No newline at end of file
+}
diff --git a/altosui/AltosUIPreferencesBackend.java b/altosui/AltosUIPreferencesBackend.java
new file mode 100644 (file)
index 0000000..210dcb8
--- /dev/null
@@ -0,0 +1,95 @@
+/*
+ * 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.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 String[] keys() {
+               try {
+                       return _preferences.keys();
+               } catch (BackingStoreException be) {
+                       return null;
+               }
+       }
+
+       public void remove(String key) {
+               _preferences.remove(key);
+       }
+
+       public void    flush() {
+               try {
+                       _preferences.flush();
+               } catch (BackingStoreException ee) {
+                       System.err.printf("Cannot save preferences\n");
+               }
+       }
+
+}
index 6010df5..9f03ceb 100644 (file)
@@ -65,6 +65,7 @@ altosui_JAVA = \
        AltosLights.java \
        AltosPad.java \
        AltosUIPreferences.java \
+       AltosUIPreferencesBackend.java \
        AltosRomconfig.java \
        AltosRomconfigUI.java \
        AltosScanUI.java \
@@ -338,4 +339,4 @@ $(WINDOWS_DIST): $(WINDOWS_FILES) altos-windows.nsi
        makensis -Oaltos-windows.log "-XOutFile $@" "-DVERSION=$(VERSION)" altos-windows.nsi
 
 publish:
-       scp launch-sites.txt gag.com:public_html
\ No newline at end of file
+       scp launch-sites.txt gag.com:public_html