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 /* Map directory -- hangs of logdir */
58 /* Channel (map serial to channel) */
59 static Hashtable<Integer, Integer> channels;
61 /* Voice preference */
64 /* Callsign preference */
65 static String callsign;
67 /* Firmware directory */
68 static File firmwaredir;
70 public static void init(Component ui) {
71 preferences = Preferences.userRoot().node("/org/altusmetrum/altosui");
75 /* Initialize logdir from preferences */
76 String logdir_string = preferences.get(logdirPreference, null);
77 if (logdir_string != null)
78 logdir = new File(logdir_string);
80 /* Use the file system view default directory */
81 logdir = new File(FileSystemView.getFileSystemView().getDefaultDirectory(), logdirName);
85 mapdir = new File(logdir, "maps");
89 channels = new Hashtable<Integer,Integer>();
91 voice = preferences.getBoolean(voicePreference, true);
93 callsign = preferences.get(callsignPreference,"N0CALL");
95 String firmwaredir_string = preferences.get(firmwaredirPreference, null);
96 if (firmwaredir_string != null)
97 firmwaredir = new File(firmwaredir_string);
102 static void flush_preferences() {
105 } catch (BackingStoreException ee) {
106 JOptionPane.showMessageDialog(component,
107 preferences.absolutePath(),
108 "Cannot save prefernces",
109 JOptionPane.ERROR_MESSAGE);
113 public static void set_logdir(File new_logdir) {
115 mapdir = new File(logdir, "maps");
116 if (!mapdir.exists())
118 synchronized (preferences) {
119 preferences.put(logdirPreference, logdir.getPath());
124 private static boolean check_dir(File dir) {
127 JOptionPane.showMessageDialog(component,
129 "Cannot create directory",
130 JOptionPane.ERROR_MESSAGE);
133 } else if (!dir.isDirectory()) {
134 JOptionPane.showMessageDialog(component,
136 "Is not a directory",
137 JOptionPane.ERROR_MESSAGE);
143 /* Configure the log directory. This is where all telemetry and eeprom files
144 * will be written to, and where replay will look for telemetry files
146 public static void ConfigureLog() {
147 JFileChooser logdir_chooser = new JFileChooser(logdir.getParentFile());
149 logdir_chooser.setDialogTitle("Configure Data Logging Directory");
150 logdir_chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
152 if (logdir_chooser.showDialog(component, "Select Directory") == JFileChooser.APPROVE_OPTION) {
153 File dir = logdir_chooser.getSelectedFile();
159 public static File logdir() {
163 public static File mapdir() {
167 public static void set_channel(int serial, int new_channel) {
168 channels.put(serial, new_channel);
169 synchronized (preferences) {
170 preferences.putInt(String.format(channelPreferenceFormat, serial), new_channel);
175 public static int channel(int serial) {
176 if (channels.containsKey(serial))
177 return channels.get(serial);
178 int channel = preferences.getInt(String.format(channelPreferenceFormat, serial), 0);
179 channels.put(serial, channel);
183 public static void set_voice(boolean new_voice) {
185 synchronized (preferences) {
186 preferences.putBoolean(voicePreference, voice);
191 public static boolean voice() {
195 public static void set_callsign(String new_callsign) {
196 callsign = new_callsign;
197 synchronized(preferences) {
198 preferences.put(callsignPreference, callsign);
203 public static String callsign() {
207 public static void set_firmwaredir(File new_firmwaredir) {
208 firmwaredir = new_firmwaredir;
209 synchronized (preferences) {
210 preferences.put(firmwaredirPreference, firmwaredir.getPath());
215 public static File firmwaredir() {