2 * Copyright © 2014 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 java.awt.event.*;
24 import org.altusmetrum.altoslib_4.*;
25 import org.altusmetrum.altosuilib_2.*;
27 public class TeleGPSState extends JComponent implements AltosFlightDisplay, HierarchyListener {
31 private AltosState last_state;
32 private AltosListenerState last_listener_state;
34 abstract class Value extends AltosUIUnitsIndicator {
35 public Value (Container container, int y, AltosUnits units, String text) {
36 super(container, y, units, text, 1, false, 2);
40 abstract class DualValue extends AltosUIUnitsIndicator {
41 public DualValue (Container container, int y, AltosUnits units, String text) {
42 super(container, y, units, text, 2, false, 1);
46 abstract class ValueHold extends DualValue {
49 last_values[1] = AltosLib.MISSING;
51 public ValueHold (Container container, int y, AltosUnits units, String text) {
52 super(container, y, units, text);
56 class Height extends ValueHold {
57 public double value(AltosState state, int i) {
59 return state.height();
61 return state.max_height();
64 public Height(Container container, int y) {
65 super(container, y, AltosConvert.height, "Height");
69 class Speed extends ValueHold {
70 public double value(AltosState state, int i) {
72 return state.gps_speed();
74 return state.max_gps_speed();
77 public Speed(Container container, int y) {
78 super(container, y, AltosConvert.speed, "Speed");
82 class Distance extends Value {
83 public double value(AltosState state, int i) {
84 if (state.from_pad != null)
85 return state.from_pad.distance;
87 return AltosLib.MISSING;
90 public Distance(Container container, int y) {
91 super(container, y, AltosConvert.distance, "Distance");
95 class Range extends Value {
96 public double value(AltosState state, int i) {
99 public Range (Container container, int y) {
100 super (container, y, AltosConvert.distance, "Range");
104 class Bearing extends AltosUIIndicator {
105 public void show (AltosState state, AltosListenerState listener_state) {
106 if (state.from_pad != null) {
107 show( String.format("%3.0f°", state.from_pad.bearing),
108 state.from_pad.bearing_words(
109 AltosGreatCircle.BEARING_LONG));
114 public Bearing (Container container, int y) {
115 super (container, y, "Bearing", 2, false, 1);
119 class Elevation extends AltosUIIndicator {
120 public void show (AltosState state, AltosListenerState listener_state) {
121 show("%3.0f°", state.elevation);
123 public Elevation (Container container, int y) {
124 super (container, y, "Elevation", 1, false, 2);
128 class FirmwareVersion extends AltosUIIndicator {
129 public void show(AltosState state, AltosListenerState listener_state) {
130 if (state.firmware_version == null)
133 show(state.firmware_version);
136 public FirmwareVersion(Container container, int y) {
137 super(container, y, "Firmware Version", 1, false, 2);
141 class FlightLogMax extends AltosUIIndicator {
142 public void show(AltosState state, AltosListenerState listener_state) {
143 if (state.flight_log_max == AltosLib.MISSING)
146 show(String.format("%dkB", state.flight_log_max));
149 public FlightLogMax(Container container, int y) {
150 super(container, y, "Flight Log Storage", 1, false, 2);
154 class BatteryVoltage extends AltosUIVoltageIndicator {
155 public double voltage(AltosState state) {
156 return state.battery_voltage;
159 public double good() {
160 return AltosLib.ao_battery_good;
163 public BatteryVoltage(Container container, int y) {
164 super(container, y, "Battery Voltage", 2);
168 LinkedList<AltosUIIndicator> indicators = new LinkedList<AltosUIIndicator>();
170 public void labels(Container container, int y) {
171 GridBagLayout layout = (GridBagLayout)(container.getLayout());
172 GridBagConstraints c;
174 cur = new JLabel("Current");
175 cur.setFont(AltosUILib.label_font);
176 c = new GridBagConstraints();
177 c.gridx = 2; c.gridy = y;
178 c.insets = new Insets(AltosUILib.tab_elt_pad, AltosUILib.tab_elt_pad, AltosUILib.tab_elt_pad, AltosUILib.tab_elt_pad);
179 layout.setConstraints(cur, c);
182 max = new JLabel("Maximum");
183 max.setFont(AltosUILib.label_font);
184 c.gridx = 3; c.gridy = y;
185 layout.setConstraints(max, c);
189 public void reset() {
190 for (AltosUIIndicator i : indicators)
194 public void font_size_changed(int font_size) {
195 for (AltosUIIndicator i : indicators)
196 i.font_size_changed(font_size);
199 public void units_changed(boolean imperial_units) {
200 for (AltosUIIndicator i : indicators)
201 i.units_changed(imperial_units);
204 public void show(AltosState state, AltosListenerState listener_state) {
207 last_listener_state = listener_state;
211 for (AltosUIIndicator i : indicators)
212 i.show(state, listener_state);
215 public String getName() {
219 public void hierarchyChanged(HierarchyEvent e) {
220 if (last_state != null && isShowing()) {
221 AltosState state = last_state;
222 AltosListenerState listener_state = last_listener_state;
225 last_listener_state = null;
226 show(state, listener_state);
230 public TeleGPSState() {
231 layout = new GridBagLayout();
235 /* Elements in state display:
243 indicators.add(new Height(this, y++));
244 indicators.add(new Speed(this, y++));
245 indicators.add(new Distance(this, y++));
246 indicators.add(new Range(this, y++));
247 indicators.add(new Bearing(this, y++));
248 indicators.add(new Elevation(this, y++));
249 indicators.add(new FirmwareVersion(this, y++));
250 indicators.add(new FlightLogMax(this, y++));
251 indicators.add(new BatteryVoltage(this, y++));
252 addHierarchyListener(this);