altos/test: Adjust CRC error rate after FEC fix
[fw/altos] / altosuilib / 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; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with this program; if not, write to the Free Software Foundation, Inc.,
16  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
17  */
18
19 package org.altusmetrum.altosuilib_14;
20
21 import java.io.*;
22 import java.util.*;
23 import java.awt.Component;
24 import javax.swing.*;
25 import org.altusmetrum.altoslib_14.*;
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         /* Window position preference name */
36         final static String positionPreference = "POSITION";
37
38         /* Maps cache size preference name */
39         final static String mapCachePreference = "MAP-CACHE";
40
41         /* UI Component to pop dialogs up */
42         static Component component;
43
44         static LinkedList<AltosFontListener> font_listeners;
45
46         static int font_size = AltosUILib.font_size_medium;
47
48         static LinkedList<AltosUIListener> ui_listeners;
49
50         static String look_and_feel = null;
51
52         /* Serial debug */
53         public static boolean serial_debug;
54
55         static LinkedList<AltosPositionListener> position_listeners;
56
57         public static int position = AltosUILib.position_top_left;
58
59         public static void init() {
60                 AltosPreferences.init(new AltosUIPreferencesBackend());
61
62                 font_listeners = new LinkedList<AltosFontListener>();
63
64                 font_size = backend.getInt(fontSizePreference, AltosUILib.font_size_medium);
65                 AltosUILib.set_fonts(font_size);
66                 look_and_feel = backend.getString(lookAndFeelPreference, UIManager.getSystemLookAndFeelClassName());
67
68                 ui_listeners = new LinkedList<AltosUIListener>();
69                 serial_debug = backend.getBoolean(serialDebugPreference, false);
70
71                 AltosLink.set_debug(serial_debug);
72
73                 position = backend.getInt(positionPreference, AltosUILib.position_top_left);
74                 position_listeners = new LinkedList<AltosPositionListener>();
75         }
76
77         static { init(); }
78
79         public static void set_component(Component in_component) {
80                 component = in_component;
81         }
82
83         private static boolean check_dir(File dir) {
84                 if (!dir.exists()) {
85                         if (!dir.mkdirs()) {
86                                 JOptionPane.showMessageDialog(component,
87                                                               dir.getName(),
88                                                               "Cannot create directory",
89                                                               JOptionPane.ERROR_MESSAGE);
90                                 return false;
91                         }
92                 } else if (!dir.isDirectory()) {
93                         JOptionPane.showMessageDialog(component,
94                                                       dir.getName(),
95                                                       "Is not a directory",
96                                                       JOptionPane.ERROR_MESSAGE);
97                         return false;
98                 }
99                 return true;
100         }
101
102         /* Configure the log directory. This is where all telemetry and eeprom files
103          * will be written to, and where replay will look for telemetry files
104          */
105         public static void ConfigureLog() {
106                 JFileChooser    logdir_chooser = new JFileChooser(logdir.getParentFile());
107
108                 logdir_chooser.setDialogTitle("Configure Data Logging Directory");
109                 logdir_chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
110
111                 if (logdir_chooser.showDialog(component, "Select Directory") == JFileChooser.APPROVE_OPTION) {
112                         File dir = logdir_chooser.getSelectedFile();
113                         if (check_dir(dir))
114                                 set_logdir(dir);
115                 }
116         }
117         public static int font_size() {
118                 synchronized (backend) {
119                         return font_size;
120                 }
121         }
122
123         static void set_fonts() {
124         }
125
126         public static void set_font_size(int new_font_size) {
127                 synchronized (backend) {
128                         font_size = new_font_size;
129                         backend.putInt(fontSizePreference, font_size);
130                         flush_preferences();
131                         AltosUILib.set_fonts(font_size);
132                         for (AltosFontListener l : font_listeners) {
133                                 l.font_size_changed(font_size);
134                         }
135                 }
136         }
137
138         public static void register_font_listener(AltosFontListener l) {
139                 synchronized (backend) {
140                         font_listeners.add(l);
141                 }
142         }
143
144         public static void unregister_font_listener(AltosFontListener l) {
145                 synchronized (backend) {
146                         font_listeners.remove(l);
147                 }
148         }
149
150         public static void set_look_and_feel(String new_look_and_feel) {
151                 try {
152                         UIManager.setLookAndFeel(new_look_and_feel);
153                 } catch (Exception e) {
154                 }
155                 synchronized(backend) {
156                         look_and_feel = new_look_and_feel;
157                         backend.putString(lookAndFeelPreference, look_and_feel);
158                         flush_preferences();
159                         for (AltosUIListener l : ui_listeners)
160                                 l.ui_changed(look_and_feel);
161                 }
162         }
163
164         public static String look_and_feel() {
165                 synchronized (backend) {
166                         return look_and_feel;
167                 }
168         }
169
170         public static void register_ui_listener(AltosUIListener l) {
171                 synchronized(backend) {
172                         ui_listeners.add(l);
173                 }
174         }
175
176         public static void unregister_ui_listener(AltosUIListener l) {
177                 synchronized (backend) {
178                         ui_listeners.remove(l);
179                 }
180         }
181         public static void set_serial_debug(boolean new_serial_debug) {
182                 AltosLink.set_debug(new_serial_debug);
183                 synchronized (backend) {
184                         serial_debug = new_serial_debug;
185                         backend.putBoolean(serialDebugPreference, serial_debug);
186                         flush_preferences();
187                 }
188         }
189
190         public static boolean serial_debug() {
191                 synchronized (backend) {
192                         return serial_debug;
193                 }
194         }
195
196         public static void register_position_listener(AltosPositionListener l) {
197                 synchronized(backend) {
198                         position_listeners.add(l);
199                 }
200         }
201
202         public static void unregister_position_listener(AltosPositionListener l) {
203                 synchronized (backend) {
204                         position_listeners.remove(l);
205                 }
206         }
207
208         public static void set_position(int new_position) {
209                 synchronized (backend) {
210                         position = new_position;
211                         backend.putInt(positionPreference, position);
212                         flush_preferences();
213                         for (AltosPositionListener l : position_listeners)
214                                 l.position_changed(position);
215                 }
216         }
217
218         public static int position() {
219                 synchronized (backend) {
220                         return position;
221                 }
222         }
223 }