1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
|
/*
* Copyright © 2010 Keith Packard <keithp@keithp.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; version 2 of the License.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
*/
package altosui;
import java.io.*;
import java.util.*;
import java.text.*;
import java.util.prefs.*;
import java.util.concurrent.LinkedBlockingQueue;
import java.awt.Component;
import javax.swing.*;
import javax.swing.filechooser.FileSystemView;
class AltosPreferences {
static Preferences preferences;
/* logdir preference name */
final static String logdirPreference = "LOGDIR";
/* channel preference name */
final static String channelPreference = "CHANNEL";
/* voice preference name */
final static String voicePreference = "VOICE";
/* callsign preference name */
final static String callsignPreference = "CALLSIGN";
/* Default logdir is ~/TeleMetrum */
final static String logdirName = "TeleMetrum";
/* UI Component to pop dialogs up */
static Component component;
/* Log directory */
static File logdir;
/* Telemetry channel */
static int channel;
/* Voice preference */
static boolean voice;
static String callsign;
public static void init(Component ui) {
preferences = Preferences.userRoot().node("/org/altusmetrum/altosui");
component = ui;
/* Initialize logdir from preferences */
String logdir_string = preferences.get(logdirPreference, null);
if (logdir_string != null)
logdir = new File(logdir_string);
else {
/* Use the file system view default directory */
logdir = new File(FileSystemView.getFileSystemView().getDefaultDirectory(), logdirName);
if (!logdir.exists())
logdir.mkdirs();
}
channel = preferences.getInt(channelPreference, 0);
voice = preferences.getBoolean(voicePreference, true);
callsign = preferences.get(callsignPreference,"N0CALL");
}
static void flush_preferences() {
try {
preferences.flush();
} catch (BackingStoreException ee) {
JOptionPane.showMessageDialog(component,
preferences.absolutePath(),
"Cannot save prefernces",
JOptionPane.ERROR_MESSAGE);
}
}
public static void set_logdir(File new_logdir) {
logdir = new_logdir;
synchronized (preferences) {
preferences.put(logdirPreference, logdir.getPath());
flush_preferences();
}
}
private static boolean check_dir(File dir) {
if (!dir.exists()) {
if (!dir.mkdirs()) {
JOptionPane.showMessageDialog(component,
dir.getName(),
"Cannot create directory",
JOptionPane.ERROR_MESSAGE);
return false;
}
} else if (!dir.isDirectory()) {
JOptionPane.showMessageDialog(component,
dir.getName(),
"Is not a directory",
JOptionPane.ERROR_MESSAGE);
return false;
}
return true;
}
/* Configure the log directory. This is where all telemetry and eeprom files
* will be written to, and where replay will look for telemetry files
*/
public static void ConfigureLog() {
JFileChooser logdir_chooser = new JFileChooser(logdir.getParentFile());
logdir_chooser.setDialogTitle("Configure Data Logging Directory");
logdir_chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
if (logdir_chooser.showDialog(component, "Select Directory") == JFileChooser.APPROVE_OPTION) {
File dir = logdir_chooser.getSelectedFile();
if (check_dir(dir))
set_logdir(dir);
}
}
public static File logdir() {
return logdir;
}
public static void set_channel(int new_channel) {
channel = new_channel;
synchronized (preferences) {
preferences.putInt(channelPreference, channel);
flush_preferences();
}
}
public static int channel() {
return channel;
}
public static void set_voice(boolean new_voice) {
voice = new_voice;
synchronized (preferences) {
preferences.putBoolean(voicePreference, voice);
flush_preferences();
}
}
public static boolean voice() {
return voice;
}
public static void set_callsign(String new_callsign) {
callsign = new_callsign;
synchronized(preferences) {
preferences.put(callsignPreference, callsign);
flush_preferences();
}
}
public static String callsign() {
return callsign;
}
}
|