altosui: Split out UI-specific preferences
[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
29 /* import org.altusmetrum.AltosLib.*; */
30
31 class AltosUIPreferences extends AltosPreferences {
32
33         /* font size preferences name */
34         final static String fontSizePreference = "FONT-SIZE";
35
36         /* Look&Feel preference name */
37         final static String lookAndFeelPreference = "LOOK-AND-FEEL";
38
39         /* UI Component to pop dialogs up */
40         static Component component;
41
42         static LinkedList<AltosFontListener> font_listeners;
43
44         static int font_size = Altos.font_size_medium;
45
46         static LinkedList<AltosUIListener> ui_listeners;
47
48         static String look_and_feel = null;
49
50         public static void init() {
51                 font_listeners = new LinkedList<AltosFontListener>();
52
53                 font_size = preferences.getInt(fontSizePreference, Altos.font_size_medium);
54                 Altos.set_fonts(font_size);
55                 look_and_feel = preferences.get(lookAndFeelPreference, UIManager.getSystemLookAndFeelClassName());
56
57                 ui_listeners = new LinkedList<AltosUIListener>();
58         }
59
60         static { init(); }
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                 return font_size;
102         }
103
104         static void set_fonts() {
105         }
106
107         public static void set_font_size(int new_font_size) {
108                 font_size = new_font_size;
109                 synchronized (preferences) {
110                         preferences.putInt(fontSizePreference, font_size);
111                         flush_preferences();
112                         Altos.set_fonts(font_size);
113                         for (AltosFontListener l : font_listeners)
114                                 l.font_size_changed(font_size);
115                 }
116         }
117
118         public static void register_font_listener(AltosFontListener l) {
119                 synchronized (preferences) {
120                         font_listeners.add(l);
121                 }
122         }
123
124         public static void unregister_font_listener(AltosFontListener l) {
125                 synchronized (preferences) {
126                         font_listeners.remove(l);
127                 }
128         }
129
130         public static void set_look_and_feel(String new_look_and_feel) {
131                 look_and_feel = new_look_and_feel;
132                 try {
133                         UIManager.setLookAndFeel(look_and_feel);
134                 } catch (Exception e) {
135                 }
136                 synchronized(preferences) {
137                         preferences.put(lookAndFeelPreference, look_and_feel);
138                         flush_preferences();
139                         for (AltosUIListener l : ui_listeners)
140                                 l.ui_changed(look_and_feel);
141                 }
142         }
143
144         public static String look_and_feel() {
145                 return look_and_feel;
146         }
147
148         public static void register_ui_listener(AltosUIListener l) {
149                 synchronized(preferences) {
150                         ui_listeners.add(l);
151                 }
152         }
153
154         public static void unregister_ui_listener(AltosUIListener l) {
155                 synchronized (preferences) {
156                         ui_listeners.remove(l);
157                 }
158         }
159 }