altosuilib: Eliminate a couple java compiler warnings
[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; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with this program; if not, write to the Free Software Foundation, Inc.,
16  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
17  */
18
19 package org.altusmetrum.altosuilib_13;
20
21 import java.awt.*;
22 import java.awt.event.*;
23 import java.beans.*;
24 import javax.swing.*;
25 import javax.swing.event.*;
26 import org.altusmetrum.altoslib_13.*;
27
28 class DelegatingRenderer implements ListCellRenderer<Object> {
29
30         // ...
31         public static void install(JComboBox<Object> comboBox) {
32                 DelegatingRenderer renderer = new DelegatingRenderer(comboBox);
33                 renderer.initialise();
34                 comboBox.setRenderer(renderer);
35         }
36
37         // ...
38         private final JComboBox comboBox;
39
40         // ...
41         private ListCellRenderer<? super Object> delegate;
42
43         // ...
44         private DelegatingRenderer(JComboBox comboBox) {
45                 this.comboBox = comboBox;
46         }
47
48         // ...
49         private void initialise() {
50                 JComboBox<Object> c = new JComboBox<Object>();
51                 delegate = c.getRenderer();
52                 comboBox.addPropertyChangeListener("UI", new PropertyChangeListener() {
53
54                                 public void propertyChange(PropertyChangeEvent evt) {
55                                         delegate = new JComboBox<Object>().getRenderer();
56                                 }
57                         });
58         }
59
60         // ...
61         public Component getListCellRendererComponent(JList<?> list,
62                                                       Object value, int index, boolean isSelected, boolean cellHasFocus) {
63
64                 return delegate.getListCellRendererComponent(list,
65                                                              ((UIManager.LookAndFeelInfo)value).getName(),
66                                                              index, isSelected, cellHasFocus);
67         }
68 }
69
70 public class AltosUIConfigure
71         extends AltosUIDialog
72 {
73         public JFrame           owner;
74         public Container        pane;
75
76         public int              row;
77
78         final static String[] font_size_names = { "Small", "Medium", "Large", "Huge" };
79
80         public GridBagConstraints constraints (int x, int width, int fill) {
81                 GridBagConstraints c = new GridBagConstraints();
82                 Insets insets = new Insets(4, 4, 4, 4);
83
84                 c.insets = insets;
85                 c.fill = fill;
86                 if (width == 3)
87                         c.anchor = GridBagConstraints.CENTER;
88                 else if (x == 2)
89                         c.anchor = GridBagConstraints.EAST;
90                 else
91                         c.anchor = GridBagConstraints.WEST;
92                 c.gridx = x;
93                 c.gridwidth = width;
94                 c.gridy = row;
95                 return c;
96         }
97
98         public GridBagConstraints constraints(int x, int width) {
99                 return constraints(x, width, GridBagConstraints.NONE);
100         }
101
102         public void add_voice() {
103         }
104
105         public void add_log_dir() {
106                 /* Log directory settings */
107                 pane.add(new JLabel("Log Directory"), constraints(0, 1));
108
109                 final JButton configure_log = new JButton(AltosUIPreferences.logdir().getPath());
110                 configure_log.addActionListener(new ActionListener() {
111                                 public void actionPerformed(ActionEvent e) {
112                                         AltosUIPreferences.ConfigureLog();
113                                         configure_log.setText(AltosUIPreferences.logdir().getPath());
114                                 }
115                         });
116                 pane.add(configure_log, constraints(1, 2));
117                 configure_log.setToolTipText("Which directory flight logs are stored in");
118                 row++;
119         }
120
121         public void add_callsign() {
122         }
123
124         public void add_units() {
125                 /* Imperial units setting */
126                 pane.add(new JLabel("Imperial Units"), constraints(0, 1));
127
128                 JRadioButton imperial_units = new JRadioButton("Enable", AltosUIPreferences.imperial_units());
129                 imperial_units.addActionListener(new ActionListener() {
130                                 public void actionPerformed(ActionEvent e) {
131                                         JRadioButton item = (JRadioButton) e.getSource();
132                                         boolean enabled = item.isSelected();
133                                         AltosUIPreferences.set_imperial_units(enabled);
134                                 }
135                         });
136                 imperial_units.setToolTipText("Use Imperial units instead of metric");
137                 pane.add(imperial_units, constraints(1, 2));
138                 row++;
139         }
140
141         public void add_font_size() {
142                 /* Font size setting */
143                 pane.add(new JLabel("Font size"), constraints(0, 1));
144
145                 final JComboBox<String> font_size_value = new JComboBox<String>(font_size_names);
146                 int font_size = AltosUIPreferences.font_size();
147                 font_size_value.setSelectedIndex(font_size - AltosUILib.font_size_small);
148                 font_size_value.addActionListener(new ActionListener() {
149                                 public void actionPerformed(ActionEvent e) {
150                                         int     size = font_size_value.getSelectedIndex() + AltosUILib.font_size_small;
151
152                                         AltosUIPreferences.set_font_size(size);
153                                 }
154                         });
155                 pane.add(font_size_value, constraints(1, 2, GridBagConstraints.BOTH));
156                 font_size_value.setToolTipText("Font size used in telemetry window");
157                 row++;
158         }
159
160         public void add_look_and_feel() {
161                 /* Look & Feel setting */
162                 pane.add(new JLabel("Look & feel"), constraints(0, 1));
163
164                 /*
165                 class LookAndFeelRenderer extends BasicComboBoxRenderer implements ListCellRenderer {
166
167                         public LookAndFeelRenderer() {
168                                 super();
169                         }
170
171                         public Component getListCellRendererComponent(
172                                 JList list,
173                                 Object value,
174                                 int index,
175                                 boolean isSelected,
176                                 boolean cellHasFocus)
177                         {
178                                 super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
179                                 setText(((UIManager.LookAndFeelInfo) value).getName());
180                                 return this;
181                         }
182                 }
183                 */
184
185                 final UIManager.LookAndFeelInfo[] look_and_feels = UIManager.getInstalledLookAndFeels();
186
187                 final JComboBox<Object> look_and_feel_value = new JComboBox<Object>(look_and_feels);
188
189                 DelegatingRenderer.install(look_and_feel_value);
190
191                 String look_and_feel  = AltosUIPreferences.look_and_feel();
192                 for (int i = 0; i < look_and_feels.length; i++)
193                         if (look_and_feel.equals(look_and_feels[i].getClassName()))
194                                 look_and_feel_value.setSelectedIndex(i);
195
196                 look_and_feel_value.addActionListener(new ActionListener() {
197                                 public void actionPerformed(ActionEvent e) {
198                                         int     id = look_and_feel_value.getSelectedIndex();
199
200                                         AltosUIPreferences.set_look_and_feel(look_and_feels[id].getClassName());
201                                 }
202                         });
203                 pane.add(look_and_feel_value, constraints(1, 2, GridBagConstraints.BOTH));
204                 look_and_feel_value.setToolTipText("Look&feel used for new windows");
205                 row++;
206         }
207
208         public void add_position () {
209         }
210
211         public void add_serial_debug() {
212                 /* Serial debug setting */
213                 pane.add(new JLabel("Serial Debug"), constraints(0, 1));
214
215                 JRadioButton serial_debug = new JRadioButton("Enable", AltosUIPreferences.serial_debug());
216                 serial_debug.addActionListener(new ActionListener() {
217                                 public void actionPerformed(ActionEvent e) {
218                                         JRadioButton item = (JRadioButton) e.getSource();
219                                         boolean enabled = item.isSelected();
220                                         AltosUIPreferences.set_serial_debug(enabled);
221                                 }
222                         });
223                 serial_debug.setToolTipText("Enable/Disable USB I/O getting sent to the console");
224                 pane.add(serial_debug, constraints(1,2));
225                 row++;
226         }
227
228         static final Integer map_caches[] = { 9, 25, 100 };
229
230         public void add_map_cache() {
231                 pane.add(new JLabel("Map Cache Size"), constraints(0, 1));
232
233                 final JComboBox<Integer> map_cache = new JComboBox<Integer>(map_caches);
234
235                 map_cache.setEditable(true);
236                 map_cache.addActionListener(new ActionListener() {
237                                 public void actionPerformed(ActionEvent e) {
238                                         try {
239                                                 int     size = (Integer) (map_cache.getSelectedItem());
240
241                                                 AltosUIPreferences.set_map_cache(size);
242                                         } catch (ClassCastException ce) {
243                                                 map_cache.setSelectedItem(AltosUIPreferences.map_cache());
244                                         }
245                                 }
246                         });
247
248                 map_cache.setSelectedItem (AltosUIPreferences.map_cache());
249                 pane.add(map_cache, constraints(1, 2, GridBagConstraints.BOTH));
250                 row++;
251         }
252
253         public void add_bluetooth() {
254         }
255
256         public void add_frequencies() {
257         }
258
259         public AltosUIConfigure(JFrame in_owner, String name, String label) {
260                 super(in_owner, name, false);
261
262                 owner = in_owner;
263                 pane = getContentPane();
264                 pane.setLayout(new GridBagLayout());
265
266                 row = 0;
267
268                 /* Nice label at the top */
269                 pane.add(new JLabel (label),
270                          constraints(0, 3));
271                 row++;
272
273                 pane.add(new JLabel (String.format("AltOS version %s",
274                                                    AltosVersion.version)),
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 }