altosui: Add dialogs to configure 'common' frequencies
[fw/altos] / altosui / AltosPreferences.java
index d4df4e163efecaf18462fc0f49fce012564f138a..e92b9532466fa2e11e00dc03291c9425139264da 100644 (file)
@@ -34,6 +34,9 @@ class AltosPreferences {
        /* channel preference name */
        final static String channelPreferenceFormat = "CHANNEL-%d";
 
+       /* telemetry format preference name */
+       final static String telemetryPreferenceFormat = "TELEMETRY-%d";
+
        /* voice preference name */
        final static String voicePreference = "VOICE";
 
@@ -61,6 +64,12 @@ class AltosPreferences {
        /* Channel (map serial to channel) */
        static Hashtable<Integer, Integer> channels;
 
+       /* Frequency (map serial to frequency) */
+       static Hashtable<Integer, Double> frequencies;
+
+       /* Telemetry (map serial to telemetry format) */
+       static Hashtable<Integer, Integer> telemetries;
+
        /* Voice preference */
        static boolean voice;
 
@@ -73,10 +82,57 @@ class AltosPreferences {
        /* Serial debug */
        static boolean serial_debug;
 
-       public static void init(Component ui) {
-               preferences = Preferences.userRoot().node("/org/altusmetrum/altosui");
+       /* List of frequencies */
+       final static String common_frequencies_node_name = "COMMON-FREQUENCIES";
+       static AltosFrequency[] common_frequencies;
+
+       final static String     frequency_count = "COUNT";
+       final static String     frequency_format = "FREQUENCY-%d";
+       final static String     description_format = "DESCRIPTION-%d";
 
-               component = ui;
+       static AltosFrequency[] load_common_frequencies() {
+               AltosFrequency[] frequencies = null;
+               boolean existing = false;
+               try {
+                       existing = preferences.nodeExists(common_frequencies_node_name);
+               } catch (BackingStoreException be) {
+                       existing = false;
+               }
+               if (existing) {
+                       Preferences     node = preferences.node(common_frequencies_node_name);
+                       int             count = node.getInt(frequency_count, 0);
+
+                       frequencies = new AltosFrequency[count];
+                       for (int i = 0; i < count; i++) {
+                               double  frequency;
+                               String  description;
+
+                               frequency = node.getDouble(String.format(frequency_format, i), 0.0);
+                               description = node.get(String.format(description_format, i), null);
+                               frequencies[i] = new AltosFrequency(frequency, description);
+                       }
+               } else {
+                       frequencies = new AltosFrequency[10];
+                       for (int i = 0; i < 10; i++) {
+                               frequencies[i] = new AltosFrequency(434.550 + i * .1,
+                                                                          String.format("Channel %d", i));
+                       }
+               }
+               return frequencies;
+       }
+
+       static void save_common_frequencies(AltosFrequency[] frequencies) {
+               Preferences     node = preferences.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);
+               }
+       }
+
+       public static void init() {
+               preferences = Preferences.userRoot().node("/org/altusmetrum/altosui");
 
                /* Initialize logdir from preferences */
                String logdir_string = preferences.get(logdirPreference, null);
@@ -94,6 +150,8 @@ class AltosPreferences {
 
                channels = new Hashtable<Integer,Integer>();
 
+               telemetries = new Hashtable<Integer,Integer>();
+
                voice = preferences.getBoolean(voicePreference, true);
 
                callsign = preferences.get(callsignPreference,"N0CALL");
@@ -106,16 +164,27 @@ class AltosPreferences {
 
                serial_debug = preferences.getBoolean(serialDebugPreference, false);
                AltosSerial.set_debug(serial_debug);
+
+               common_frequencies = load_common_frequencies();
+       }
+
+       static { init(); }
+
+       static void set_component(Component in_component) {
+               component = in_component;
        }
 
        static void flush_preferences() {
                try {
                        preferences.flush();
                } catch (BackingStoreException ee) {
-                       JOptionPane.showMessageDialog(component,
-                                                     preferences.absolutePath(),
-                                                     "Cannot save prefernces",
-                                                     JOptionPane.ERROR_MESSAGE);
+                       if (component != null)
+                               JOptionPane.showMessageDialog(component,
+                                                             preferences.absolutePath(),
+                                                             "Cannot save prefernces",
+                                                             JOptionPane.ERROR_MESSAGE);
+                       else
+                               System.err.printf("Cannot save preferences\n");
                }
        }
 
@@ -189,6 +258,23 @@ class AltosPreferences {
                return channel;
        }
 
+       public static void set_telemetry(int serial, int new_telemetry) {
+               telemetries.put(serial, new_telemetry);
+               synchronized (preferences) {
+                       preferences.putInt(String.format(telemetryPreferenceFormat, serial), new_telemetry);
+                       flush_preferences();
+               }
+       }
+
+       public static int telemetry(int serial) {
+               if (telemetries.containsKey(serial))
+                       return telemetries.get(serial);
+               int telemetry = preferences.getInt(String.format(telemetryPreferenceFormat, serial),
+                                                  Altos.ao_telemetry_standard);
+               telemetries.put(serial, telemetry);
+               return telemetry;
+       }
+
        public static void set_voice(boolean new_voice) {
                voice = new_voice;
                synchronized (preferences) {
@@ -237,4 +323,20 @@ class AltosPreferences {
        public static boolean serial_debug() {
                return serial_debug;
        }
+
+       public static Preferences bt_devices() {
+               return preferences.node("bt_devices");
+       }
+
+       public static AltosFrequency[] common_frequencies() {
+               return common_frequencies;
+       }
+
+       public static void set_common_frequencies(AltosFrequency[] frequencies) {
+               common_frequencies = frequencies;
+               synchronized(preferences) {
+                       save_common_frequencies(frequencies);
+                       flush_preferences();
+               }
+       }
 }