altosuilib: Move AltosFlightStatsTable to altosuilib
[fw/altos] / altosuilib / AltosFlightStatsTable.java
1 /*
2  * Copyright © 2011 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 org.altusmetrum.altosuilib_2;
19
20 import java.awt.*;
21 import javax.swing.*;
22 import org.altusmetrum.altoslib_4.*;
23
24 public class AltosFlightStatsTable extends JComponent {
25         GridBagLayout   layout;
26
27         class FlightStat {
28                 JLabel          label;
29                 JTextField      value;
30
31                 public FlightStat(GridBagLayout layout, int y, String label_text, String ... values) {
32                         GridBagConstraints      c = new GridBagConstraints();
33                         c.insets = new Insets(AltosUILib.tab_elt_pad, AltosUILib.tab_elt_pad, AltosUILib.tab_elt_pad, AltosUILib.tab_elt_pad);
34                         c.weighty = 1;
35
36                         label = new JLabel(label_text);
37                         label.setFont(AltosUILib.label_font);
38                         label.setHorizontalAlignment(SwingConstants.LEFT);
39                         c.gridx = 0; c.gridy = y;
40                         c.anchor = GridBagConstraints.WEST;
41                         c.fill = GridBagConstraints.VERTICAL;
42                         c.weightx = 0;
43                         layout.setConstraints(label, c);
44                         add(label);
45
46                         for (int j = 0; j < values.length; j++) {
47                                 value = new JTextField(values[j]);
48                                 value.setFont(AltosUILib.value_font);
49                                 value.setHorizontalAlignment(SwingConstants.RIGHT);
50                                 c.gridx = j+1; c.gridy = y;
51                                 c.anchor = GridBagConstraints.EAST;
52                                 c.fill = GridBagConstraints.BOTH;
53                                 c.weightx = 1;
54                                 layout.setConstraints(value, c);
55                                 add(value);
56                         }
57                 }
58
59         }
60
61         static String pos(double p, String pos, String neg) {
62                 String  h = pos;
63                 if (p < 0) {
64                         h = neg;
65                         p = -p;
66                 }
67                 int deg = (int) Math.floor(p);
68                 double min = (p - Math.floor(p)) * 60.0;
69                 return String.format("%s %4d° %9.6f'", h, deg, min);
70         }
71
72         public AltosFlightStatsTable(AltosFlightStats stats) {
73                 layout = new GridBagLayout();
74
75                 setLayout(layout);
76                 int y = 0;
77                 new FlightStat(layout, y++, "Serial", String.format("%d", stats.serial));
78                 new FlightStat(layout, y++, "Flight", String.format("%d", stats.flight));
79                 if (stats.year != AltosLib.MISSING && stats.hour != AltosLib.MISSING)
80                         new FlightStat(layout, y++, "Date/Time",
81                                        String.format("%04d-%02d-%02d", stats.year, stats.month, stats.day),
82                                        String.format("%02d:%02d:%02d UTC", stats.hour, stats.minute, stats.second));
83                 else {
84                         if (stats.year != AltosLib.MISSING)
85                                 new FlightStat(layout, y++, "Date",
86                                                String.format("%04d-%02d-%02d", stats.year, stats.month, stats.day));
87                         if (stats.hour != AltosLib.MISSING)
88                                 new FlightStat(layout, y++, "Time",
89                                                String.format("%02d:%02d:%02d UTC", stats.hour, stats.minute, stats.second));
90                 }
91                 if (stats.max_height != AltosLib.MISSING) {
92                         new FlightStat(layout, y++, "Maximum height",
93                                        String.format("%5.0f m", stats.max_height),
94                                        String.format("%5.0f ft", AltosConvert.meters_to_feet(stats.max_height)));
95                 }
96                 if (stats.max_gps_height != AltosLib.MISSING) {
97                         new FlightStat(layout, y++, "Maximum GPS height",
98                                        String.format("%5.0f m", stats.max_gps_height),
99                                        String.format("%5.0f ft", AltosConvert.meters_to_feet(stats.max_gps_height)));
100                 }
101                 new FlightStat(layout, y++, "Maximum speed",
102                                String.format("%5.0f m/s", stats.max_speed),
103                                String.format("%5.0f mph", AltosConvert.meters_to_mph(stats.max_speed)),
104                                String.format("Mach %4.1f", AltosConvert.meters_to_mach(stats.max_speed)));
105                 if (stats.max_acceleration != AltosLib.MISSING) {
106                         new FlightStat(layout, y++, "Maximum boost acceleration",
107                                        String.format("%5.0f m/s²", stats.max_acceleration),
108                                        String.format("%5.0f ft/s²", AltosConvert.meters_to_feet(stats.max_acceleration)),
109                                        String.format("%5.0f G", AltosConvert.meters_to_g(stats.max_acceleration)));
110                         new FlightStat(layout, y++, "Average boost acceleration",
111                                        String.format("%5.0f m/s²", stats.state_accel[AltosLib.ao_flight_boost]),
112                                        String.format("%5.0f ft/s²", AltosConvert.meters_to_feet(stats.state_accel[AltosLib.ao_flight_boost])),
113                                        String.format("%5.0f G", AltosConvert.meters_to_g(stats.state_accel[AltosLib.ao_flight_boost])));
114                 }
115                 if (stats.state_speed[AltosLib.ao_flight_drogue] != AltosLib.MISSING)
116                         new FlightStat(layout, y++, "Drogue descent rate",
117                                        String.format("%5.0f m/s", stats.state_speed[AltosLib.ao_flight_drogue]),
118                                        String.format("%5.0f ft/s", AltosConvert.meters_to_feet(stats.state_speed[AltosLib.ao_flight_drogue])));
119                 if (stats.state_speed[AltosLib.ao_flight_main] != AltosLib.MISSING)
120                         new FlightStat(layout, y++, "Main descent rate",
121                                        String.format("%5.0f m/s", stats.state_speed[AltosLib.ao_flight_main]),
122                                        String.format("%5.0f ft/s", AltosConvert.meters_to_feet(stats.state_speed[AltosLib.ao_flight_main])));
123                 if (stats.state_start[AltosLib.ao_flight_boost] < stats.state_end[AltosLib.ao_flight_coast])
124                         new FlightStat(layout, y++, "Ascent time",
125                                        String.format("%6.1f s %s", stats.state_end[AltosLib.ao_flight_boost] - stats.state_start[AltosLib.ao_flight_boost],
126                                                      AltosLib.state_name(AltosLib.ao_flight_boost)),
127                                        String.format("%6.1f s %s", stats.state_end[AltosLib.ao_flight_fast] - stats.state_start[AltosLib.ao_flight_fast],
128                                                      AltosLib.state_name(AltosLib.ao_flight_fast)),
129                                        String.format("%6.1f s %s", stats.state_end[AltosLib.ao_flight_coast] - stats.state_start[AltosLib.ao_flight_coast],
130                                                      AltosLib.state_name(AltosLib.ao_flight_coast)));
131                 if (stats.state_start[AltosLib.ao_flight_drogue] < stats.state_end[AltosLib.ao_flight_main])
132                         new FlightStat(layout, y++, "Descent time",
133                                        String.format("%6.1f s %s", stats.state_end[AltosLib.ao_flight_drogue] - stats.state_start[AltosLib.ao_flight_drogue],
134                                                      AltosLib.state_name(AltosLib.ao_flight_drogue)),
135                                        String.format("%6.1f s %s", stats.state_end[AltosLib.ao_flight_main] - stats.state_start[AltosLib.ao_flight_main],
136                                                      AltosLib.state_name(AltosLib.ao_flight_main)));
137                 if (stats.state_start[AltosLib.ao_flight_boost] < stats.state_end[AltosLib.ao_flight_main])
138                         new FlightStat(layout, y++, "Flight time",
139                                        String.format("%6.1f s", stats.state_end[AltosLib.ao_flight_main] -
140                                                      stats.state_start[AltosLib.ao_flight_boost]));
141                 if (stats.has_gps) {
142                         new FlightStat(layout, y++, "Pad location",
143                                        pos(stats.pad_lat,"N","S"),
144                                        pos(stats.pad_lon,"E","W"));
145                         new FlightStat(layout, y++, "Last reported location",
146                                        pos(stats.lat,"N","S"),
147                                        pos(stats.lon,"E","W"));
148                 }
149         }
150
151 }