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