2 * Copyright © 2011 Keith Packard <keithp@keithp.com>
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.
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.
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.
18 package org.altusmetrum.altosuilib_6;
22 import java.awt.Component;
24 import org.altusmetrum.altoslib_6.*;
26 public class AltosUIPreferences extends AltosPreferences {
28 /* font size preferences name */
29 final static String fontSizePreference = "FONT-SIZE";
31 /* Look&Feel preference name */
32 final static String lookAndFeelPreference = "LOOK-AND-FEEL";
34 /* Window position preference name */
35 final static String positionPreference = "POSITION";
37 /* Maps cache size preference name */
38 final static String mapCachePreference = "MAP-CACHE";
40 /* UI Component to pop dialogs up */
41 static Component component;
43 static LinkedList<AltosFontListener> font_listeners;
45 static int font_size = AltosUILib.font_size_medium;
47 static LinkedList<AltosUIListener> ui_listeners;
49 static String look_and_feel = null;
52 public static boolean serial_debug;
54 static LinkedList<AltosPositionListener> position_listeners;
56 public static int position = AltosUILib.position_top_left;
58 static LinkedList<AltosUIMapCacheListener> map_cache_listeners;
60 public static int map_cache = 9;
62 public static void init() {
63 AltosPreferences.init(new AltosUIPreferencesBackend());
65 font_listeners = new LinkedList<AltosFontListener>();
67 font_size = backend.getInt(fontSizePreference, AltosUILib.font_size_medium);
68 AltosUILib.set_fonts(font_size);
69 look_and_feel = backend.getString(lookAndFeelPreference, UIManager.getSystemLookAndFeelClassName());
71 ui_listeners = new LinkedList<AltosUIListener>();
72 serial_debug = backend.getBoolean(serialDebugPreference, false);
74 AltosLink.set_debug(serial_debug);
76 position = backend.getInt(positionPreference, AltosUILib.position_top_left);
77 position_listeners = new LinkedList<AltosPositionListener>();
79 map_cache = backend.getInt(mapCachePreference, 9);
80 map_cache_listeners = new LinkedList<AltosUIMapCacheListener>();
85 public static void set_component(Component in_component) {
86 component = in_component;
89 private static boolean check_dir(File dir) {
92 JOptionPane.showMessageDialog(component,
94 "Cannot create directory",
95 JOptionPane.ERROR_MESSAGE);
98 } else if (!dir.isDirectory()) {
99 JOptionPane.showMessageDialog(component,
101 "Is not a directory",
102 JOptionPane.ERROR_MESSAGE);
108 /* Configure the log directory. This is where all telemetry and eeprom files
109 * will be written to, and where replay will look for telemetry files
111 public static void ConfigureLog() {
112 JFileChooser logdir_chooser = new JFileChooser(logdir.getParentFile());
114 logdir_chooser.setDialogTitle("Configure Data Logging Directory");
115 logdir_chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
117 if (logdir_chooser.showDialog(component, "Select Directory") == JFileChooser.APPROVE_OPTION) {
118 File dir = logdir_chooser.getSelectedFile();
123 public static int font_size() {
124 synchronized (backend) {
129 static void set_fonts() {
132 public static void set_font_size(int new_font_size) {
133 synchronized (backend) {
134 font_size = new_font_size;
135 backend.putInt(fontSizePreference, font_size);
137 AltosUILib.set_fonts(font_size);
138 for (AltosFontListener l : font_listeners)
139 l.font_size_changed(font_size);
143 public static void register_font_listener(AltosFontListener l) {
144 synchronized (backend) {
145 font_listeners.add(l);
149 public static void unregister_font_listener(AltosFontListener l) {
150 synchronized (backend) {
151 font_listeners.remove(l);
155 public static void set_look_and_feel(String new_look_and_feel) {
157 UIManager.setLookAndFeel(new_look_and_feel);
158 } catch (Exception e) {
160 synchronized(backend) {
161 look_and_feel = new_look_and_feel;
162 backend.putString(lookAndFeelPreference, look_and_feel);
164 for (AltosUIListener l : ui_listeners)
165 l.ui_changed(look_and_feel);
169 public static String look_and_feel() {
170 synchronized (backend) {
171 return look_and_feel;
175 public static void register_ui_listener(AltosUIListener l) {
176 synchronized(backend) {
181 public static void unregister_ui_listener(AltosUIListener l) {
182 synchronized (backend) {
183 ui_listeners.remove(l);
186 public static void set_serial_debug(boolean new_serial_debug) {
187 AltosLink.set_debug(new_serial_debug);
188 synchronized (backend) {
189 serial_debug = new_serial_debug;
190 backend.putBoolean(serialDebugPreference, serial_debug);
195 public static boolean serial_debug() {
196 synchronized (backend) {
201 public static void register_position_listener(AltosPositionListener l) {
202 synchronized(backend) {
203 position_listeners.add(l);
207 public static void unregister_position_listener(AltosPositionListener l) {
208 synchronized (backend) {
209 position_listeners.remove(l);
213 public static void set_position(int new_position) {
214 synchronized (backend) {
215 position = new_position;
216 backend.putInt(positionPreference, position);
218 for (AltosPositionListener l : position_listeners)
219 l.position_changed(position);
223 public static int position() {
224 synchronized (backend) {
229 public static void register_map_cache_listener(AltosUIMapCacheListener l) {
230 synchronized(backend) {
231 map_cache_listeners.add(l);
235 public static void unregister_map_cache_listener(AltosUIMapCacheListener l) {
236 synchronized (backend) {
237 map_cache_listeners.remove(l);
241 public static void set_map_cache(int new_map_cache) {
242 synchronized(backend) {
243 map_cache = new_map_cache;
244 backend.putInt(mapCachePreference, map_cache);
246 for (AltosUIMapCacheListener l: map_cache_listeners)
247 l.map_cache_changed(map_cache);
251 public static int map_cache() {
252 synchronized(backend) {