make infotable scrollable, revert its fontsize to 14
[fw/altos] / ao-tools / altosui / AltosPreferences.java
1 /*
2  * Copyright © 2010 Keith Packard <keithp@keithp.com>
3  *
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.
7  *
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.
12  *
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.
16  */
17
18 package altosui;
19 import java.io.*;
20 import java.util.*;
21 import java.text.*;
22 import java.util.prefs.*;
23 import java.util.concurrent.LinkedBlockingQueue;
24 import java.awt.Component;
25 import javax.swing.*;
26 import javax.swing.filechooser.FileSystemView;
27
28 class AltosPreferences {
29         static Preferences preferences;
30
31         /* logdir preference name */
32         final static String logdirPreference = "LOGDIR";
33
34         /* channel preference name */
35         final static String channelPreferenceFormat = "CHANNEL-%d";
36
37         /* voice preference name */
38         final static String voicePreference = "VOICE";
39
40         /* callsign preference name */
41         final static String callsignPreference = "CALLSIGN";
42
43         /* firmware directory preference name */
44         final static String firmwaredirPreference = "FIRMWARE";
45
46         /* Default logdir is ~/TeleMetrum */
47         final static String logdirName = "TeleMetrum";
48
49         /* UI Component to pop dialogs up */
50         static Component component;
51
52         /* Log directory */
53         static File logdir;
54
55         /* Channel (map serial to channel) */
56         static Hashtable<Integer, Integer> channels;
57
58         /* Voice preference */
59         static boolean voice;
60
61         /* Callsign preference */
62         static String callsign;
63
64         /* Firmware directory */
65         static File firmwaredir;
66
67         public static void init(Component ui) {
68                 preferences = Preferences.userRoot().node("/org/altusmetrum/altosui");
69
70                 component = ui;
71
72                 /* Initialize logdir from preferences */
73                 String logdir_string = preferences.get(logdirPreference, null);
74                 if (logdir_string != null)
75                         logdir = new File(logdir_string);
76                 else {
77                         /* Use the file system view default directory */
78                         logdir = new File(FileSystemView.getFileSystemView().getDefaultDirectory(), logdirName);
79                         if (!logdir.exists())
80                                 logdir.mkdirs();
81                 }
82
83                 channels = new Hashtable<Integer,Integer>();
84
85                 voice = preferences.getBoolean(voicePreference, true);
86
87                 callsign = preferences.get(callsignPreference,"N0CALL");
88
89                 String firmwaredir_string = preferences.get(firmwaredirPreference, null);
90                 if (firmwaredir_string != null)
91                         firmwaredir = new File(firmwaredir_string);
92                 else
93                         firmwaredir = null;
94         }
95
96         static void flush_preferences() {
97                 try {
98                         preferences.flush();
99                 } catch (BackingStoreException ee) {
100                         JOptionPane.showMessageDialog(component,
101                                                       preferences.absolutePath(),
102                                                       "Cannot save prefernces",
103                                                       JOptionPane.ERROR_MESSAGE);
104                 }
105         }
106
107         public static void set_logdir(File new_logdir) {
108                 logdir = new_logdir;
109                 synchronized (preferences) {
110                         preferences.put(logdirPreference, logdir.getPath());
111                         flush_preferences();
112                 }
113         }
114
115         private static boolean check_dir(File dir) {
116                 if (!dir.exists()) {
117                         if (!dir.mkdirs()) {
118                                 JOptionPane.showMessageDialog(component,
119                                                               dir.getName(),
120                                                               "Cannot create directory",
121                                                               JOptionPane.ERROR_MESSAGE);
122                                 return false;
123                         }
124                 } else if (!dir.isDirectory()) {
125                         JOptionPane.showMessageDialog(component,
126                                                       dir.getName(),
127                                                       "Is not a directory",
128                                                       JOptionPane.ERROR_MESSAGE);
129                         return false;
130                 }
131                 return true;
132         }
133
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
136          */
137         public static void ConfigureLog() {
138                 JFileChooser    logdir_chooser = new JFileChooser(logdir.getParentFile());
139
140                 logdir_chooser.setDialogTitle("Configure Data Logging Directory");
141                 logdir_chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
142
143                 if (logdir_chooser.showDialog(component, "Select Directory") == JFileChooser.APPROVE_OPTION) {
144                         File dir = logdir_chooser.getSelectedFile();
145                         if (check_dir(dir))
146                                 set_logdir(dir);
147                 }
148         }
149
150         public static File logdir() {
151                 return logdir;
152         }
153
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);
158                         flush_preferences();
159                 }
160         }
161
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);
167                 return channel;
168         }
169
170         public static void set_voice(boolean new_voice) {
171                 voice = new_voice;
172                 synchronized (preferences) {
173                         preferences.putBoolean(voicePreference, voice);
174                         flush_preferences();
175                 }
176         }
177
178         public static boolean voice() {
179                 return voice;
180         }
181
182         public static void set_callsign(String new_callsign) {
183                 callsign = new_callsign;
184                 synchronized(preferences) {
185                         preferences.put(callsignPreference, callsign);
186                         flush_preferences();
187                 }
188         }
189
190         public static String callsign() {
191                 return callsign;
192         }
193
194         public static void set_firmwaredir(File new_firmwaredir) {
195                 firmwaredir = new_firmwaredir;
196                 synchronized (preferences) {
197                         preferences.put(firmwaredirPreference, firmwaredir.getPath());
198                         flush_preferences();
199                 }
200         }
201
202         public static File firmwaredir() {
203                 return firmwaredir;
204         }
205 }