altosui: Show "Missing" instead of huge numbers in descent/landed tabs
[fw/altos] / altosui / AltosLanded.java
1 /*
2  * Copyright © 2010 Keith Packard <keithp@keithp.com>
3  *
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.
7  *
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.
12  *
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.
16  */
17
18 package altosui;
19
20 import java.awt.*;
21 import java.awt.event.*;
22 import javax.swing.*;
23 import java.io.*;
24 import org.altusmetrum.altoslib_4.*;
25 import org.altusmetrum.altosuilib_2.*;
26
27 public class AltosLanded extends JComponent implements AltosFlightDisplay, ActionListener {
28         GridBagLayout   layout;
29
30         public abstract class LandedValue implements AltosFontListener, AltosUnitsListener {
31                 JLabel          label;
32                 JTextField      value;
33                 AltosUnits      units;
34                 double          v;
35
36                 abstract void show(AltosState state, AltosListenerState listener_state);
37
38                 void reset() {
39                         value.setText("");
40                 }
41
42                 void show() {
43                         label.setVisible(true);
44                         value.setVisible(true);
45                 }
46
47                 void show(String s) {
48                         show();
49                         value.setText(s);
50                 }
51
52                 void show(double v) {
53                         this.v = v;
54                         if (v == AltosLib.MISSING)
55                                 show("Missing");
56                         else
57                                 show(units.show(8, v));
58                 }
59
60                 void show(String format, double v) {
61                         show(String.format(format, v));
62                 }
63
64                 public void font_size_changed(int font_size) {
65                         label.setFont(Altos.label_font);
66                         value.setFont(Altos.value_font);
67                 }
68
69                 public void units_changed(boolean imperial_units) {
70                         if (units != null)
71                                 show(v);
72                 }
73
74                 void hide() {
75                         label.setVisible(false);
76                         value.setVisible(false);
77                 }
78
79                 public LandedValue (GridBagLayout layout, int y, AltosUnits units, String text) {
80                         this.units = units;
81
82                         GridBagConstraints      c = new GridBagConstraints();
83                         c.weighty = 1;
84
85                         label = new JLabel(text);
86                         label.setFont(Altos.label_font);
87                         label.setHorizontalAlignment(SwingConstants.LEFT);
88                         c.gridx = 0; c.gridy = y;
89                         c.insets = new Insets(10, 10, 10, 10);
90                         c.anchor = GridBagConstraints.WEST;
91                         c.weightx = 0;
92                         c.fill = GridBagConstraints.VERTICAL;
93                         layout.setConstraints(label, c);
94                         add(label);
95
96                         value = new JTextField(Altos.text_width);
97                         value.setFont(Altos.value_font);
98                         value.setHorizontalAlignment(SwingConstants.RIGHT);
99                         c.gridx = 1; c.gridy = y;
100                         c.anchor = GridBagConstraints.WEST;
101                         c.weightx = 1;
102                         c.fill = GridBagConstraints.BOTH;
103                         layout.setConstraints(value, c);
104                         add(value);
105                 }
106
107                 public LandedValue (GridBagLayout layout, int y, String text) {
108                         this(layout, y, null, text);
109                 }
110         }
111
112         String pos(double p, String pos, String neg) {
113                 String  h = pos;
114                 if (p < 0) {
115                         h = neg;
116                         p = -p;
117                 }
118                 int deg = (int) Math.floor(p);
119                 double min = (p - Math.floor(p)) * 60.0;
120                 return String.format("%s %4d° %9.6f", h, deg, min);
121         }
122
123         class Lat extends LandedValue {
124                 void show (AltosState state, AltosListenerState listener_state) {
125                         show();
126                         if (state.gps != null && state.gps.connected && state.gps.lat != AltosLib.MISSING)
127                                 show(pos(state.gps.lat,"N", "S"));
128                         else
129                                 show("???");
130                 }
131                 public Lat (GridBagLayout layout, int y) {
132                         super (layout, y, "Latitude");
133                 }
134         }
135
136         Lat lat;
137
138         class Lon extends LandedValue {
139                 void show (AltosState state, AltosListenerState listener_state) {
140                         show();
141                         if (state.gps != null && state.gps.connected && state.gps.lon != AltosLib.MISSING)
142                                 show(pos(state.gps.lon,"E", "W"));
143                         else
144                                 show("???");
145                 }
146                 public Lon (GridBagLayout layout, int y) {
147                         super (layout, y, "Longitude");
148                 }
149         }
150
151         Lon lon;
152
153         class Bearing extends LandedValue {
154                 void show (AltosState state, AltosListenerState listener_state) {
155                         show();
156                         if (state.from_pad != null)
157                                 show("%3.0f°", state.from_pad.bearing);
158                         else
159                                 show("???");
160                 }
161                 public Bearing (GridBagLayout layout, int y) {
162                         super (layout, y, "Bearing");
163                 }
164         }
165
166         Bearing bearing;
167
168         class Distance extends LandedValue {
169                 void show (AltosState state, AltosListenerState listener_state) {
170                         show();
171                         if (state.from_pad != null)
172                                 show(state.from_pad.distance);
173                         else
174                                 show("???");
175                 }
176                 public Distance (GridBagLayout layout, int y) {
177                         super (layout, y, AltosConvert.distance, "Distance");
178                 }
179         }
180
181         Distance distance;
182
183         class Height extends LandedValue {
184                 void show (AltosState state, AltosListenerState listener_state) {
185                         show(state.max_height());
186                 }
187                 public Height (GridBagLayout layout, int y) {
188                         super (layout, y, AltosConvert.height, "Maximum Height");
189                 }
190         }
191
192         Height  height;
193
194         class Speed extends LandedValue {
195                 void show (AltosState state, AltosListenerState listener_state) {
196                         show(state.max_speed());
197                 }
198                 public Speed (GridBagLayout layout, int y) {
199                         super (layout, y, AltosConvert.speed, "Maximum Speed");
200                 }
201         }
202
203         Speed   speed;
204
205         class Accel extends LandedValue {
206                 void show (AltosState state, AltosListenerState listener_state) {
207                         show(state.max_acceleration());
208                 }
209                 public Accel (GridBagLayout layout, int y) {
210                         super (layout, y, AltosConvert.accel, "Maximum Acceleration");
211                 }
212         }
213
214         Accel   accel;
215
216         public void reset() {
217                 lat.reset();
218                 lon.reset();
219                 bearing.reset();
220                 distance.reset();
221                 height.reset();
222                 speed.reset();
223                 accel.reset();
224         }
225
226         public void font_size_changed(int font_size) {
227                 lat.font_size_changed(font_size);
228                 lon.font_size_changed(font_size);
229                 bearing.font_size_changed(font_size);
230                 distance.font_size_changed(font_size);
231                 height.font_size_changed(font_size);
232                 speed.font_size_changed(font_size);
233                 accel.font_size_changed(font_size);
234         }
235
236         public void units_changed(boolean imperial_units) {
237                 lat.units_changed(imperial_units);
238                 lon.units_changed(imperial_units);
239                 bearing.units_changed(imperial_units);
240                 distance.units_changed(imperial_units);
241                 height.units_changed(imperial_units);
242                 speed.units_changed(imperial_units);
243                 accel.units_changed(imperial_units);
244         }
245
246         public void show(AltosState state, AltosListenerState listener_state) {
247                 if (state.gps != null && state.gps.connected) {
248                         bearing.show(state, listener_state);
249                         distance.show(state, listener_state);
250                         lat.show(state, listener_state);
251                         lon.show(state, listener_state);
252                 } else {
253                         bearing.hide();
254                         distance.hide();
255                         lat.hide();
256                         lon.hide();
257                 }
258                 height.show(state, listener_state);
259                 speed.show(state, listener_state);
260                 accel.show(state, listener_state);
261                 if (reader.backing_file() != null)
262                         graph.setEnabled(true);
263         }
264
265         JButton graph;
266         AltosFlightReader reader;
267
268         public void actionPerformed(ActionEvent e) {
269                 String  cmd = e.getActionCommand();
270
271                 if (cmd.equals("graph")) {
272                         File    file = reader.backing_file();
273                         if (file != null) {
274                                 String  filename = file.getName();
275                                 try {
276                                         AltosStateIterable states = null;
277                                         if (filename.endsWith("eeprom")) {
278                                                 FileInputStream in = new FileInputStream(file);
279                                                 states = new AltosEepromFile(in);
280                                         } else if (filename.endsWith("telem")) {
281                                                 FileInputStream in = new FileInputStream(file);
282                                                 states = new AltosTelemetryFile(in);
283                                         } else {
284                                                 throw new FileNotFoundException(filename);
285                                         }
286                                         try {
287                                                 new AltosGraphUI(states, file);
288                                         } catch (InterruptedException ie) {
289                                         } catch (IOException ie) {
290                                         }
291                                 } catch (FileNotFoundException fe) {
292                                         JOptionPane.showMessageDialog(null,
293                                                                       fe.getMessage(),
294                                                                       "Cannot open file",
295                                                                       JOptionPane.ERROR_MESSAGE);
296                                 }
297                         }
298                 }
299         }
300
301         public String getName() {
302                 return "Landed";
303         }
304
305         public AltosLanded(AltosFlightReader in_reader) {
306                 layout = new GridBagLayout();
307
308                 reader = in_reader;
309
310                 setLayout(layout);
311
312                 /* Elements in descent display */
313                 bearing = new Bearing(layout, 0);
314                 distance = new Distance(layout, 1);
315                 lat = new Lat(layout, 2);
316                 lon = new Lon(layout, 3);
317                 height = new Height(layout, 4);
318                 speed = new Speed(layout, 5);
319                 accel = new Accel(layout, 6);
320
321                 graph = new JButton ("Graph Flight");
322                 graph.setActionCommand("graph");
323                 graph.addActionListener(this);
324                 graph.setEnabled(false);
325
326                 GridBagConstraints      c = new GridBagConstraints();
327
328                 c.gridx = 0; c.gridy = 7;
329                 c.insets = new Insets(10, 10, 10, 10);
330                 c.anchor = GridBagConstraints.WEST;
331                 c.weightx = 0;
332                 c.weighty = 0;
333                 c.fill = GridBagConstraints.VERTICAL;
334                 add(graph, c);
335         }
336 }