doc: Add 1.9.5 release notes
[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                                 System.out.printf("notifying %s of font size change\n", l);
134                                 l.font_size_changed(font_size);
135                         }
136                         System.out.printf("all fonts changed\n");
137                 }
138         }
139
140         public static void register_font_listener(AltosFontListener l) {
141                 System.out.printf("register font listener\n");
142                 synchronized (backend) {
143                         font_listeners.add(l);
144                 }
145         }
146
147         public static void unregister_font_listener(AltosFontListener l) {
148                 System.out.printf("unregister font listener\n");
149                 synchronized (backend) {
150                         font_listeners.remove(l);
151                 }
152         }
153
154         public static void set_look_and_feel(String new_look_and_feel) {
155                 try {
156                         UIManager.setLookAndFeel(new_look_and_feel);
157                 } catch (Exception e) {
158                 }
159                 synchronized(backend) {
160                         look_and_feel = new_look_and_feel;
161                         backend.putString(lookAndFeelPreference, look_and_feel);
162                         flush_preferences();
163                         for (AltosUIListener l : ui_listeners)
164                                 l.ui_changed(look_and_feel);
165                 }
166         }
167
168         public static String look_and_feel() {
169                 synchronized (backend) {
170                         return look_and_feel;
171                 }
172         }
173
174         public static void register_ui_listener(AltosUIListener l) {
175                 synchronized(backend) {
176                         ui_listeners.add(l);
177                 }
178         }
179
180         public static void unregister_ui_listener(AltosUIListener l) {
181                 synchronized (backend) {
182                         ui_listeners.remove(l);
183                 }
184         }
185         public static void set_serial_debug(boolean new_serial_debug) {
186                 AltosLink.set_debug(new_serial_debug);
187                 synchronized (backend) {
188                         serial_debug = new_serial_debug;
189                         backend.putBoolean(serialDebugPreference, serial_debug);
190                         flush_preferences();
191                 }
192         }
193
194         public static boolean serial_debug() {
195                 synchronized (backend) {
196                         return serial_debug;
197                 }
198         }
199
200         public static void register_position_listener(AltosPositionListener l) {
201                 synchronized(backend) {
202                         position_listeners.add(l);
203                 }
204         }
205
206         public static void unregister_position_listener(AltosPositionListener l) {
207                 synchronized (backend) {
208                         position_listeners.remove(l);
209                 }
210         }
211
212         public static void set_position(int new_position) {
213                 synchronized (backend) {
214                         position = new_position;
215                         backend.putInt(positionPreference, position);
216                         flush_preferences();
217                         for (AltosPositionListener l : position_listeners)
218                                 l.position_changed(position);
219                 }
220         }
221
222         public static int position() {
223                 synchronized (backend) {
224                         return position;
225                 }
226         }
227 }