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