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; either version 2 of the License, or
7 * (at your option) any later version.
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.
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.
19 package org.altusmetrum.altosuilib_11;
22 import java.awt.event.*;
25 import javax.swing.event.*;
26 import org.altusmetrum.altoslib_11.*;
28 class DelegatingRenderer implements ListCellRenderer<Object> {
31 public static void install(JComboBox<Object> comboBox) {
32 DelegatingRenderer renderer = new DelegatingRenderer(comboBox);
33 renderer.initialise();
34 comboBox.setRenderer(renderer);
38 private final JComboBox comboBox;
41 private ListCellRenderer<? super Object> delegate;
44 private DelegatingRenderer(JComboBox comboBox) {
45 this.comboBox = comboBox;
49 private void initialise() {
50 JComboBox<Object> c = new JComboBox<Object>();
51 delegate = c.getRenderer();
52 comboBox.addPropertyChangeListener("UI", new PropertyChangeListener() {
54 public void propertyChange(PropertyChangeEvent evt) {
55 delegate = new JComboBox<Object>().getRenderer();
61 public Component getListCellRendererComponent(JList<?> list,
62 Object value, int index, boolean isSelected, boolean cellHasFocus) {
64 return delegate.getListCellRendererComponent(list,
65 ((UIManager.LookAndFeelInfo)value).getName(),
66 index, isSelected, cellHasFocus);
70 public class AltosUIConfigure
74 public Container pane;
78 final static String[] font_size_names = { "Small", "Medium", "Large" };
80 public GridBagConstraints constraints (int x, int width, int fill) {
81 GridBagConstraints c = new GridBagConstraints();
82 Insets insets = new Insets(4, 4, 4, 4);
87 c.anchor = GridBagConstraints.CENTER;
89 c.anchor = GridBagConstraints.EAST;
91 c.anchor = GridBagConstraints.WEST;
98 public GridBagConstraints constraints(int x, int width) {
99 return constraints(x, width, GridBagConstraints.NONE);
102 public void add_voice() {
105 public void add_log_dir() {
106 /* Log directory settings */
107 pane.add(new JLabel("Log Directory"), constraints(0, 1));
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());
116 pane.add(configure_log, constraints(1, 2));
117 configure_log.setToolTipText("Which directory flight logs are stored in");
121 public void add_callsign() {
124 public void add_units() {
125 /* Imperial units setting */
126 pane.add(new JLabel("Imperial Units"), constraints(0, 1));
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);
136 imperial_units.setToolTipText("Use Imperial units instead of metric");
137 pane.add(imperial_units, constraints(1, 2));
141 public void add_font_size() {
142 /* Font size setting */
143 pane.add(new JLabel("Font size"), constraints(0, 1));
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;
152 AltosUIPreferences.set_font_size(size);
155 pane.add(font_size_value, constraints(1, 2, GridBagConstraints.BOTH));
156 font_size_value.setToolTipText("Font size used in telemetry window");
160 public void add_look_and_feel() {
161 /* Look & Feel setting */
162 pane.add(new JLabel("Look & feel"), constraints(0, 1));
165 class LookAndFeelRenderer extends BasicComboBoxRenderer implements ListCellRenderer {
167 public LookAndFeelRenderer() {
171 public Component getListCellRendererComponent(
176 boolean cellHasFocus)
178 super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
179 setText(((UIManager.LookAndFeelInfo) value).getName());
185 final UIManager.LookAndFeelInfo[] look_and_feels = UIManager.getInstalledLookAndFeels();
187 final JComboBox<Object> look_and_feel_value = new JComboBox<Object>(look_and_feels);
189 DelegatingRenderer.install(look_and_feel_value);
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);
196 look_and_feel_value.addActionListener(new ActionListener() {
197 public void actionPerformed(ActionEvent e) {
198 int id = look_and_feel_value.getSelectedIndex();
200 AltosUIPreferences.set_look_and_feel(look_and_feels[id].getClassName());
203 pane.add(look_and_feel_value, constraints(1, 2, GridBagConstraints.BOTH));
204 look_and_feel_value.setToolTipText("Look&feel used for new windows");
208 public void add_position () {
211 public void add_serial_debug() {
212 /* Serial debug setting */
213 pane.add(new JLabel("Serial Debug"), constraints(0, 1));
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);
223 serial_debug.setToolTipText("Enable/Disable USB I/O getting sent to the console");
224 pane.add(serial_debug, constraints(1,2));
228 static final Integer map_caches[] = { 9, 25, 100 };
230 public void add_map_cache() {
231 pane.add(new JLabel("Map Cache Size"), constraints(0, 1));
233 final JComboBox<Integer> map_cache = new JComboBox<Integer>(map_caches);
235 map_cache.setEditable(true);
236 map_cache.addActionListener(new ActionListener() {
237 public void actionPerformed(ActionEvent e) {
239 int size = (Integer) (map_cache.getSelectedItem());
241 AltosUIPreferences.set_map_cache(size);
242 } catch (ClassCastException ce) {
243 map_cache.setSelectedItem(new Integer(AltosUIPreferences.map_cache()));
248 map_cache.setSelectedItem (new Integer(AltosUIPreferences.map_cache()));
249 pane.add(map_cache, constraints(1, 2, GridBagConstraints.BOTH));
253 public void add_bluetooth() {
256 public void add_frequencies() {
259 public AltosUIConfigure(JFrame in_owner, String name, String label) {
260 super(in_owner, name, false);
263 pane = getContentPane();
264 pane.setLayout(new GridBagLayout());
268 /* Nice label at the top */
269 pane.add(new JLabel (label),
273 pane.add(new JLabel (String.format("AltOS version %s (%smaps key)",
274 AltosVersion.version,
275 AltosVersion.has_google_maps_api_key() ? "" : "no ")),
288 if (AltosUILib.has_bluetooth)
292 /* And a close button at the bottom */
293 JButton close = new JButton("Close");
294 close.addActionListener(new ActionListener() {
295 public void actionPerformed(ActionEvent e) {
299 pane.add(close, constraints(0, 3));
302 setLocationRelativeTo(owner);
306 public AltosUIConfigure(JFrame in_owner) {
307 this(in_owner, "Configure AltosUI", "Configure AltOS UI");