altosui/altoslib: bug fixes, update Makefile.am
[fw/altos] / altosui / 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 altosui;
19
20 import java.io.*;
21 import java.util.*;
22 import java.awt.Component;
23 import javax.swing.*;
24 import org.altusmetrum.AltosLib.*;
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         /* UI Component to pop dialogs up */
35         static Component component;
36
37         static LinkedList<AltosFontListener> font_listeners;
38
39         static int font_size = Altos.font_size_medium;
40
41         static LinkedList<AltosUIListener> ui_listeners;
42
43         static String look_and_feel = null;
44
45         /* Serial debug */
46         static boolean serial_debug;
47
48         public static void init(AltosUIPreferencesBackend in_backend) {
49                 AltosPreferences.init(in_backend);
50
51                 font_listeners = new LinkedList<AltosFontListener>();
52
53                 font_size = backend.getInt(fontSizePreference, Altos.font_size_medium);
54                 Altos.set_fonts(font_size);
55                 look_and_feel = backend.getString(lookAndFeelPreference, UIManager.getSystemLookAndFeelClassName());
56
57                 ui_listeners = new LinkedList<AltosUIListener>();
58                 serial_debug = backend.getBoolean(serialDebugPreference, false);
59                 AltosLink.set_debug(serial_debug);
60         }
61
62         static void set_component(Component in_component) {
63                 component = in_component;
64         }
65
66         private static boolean check_dir(File dir) {
67                 if (!dir.exists()) {
68                         if (!dir.mkdirs()) {
69                                 JOptionPane.showMessageDialog(component,
70                                                               dir.getName(),
71                                                               "Cannot create directory",
72                                                               JOptionPane.ERROR_MESSAGE);
73                                 return false;
74                         }
75                 } else if (!dir.isDirectory()) {
76                         JOptionPane.showMessageDialog(component,
77                                                       dir.getName(),
78                                                       "Is not a directory",
79                                                       JOptionPane.ERROR_MESSAGE);
80                         return false;
81                 }
82                 return true;
83         }
84
85         /* Configure the log directory. This is where all telemetry and eeprom files
86          * will be written to, and where replay will look for telemetry files
87          */
88         public static void ConfigureLog() {
89                 JFileChooser    logdir_chooser = new JFileChooser(logdir.getParentFile());
90
91                 logdir_chooser.setDialogTitle("Configure Data Logging Directory");
92                 logdir_chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
93
94                 if (logdir_chooser.showDialog(component, "Select Directory") == JFileChooser.APPROVE_OPTION) {
95                         File dir = logdir_chooser.getSelectedFile();
96                         if (check_dir(dir))
97                                 set_logdir(dir);
98                 }
99         }
100         public static int font_size() {
101                 synchronized (backend) {
102                         return font_size;
103                 }
104         }
105
106         static void set_fonts() {
107         }
108
109         public static void set_font_size(int new_font_size) {
110                 synchronized (backend) {
111                         font_size = new_font_size;
112                         backend.putInt(fontSizePreference, font_size);
113                         flush_preferences();
114                         Altos.set_fonts(font_size);
115                         for (AltosFontListener l : font_listeners)
116                                 l.font_size_changed(font_size);
117                 }
118         }
119
120         public static void register_font_listener(AltosFontListener l) {
121                 synchronized (backend) {
122                         font_listeners.add(l);
123                 }
124         }
125
126         public static void unregister_font_listener(AltosFontListener l) {
127                 synchronized (backend) {
128                         font_listeners.remove(l);
129                 }
130         }
131
132         public static void set_look_and_feel(String new_look_and_feel) {
133                 try {
134                         UIManager.setLookAndFeel(new_look_and_feel);
135                 } catch (Exception e) {
136                 }
137                 synchronized(backend) {
138                         look_and_feel = new_look_and_feel;
139                         backend.putString(lookAndFeelPreference, look_and_feel);
140                         flush_preferences();
141                         for (AltosUIListener l : ui_listeners)
142                                 l.ui_changed(look_and_feel);
143                 }
144         }
145
146         public static String look_and_feel() {
147                 synchronized (backend) {
148                         return look_and_feel;
149                 }
150         }
151
152         public static void register_ui_listener(AltosUIListener l) {
153                 synchronized(backend) {
154                         ui_listeners.add(l);
155                 }
156         }
157
158         public static void unregister_ui_listener(AltosUIListener l) {
159                 synchronized (backend) {
160                         ui_listeners.remove(l);
161                 }
162         }
163         public static void set_serial_debug(boolean new_serial_debug) {
164                 AltosLink.set_debug(serial_debug);
165                 synchronized (backend) {
166                         serial_debug = new_serial_debug;
167                         backend.putBoolean(serialDebugPreference, serial_debug);
168                         flush_preferences();
169                 }
170         }
171
172         public static boolean serial_debug() {
173                 synchronized (backend) {
174                         return serial_debug;
175                 }
176         }
177
178 }