Merge branch 'master' of ssh://git.gag.com/scm/git/fw/altos
[fw/altos] / altosuilib / AltosUIConfigure.java
1 /*
2  * Copyright © 2010 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_8;
19
20 import java.awt.*;
21 import java.awt.event.*;
22 import java.beans.*;
23 import javax.swing.*;
24 import javax.swing.event.*;
25 import org.altusmetrum.altoslib_8.*;
26
27 class DelegatingRenderer implements ListCellRenderer<Object> {
28
29         // ...
30         public static void install(JComboBox<Object> comboBox) {
31                 DelegatingRenderer renderer = new DelegatingRenderer(comboBox);
32                 renderer.initialise();
33                 comboBox.setRenderer(renderer);
34         }
35
36         // ...
37         private final JComboBox comboBox;
38
39         // ...
40         private ListCellRenderer<? super Object> delegate;
41
42         // ...
43         private DelegatingRenderer(JComboBox comboBox) {
44                 this.comboBox = comboBox;
45         }
46
47         // ...
48         private void initialise() {
49                 JComboBox<Object> c = new JComboBox<Object>();
50                 delegate = c.getRenderer();
51                 comboBox.addPropertyChangeListener("UI", new PropertyChangeListener() {
52
53                                 public void propertyChange(PropertyChangeEvent evt) {
54                                         delegate = new JComboBox<Object>().getRenderer();
55                                 }
56                         });
57         }
58
59         // ...
60         public Component getListCellRendererComponent(JList<?> list,
61                                                       Object value, int index, boolean isSelected, boolean cellHasFocus) {
62
63                 return delegate.getListCellRendererComponent(list,
64                                                              ((UIManager.LookAndFeelInfo)value).getName(),
65                                                              index, isSelected, cellHasFocus);
66         }
67 }
68
69 public class AltosUIConfigure
70         extends AltosUIDialog
71 {
72         public JFrame           owner;
73         public Container        pane;
74
75         public int              row;
76
77         final static String[] font_size_names = { "Small", "Medium", "Large" };
78
79         public GridBagConstraints constraints (int x, int width, int fill) {
80                 GridBagConstraints c = new GridBagConstraints();
81                 Insets insets = new Insets(4, 4, 4, 4);
82
83                 c.insets = insets;
84                 c.fill = fill;
85                 if (width == 3)
86                         c.anchor = GridBagConstraints.CENTER;
87                 else if (x == 2)
88                         c.anchor = GridBagConstraints.EAST;
89                 else
90                         c.anchor = GridBagConstraints.WEST;
91                 c.gridx = x;
92                 c.gridwidth = width;
93                 c.gridy = row;
94                 return c;
95         }
96
97         public GridBagConstraints constraints(int x, int width) {
98                 return constraints(x, width, GridBagConstraints.NONE);
99         }
100
101         public void add_voice() {
102         }
103
104         public void add_log_dir() {
105                 /* Log directory settings */
106                 pane.add(new JLabel("Log Directory"), constraints(0, 1));
107
108                 final JButton configure_log = new JButton(AltosUIPreferences.logdir().getPath());
109                 configure_log.addActionListener(new ActionListener() {
110                                 public void actionPerformed(ActionEvent e) {
111                                         AltosUIPreferences.ConfigureLog();
112                                         configure_log.setText(AltosUIPreferences.logdir().getPath());
113                                 }
114                         });
115                 pane.add(configure_log, constraints(1, 2));
116                 configure_log.setToolTipText("Which directory flight logs are stored in");
117                 row++;
118         }
119
120         public void add_callsign() {
121         }
122
123         public void add_units() {
124                 /* Imperial units setting */
125                 pane.add(new JLabel("Imperial Units"), constraints(0, 1));
126
127                 JRadioButton imperial_units = new JRadioButton("Enable", AltosUIPreferences.imperial_units());
128                 imperial_units.addActionListener(new ActionListener() {
129                                 public void actionPerformed(ActionEvent e) {
130                                         JRadioButton item = (JRadioButton) e.getSource();
131                                         boolean enabled = item.isSelected();
132                                         AltosUIPreferences.set_imperial_units(enabled);
133                                 }
134                         });
135                 imperial_units.setToolTipText("Use Imperial units instead of metric");
136                 pane.add(imperial_units, constraints(1, 2));
137                 row++;
138         }
139
140         public void add_font_size() {
141                 /* Font size setting */
142                 pane.add(new JLabel("Font size"), constraints(0, 1));
143
144                 final JComboBox<String> font_size_value = new JComboBox<String>(font_size_names);
145                 int font_size = AltosUIPreferences.font_size();
146                 font_size_value.setSelectedIndex(font_size - AltosUILib.font_size_small);
147                 font_size_value.addActionListener(new ActionListener() {
148                                 public void actionPerformed(ActionEvent e) {
149                                         int     size = font_size_value.getSelectedIndex() + AltosUILib.font_size_small;
150
151                                         AltosUIPreferences.set_font_size(size);
152                                 }
153                         });
154                 pane.add(font_size_value, constraints(1, 2, GridBagConstraints.BOTH));
155                 font_size_value.setToolTipText("Font size used in telemetry window");
156                 row++;
157         }
158
159         public void add_look_and_feel() {
160                 /* Look & Feel setting */
161                 pane.add(new JLabel("Look & feel"), constraints(0, 1));
162
163                 /*
164                 class LookAndFeelRenderer extends BasicComboBoxRenderer implements ListCellRenderer {
165
166                         public LookAndFeelRenderer() {
167                                 super();
168                         }
169
170                         public Component getListCellRendererComponent(
171                                 JList list,
172                                 Object value,
173                                 int index,
174                                 boolean isSelected,
175                                 boolean cellHasFocus)
176                         {
177                                 super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
178                                 setText(((UIManager.LookAndFeelInfo) value).getName());
179                                 return this;
180                         }
181                 }
182                 */
183
184                 final UIManager.LookAndFeelInfo[] look_and_feels = UIManager.getInstalledLookAndFeels();
185
186                 final JComboBox<Object> look_and_feel_value = new JComboBox<Object>(look_and_feels);
187
188                 DelegatingRenderer.install(look_and_feel_value);
189
190                 String look_and_feel  = AltosUIPreferences.look_and_feel();
191                 for (int i = 0; i < look_and_feels.length; i++)
192                         if (look_and_feel.equals(look_and_feels[i].getClassName()))
193                                 look_and_feel_value.setSelectedIndex(i);
194
195                 look_and_feel_value.addActionListener(new ActionListener() {
196                                 public void actionPerformed(ActionEvent e) {
197                                         int     id = look_and_feel_value.getSelectedIndex();
198
199                                         AltosUIPreferences.set_look_and_feel(look_and_feels[id].getClassName());
200                                 }
201                         });
202                 pane.add(look_and_feel_value, constraints(1, 2, GridBagConstraints.BOTH));
203                 look_and_feel_value.setToolTipText("Look&feel used for new windows");
204                 row++;
205         }
206
207         public void add_position () {
208         }
209
210         public void add_serial_debug() {
211                 /* Serial debug setting */
212                 pane.add(new JLabel("Serial Debug"), constraints(0, 1));
213
214                 JRadioButton serial_debug = new JRadioButton("Enable", AltosUIPreferences.serial_debug());
215                 serial_debug.addActionListener(new ActionListener() {
216                                 public void actionPerformed(ActionEvent e) {
217                                         JRadioButton item = (JRadioButton) e.getSource();
218                                         boolean enabled = item.isSelected();
219                                         AltosUIPreferences.set_serial_debug(enabled);
220                                 }
221                         });
222                 serial_debug.setToolTipText("Enable/Disable USB I/O getting sent to the console");
223                 pane.add(serial_debug, constraints(1,2));
224                 row++;
225         }
226
227         static final Integer map_caches[] = { 9, 25, 100 };
228
229         public void add_map_cache() {
230                 pane.add(new JLabel("Map Cache Size"), constraints(0, 1));
231
232                 final JComboBox<Integer> map_cache = new JComboBox<Integer>(map_caches);
233
234                 map_cache.setEditable(true);
235                 map_cache.addActionListener(new ActionListener() {
236                                 public void actionPerformed(ActionEvent e) {
237                                         try {
238                                                 int     size = (Integer) (map_cache.getSelectedItem());
239
240                                                 AltosUIPreferences.set_map_cache(size);
241                                         } catch (ClassCastException ce) {
242                                                 map_cache.setSelectedItem(new Integer(AltosUIPreferences.map_cache()));
243                                         }
244                                 }
245                         });
246
247                 map_cache.setSelectedItem (new Integer(AltosUIPreferences.map_cache()));
248                 pane.add(map_cache, constraints(1, 2, GridBagConstraints.BOTH));
249                 row++;
250         }
251
252         public void add_bluetooth() {
253         }
254
255         public void add_frequencies() {
256         }
257
258         public AltosUIConfigure(JFrame in_owner, String name, String label) {
259                 super(in_owner, name, false);
260
261                 owner = in_owner;
262                 pane = getContentPane();
263                 pane.setLayout(new GridBagLayout());
264
265                 row = 0;
266
267                 /* Nice label at the top */
268                 pane.add(new JLabel (label),
269                          constraints(0, 3));
270                 row++;
271
272                 pane.add(new JLabel (String.format("AltOS version %s (%smaps key)",
273                                                    AltosVersion.version,
274                                                    AltosVersion.has_google_maps_api_key() ? "" : "no ")),
275                          constraints(0, 3));
276                 row++;
277
278                 add_voice();
279                 add_log_dir();
280                 add_callsign();
281                 add_units();
282                 add_serial_debug();
283                 add_font_size();
284                 add_look_and_feel();
285                 add_position();
286                 add_map_cache();
287                 if (AltosUILib.has_bluetooth)
288                         add_bluetooth();
289                 add_frequencies();
290
291                 /* And a close button at the bottom */
292                 JButton close = new JButton("Close");
293                 close.addActionListener(new ActionListener() {
294                                 public void actionPerformed(ActionEvent e) {
295                                         setVisible(false);
296                                 }
297                         });
298                 pane.add(close, constraints(0, 3));
299
300                 pack();
301                 setLocationRelativeTo(owner);
302                 setVisible(true);
303         }
304
305         public AltosUIConfigure(JFrame in_owner) {
306                 this(in_owner, "Configure AltosUI", "Configure AltOS UI");
307         }
308 }