Lots more work on the MicroPeak application
[fw/altos] / micropeak / MicroPreferences.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 org.altusmetrum.micropeak;
19
20 import java.io.*;
21 import java.util.*;
22 import java.awt.Component;
23 import javax.swing.*;
24 import java.awt.*;
25 import org.altusmetrum.AltosLib.*;
26
27 public class MicroPreferences extends AltosPreferences {
28
29         static final int tab_elt_pad = 5;
30
31         static Font label_font;
32         static Font value_font;
33         static Font status_font;
34         static Font table_label_font;
35         static Font table_value_font;
36
37         final static int font_size_small = 1;
38         final static int font_size_medium = 2;
39         final static int font_size_large = 3;
40
41         static void set_fonts(int size) {
42                 int     brief_size;
43                 int     table_size;
44                 int     status_size;
45
46                 switch (size) {
47                 case font_size_small:
48                         brief_size = 16;
49                         status_size = 18;
50                         table_size = 11;
51                         break;
52                 default:
53                 case font_size_medium:
54                         brief_size = 22;
55                         status_size = 24;
56                         table_size = 14;
57                         break;
58                 case font_size_large:
59                         brief_size = 26;
60                         status_size = 30;
61                         table_size = 17;
62                         break;
63                 }
64                 label_font = new Font("Dialog", Font.PLAIN, brief_size);
65                 value_font = new Font("Monospaced", Font.PLAIN, brief_size);
66                 status_font = new Font("SansSerif", Font.BOLD, status_size);
67                 table_label_font = new Font("SansSerif", Font.PLAIN, table_size);
68                 table_value_font = new Font("Monospaced", Font.PLAIN, table_size);
69         }
70
71         /* font size preferences name */
72         final static String fontSizePreference = "FONT-SIZE";
73
74         /* Look&Feel preference name */
75         final static String lookAndFeelPreference = "LOOK-AND-FEEL";
76
77         /* UI Component to pop dialogs up */
78         static Component component;
79
80         static LinkedList<MicroFontListener> font_listeners;
81
82         static int font_size = font_size_medium;
83
84         static LinkedList<MicroUIListener> ui_listeners;
85
86         static String look_and_feel = null;
87
88         /* Serial debug */
89         static boolean serial_debug;
90
91         public static void init() {
92                 AltosPreferences.init(new MicroPreferencesBackend());
93
94                 font_listeners = new LinkedList<MicroFontListener>();
95
96                 font_size = backend.getInt(fontSizePreference, font_size_medium);
97                 set_fonts(font_size);
98                 look_and_feel = backend.getString(lookAndFeelPreference, UIManager.getSystemLookAndFeelClassName());
99
100                 ui_listeners = new LinkedList<MicroUIListener>();
101                 serial_debug = backend.getBoolean(serialDebugPreference, false);
102         }
103
104         static { init(); }
105
106         static void set_component(Component in_component) {
107                 component = in_component;
108         }
109
110         private static boolean check_dir(File dir) {
111                 if (!dir.exists()) {
112                         if (!dir.mkdirs()) {
113                                 JOptionPane.showMessageDialog(component,
114                                                               dir.getName(),
115                                                               "Cannot create directory",
116                                                               JOptionPane.ERROR_MESSAGE);
117                                 return false;
118                         }
119                 } else if (!dir.isDirectory()) {
120                         JOptionPane.showMessageDialog(component,
121                                                       dir.getName(),
122                                                       "Is not a directory",
123                                                       JOptionPane.ERROR_MESSAGE);
124                         return false;
125                 }
126                 return true;
127         }
128
129         /* Configure the log directory. This is where all telemetry and eeprom files
130          * will be written to, and where replay will look for telemetry files
131          */
132         public static void ConfigureLog() {
133                 JFileChooser    logdir_chooser = new JFileChooser(logdir.getParentFile());
134
135                 logdir_chooser.setDialogTitle("Configure Data Logging Directory");
136                 logdir_chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
137
138                 if (logdir_chooser.showDialog(component, "Select Directory") == JFileChooser.APPROVE_OPTION) {
139                         File dir = logdir_chooser.getSelectedFile();
140                         if (check_dir(dir))
141                                 set_logdir(dir);
142                 }
143         }
144         public static int font_size() {
145                 synchronized (backend) {
146                         return font_size;
147                 }
148         }
149
150         static void set_fonts() {
151         }
152
153         public static void set_font_size(int new_font_size) {
154                 synchronized (backend) {
155                         font_size = new_font_size;
156                         backend.putInt(fontSizePreference, font_size);
157                         flush_preferences();
158                         set_fonts(font_size);
159                         for (MicroFontListener l : font_listeners)
160                                 l.font_size_changed(font_size);
161                 }
162         }
163
164         public static void register_font_listener(MicroFontListener l) {
165                 synchronized (backend) {
166                         font_listeners.add(l);
167                 }
168         }
169
170         public static void unregister_font_listener(MicroFontListener l) {
171                 synchronized (backend) {
172                         font_listeners.remove(l);
173                 }
174         }
175
176         public static void set_look_and_feel(String new_look_and_feel) {
177                 try {
178                         UIManager.setLookAndFeel(new_look_and_feel);
179                 } catch (Exception e) {
180                 }
181                 synchronized(backend) {
182                         look_and_feel = new_look_and_feel;
183                         backend.putString(lookAndFeelPreference, look_and_feel);
184                         flush_preferences();
185                         for (MicroUIListener l : ui_listeners)
186                                 l.ui_changed(look_and_feel);
187                 }
188         }
189
190         public static String look_and_feel() {
191                 synchronized (backend) {
192                         return look_and_feel;
193                 }
194         }
195
196         public static void register_ui_listener(MicroUIListener l) {
197                 synchronized(backend) {
198                         ui_listeners.add(l);
199                 }
200         }
201
202         public static void unregister_ui_listener(MicroUIListener l) {
203                 synchronized (backend) {
204                         ui_listeners.remove(l);
205                 }
206         }
207         public static void set_serial_debug(boolean new_serial_debug) {
208                 synchronized (backend) {
209                         serial_debug = new_serial_debug;
210                         backend.putBoolean(serialDebugPreference, serial_debug);
211                         flush_preferences();
212                 }
213         }
214
215         public static boolean serial_debug() {
216                 synchronized (backend) {
217                         return serial_debug;
218                 }
219         }
220
221 }