760b2d64e1456a6ebb4ddccf68414b4621eac4c8
[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, HierarchyListener {
28         GridBagLayout   layout;
29
30         private AltosState              last_state;
31         private AltosListenerState      last_listener_state;
32
33         public abstract class LandedValue implements AltosFontListener, AltosUnitsListener {
34                 JLabel          label;
35                 JTextField      value;
36                 AltosUnits      units;
37                 double          v;
38                 String          last_value = "";
39
40                 abstract void show(AltosState state, AltosListenerState listener_state);
41
42                 void reset() {
43                         value.setText("");
44                 }
45
46                 void show() {
47                         label.setVisible(true);
48                         value.setVisible(true);
49                 }
50
51                 void show(String s) {
52                         show();
53                         if (!last_value.equals(s)) {
54                                 value.setText(s);
55                                 last_value = s;
56                         }
57                 }
58
59                 void show(double v) {
60                         this.v = v;
61                         if (v == AltosLib.MISSING)
62                                 show("Missing");
63                         else
64                                 show(units.show(8, v));
65                 }
66
67                 void show(String format, double v) {
68                         show(String.format(format, v));
69                 }
70
71                 public void font_size_changed(int font_size) {
72                         label.setFont(Altos.label_font);
73                         value.setFont(Altos.value_font);
74                 }
75
76                 public void units_changed(boolean imperial_units) {
77                         if (units != null)
78                                 show(v);
79                 }
80
81                 void hide() {
82                         label.setVisible(false);
83                         value.setVisible(false);
84                 }
85
86                 public LandedValue (GridBagLayout layout, int y, AltosUnits units, String text) {
87                         this.units = units;
88
89                         GridBagConstraints      c = new GridBagConstraints();
90                         c.weighty = 1;
91
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;
98                         c.weightx = 0;
99                         c.fill = GridBagConstraints.VERTICAL;
100                         layout.setConstraints(label, c);
101                         add(label);
102
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;
109                         c.weightx = 1;
110                         c.fill = GridBagConstraints.BOTH;
111                         layout.setConstraints(value, c);
112                         add(value);
113                 }
114
115                 public LandedValue (GridBagLayout layout, int y, String text) {
116                         this(layout, y, null, text);
117                 }
118         }
119
120         String pos(double p, String pos, String neg) {
121                 String  h = pos;
122                 if (p < 0) {
123                         h = neg;
124                         p = -p;
125                 }
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);
129         }
130
131         class Lat extends LandedValue {
132                 void show (AltosState state, AltosListenerState listener_state) {
133                         show();
134                         if (state.gps != null && state.gps.connected && state.gps.lat != AltosLib.MISSING)
135                                 show(pos(state.gps.lat,"N", "S"));
136                         else
137                                 show("???");
138                 }
139                 public Lat (GridBagLayout layout, int y) {
140                         super (layout, y, "Latitude");
141                 }
142         }
143
144         Lat lat;
145
146         class Lon extends LandedValue {
147                 void show (AltosState state, AltosListenerState listener_state) {
148                         show();
149                         if (state.gps != null && state.gps.connected && state.gps.lon != AltosLib.MISSING)
150                                 show(pos(state.gps.lon,"E", "W"));
151                         else
152                                 show("???");
153                 }
154                 public Lon (GridBagLayout layout, int y) {
155                         super (layout, y, "Longitude");
156                 }
157         }
158
159         Lon lon;
160
161         class Bearing extends LandedValue {
162                 void show (AltosState state, AltosListenerState listener_state) {
163                         show();
164                         if (state.from_pad != null)
165                                 show("%3.0f°", state.from_pad.bearing);
166                         else
167                                 show("???");
168                 }
169                 public Bearing (GridBagLayout layout, int y) {
170                         super (layout, y, "Bearing");
171                 }
172         }
173
174         Bearing bearing;
175
176         class Distance extends LandedValue {
177                 void show (AltosState state, AltosListenerState listener_state) {
178                         show();
179                         if (state.from_pad != null)
180                                 show(state.from_pad.distance);
181                         else
182                                 show("???");
183                 }
184                 public Distance (GridBagLayout layout, int y) {
185                         super (layout, y, AltosConvert.distance, "Distance");
186                 }
187         }
188
189         Distance distance;
190
191         class Height extends LandedValue {
192                 void show (AltosState state, AltosListenerState listener_state) {
193                         show(state.max_height());
194                 }
195                 public Height (GridBagLayout layout, int y) {
196                         super (layout, y, AltosConvert.height, "Maximum Height");
197                 }
198         }
199
200         Height  height;
201
202         class Speed extends LandedValue {
203                 void show (AltosState state, AltosListenerState listener_state) {
204                         show(state.max_speed());
205                 }
206                 public Speed (GridBagLayout layout, int y) {
207                         super (layout, y, AltosConvert.speed, "Maximum Speed");
208                 }
209         }
210
211         Speed   speed;
212
213         class Accel extends LandedValue {
214                 void show (AltosState state, AltosListenerState listener_state) {
215                         show(state.max_acceleration());
216                 }
217                 public Accel (GridBagLayout layout, int y) {
218                         super (layout, y, AltosConvert.accel, "Maximum Acceleration");
219                 }
220         }
221
222         Accel   accel;
223
224         public void reset() {
225                 lat.reset();
226                 lon.reset();
227                 bearing.reset();
228                 distance.reset();
229                 height.reset();
230                 speed.reset();
231                 accel.reset();
232         }
233
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);
242         }
243
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);
252         }
253
254         public void show(AltosState state, AltosListenerState listener_state) {
255                 if (!isShowing()) {
256                         last_state = state;
257                         last_listener_state = listener_state;
258                         return;
259                 }
260
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);
266                 } else {
267                         bearing.hide();
268                         distance.hide();
269                         lat.hide();
270                         lon.hide();
271                 }
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);
277         }
278
279         JButton graph;
280         AltosFlightReader reader;
281
282         public void actionPerformed(ActionEvent e) {
283                 String  cmd = e.getActionCommand();
284
285                 if (cmd.equals("graph")) {
286                         File    file = reader.backing_file();
287                         if (file != null) {
288                                 String  filename = file.getName();
289                                 try {
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);
297                                         } else {
298                                                 throw new FileNotFoundException(filename);
299                                         }
300                                         try {
301                                                 new AltosGraphUI(states, file);
302                                         } catch (InterruptedException ie) {
303                                         } catch (IOException ie) {
304                                         }
305                                 } catch (FileNotFoundException fe) {
306                                         JOptionPane.showMessageDialog(null,
307                                                                       fe.getMessage(),
308                                                                       "Cannot open file",
309                                                                       JOptionPane.ERROR_MESSAGE);
310                                 }
311                         }
312                 }
313         }
314
315         public String getName() {
316                 return "Landed";
317         }
318
319         public void hierarchyChanged(HierarchyEvent e) {
320                 if (last_state != null && isShowing()) {
321                         AltosState              state = last_state;
322                         AltosListenerState      listener_state = last_listener_state;
323
324                         last_state = null;
325                         last_listener_state = null;
326                         show(state, listener_state);
327                 }
328         }
329
330         public AltosLanded(AltosFlightReader in_reader) {
331                 layout = new GridBagLayout();
332
333                 reader = in_reader;
334
335                 setLayout(layout);
336
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);
345
346                 graph = new JButton ("Graph Flight");
347                 graph.setActionCommand("graph");
348                 graph.addActionListener(this);
349                 graph.setEnabled(false);
350
351                 GridBagConstraints      c = new GridBagConstraints();
352
353                 c.gridx = 0; c.gridy = 7;
354                 c.insets = new Insets(10, 10, 10, 10);
355                 c.anchor = GridBagConstraints.WEST;
356                 c.weightx = 0;
357                 c.weighty = 0;
358                 c.fill = GridBagConstraints.VERTICAL;
359                 add(graph, c);
360                 addHierarchyListener(this);
361         }
362 }