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.
21 import java.awt.event.*;
23 import org.altusmetrum.altoslib_4.*;
24 import org.altusmetrum.altosuilib_2.*;
26 public class AltosDescent extends JComponent implements AltosFlightDisplay, HierarchyListener {
29 private AltosState last_state;
30 private AltosListenerState last_listener_state;
32 public abstract class DescentStatus implements AltosFontListener, AltosUnitsListener {
37 abstract void show(AltosState state, AltosListenerState listener_state);
40 label.setVisible(true);
41 value.setVisible(true);
42 lights.setVisible(true);
50 void show(String format, double value) {
51 show(String.format(format, value));
55 label.setVisible(false);
56 value.setVisible(false);
57 lights.setVisible(false);
65 public void font_size_changed(int font_size) {
66 label.setFont(Altos.label_font);
67 value.setFont(Altos.value_font);
70 public void units_changed(boolean imperial_units) {
73 public DescentStatus (GridBagLayout layout, int y, String text) {
74 GridBagConstraints c = new GridBagConstraints();
77 lights = new AltosLights();
78 c.gridx = 0; c.gridy = y;
79 c.anchor = GridBagConstraints.CENTER;
80 c.fill = GridBagConstraints.VERTICAL;
82 layout.setConstraints(lights, c);
85 label = new JLabel(text);
86 label.setFont(Altos.label_font);
87 label.setHorizontalAlignment(SwingConstants.LEFT);
88 c.gridx = 1; c.gridy = y;
89 c.insets = new Insets(Altos.tab_elt_pad, Altos.tab_elt_pad, Altos.tab_elt_pad, Altos.tab_elt_pad);
90 c.anchor = GridBagConstraints.WEST;
91 c.fill = GridBagConstraints.VERTICAL;
94 layout.setConstraints(label, c);
97 value = new JTextField(Altos.text_width);
98 value.setEditable(false);
99 value.setFont(Altos.value_font);
100 value.setHorizontalAlignment(SwingConstants.RIGHT);
101 c.gridx = 4; c.gridy = y;
103 c.anchor = GridBagConstraints.WEST;
104 c.fill = GridBagConstraints.BOTH;
106 layout.setConstraints(value, c);
112 public abstract class DescentValue implements AltosFontListener, AltosUnitsListener {
117 String last_value = "";
123 abstract void show(AltosState state, AltosListenerState listener_state);
126 label.setVisible(true);
127 value.setVisible(true);
131 label.setVisible(false);
132 value.setVisible(false);
135 void show(String v) {
138 if (!last_value.equals(v)) {
144 void show(double v) {
146 if (v == AltosLib.MISSING)
149 show(units.show(8, v));
152 void show(String format, double v) {
153 if (v == AltosLib.MISSING)
156 show(String.format(format, v));
159 public void font_size_changed(int font_size) {
160 label.setFont(Altos.label_font);
161 value.setFont(Altos.value_font);
164 public void units_changed(boolean imperial_units) {
169 public DescentValue (GridBagLayout layout, int x, int y, AltosUnits units, String text) {
171 GridBagConstraints c = new GridBagConstraints();
174 label = new JLabel(text);
175 label.setFont(Altos.label_font);
176 label.setHorizontalAlignment(SwingConstants.LEFT);
177 c.gridx = x + 1; c.gridy = y;
178 c.insets = new Insets(Altos.tab_elt_pad, Altos.tab_elt_pad, Altos.tab_elt_pad, Altos.tab_elt_pad);
179 c.anchor = GridBagConstraints.WEST;
180 c.fill = GridBagConstraints.VERTICAL;
184 value = new JTextField(Altos.text_width);
185 value.setEditable(false);
186 value.setFont(Altos.value_font);
187 value.setHorizontalAlignment(SwingConstants.RIGHT);
188 c.gridx = x + 2; c.gridy = y;
190 c.anchor = GridBagConstraints.WEST;
191 c.fill = GridBagConstraints.BOTH;
196 public DescentValue (GridBagLayout layout, int x, int y, String text) {
197 this(layout, x, y, null, text);
201 public abstract class DescentDualValue implements AltosFontListener, AltosUnitsListener {
212 label.setVisible(true);
213 value1.setVisible(true);
214 value2.setVisible(true);
218 label.setVisible(false);
219 value1.setVisible(false);
220 value2.setVisible(false);
223 public void font_size_changed(int font_size) {
224 label.setFont(Altos.label_font);
225 value1.setFont(Altos.value_font);
226 value2.setFont(Altos.value_font);
229 public void units_changed(boolean imperial_units) {
232 abstract void show(AltosState state, AltosListenerState listener_state);
234 void show(String v1, String v2) {
239 void show(String f1, double v1, String f2, double v2) {
241 value1.setText(String.format(f1, v1));
242 value2.setText(String.format(f2, v2));
245 public DescentDualValue (GridBagLayout layout, int x, int y, String text) {
246 GridBagConstraints c = new GridBagConstraints();
249 label = new JLabel(text);
250 label.setFont(Altos.label_font);
251 label.setHorizontalAlignment(SwingConstants.LEFT);
252 c.gridx = x + 1; c.gridy = y;
253 c.insets = new Insets(Altos.tab_elt_pad, Altos.tab_elt_pad, Altos.tab_elt_pad, Altos.tab_elt_pad);
254 c.anchor = GridBagConstraints.WEST;
255 c.fill = GridBagConstraints.VERTICAL;
257 layout.setConstraints(label, c);
260 value1 = new JTextField(Altos.text_width);
261 value1.setEditable(false);
262 value1.setFont(Altos.value_font);
263 value1.setHorizontalAlignment(SwingConstants.RIGHT);
264 c.gridx = x + 2; c.gridy = y;
265 c.anchor = GridBagConstraints.WEST;
266 c.fill = GridBagConstraints.BOTH;
268 layout.setConstraints(value1, c);
271 value2 = new JTextField(Altos.text_width);
272 value2.setEditable(false);
273 value2.setFont(Altos.value_font);
274 value2.setHorizontalAlignment(SwingConstants.RIGHT);
275 c.gridx = x + 4; c.gridy = y;
276 c.anchor = GridBagConstraints.WEST;
277 c.fill = GridBagConstraints.BOTH;
280 layout.setConstraints(value2, c);
285 class Height extends DescentValue {
286 void show (AltosState state, AltosListenerState listener_state) {
287 show(state.height());
289 public Height (GridBagLayout layout, int x, int y) {
290 super (layout, x, y, AltosConvert.height, "Height");
296 class Speed extends DescentValue {
297 void show (AltosState state, AltosListenerState listener_state) {
300 public Speed (GridBagLayout layout, int x, int y) {
301 super (layout, x, y, AltosConvert.speed, "Speed");
307 String pos(double p, String pos, String neg) {
313 int deg = (int) Math.floor(p);
314 double min = (p - Math.floor(p)) * 60.0;
315 return String.format("%s %d° %9.6f", h, deg, min);
318 class Lat extends DescentValue {
319 void show (AltosState state, AltosListenerState listener_state) {
320 if (state.gps != null && state.gps.connected && state.gps.lat != AltosLib.MISSING)
321 show(pos(state.gps.lat,"N", "S"));
325 public Lat (GridBagLayout layout, int x, int y) {
326 super (layout, x, y, "Latitude");
332 class Lon extends DescentValue {
333 void show (AltosState state, AltosListenerState listener_state) {
334 if (state.gps != null && state.gps.connected && state.gps.lon != AltosLib.MISSING)
335 show(pos(state.gps.lon,"W", "E"));
339 public Lon (GridBagLayout layout, int x, int y) {
340 super (layout, x, y, "Longitude");
346 class Distance extends DescentValue {
347 void show(AltosState state, AltosListenerState listener_state) {
348 if (state.from_pad != null)
349 show(state.from_pad.distance);
354 public Distance (GridBagLayout layout, int x, int y) {
355 super(layout, x, y, AltosConvert.distance, "Ground Distance");
362 class Apogee extends DescentStatus {
363 void show (AltosState state, AltosListenerState listener_state) {
364 show("%4.2f V", state.apogee_voltage);
365 lights.set(state.apogee_voltage >= AltosLib.ao_igniter_good);
367 public Apogee (GridBagLayout layout, int y) {
368 super(layout, y, "Apogee Igniter Voltage");
374 class Main extends DescentStatus {
375 void show (AltosState state, AltosListenerState listener_state) {
376 show("%4.2f V", state.main_voltage);
377 lights.set(state.main_voltage >= AltosLib.ao_igniter_good);
379 public Main (GridBagLayout layout, int y) {
380 super(layout, y, "Main Igniter Voltage");
386 class Bearing extends DescentDualValue {
387 void show (AltosState state, AltosListenerState listener_state) {
388 if (state.from_pad != null) {
389 show( String.format("%3.0f°", state.from_pad.bearing),
390 state.from_pad.bearing_words(
391 AltosGreatCircle.BEARING_LONG));
396 public Bearing (GridBagLayout layout, int x, int y) {
397 super (layout, x, y, "Bearing");
403 class Range extends DescentValue {
404 void show (AltosState state, AltosListenerState listener_state) {
407 public Range (GridBagLayout layout, int x, int y) {
408 super (layout, x, y, AltosConvert.distance, "Range");
414 class Elevation extends DescentValue {
415 void show (AltosState state, AltosListenerState listener_state) {
416 show("%3.0f°", state.elevation);
418 public Elevation (GridBagLayout layout, int x, int y) {
419 super (layout, x, y, "Elevation");
425 public void reset() {
438 public void font_size_changed(int font_size) {
439 lat.font_size_changed(font_size);
440 lon.font_size_changed(font_size);
441 height.font_size_changed(font_size);
442 speed.font_size_changed(font_size);
443 bearing.font_size_changed(font_size);
444 range.font_size_changed(font_size);
445 distance.font_size_changed(font_size);
446 elevation.font_size_changed(font_size);
447 main.font_size_changed(font_size);
448 apogee.font_size_changed(font_size);
451 public void units_changed(boolean imperial_units) {
452 lat.units_changed(imperial_units);
453 lon.units_changed(imperial_units);
454 height.units_changed(imperial_units);
455 speed.units_changed(imperial_units);
456 bearing.units_changed(imperial_units);
457 range.units_changed(imperial_units);
458 distance.units_changed(imperial_units);
459 elevation.units_changed(imperial_units);
460 main.units_changed(imperial_units);
461 apogee.units_changed(imperial_units);
464 public void show(AltosState state, AltosListenerState listener_state) {
467 last_listener_state = listener_state;
471 height.show(state, listener_state);
472 speed.show(state, listener_state);
473 if (state.gps != null && state.gps.connected) {
474 bearing.show(state, listener_state);
475 range.show(state, listener_state);
476 distance.show(state, listener_state);
477 elevation.show(state, listener_state);
478 lat.show(state, listener_state);
479 lon.show(state, listener_state);
488 if (state.main_voltage != AltosLib.MISSING)
489 main.show(state, listener_state);
492 if (state.apogee_voltage != AltosLib.MISSING)
493 apogee.show(state, listener_state);
498 public String getName() {
502 public void hierarchyChanged(HierarchyEvent e) {
503 if (last_state != null && isShowing()) {
504 AltosState state = last_state;
505 AltosListenerState listener_state = last_listener_state;
508 last_listener_state = null;
509 show(state, listener_state);
513 public AltosDescent() {
514 layout = new GridBagLayout();
518 /* Elements in descent display */
519 speed = new Speed(layout, 0, 0);
520 height = new Height(layout, 2, 0);
521 elevation = new Elevation(layout, 0, 1);
522 range = new Range(layout, 2, 1);
523 bearing = new Bearing(layout, 0, 2);
524 distance = new Distance(layout, 0, 3);
525 lat = new Lat(layout, 0, 4);
526 lon = new Lon(layout, 2, 4);
528 apogee = new Apogee(layout, 5);
529 main = new Main(layout, 6);
530 addHierarchyListener(this);