0296d9355db7a2086ade1aa97ce737c028ed58d2
[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         /* Default logdir is ~/TeleMetrum */
35         final static String logdirName = "TeleMetrum";
36
37         /* UI Component to pop dialogs up */
38         static Component component;
39
40         /* Log directory */
41         static File logdir;
42
43         public static void init(Component ui) {
44                 preferences = Preferences.userRoot().node("/org/altusmetrum/altosui");
45
46                 component = ui;
47
48                 /* Initialize logdir from preferences */
49                 String logdir_string = preferences.get(logdirPreference, null);
50                 if (logdir_string != null)
51                         logdir = new File(logdir_string);
52                 else {
53                         /* Use the file system view default directory */
54                         logdir = new File(FileSystemView.getFileSystemView().getDefaultDirectory(), logdirName);
55                         if (!logdir.exists())
56                                 logdir.mkdirs();
57                 }
58         }
59
60         static void flush_preferences() {
61                 try {
62                         preferences.flush();
63                 } catch (BackingStoreException ee) {
64                         JOptionPane.showMessageDialog(component,
65                                                       preferences.absolutePath(),
66                                                       "Cannot save prefernces",
67                                                       JOptionPane.ERROR_MESSAGE);
68                 }
69         }
70
71         public static void set_logdir(File new_logdir) {
72                 logdir = new_logdir;
73                 synchronized (preferences) {
74                         preferences.put(logdirPreference, logdir.getPath());
75                         flush_preferences();
76                 }
77         }
78
79         private static boolean check_dir(File dir) {
80                 if (!dir.exists()) {
81                         if (!dir.mkdirs()) {
82                                 JOptionPane.showMessageDialog(component,
83                                                               dir.getName(),
84                                                               "Cannot create directory",
85                                                               JOptionPane.ERROR_MESSAGE);
86                                 return false;
87                         }
88                 } else if (!dir.isDirectory()) {
89                         JOptionPane.showMessageDialog(component,
90                                                       dir.getName(),
91                                                       "Is not a directory",
92                                                       JOptionPane.ERROR_MESSAGE);
93                         return false;
94                 }
95                 return true;
96         }
97
98         /* Configure the log directory. This is where all telemetry and eeprom files
99          * will be written to, and where replay will look for telemetry files
100          */
101         public static void ConfigureLog() {
102                 JFileChooser    logdir_chooser = new JFileChooser(logdir.getParentFile());
103
104                 logdir_chooser.setDialogTitle("Configure Data Logging Directory");
105                 logdir_chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
106
107                 if (logdir_chooser.showDialog(component, "Select Directory") == JFileChooser.APPROVE_OPTION) {
108                         File dir = logdir_chooser.getSelectedFile();
109                         if (check_dir(dir))
110                                 set_logdir(dir);
111                 }
112         }
113
114         public static File logdir() {
115                 return logdir;
116         }
117 }