2 * Copyright © 2010 Keith Packard <keithp@keithp.com>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; version 2 of the License.
8 * This program is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
22 import java.util.prefs.*;
23 import java.util.concurrent.LinkedBlockingQueue;
24 import java.awt.Component;
26 import javax.swing.filechooser.FileSystemView;
28 class AltosPreferences {
29 static Preferences preferences;
31 /* logdir preference name */
32 final static String logdirPreference = "LOGDIR";
34 /* channel preference name */
35 final static String channelPreferenceFormat = "CHANNEL-%d";
37 /* frequency preference name */
38 final static String frequencyPreferenceFormat = "FREQUENCY-%d";
40 /* telemetry format preference name */
41 final static String telemetryPreferenceFormat = "TELEMETRY-%d";
43 /* voice preference name */
44 final static String voicePreference = "VOICE";
46 /* callsign preference name */
47 final static String callsignPreference = "CALLSIGN";
49 /* firmware directory preference name */
50 final static String firmwaredirPreference = "FIRMWARE";
52 /* serial debug preference name */
53 final static String serialDebugPreference = "SERIAL-DEBUG";
55 /* scanning telemetry preferences name */
56 final static String scanningTelemetryPreference = "SCANNING-TELEMETRY";
58 /* font size preferences name */
59 final static String fontSizePreference = "FONT-SIZE";
61 /* Launcher serial preference name */
62 final static String launcherSerialPreference = "LAUNCHER-SERIAL";
64 /* Launcher channel prefernce name */
65 final static String launcherChannelPreference = "LAUNCHER-CHANNEL";
67 /* Default logdir is ~/TeleMetrum */
68 final static String logdirName = "TeleMetrum";
70 /* UI Component to pop dialogs up */
71 static Component component;
76 /* Map directory -- hangs of logdir */
79 /* Frequency (map serial to frequency) */
80 static Hashtable<Integer, Double> frequencies;
82 /* Telemetry (map serial to telemetry format) */
83 static Hashtable<Integer, Integer> telemetries;
85 /* Voice preference */
88 /* Callsign preference */
89 static String callsign;
91 /* Firmware directory */
92 static File firmwaredir;
95 static boolean serial_debug;
97 /* Scanning telemetry */
98 static int scanning_telemetry;
100 static LinkedList<AltosFontListener> font_listeners;
102 static int font_size = Altos.font_size_medium;
104 /* List of frequencies */
105 final static String common_frequencies_node_name = "COMMON-FREQUENCIES";
106 static AltosFrequency[] common_frequencies;
108 final static String frequency_count = "COUNT";
109 final static String frequency_format = "FREQUENCY-%d";
110 final static String description_format = "DESCRIPTION-%d";
112 static AltosFrequency[] load_common_frequencies() {
113 AltosFrequency[] frequencies = null;
114 boolean existing = false;
116 existing = preferences.nodeExists(common_frequencies_node_name);
117 } catch (BackingStoreException be) {
121 Preferences node = preferences.node(common_frequencies_node_name);
122 int count = node.getInt(frequency_count, 0);
124 frequencies = new AltosFrequency[count];
125 for (int i = 0; i < count; i++) {
129 frequency = node.getDouble(String.format(frequency_format, i), 0.0);
130 description = node.get(String.format(description_format, i), null);
131 frequencies[i] = new AltosFrequency(frequency, description);
134 frequencies = new AltosFrequency[10];
135 for (int i = 0; i < 10; i++) {
136 frequencies[i] = new AltosFrequency(434.550 + i * .1,
137 String.format("Channel %d", i));
143 static void save_common_frequencies(AltosFrequency[] frequencies) {
144 Preferences node = preferences.node(common_frequencies_node_name);
146 node.putInt(frequency_count, frequencies.length);
147 for (int i = 0; i < frequencies.length; i++) {
148 node.putDouble(String.format(frequency_format, i), frequencies[i].frequency);
149 node.put(String.format(description_format, i), frequencies[i].description);
152 static int launcher_serial;
154 static int launcher_channel;
156 public static void init() {
157 preferences = Preferences.userRoot().node("/org/altusmetrum/altosui");
159 /* Initialize logdir from preferences */
160 String logdir_string = preferences.get(logdirPreference, null);
161 if (logdir_string != null)
162 logdir = new File(logdir_string);
164 /* Use the file system view default directory */
165 logdir = new File(FileSystemView.getFileSystemView().getDefaultDirectory(), logdirName);
166 if (!logdir.exists())
169 mapdir = new File(logdir, "maps");
170 if (!mapdir.exists())
173 frequencies = new Hashtable<Integer, Double>();
175 telemetries = new Hashtable<Integer,Integer>();
177 voice = preferences.getBoolean(voicePreference, true);
179 callsign = preferences.get(callsignPreference,"N0CALL");
181 scanning_telemetry = preferences.getInt(scanningTelemetryPreference,(1 << Altos.ao_telemetry_standard));
183 font_listeners = new LinkedList<AltosFontListener>();
185 font_size = preferences.getInt(fontSizePreference, Altos.font_size_medium);
186 Altos.set_fonts(font_size);
188 launcher_serial = preferences.getInt(launcherSerialPreference, 0);
190 launcher_channel = preferences.getInt(launcherChannelPreference, 0);
192 String firmwaredir_string = preferences.get(firmwaredirPreference, null);
193 if (firmwaredir_string != null)
194 firmwaredir = new File(firmwaredir_string);
198 serial_debug = preferences.getBoolean(serialDebugPreference, false);
199 AltosSerial.set_debug(serial_debug);
201 common_frequencies = load_common_frequencies();
206 static void set_component(Component in_component) {
207 component = in_component;
210 static void flush_preferences() {
213 } catch (BackingStoreException ee) {
214 if (component != null)
215 JOptionPane.showMessageDialog(component,
216 preferences.absolutePath(),
217 "Cannot save prefernces",
218 JOptionPane.ERROR_MESSAGE);
220 System.err.printf("Cannot save preferences\n");
224 public static void set_logdir(File new_logdir) {
226 mapdir = new File(logdir, "maps");
227 if (!mapdir.exists())
229 synchronized (preferences) {
230 preferences.put(logdirPreference, logdir.getPath());
235 private static boolean check_dir(File dir) {
238 JOptionPane.showMessageDialog(component,
240 "Cannot create directory",
241 JOptionPane.ERROR_MESSAGE);
244 } else if (!dir.isDirectory()) {
245 JOptionPane.showMessageDialog(component,
247 "Is not a directory",
248 JOptionPane.ERROR_MESSAGE);
254 /* Configure the log directory. This is where all telemetry and eeprom files
255 * will be written to, and where replay will look for telemetry files
257 public static void ConfigureLog() {
258 JFileChooser logdir_chooser = new JFileChooser(logdir.getParentFile());
260 logdir_chooser.setDialogTitle("Configure Data Logging Directory");
261 logdir_chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
263 if (logdir_chooser.showDialog(component, "Select Directory") == JFileChooser.APPROVE_OPTION) {
264 File dir = logdir_chooser.getSelectedFile();
270 public static File logdir() {
274 public static File mapdir() {
278 public static void set_frequency(int serial, double new_frequency) {
279 frequencies.put(serial, new_frequency);
280 synchronized (preferences) {
281 preferences.putDouble(String.format(frequencyPreferenceFormat, serial), new_frequency);
286 public static double frequency(int serial) {
287 if (frequencies.containsKey(serial))
288 return frequencies.get(serial);
289 double frequency = preferences.getDouble(String.format(frequencyPreferenceFormat, serial), 0);
290 if (frequency == 0.0) {
291 int channel = preferences.getInt(String.format(channelPreferenceFormat, serial), 0);
292 frequency = AltosConvert.radio_channel_to_frequency(channel);
294 frequencies.put(serial, frequency);
298 public static void set_telemetry(int serial, int new_telemetry) {
299 telemetries.put(serial, new_telemetry);
300 synchronized (preferences) {
301 preferences.putInt(String.format(telemetryPreferenceFormat, serial), new_telemetry);
306 public static int telemetry(int serial) {
307 if (telemetries.containsKey(serial))
308 return telemetries.get(serial);
309 int telemetry = preferences.getInt(String.format(telemetryPreferenceFormat, serial),
310 Altos.ao_telemetry_standard);
311 telemetries.put(serial, telemetry);
315 public static void set_scanning_telemetry(int new_scanning_telemetry) {
316 scanning_telemetry = new_scanning_telemetry;
317 synchronized (preferences) {
318 preferences.putInt(scanningTelemetryPreference, scanning_telemetry);
323 public static int scanning_telemetry() {
324 return scanning_telemetry;
327 public static void set_voice(boolean new_voice) {
329 synchronized (preferences) {
330 preferences.putBoolean(voicePreference, voice);
335 public static boolean voice() {
339 public static void set_callsign(String new_callsign) {
340 callsign = new_callsign;
341 synchronized(preferences) {
342 preferences.put(callsignPreference, callsign);
347 public static String callsign() {
351 public static void set_firmwaredir(File new_firmwaredir) {
352 firmwaredir = new_firmwaredir;
353 synchronized (preferences) {
354 preferences.put(firmwaredirPreference, firmwaredir.getPath());
359 public static File firmwaredir() {
363 public static int font_size() {
367 static void set_fonts() {
370 public static void set_font_size(int new_font_size) {
371 font_size = new_font_size;
372 synchronized (preferences) {
373 preferences.putInt(fontSizePreference, font_size);
375 Altos.set_fonts(font_size);
376 for (AltosFontListener l : font_listeners)
377 l.font_size_changed(font_size);
381 public static void register_font_listener(AltosFontListener l) {
382 synchronized (preferences) {
383 font_listeners.add(l);
387 public static void unregister_font_listener(AltosFontListener l) {
388 synchronized (preferences) {
389 font_listeners.remove(l);
393 public static void set_serial_debug(boolean new_serial_debug) {
394 serial_debug = new_serial_debug;
395 AltosSerial.set_debug(serial_debug);
396 synchronized (preferences) {
397 preferences.putBoolean(serialDebugPreference, serial_debug);
402 public static boolean serial_debug() {
406 public static void set_launcher_serial(int new_launcher_serial) {
407 launcher_serial = new_launcher_serial;
408 System.out.printf("set launcher serial to %d\n", new_launcher_serial);
409 synchronized (preferences) {
410 preferences.putInt(launcherSerialPreference, launcher_serial);
415 public static int launcher_serial() {
416 return launcher_serial;
419 public static void set_launcher_channel(int new_launcher_channel) {
420 launcher_channel = new_launcher_channel;
421 System.out.printf("set launcher channel to %d\n", new_launcher_channel);
422 synchronized (preferences) {
423 preferences.putInt(launcherChannelPreference, launcher_channel);
428 public static int launcher_channel() {
429 return launcher_channel;
432 public static Preferences bt_devices() {
433 return preferences.node("bt_devices");
436 public static AltosFrequency[] common_frequencies() {
437 return common_frequencies;
440 public static void set_common_frequencies(AltosFrequency[] frequencies) {
441 common_frequencies = frequencies;
442 synchronized(preferences) {
443 save_common_frequencies(frequencies);
448 public static void add_common_frequency(AltosFrequency frequency) {
449 AltosFrequency[] new_frequencies = new AltosFrequency[common_frequencies.length + 1];
452 for (i = 0; i < common_frequencies.length; i++) {
453 if (frequency.frequency == common_frequencies[i].frequency)
455 if (frequency.frequency < common_frequencies[i].frequency)
457 new_frequencies[i] = common_frequencies[i];
459 new_frequencies[i] = frequency;
460 for (; i < common_frequencies.length; i++)
461 new_frequencies[i+1] = common_frequencies[i];
462 set_common_frequencies(new_frequencies);