2 * Copyright © 2010 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;
21 import java.awt.event.*;
24 import javax.swing.event.*;
26 class DelegatingRenderer implements ListCellRenderer<Object> {
29 public static void install(JComboBox<Object> comboBox) {
30 DelegatingRenderer renderer = new DelegatingRenderer(comboBox);
31 renderer.initialise();
32 comboBox.setRenderer(renderer);
36 private final JComboBox comboBox;
39 private ListCellRenderer<? super Object> delegate;
42 private DelegatingRenderer(JComboBox comboBox) {
43 this.comboBox = comboBox;
47 private void initialise() {
48 JComboBox<Object> c = new JComboBox<Object>();
49 delegate = c.getRenderer();
50 comboBox.addPropertyChangeListener("UI", new PropertyChangeListener() {
52 public void propertyChange(PropertyChangeEvent evt) {
53 delegate = new JComboBox<Object>().getRenderer();
59 public Component getListCellRendererComponent(JList<?> list,
60 Object value, int index, boolean isSelected, boolean cellHasFocus) {
62 return delegate.getListCellRendererComponent(list,
63 ((UIManager.LookAndFeelInfo)value).getName(),
64 index, isSelected, cellHasFocus);
68 public class AltosUIConfigure
72 public Container pane;
76 final static String[] font_size_names = { "Small", "Medium", "Large" };
78 public GridBagConstraints constraints (int x, int width, int fill) {
79 GridBagConstraints c = new GridBagConstraints();
80 Insets insets = new Insets(4, 4, 4, 4);
85 c.anchor = GridBagConstraints.CENTER;
87 c.anchor = GridBagConstraints.EAST;
89 c.anchor = GridBagConstraints.WEST;
96 public GridBagConstraints constraints(int x, int width) {
97 return constraints(x, width, GridBagConstraints.NONE);
100 public void add_voice() {
103 public void add_log_dir() {
104 /* Log directory settings */
105 pane.add(new JLabel("Log Directory"), constraints(0, 1));
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());
114 pane.add(configure_log, constraints(1, 2));
115 configure_log.setToolTipText("Which directory flight logs are stored in");
119 public void add_callsign() {
122 public void add_units() {
123 /* Imperial units setting */
124 pane.add(new JLabel("Imperial Units"), constraints(0, 1));
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);
134 imperial_units.setToolTipText("Use Imperial units instead of metric");
135 pane.add(imperial_units, constraints(1, 2));
139 public void add_font_size() {
140 /* Font size setting */
141 pane.add(new JLabel("Font size"), constraints(0, 1));
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;
150 AltosUIPreferences.set_font_size(size);
153 pane.add(font_size_value, constraints(1, 2, GridBagConstraints.BOTH));
154 font_size_value.setToolTipText("Font size used in telemetry window");
158 public void add_look_and_feel() {
159 /* Look & Feel setting */
160 pane.add(new JLabel("Look & feel"), constraints(0, 1));
163 class LookAndFeelRenderer extends BasicComboBoxRenderer implements ListCellRenderer {
165 public LookAndFeelRenderer() {
169 public Component getListCellRendererComponent(
174 boolean cellHasFocus)
176 super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
177 setText(((UIManager.LookAndFeelInfo) value).getName());
183 final UIManager.LookAndFeelInfo[] look_and_feels = UIManager.getInstalledLookAndFeels();
185 final JComboBox<Object> look_and_feel_value = new JComboBox<Object>(look_and_feels);
187 DelegatingRenderer.install(look_and_feel_value);
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);
194 look_and_feel_value.addActionListener(new ActionListener() {
195 public void actionPerformed(ActionEvent e) {
196 int id = look_and_feel_value.getSelectedIndex();
198 AltosUIPreferences.set_look_and_feel(look_and_feels[id].getClassName());
201 pane.add(look_and_feel_value, constraints(1, 2, GridBagConstraints.BOTH));
202 look_and_feel_value.setToolTipText("Look&feel used for new windows");
206 public void add_position () {
209 public void add_serial_debug() {
210 /* Serial debug setting */
211 pane.add(new JLabel("Serial Debug"), constraints(0, 1));
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);
221 serial_debug.setToolTipText("Enable/Disable USB I/O getting sent to the console");
222 pane.add(serial_debug, constraints(1,2));
226 static final Integer map_caches[] = { 9, 25, 100 };
228 public void add_map_cache() {
229 pane.add(new JLabel("Map Cache Size"), constraints(0, 1));
231 final JComboBox<Integer> map_cache = new JComboBox<Integer>(map_caches);
233 map_cache.setEditable(true);
234 map_cache.addActionListener(new ActionListener() {
235 public void actionPerformed(ActionEvent e) {
237 int size = (Integer) (map_cache.getSelectedItem());
239 AltosUIPreferences.set_map_cache(size);
240 } catch (ClassCastException ce) {
241 map_cache.setSelectedItem(new Integer(AltosUIPreferences.map_cache()));
246 map_cache.setSelectedItem (new Integer(AltosUIPreferences.map_cache()));
247 pane.add(map_cache, constraints(1, 2, GridBagConstraints.BOTH));
251 public void add_bluetooth() {
254 public void add_frequencies() {
257 public AltosUIConfigure(JFrame in_owner, String name, String label) {
258 super(in_owner, name, false);
261 pane = getContentPane();
262 pane.setLayout(new GridBagLayout());
266 /* Nice label at the top */
267 pane.add(new JLabel (label),
271 pane.add(new JLabel (String.format("AltOS version %s (%smaps key)",
272 AltosUIVersion.version,
273 AltosUIVersion.has_google_maps_api_key() ? "" : "no ")),
286 if (AltosUILib.has_bluetooth)
290 /* And a close button at the bottom */
291 JButton close = new JButton("Close");
292 close.addActionListener(new ActionListener() {
293 public void actionPerformed(ActionEvent e) {
297 pane.add(close, constraints(0, 3));
300 setLocationRelativeTo(owner);
304 public AltosUIConfigure(JFrame in_owner) {
305 this(in_owner, "Configure AltosUI", "Configure AltOS UI");