submodule madness
[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_9;
19
20 import java.io.*;
21 import java.util.*;
22 import java.awt.Component;
23 import javax.swing.*;
24 import org.altusmetrum.altoslib_9.*;
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         public static void init() {
59                 AltosPreferences.init(new AltosUIPreferencesBackend());
60
61                 font_listeners = new LinkedList<AltosFontListener>();
62
63                 font_size = backend.getInt(fontSizePreference, AltosUILib.font_size_medium);
64                 AltosUILib.set_fonts(font_size);
65                 look_and_feel = backend.getString(lookAndFeelPreference, UIManager.getSystemLookAndFeelClassName());
66
67                 ui_listeners = new LinkedList<AltosUIListener>();
68                 serial_debug = backend.getBoolean(serialDebugPreference, false);
69
70                 AltosLink.set_debug(serial_debug);
71
72                 position = backend.getInt(positionPreference, AltosUILib.position_top_left);
73                 position_listeners = new LinkedList<AltosPositionListener>();
74         }
75
76         static { init(); }
77
78         public static void set_component(Component in_component) {
79                 component = in_component;
80         }
81
82         private static boolean check_dir(File dir) {
83                 if (!dir.exists()) {
84                         if (!dir.mkdirs()) {
85                                 JOptionPane.showMessageDialog(component,
86                                                               dir.getName(),
87                                                               "Cannot create directory",
88                                                               JOptionPane.ERROR_MESSAGE);
89                                 return false;
90                         }
91                 } else if (!dir.isDirectory()) {
92                         JOptionPane.showMessageDialog(component,
93                                                       dir.getName(),
94                                                       "Is not a directory",
95                                                       JOptionPane.ERROR_MESSAGE);
96                         return false;
97                 }
98                 return true;
99         }
100
101         /* Configure the log directory. This is where all telemetry and eeprom files
102          * will be written to, and where replay will look for telemetry files
103          */
104         public static void ConfigureLog() {
105                 JFileChooser    logdir_chooser = new JFileChooser(logdir.getParentFile());
106
107                 logdir_chooser.setDialogTitle("Configure Data Logging Directory");
108                 logdir_chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
109
110                 if (logdir_chooser.showDialog(component, "Select Directory") == JFileChooser.APPROVE_OPTION) {
111                         File dir = logdir_chooser.getSelectedFile();
112                         if (check_dir(dir))
113                                 set_logdir(dir);
114                 }
115         }
116         public static int font_size() {
117                 synchronized (backend) {
118                         return font_size;
119                 }
120         }
121
122         static void set_fonts() {
123         }
124
125         public static void set_font_size(int new_font_size) {
126                 synchronized (backend) {
127                         font_size = new_font_size;
128                         backend.putInt(fontSizePreference, font_size);
129                         flush_preferences();
130                         AltosUILib.set_fonts(font_size);
131                         for (AltosFontListener l : font_listeners)
132                                 l.font_size_changed(font_size);
133                 }
134         }
135
136         public static void register_font_listener(AltosFontListener l) {
137                 synchronized (backend) {
138                         font_listeners.add(l);
139                 }
140         }
141
142         public static void unregister_font_listener(AltosFontListener l) {
143                 synchronized (backend) {
144                         font_listeners.remove(l);
145                 }
146         }
147
148         public static void set_look_and_feel(String new_look_and_feel) {
149                 try {
150                         UIManager.setLookAndFeel(new_look_and_feel);
151                 } catch (Exception e) {
152                 }
153                 synchronized(backend) {
154                         look_and_feel = new_look_and_feel;
155                         backend.putString(lookAndFeelPreference, look_and_feel);
156                         flush_preferences();
157                         for (AltosUIListener l : ui_listeners)
158                                 l.ui_changed(look_and_feel);
159                 }
160         }
161
162         public static String look_and_feel() {
163                 synchronized (backend) {
164                         return look_and_feel;
165                 }
166         }
167
168         public static void register_ui_listener(AltosUIListener l) {
169                 synchronized(backend) {
170                         ui_listeners.add(l);
171                 }
172         }
173
174         public static void unregister_ui_listener(AltosUIListener l) {
175                 synchronized (backend) {
176                         ui_listeners.remove(l);
177                 }
178         }
179         public static void set_serial_debug(boolean new_serial_debug) {
180                 AltosLink.set_debug(new_serial_debug);
181                 synchronized (backend) {
182                         serial_debug = new_serial_debug;
183                         backend.putBoolean(serialDebugPreference, serial_debug);
184                         flush_preferences();
185                 }
186         }
187
188         public static boolean serial_debug() {
189                 synchronized (backend) {
190                         return serial_debug;
191                 }
192         }
193
194         public static void register_position_listener(AltosPositionListener l) {
195                 synchronized(backend) {
196                         position_listeners.add(l);
197                 }
198         }
199
200         public static void unregister_position_listener(AltosPositionListener l) {
201                 synchronized (backend) {
202                         position_listeners.remove(l);
203                 }
204         }
205
206         public static void set_position(int new_position) {
207                 synchronized (backend) {
208                         position = new_position;
209                         backend.putInt(positionPreference, position);
210                         flush_preferences();
211                         for (AltosPositionListener l : position_listeners)
212                                 l.position_changed(position);
213                 }
214         }
215
216         public static int position() {
217                 synchronized (backend) {
218                         return position;
219                 }
220         }
221 }