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