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 /* voice preference name */
38 final static String voicePreference = "VOICE";
40 /* callsign preference name */
41 final static String callsignPreference = "CALLSIGN";
43 /* firmware directory preference name */
44 final static String firmwaredirPreference = "FIRMWARE";
46 /* serial debug preference name */
47 final static String serialDebugPreference = "SERIAL-DEBUG";
49 /* Default logdir is ~/TeleMetrum */
50 final static String logdirName = "TeleMetrum";
52 /* UI Component to pop dialogs up */
53 static Component component;
58 /* Map directory -- hangs of logdir */
61 /* Channel (map serial to channel) */
62 static Hashtable<Integer, Integer> channels;
64 /* Voice preference */
67 /* Callsign preference */
68 static String callsign;
70 /* Firmware directory */
71 static File firmwaredir;
74 static boolean serial_debug;
76 public static void init(Component ui) {
77 preferences = Preferences.userRoot().node("/org/altusmetrum/altosui");
81 /* Initialize logdir from preferences */
82 String logdir_string = preferences.get(logdirPreference, null);
83 if (logdir_string != null)
84 logdir = new File(logdir_string);
86 /* Use the file system view default directory */
87 logdir = new File(FileSystemView.getFileSystemView().getDefaultDirectory(), logdirName);
91 mapdir = new File(logdir, "maps");
95 channels = new Hashtable<Integer,Integer>();
97 voice = preferences.getBoolean(voicePreference, true);
99 callsign = preferences.get(callsignPreference,"N0CALL");
101 String firmwaredir_string = preferences.get(firmwaredirPreference, null);
102 if (firmwaredir_string != null)
103 firmwaredir = new File(firmwaredir_string);
107 serial_debug = preferences.getBoolean(serialDebugPreference, false);
108 AltosSerial.set_debug(serial_debug);
111 static void flush_preferences() {
114 } catch (BackingStoreException ee) {
115 JOptionPane.showMessageDialog(component,
116 preferences.absolutePath(),
117 "Cannot save prefernces",
118 JOptionPane.ERROR_MESSAGE);
122 public static void set_logdir(File new_logdir) {
124 mapdir = new File(logdir, "maps");
125 if (!mapdir.exists())
127 synchronized (preferences) {
128 preferences.put(logdirPreference, logdir.getPath());
133 private static boolean check_dir(File dir) {
136 JOptionPane.showMessageDialog(component,
138 "Cannot create directory",
139 JOptionPane.ERROR_MESSAGE);
142 } else if (!dir.isDirectory()) {
143 JOptionPane.showMessageDialog(component,
145 "Is not a directory",
146 JOptionPane.ERROR_MESSAGE);
152 /* Configure the log directory. This is where all telemetry and eeprom files
153 * will be written to, and where replay will look for telemetry files
155 public static void ConfigureLog() {
156 JFileChooser logdir_chooser = new JFileChooser(logdir.getParentFile());
158 logdir_chooser.setDialogTitle("Configure Data Logging Directory");
159 logdir_chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
161 if (logdir_chooser.showDialog(component, "Select Directory") == JFileChooser.APPROVE_OPTION) {
162 File dir = logdir_chooser.getSelectedFile();
168 public static File logdir() {
172 public static File mapdir() {
176 public static void set_channel(int serial, int new_channel) {
177 channels.put(serial, new_channel);
178 synchronized (preferences) {
179 preferences.putInt(String.format(channelPreferenceFormat, serial), new_channel);
184 public static int channel(int serial) {
185 if (channels.containsKey(serial))
186 return channels.get(serial);
187 int channel = preferences.getInt(String.format(channelPreferenceFormat, serial), 0);
188 channels.put(serial, channel);
192 public static void set_voice(boolean new_voice) {
194 synchronized (preferences) {
195 preferences.putBoolean(voicePreference, voice);
200 public static boolean voice() {
204 public static void set_callsign(String new_callsign) {
205 callsign = new_callsign;
206 synchronized(preferences) {
207 preferences.put(callsignPreference, callsign);
212 public static String callsign() {
216 public static void set_firmwaredir(File new_firmwaredir) {
217 firmwaredir = new_firmwaredir;
218 synchronized (preferences) {
219 preferences.put(firmwaredirPreference, firmwaredir.getPath());
224 public static File firmwaredir() {
228 public static void set_serial_debug(boolean new_serial_debug) {
229 serial_debug = new_serial_debug;
230 AltosSerial.set_debug(serial_debug);
231 synchronized (preferences) {
232 preferences.putBoolean(serialDebugPreference, serial_debug);
237 public static boolean serial_debug() {