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_8;
21 import java.awt.event.*;
24 import javax.swing.event.*;
25 import org.altusmetrum.altoslib_8.*;
27 class DelegatingRenderer implements ListCellRenderer<Object> {
30 public static void install(JComboBox<Object> comboBox) {
31 DelegatingRenderer renderer = new DelegatingRenderer(comboBox);
32 renderer.initialise();
33 comboBox.setRenderer(renderer);
37 private final JComboBox comboBox;
40 private ListCellRenderer<? super Object> delegate;
43 private DelegatingRenderer(JComboBox comboBox) {
44 this.comboBox = comboBox;
48 private void initialise() {
49 JComboBox<Object> c = new JComboBox<Object>();
50 delegate = c.getRenderer();
51 comboBox.addPropertyChangeListener("UI", new PropertyChangeListener() {
53 public void propertyChange(PropertyChangeEvent evt) {
54 delegate = new JComboBox<Object>().getRenderer();
60 public Component getListCellRendererComponent(JList<?> list,
61 Object value, int index, boolean isSelected, boolean cellHasFocus) {
63 return delegate.getListCellRendererComponent(list,
64 ((UIManager.LookAndFeelInfo)value).getName(),
65 index, isSelected, cellHasFocus);
69 public class AltosUIConfigure
73 public Container pane;
77 final static String[] font_size_names = { "Small", "Medium", "Large" };
79 public GridBagConstraints constraints (int x, int width, int fill) {
80 GridBagConstraints c = new GridBagConstraints();
81 Insets insets = new Insets(4, 4, 4, 4);
86 c.anchor = GridBagConstraints.CENTER;
88 c.anchor = GridBagConstraints.EAST;
90 c.anchor = GridBagConstraints.WEST;
97 public GridBagConstraints constraints(int x, int width) {
98 return constraints(x, width, GridBagConstraints.NONE);
101 public void add_voice() {
104 public void add_log_dir() {
105 /* Log directory settings */
106 pane.add(new JLabel("Log Directory"), constraints(0, 1));
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());
115 pane.add(configure_log, constraints(1, 2));
116 configure_log.setToolTipText("Which directory flight logs are stored in");
120 public void add_callsign() {
123 public void add_units() {
124 /* Imperial units setting */
125 pane.add(new JLabel("Imperial Units"), constraints(0, 1));
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);
135 imperial_units.setToolTipText("Use Imperial units instead of metric");
136 pane.add(imperial_units, constraints(1, 2));
140 public void add_font_size() {
141 /* Font size setting */
142 pane.add(new JLabel("Font size"), constraints(0, 1));
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;
151 AltosUIPreferences.set_font_size(size);
154 pane.add(font_size_value, constraints(1, 2, GridBagConstraints.BOTH));
155 font_size_value.setToolTipText("Font size used in telemetry window");
159 public void add_look_and_feel() {
160 /* Look & Feel setting */
161 pane.add(new JLabel("Look & feel"), constraints(0, 1));
164 class LookAndFeelRenderer extends BasicComboBoxRenderer implements ListCellRenderer {
166 public LookAndFeelRenderer() {
170 public Component getListCellRendererComponent(
175 boolean cellHasFocus)
177 super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
178 setText(((UIManager.LookAndFeelInfo) value).getName());
184 final UIManager.LookAndFeelInfo[] look_and_feels = UIManager.getInstalledLookAndFeels();
186 final JComboBox<Object> look_and_feel_value = new JComboBox<Object>(look_and_feels);
188 DelegatingRenderer.install(look_and_feel_value);
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);
195 look_and_feel_value.addActionListener(new ActionListener() {
196 public void actionPerformed(ActionEvent e) {
197 int id = look_and_feel_value.getSelectedIndex();
199 AltosUIPreferences.set_look_and_feel(look_and_feels[id].getClassName());
202 pane.add(look_and_feel_value, constraints(1, 2, GridBagConstraints.BOTH));
203 look_and_feel_value.setToolTipText("Look&feel used for new windows");
207 public void add_position () {
210 public void add_serial_debug() {
211 /* Serial debug setting */
212 pane.add(new JLabel("Serial Debug"), constraints(0, 1));
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);
222 serial_debug.setToolTipText("Enable/Disable USB I/O getting sent to the console");
223 pane.add(serial_debug, constraints(1,2));
227 static final Integer map_caches[] = { 9, 25, 100 };
229 public void add_map_cache() {
230 pane.add(new JLabel("Map Cache Size"), constraints(0, 1));
232 final JComboBox<Integer> map_cache = new JComboBox<Integer>(map_caches);
234 map_cache.setEditable(true);
235 map_cache.addActionListener(new ActionListener() {
236 public void actionPerformed(ActionEvent e) {
238 int size = (Integer) (map_cache.getSelectedItem());
240 AltosUIPreferences.set_map_cache(size);
241 } catch (ClassCastException ce) {
242 map_cache.setSelectedItem(new Integer(AltosUIPreferences.map_cache()));
247 map_cache.setSelectedItem (new Integer(AltosUIPreferences.map_cache()));
248 pane.add(map_cache, constraints(1, 2, GridBagConstraints.BOTH));
252 public void add_bluetooth() {
255 public void add_frequencies() {
258 public AltosUIConfigure(JFrame in_owner, String name, String label) {
259 super(in_owner, name, false);
262 pane = getContentPane();
263 pane.setLayout(new GridBagLayout());
267 /* Nice label at the top */
268 pane.add(new JLabel (label),
272 pane.add(new JLabel (String.format("AltOS version %s (%smaps key)",
273 AltosVersion.version,
274 AltosVersion.has_google_maps_api_key() ? "" : "no ")),
287 if (AltosUILib.has_bluetooth)
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) {
298 pane.add(close, constraints(0, 3));
301 setLocationRelativeTo(owner);
305 public AltosUIConfigure(JFrame in_owner) {
306 this(in_owner, "Configure AltosUI", "Configure AltOS UI");