altoslib: When flashing hardware, pull USB data from device if needed
[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_13;
20
21 import java.io.*;
22 import java.util.*;
23 import java.awt.Component;
24 import javax.swing.*;
25 import org.altusmetrum.altoslib_13.*;
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         public static void register_font_listener(AltosFontListener l) {
138                 synchronized (backend) {
139                         font_listeners.add(l);
140                 }
141         }
142
143         public static void unregister_font_listener(AltosFontListener l) {
144                 synchronized (backend) {
145                         font_listeners.remove(l);
146                 }
147         }
148
149         public static void set_look_and_feel(String new_look_and_feel) {
150                 try {
151                         UIManager.setLookAndFeel(new_look_and_feel);
152                 } catch (Exception e) {
153                 }
154                 synchronized(backend) {
155                         look_and_feel = new_look_and_feel;
156                         backend.putString(lookAndFeelPreference, look_and_feel);
157                         flush_preferences();
158                         for (AltosUIListener l : ui_listeners)
159                                 l.ui_changed(look_and_feel);
160                 }
161         }
162
163         public static String look_and_feel() {
164                 synchronized (backend) {
165                         return look_and_feel;
166                 }
167         }
168
169         public static void register_ui_listener(AltosUIListener l) {
170                 synchronized(backend) {
171                         ui_listeners.add(l);
172                 }
173         }
174
175         public static void unregister_ui_listener(AltosUIListener l) {
176                 synchronized (backend) {
177                         ui_listeners.remove(l);
178                 }
179         }
180         public static void set_serial_debug(boolean new_serial_debug) {
181                 AltosLink.set_debug(new_serial_debug);
182                 synchronized (backend) {
183                         serial_debug = new_serial_debug;
184                         backend.putBoolean(serialDebugPreference, serial_debug);
185                         flush_preferences();
186                 }
187         }
188
189         public static boolean serial_debug() {
190                 synchronized (backend) {
191                         return serial_debug;
192                 }
193         }
194
195         public static void register_position_listener(AltosPositionListener l) {
196                 synchronized(backend) {
197                         position_listeners.add(l);
198                 }
199         }
200
201         public static void unregister_position_listener(AltosPositionListener l) {
202                 synchronized (backend) {
203                         position_listeners.remove(l);
204                 }
205         }
206
207         public static void set_position(int new_position) {
208                 synchronized (backend) {
209                         position = new_position;
210                         backend.putInt(positionPreference, position);
211                         flush_preferences();
212                         for (AltosPositionListener l : position_listeners)
213                                 l.position_changed(position);
214                 }
215         }
216
217         public static int position() {
218                 synchronized (backend) {
219                         return position;
220                 }
221         }
222 }