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.telegps;
22 import org.altusmetrum.altoslib_4.*;
23 import org.altusmetrum.altosuilib_2.*;
25 public class TeleGPSInfo extends JComponent implements AltosFlightDisplay {
35 value.setVisible(true);
36 lights.setVisible(true);
37 label.setVisible(true);
41 value.setVisible(false);
42 lights.setVisible(false);
43 label.setVisible(false);
46 void show(AltosState state, AltosListenerState listener_state) {}
53 void show(AltosUnits units, double v) {
54 show(units.show(8, v));
57 void show(String format, double v) {
58 show(String.format(format, v));
67 label.setFont(AltosUILib.label_font);
68 value.setFont(AltosUILib.value_font);
71 public Info (GridBagLayout layout, int y, String text) {
72 GridBagConstraints c = new GridBagConstraints();
75 lights = new AltosLights();
76 c.gridx = 0; c.gridy = y;
77 c.anchor = GridBagConstraints.CENTER;
78 c.fill = GridBagConstraints.VERTICAL;
80 layout.setConstraints(lights, c);
83 label = new JLabel(text);
84 label.setFont(AltosUILib.label_font);
85 label.setHorizontalAlignment(SwingConstants.LEFT);
86 c.gridx = 1; c.gridy = y;
87 c.insets = new Insets(AltosUILib.tab_elt_pad, AltosUILib.tab_elt_pad, AltosUILib.tab_elt_pad, AltosUILib.tab_elt_pad);
88 c.anchor = GridBagConstraints.WEST;
89 c.fill = GridBagConstraints.VERTICAL;
91 layout.setConstraints(label, c);
94 value = new JTextField(AltosUILib.text_width);
95 value.setFont(AltosUILib.value_font);
96 value.setHorizontalAlignment(SwingConstants.RIGHT);
97 c.gridx = 2; c.gridy = y;
99 c.anchor = GridBagConstraints.WEST;
100 c.fill = GridBagConstraints.BOTH;
102 layout.setConstraints(value, c);
110 void show(AltosState state, AltosListenerState listener_state) {}
117 label.setVisible(true);
118 value.setVisible(true);
121 void show(String s) {
126 void show(AltosUnits units, double v) {
127 show(units.show(8, v));
130 void show(String format, double v) {
131 show(String.format(format, v));
135 label.setVisible(false);
136 value.setVisible(false);
139 label.setFont(AltosUILib.label_font);
140 value.setFont(AltosUILib.value_font);
143 public Value (GridBagLayout layout, int y, String text) {
144 GridBagConstraints c = new GridBagConstraints();
147 label = new JLabel(text);
148 label.setFont(AltosUILib.label_font);
149 label.setHorizontalAlignment(SwingConstants.LEFT);
150 c.gridx = 1; c.gridy = y;
151 c.insets = new Insets(AltosUILib.tab_elt_pad, AltosUILib.tab_elt_pad, AltosUILib.tab_elt_pad, AltosUILib.tab_elt_pad);
152 c.anchor = GridBagConstraints.WEST;
153 c.fill = GridBagConstraints.VERTICAL;
155 layout.setConstraints(label, c);
158 value = new JTextField(AltosUILib.text_width);
159 value.setFont(AltosUILib.value_font);
160 value.setHorizontalAlignment(SwingConstants.RIGHT);
161 c.gridx = 2; c.gridy = y;
162 c.anchor = GridBagConstraints.WEST;
163 c.fill = GridBagConstraints.BOTH;
166 layout.setConstraints(value, c);
171 public abstract class DualValue {
182 label.setVisible(true);
183 value1.setVisible(true);
184 value2.setVisible(true);
188 label.setVisible(false);
189 value1.setVisible(false);
190 value2.setVisible(false);
194 label.setFont(AltosUILib.label_font);
195 value1.setFont(AltosUILib.value_font);
196 value2.setFont(AltosUILib.value_font);
199 abstract void show(AltosState state, AltosListenerState listener_state);
201 void show(String v1, String v2) {
206 void show(String f1, double v1, String f2, double v2) {
208 value1.setText(String.format(f1, v1));
209 value2.setText(String.format(f2, v2));
212 public DualValue (GridBagLayout layout, int x, int y, String text) {
213 GridBagConstraints c = new GridBagConstraints();
216 label = new JLabel(text);
217 label.setFont(AltosUILib.label_font);
218 label.setHorizontalAlignment(SwingConstants.LEFT);
219 c.gridx = x + 1; c.gridy = y;
220 c.insets = new Insets(AltosUILib.tab_elt_pad, AltosUILib.tab_elt_pad, AltosUILib.tab_elt_pad, AltosUILib.tab_elt_pad);
221 c.anchor = GridBagConstraints.WEST;
222 c.fill = GridBagConstraints.VERTICAL;
224 layout.setConstraints(label, c);
227 value1 = new JTextField(AltosUILib.text_width);
228 value1.setFont(AltosUILib.value_font);
229 value1.setHorizontalAlignment(SwingConstants.RIGHT);
230 c.gridx = x + 2; c.gridy = y;
231 c.anchor = GridBagConstraints.WEST;
232 c.fill = GridBagConstraints.BOTH;
234 layout.setConstraints(value1, c);
237 value2 = new JTextField(AltosUILib.text_width);
238 value2.setFont(AltosUILib.value_font);
239 value2.setHorizontalAlignment(SwingConstants.RIGHT);
240 c.gridx = x + 3; c.gridy = y;
241 c.anchor = GridBagConstraints.WEST;
242 c.fill = GridBagConstraints.BOTH;
245 layout.setConstraints(value2, c);
250 public class ValueHold {
253 JTextField max_value;
256 void show(AltosState state, AltosListenerState listener_state) {}
260 max_value.setText("");
261 max = AltosLib.MISSING;
265 label.setFont(AltosUILib.label_font);
266 value.setFont(AltosUILib.value_font);
267 max_value.setFont(AltosUILib.value_font);
270 void show(AltosUnits units, double v) {
271 if (v == AltosLib.MISSING) {
272 value.setText("Missing");
273 max_value.setText("Missing");
275 value.setText(units.show(8, v));
276 if (v > max || max == AltosLib.MISSING) {
277 max_value.setText(units.show(8, v));
284 label.setVisible(false);
285 value.setVisible(false);
286 max_value.setVisible(false);
289 public ValueHold (GridBagLayout layout, int y, String text) {
290 GridBagConstraints c = new GridBagConstraints();
293 label = new JLabel(text);
294 label.setFont(AltosUILib.label_font);
295 label.setHorizontalAlignment(SwingConstants.LEFT);
296 c.gridx = 1; c.gridy = y;
297 c.insets = new Insets(AltosUILib.tab_elt_pad, AltosUILib.tab_elt_pad, AltosUILib.tab_elt_pad, AltosUILib.tab_elt_pad);
298 c.anchor = GridBagConstraints.WEST;
299 c.fill = GridBagConstraints.VERTICAL;
301 layout.setConstraints(label, c);
304 value = new JTextField(AltosUILib.text_width);
305 value.setFont(AltosUILib.value_font);
306 value.setHorizontalAlignment(SwingConstants.RIGHT);
307 c.gridx = 2; c.gridy = y;
308 c.anchor = GridBagConstraints.EAST;
309 c.fill = GridBagConstraints.BOTH;
311 layout.setConstraints(value, c);
314 max_value = new JTextField(AltosUILib.text_width);
315 max_value.setFont(AltosUILib.value_font);
316 max_value.setHorizontalAlignment(SwingConstants.RIGHT);
317 c.gridx = 3; c.gridy = y;
318 c.anchor = GridBagConstraints.EAST;
319 c.fill = GridBagConstraints.BOTH;
321 layout.setConstraints(max_value, c);
327 class Altitude extends ValueHold {
328 void show (AltosState state, AltosListenerState listener_state) {
329 show(AltosConvert.height, state.altitude());
331 public Altitude (GridBagLayout layout, int y) {
332 super (layout, y, "Altitude");
338 class AscentRate extends ValueHold {
339 void show (AltosState state, AltosListenerState listener_state) {
340 show(AltosConvert.speed, state.gps_ascent_rate());
342 public AscentRate (GridBagLayout layout, int y) {
343 super (layout, y, "Ascent Rate");
347 AscentRate ascent_rate;
349 class GroundSpeed extends ValueHold {
350 void show (AltosState state, AltosListenerState listener_state) {
351 show(AltosConvert.speed, state.gps_ground_speed());
353 public GroundSpeed (GridBagLayout layout, int y) {
354 super (layout, y, "Ground Speed");
358 GroundSpeed ground_speed;
360 String pos(double p, String pos, String neg) {
366 int deg = (int) Math.floor(p);
367 double min = (p - Math.floor(p)) * 60.0;
368 return String.format("%s %4d° %9.6f", h, deg, min);
371 class Course extends DualValue {
372 void show (AltosState state, AltosListenerState listener_state) {
373 double course = state.gps_course();
374 if (course != AltosLib.MISSING)
375 show( String.format("%3.0f°", course),
376 AltosConvert.bearing_to_words(
377 AltosConvert.BEARING_LONG,
380 public Course (GridBagLayout layout, int y) {
381 super (layout, 0, y, "Course");
387 class Lat extends Value {
388 void show (AltosState state, AltosListenerState listener_state) {
389 if (state.gps != null && state.gps.connected && state.gps.lat != AltosLib.MISSING)
390 show(pos(state.gps.lat,"N", "S"));
394 public Lat (GridBagLayout layout, int y) {
395 super (layout, y, "Latitude");
401 class Lon extends Value {
402 void show (AltosState state, AltosListenerState listener_state) {
403 if (state.gps != null && state.gps.connected && state.gps.lon != AltosLib.MISSING)
404 show(pos(state.gps.lon,"E", "W"));
408 public Lon (GridBagLayout layout, int y) {
409 super (layout, y, "Longitude");
415 class GPSLocked extends Info {
416 void show (AltosState state, AltosListenerState listener_state) {
417 if (state == null || state.gps == null)
420 show("%4d sats", state.gps.nsat);
421 lights.set(state.gps.locked && state.gps.nsat >= 4);
424 public GPSLocked (GridBagLayout layout, int y) {
425 super (layout, y, "GPS Locked");
429 GPSLocked gps_locked;
431 public void reset() {
435 ground_speed.reset();
441 public void set_font() {
442 cur.setFont(AltosUILib.label_font);
443 max.setFont(AltosUILib.label_font);
447 ground_speed.set_font();
448 ascent_rate.set_font();
450 gps_locked.set_font();
453 public void show(AltosState state, AltosListenerState listener_state) {
454 if (state.gps != null && state.gps.connected) {
455 lat.show(state, listener_state);
456 lon.show(state, listener_state);
461 altitude.show(state, listener_state);
462 ground_speed.show(state, listener_state);
463 ascent_rate.show(state, listener_state);
464 course.show(state, listener_state);
465 gps_locked.show(state, listener_state);
468 public void labels(GridBagLayout layout, int y) {
469 GridBagConstraints c;
471 cur = new JLabel("Current");
472 cur.setFont(AltosUILib.label_font);
473 c = new GridBagConstraints();
474 c.gridx = 2; c.gridy = y;
475 c.insets = new Insets(AltosUILib.tab_elt_pad, AltosUILib.tab_elt_pad, AltosUILib.tab_elt_pad, AltosUILib.tab_elt_pad);
476 layout.setConstraints(cur, c);
479 max = new JLabel("Maximum");
480 max.setFont(AltosUILib.label_font);
481 c.gridx = 3; c.gridy = y;
482 layout.setConstraints(max, c);
486 public String getName() {
490 public TeleGPSInfo() {
491 layout = new GridBagLayout();
495 /* Elements in ascent display:
503 altitude = new Altitude(layout, y++);
504 ground_speed = new GroundSpeed(layout, y++);
505 ascent_rate = new AscentRate(layout, y++);
506 course = new Course(layout, y++);
507 lat = new Lat(layout, y++);
508 lon = new Lon(layout, y++);
509 gps_locked = new GPSLocked(layout, y++);