Merge branch 'master' of ssh://git.gag.com/scm/git/fw/altos
[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; 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.altosuilib_3;
19
20 import java.io.*;
21 import java.util.*;
22 import java.awt.Component;
23 import javax.swing.*;
24 import org.altusmetrum.altoslib_5.*;
25
26 public class AltosUIPreferences extends AltosPreferences {
27
28         /* font size preferences name */
29         final static String fontSizePreference = "FONT-SIZE";
30
31         /* Look&Feel preference name */
32         final static String lookAndFeelPreference = "LOOK-AND-FEEL";
33
34         /* Window position preference name */
35         final static String positionPreference = "POSITION";
36
37         /* Maps cache size preference name */
38         final static String mapCachePreference = "MAP-CACHE";
39
40         /* UI Component to pop dialogs up */
41         static Component component;
42
43         static LinkedList<AltosFontListener> font_listeners;
44
45         static int font_size = AltosUILib.font_size_medium;
46
47         static LinkedList<AltosUIListener> ui_listeners;
48
49         static String look_and_feel = null;
50
51         /* Serial debug */
52         public static boolean serial_debug;
53
54         static LinkedList<AltosPositionListener> position_listeners;
55
56         public static int position = AltosUILib.position_top_left;
57
58         static LinkedList<AltosUIMapCacheListener> map_cache_listeners;
59
60         public static int map_cache = 9;
61
62         public static void init() {
63                 AltosPreferences.init(new AltosUIPreferencesBackend());
64
65                 font_listeners = new LinkedList<AltosFontListener>();
66
67                 font_size = backend.getInt(fontSizePreference, AltosUILib.font_size_medium);
68                 AltosUILib.set_fonts(font_size);
69                 look_and_feel = backend.getString(lookAndFeelPreference, UIManager.getSystemLookAndFeelClassName());
70
71                 ui_listeners = new LinkedList<AltosUIListener>();
72                 serial_debug = backend.getBoolean(serialDebugPreference, false);
73
74                 AltosLink.set_debug(serial_debug);
75
76                 position = backend.getInt(positionPreference, AltosUILib.position_top_left);
77                 position_listeners = new LinkedList<AltosPositionListener>();
78
79                 map_cache = backend.getInt(mapCachePreference, 9);
80                 map_cache_listeners = new LinkedList<AltosUIMapCacheListener>();
81         }
82
83         static { init(); }
84
85         public static void set_component(Component in_component) {
86                 component = in_component;
87         }
88
89         private static boolean check_dir(File dir) {
90                 if (!dir.exists()) {
91                         if (!dir.mkdirs()) {
92                                 JOptionPane.showMessageDialog(component,
93                                                               dir.getName(),
94                                                               "Cannot create directory",
95                                                               JOptionPane.ERROR_MESSAGE);
96                                 return false;
97                         }
98                 } else if (!dir.isDirectory()) {
99                         JOptionPane.showMessageDialog(component,
100                                                       dir.getName(),
101                                                       "Is not a directory",
102                                                       JOptionPane.ERROR_MESSAGE);
103                         return false;
104                 }
105                 return true;
106         }
107
108         /* Configure the log directory. This is where all telemetry and eeprom files
109          * will be written to, and where replay will look for telemetry files
110          */
111         public static void ConfigureLog() {
112                 JFileChooser    logdir_chooser = new JFileChooser(logdir.getParentFile());
113
114                 logdir_chooser.setDialogTitle("Configure Data Logging Directory");
115                 logdir_chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
116
117                 if (logdir_chooser.showDialog(component, "Select Directory") == JFileChooser.APPROVE_OPTION) {
118                         File dir = logdir_chooser.getSelectedFile();
119                         if (check_dir(dir))
120                                 set_logdir(dir);
121                 }
122         }
123         public static int font_size() {
124                 synchronized (backend) {
125                         return font_size;
126                 }
127         }
128
129         static void set_fonts() {
130         }
131
132         public static void set_font_size(int new_font_size) {
133                 synchronized (backend) {
134                         font_size = new_font_size;
135                         backend.putInt(fontSizePreference, font_size);
136                         flush_preferences();
137                         AltosUILib.set_fonts(font_size);
138                         for (AltosFontListener l : font_listeners)
139                                 l.font_size_changed(font_size);
140                 }
141         }
142
143         public static void register_font_listener(AltosFontListener l) {
144                 synchronized (backend) {
145                         font_listeners.add(l);
146                 }
147         }
148
149         public static void unregister_font_listener(AltosFontListener l) {
150                 synchronized (backend) {
151                         font_listeners.remove(l);
152                 }
153         }
154
155         public static void set_look_and_feel(String new_look_and_feel) {
156                 try {
157                         UIManager.setLookAndFeel(new_look_and_feel);
158                 } catch (Exception e) {
159                 }
160                 synchronized(backend) {
161                         look_and_feel = new_look_and_feel;
162                         backend.putString(lookAndFeelPreference, look_and_feel);
163                         flush_preferences();
164                         for (AltosUIListener l : ui_listeners)
165                                 l.ui_changed(look_and_feel);
166                 }
167         }
168
169         public static String look_and_feel() {
170                 synchronized (backend) {
171                         return look_and_feel;
172                 }
173         }
174
175         public static void register_ui_listener(AltosUIListener l) {
176                 synchronized(backend) {
177                         ui_listeners.add(l);
178                 }
179         }
180
181         public static void unregister_ui_listener(AltosUIListener l) {
182                 synchronized (backend) {
183                         ui_listeners.remove(l);
184                 }
185         }
186         public static void set_serial_debug(boolean new_serial_debug) {
187                 AltosLink.set_debug(new_serial_debug);
188                 synchronized (backend) {
189                         serial_debug = new_serial_debug;
190                         backend.putBoolean(serialDebugPreference, serial_debug);
191                         flush_preferences();
192                 }
193         }
194
195         public static boolean serial_debug() {
196                 synchronized (backend) {
197                         return serial_debug;
198                 }
199         }
200
201         public static void register_position_listener(AltosPositionListener l) {
202                 synchronized(backend) {
203                         position_listeners.add(l);
204                 }
205         }
206
207         public static void unregister_position_listener(AltosPositionListener l) {
208                 synchronized (backend) {
209                         position_listeners.remove(l);
210                 }
211         }
212
213         public static void set_position(int new_position) {
214                 synchronized (backend) {
215                         position = new_position;
216                         backend.putInt(positionPreference, position);
217                         flush_preferences();
218                         for (AltosPositionListener l : position_listeners)
219                                 l.position_changed(position);
220                 }
221         }
222
223         public static int position() {
224                 synchronized (backend) {
225                         return position;
226                 }
227         }
228
229         public static void register_map_cache_listener(AltosUIMapCacheListener l) {
230                 synchronized(backend) {
231                         map_cache_listeners.add(l);
232                 }
233         }
234
235         public static void unregister_map_cache_listener(AltosUIMapCacheListener l) {
236                 synchronized (backend) {
237                         map_cache_listeners.remove(l);
238                 }
239         }
240
241         public static void set_map_cache(int new_map_cache) {
242                 synchronized(backend) {
243                         map_cache = new_map_cache;
244                         backend.putInt(mapCachePreference, map_cache);
245                         flush_preferences();
246                         for (AltosUIMapCacheListener l: map_cache_listeners)
247                                 l.map_cache_changed(map_cache);
248                 }
249         }
250
251         public static int map_cache() {
252                 synchronized(backend) {
253                         return map_cache;
254                 }
255         }
256 }