Merge remote-tracking branch 'origin/micropeak-logging'
[fw/altos] / altosui / AltosUIPreferences.java
1 /*
2  * Copyright © 2011 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
20 import java.io.*;
21 import java.util.*;
22 import java.awt.Component;
23 import javax.swing.*;
24 import org.altusmetrum.AltosLib.*;
25 import org.altusmetrum.altosuilib.*;
26
27 public class AltosUIPreferences extends AltosPreferences {
28
29         /* font size preferences name */
30         final static String fontSizePreference = "FONT-SIZE";
31
32         /* Look&Feel preference name */
33         final static String lookAndFeelPreference = "LOOK-AND-FEEL";
34
35         /* UI Component to pop dialogs up */
36         static Component component;
37
38         static LinkedList<AltosFontListener> font_listeners;
39
40         static int font_size = Altos.font_size_medium;
41
42         static LinkedList<AltosUIListener> ui_listeners;
43
44         static String look_and_feel = null;
45
46         /* Serial debug */
47         static boolean serial_debug;
48
49         public static void init() {
50                 AltosPreferences.init(new AltosUIPreferencesBackend());
51
52                 font_listeners = new LinkedList<AltosFontListener>();
53
54                 font_size = backend.getInt(fontSizePreference, Altos.font_size_medium);
55                 Altos.set_fonts(font_size);
56                 look_and_feel = backend.getString(lookAndFeelPreference, UIManager.getSystemLookAndFeelClassName());
57
58                 ui_listeners = new LinkedList<AltosUIListener>();
59                 serial_debug = backend.getBoolean(serialDebugPreference, false);
60                 AltosLink.set_debug(serial_debug);
61         }
62
63         static { init(); }
64
65         static void set_component(Component in_component) {
66                 component = in_component;
67         }
68
69         private static boolean check_dir(File dir) {
70                 if (!dir.exists()) {
71                         if (!dir.mkdirs()) {
72                                 JOptionPane.showMessageDialog(component,
73                                                               dir.getName(),
74                                                               "Cannot create directory",
75                                                               JOptionPane.ERROR_MESSAGE);
76                                 return false;
77                         }
78                 } else if (!dir.isDirectory()) {
79                         JOptionPane.showMessageDialog(component,
80                                                       dir.getName(),
81                                                       "Is not a directory",
82                                                       JOptionPane.ERROR_MESSAGE);
83                         return false;
84                 }
85                 return true;
86         }
87
88         /* Configure the log directory. This is where all telemetry and eeprom files
89          * will be written to, and where replay will look for telemetry files
90          */
91         public static void ConfigureLog() {
92                 JFileChooser    logdir_chooser = new JFileChooser(logdir.getParentFile());
93
94                 logdir_chooser.setDialogTitle("Configure Data Logging Directory");
95                 logdir_chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
96
97                 if (logdir_chooser.showDialog(component, "Select Directory") == JFileChooser.APPROVE_OPTION) {
98                         File dir = logdir_chooser.getSelectedFile();
99                         if (check_dir(dir))
100                                 set_logdir(dir);
101                 }
102         }
103         public static int font_size() {
104                 synchronized (backend) {
105                         return font_size;
106                 }
107         }
108
109         static void set_fonts() {
110         }
111
112         public static void set_font_size(int new_font_size) {
113                 synchronized (backend) {
114                         font_size = new_font_size;
115                         backend.putInt(fontSizePreference, font_size);
116                         flush_preferences();
117                         Altos.set_fonts(font_size);
118                         for (AltosFontListener l : font_listeners)
119                                 l.font_size_changed(font_size);
120                 }
121         }
122
123         public static void register_font_listener(AltosFontListener l) {
124                 synchronized (backend) {
125                         font_listeners.add(l);
126                 }
127         }
128
129         public static void unregister_font_listener(AltosFontListener l) {
130                 synchronized (backend) {
131                         font_listeners.remove(l);
132                 }
133         }
134
135         public static void set_look_and_feel(String new_look_and_feel) {
136                 try {
137                         UIManager.setLookAndFeel(new_look_and_feel);
138                 } catch (Exception e) {
139                 }
140                 synchronized(backend) {
141                         look_and_feel = new_look_and_feel;
142                         backend.putString(lookAndFeelPreference, look_and_feel);
143                         flush_preferences();
144                         for (AltosUIListener l : ui_listeners)
145                                 l.ui_changed(look_and_feel);
146                 }
147         }
148
149         public static String look_and_feel() {
150                 synchronized (backend) {
151                         return look_and_feel;
152                 }
153         }
154
155         public static void register_ui_listener(AltosUIListener l) {
156                 synchronized(backend) {
157                         ui_listeners.add(l);
158                 }
159         }
160
161         public static void unregister_ui_listener(AltosUIListener l) {
162                 synchronized (backend) {
163                         ui_listeners.remove(l);
164                 }
165         }
166         public static void set_serial_debug(boolean new_serial_debug) {
167                 AltosLink.set_debug(new_serial_debug);
168                 synchronized (backend) {
169                         serial_debug = new_serial_debug;
170                         backend.putBoolean(serialDebugPreference, serial_debug);
171                         flush_preferences();
172                 }
173         }
174
175         public static boolean serial_debug() {
176                 synchronized (backend) {
177                         return serial_debug;
178                 }
179         }
180
181 }