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 /* Default logdir is ~/TeleMetrum */
47 final static String logdirName = "TeleMetrum";
49 /* UI Component to pop dialogs up */
50 static Component component;
55 /* Channel (map serial to channel) */
56 static Hashtable<Integer, Integer> channels;
58 /* Voice preference */
61 /* Callsign preference */
62 static String callsign;
64 /* Firmware directory */
65 static File firmwaredir;
67 public static void init(Component ui) {
68 preferences = Preferences.userRoot().node("/org/altusmetrum/altosui");
72 /* Initialize logdir from preferences */
73 String logdir_string = preferences.get(logdirPreference, null);
74 if (logdir_string != null)
75 logdir = new File(logdir_string);
77 /* Use the file system view default directory */
78 logdir = new File(FileSystemView.getFileSystemView().getDefaultDirectory(), logdirName);
83 channels = new Hashtable<Integer,Integer>();
85 voice = preferences.getBoolean(voicePreference, true);
87 callsign = preferences.get(callsignPreference,"N0CALL");
89 String firmwaredir_string = preferences.get(firmwaredirPreference, null);
90 if (firmwaredir_string != null)
91 firmwaredir = new File(firmwaredir_string);
96 static void flush_preferences() {
99 } catch (BackingStoreException ee) {
100 JOptionPane.showMessageDialog(component,
101 preferences.absolutePath(),
102 "Cannot save prefernces",
103 JOptionPane.ERROR_MESSAGE);
107 public static void set_logdir(File new_logdir) {
109 synchronized (preferences) {
110 preferences.put(logdirPreference, logdir.getPath());
115 private static boolean check_dir(File dir) {
118 JOptionPane.showMessageDialog(component,
120 "Cannot create directory",
121 JOptionPane.ERROR_MESSAGE);
124 } else if (!dir.isDirectory()) {
125 JOptionPane.showMessageDialog(component,
127 "Is not a directory",
128 JOptionPane.ERROR_MESSAGE);
134 /* Configure the log directory. This is where all telemetry and eeprom files
135 * will be written to, and where replay will look for telemetry files
137 public static void ConfigureLog() {
138 JFileChooser logdir_chooser = new JFileChooser(logdir.getParentFile());
140 logdir_chooser.setDialogTitle("Configure Data Logging Directory");
141 logdir_chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
143 if (logdir_chooser.showDialog(component, "Select Directory") == JFileChooser.APPROVE_OPTION) {
144 File dir = logdir_chooser.getSelectedFile();
150 public static File logdir() {
154 public static void set_channel(int serial, int new_channel) {
155 channels.put(serial, new_channel);
156 synchronized (preferences) {
157 preferences.putInt(String.format(channelPreferenceFormat, serial), new_channel);
162 public static int channel(int serial) {
163 if (channels.containsKey(serial))
164 return channels.get(serial);
165 int channel = preferences.getInt(String.format(channelPreferenceFormat, serial), 0);
166 channels.put(serial, channel);
170 public static void set_voice(boolean new_voice) {
172 synchronized (preferences) {
173 preferences.putBoolean(voicePreference, voice);
178 public static boolean voice() {
182 public static void set_callsign(String new_callsign) {
183 callsign = new_callsign;
184 synchronized(preferences) {
185 preferences.put(callsignPreference, callsign);
190 public static String callsign() {
194 public static void set_firmwaredir(File new_firmwaredir) {
195 firmwaredir = new_firmwaredir;
196 synchronized (preferences) {
197 preferences.put(firmwaredirPreference, firmwaredir.getPath());
202 public static File firmwaredir() {