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.*;
24 import org.altusmetrum.altoslib_4.*;
25 import org.altusmetrum.altosuilib_2.*;
27 public class AltosLanded extends JComponent implements AltosFlightDisplay, ActionListener, HierarchyListener {
30 private AltosState last_state;
31 private AltosListenerState last_listener_state;
33 public abstract class LandedValue implements AltosFontListener, AltosUnitsListener {
38 String last_value = "";
40 abstract void show(AltosState state, AltosListenerState listener_state);
47 label.setVisible(true);
48 value.setVisible(true);
53 if (!last_value.equals(s)) {
61 if (v == AltosLib.MISSING)
64 show(units.show(8, v));
67 void show(String format, double v) {
68 show(String.format(format, v));
71 public void font_size_changed(int font_size) {
72 label.setFont(Altos.label_font);
73 value.setFont(Altos.value_font);
76 public void units_changed(boolean imperial_units) {
82 label.setVisible(false);
83 value.setVisible(false);
86 public LandedValue (GridBagLayout layout, int y, AltosUnits units, String text) {
89 GridBagConstraints c = new GridBagConstraints();
92 label = new JLabel(text);
93 label.setFont(Altos.label_font);
94 label.setHorizontalAlignment(SwingConstants.LEFT);
95 c.gridx = 0; c.gridy = y;
96 c.insets = new Insets(10, 10, 10, 10);
97 c.anchor = GridBagConstraints.WEST;
99 c.fill = GridBagConstraints.VERTICAL;
100 layout.setConstraints(label, c);
103 value = new JTextField(Altos.text_width);
104 value.setEditable(false);
105 value.setFont(Altos.value_font);
106 value.setHorizontalAlignment(SwingConstants.RIGHT);
107 c.gridx = 1; c.gridy = y;
108 c.anchor = GridBagConstraints.WEST;
110 c.fill = GridBagConstraints.BOTH;
111 layout.setConstraints(value, c);
115 public LandedValue (GridBagLayout layout, int y, String text) {
116 this(layout, y, null, text);
120 String pos(double p, String pos, String neg) {
126 int deg = (int) Math.floor(p);
127 double min = (p - Math.floor(p)) * 60.0;
128 return String.format("%s %4d° %9.6f", h, deg, min);
131 class Lat extends LandedValue {
132 void show (AltosState state, AltosListenerState listener_state) {
134 if (state.gps != null && state.gps.connected && state.gps.lat != AltosLib.MISSING)
135 show(pos(state.gps.lat,"N", "S"));
139 public Lat (GridBagLayout layout, int y) {
140 super (layout, y, "Latitude");
146 class Lon extends LandedValue {
147 void show (AltosState state, AltosListenerState listener_state) {
149 if (state.gps != null && state.gps.connected && state.gps.lon != AltosLib.MISSING)
150 show(pos(state.gps.lon,"E", "W"));
154 public Lon (GridBagLayout layout, int y) {
155 super (layout, y, "Longitude");
161 class Bearing extends LandedValue {
162 void show (AltosState state, AltosListenerState listener_state) {
164 if (state.from_pad != null)
165 show("%3.0f°", state.from_pad.bearing);
169 public Bearing (GridBagLayout layout, int y) {
170 super (layout, y, "Bearing");
176 class Distance extends LandedValue {
177 void show (AltosState state, AltosListenerState listener_state) {
179 if (state.from_pad != null)
180 show(state.from_pad.distance);
184 public Distance (GridBagLayout layout, int y) {
185 super (layout, y, AltosConvert.distance, "Distance");
191 class Height extends LandedValue {
192 void show (AltosState state, AltosListenerState listener_state) {
193 show(state.max_height());
195 public Height (GridBagLayout layout, int y) {
196 super (layout, y, AltosConvert.height, "Maximum Height");
202 class Speed extends LandedValue {
203 void show (AltosState state, AltosListenerState listener_state) {
204 show(state.max_speed());
206 public Speed (GridBagLayout layout, int y) {
207 super (layout, y, AltosConvert.speed, "Maximum Speed");
213 class Accel extends LandedValue {
214 void show (AltosState state, AltosListenerState listener_state) {
215 show(state.max_acceleration());
217 public Accel (GridBagLayout layout, int y) {
218 super (layout, y, AltosConvert.accel, "Maximum Acceleration");
224 public void reset() {
234 public void font_size_changed(int font_size) {
235 lat.font_size_changed(font_size);
236 lon.font_size_changed(font_size);
237 bearing.font_size_changed(font_size);
238 distance.font_size_changed(font_size);
239 height.font_size_changed(font_size);
240 speed.font_size_changed(font_size);
241 accel.font_size_changed(font_size);
244 public void units_changed(boolean imperial_units) {
245 lat.units_changed(imperial_units);
246 lon.units_changed(imperial_units);
247 bearing.units_changed(imperial_units);
248 distance.units_changed(imperial_units);
249 height.units_changed(imperial_units);
250 speed.units_changed(imperial_units);
251 accel.units_changed(imperial_units);
254 public void show(AltosState state, AltosListenerState listener_state) {
257 last_listener_state = listener_state;
261 if (state.gps != null && state.gps.connected) {
262 bearing.show(state, listener_state);
263 distance.show(state, listener_state);
264 lat.show(state, listener_state);
265 lon.show(state, listener_state);
272 height.show(state, listener_state);
273 speed.show(state, listener_state);
274 accel.show(state, listener_state);
275 if (reader.backing_file() != null)
276 graph.setEnabled(true);
280 AltosFlightReader reader;
282 public void actionPerformed(ActionEvent e) {
283 String cmd = e.getActionCommand();
285 if (cmd.equals("graph")) {
286 File file = reader.backing_file();
288 String filename = file.getName();
290 AltosStateIterable states = null;
291 if (filename.endsWith("eeprom")) {
292 FileInputStream in = new FileInputStream(file);
293 states = new AltosEepromFile(in);
294 } else if (filename.endsWith("telem")) {
295 FileInputStream in = new FileInputStream(file);
296 states = new AltosTelemetryFile(in);
298 throw new FileNotFoundException(filename);
301 new AltosGraphUI(states, file);
302 } catch (InterruptedException ie) {
303 } catch (IOException ie) {
305 } catch (FileNotFoundException fe) {
306 JOptionPane.showMessageDialog(null,
309 JOptionPane.ERROR_MESSAGE);
315 public String getName() {
319 public void hierarchyChanged(HierarchyEvent e) {
320 if (last_state != null && isShowing()) {
321 AltosState state = last_state;
322 AltosListenerState listener_state = last_listener_state;
325 last_listener_state = null;
326 show(state, listener_state);
330 public AltosLanded(AltosFlightReader in_reader) {
331 layout = new GridBagLayout();
337 /* Elements in descent display */
338 bearing = new Bearing(layout, 0);
339 distance = new Distance(layout, 1);
340 lat = new Lat(layout, 2);
341 lon = new Lon(layout, 3);
342 height = new Height(layout, 4);
343 speed = new Speed(layout, 5);
344 accel = new Accel(layout, 6);
346 graph = new JButton ("Graph Flight");
347 graph.setActionCommand("graph");
348 graph.addActionListener(this);
349 graph.setEnabled(false);
351 GridBagConstraints c = new GridBagConstraints();
353 c.gridx = 0; c.gridy = 7;
354 c.insets = new Insets(10, 10, 10, 10);
355 c.anchor = GridBagConstraints.WEST;
358 c.fill = GridBagConstraints.VERTICAL;
360 addHierarchyListener(this);