altoslib,altosuilib: Remove stale source files
[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; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with this program; if not, write to the Free Software Foundation, Inc.,
16  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
17  */
18
19 package org.altusmetrum.altosuilib_11;
20
21 import java.awt.*;
22 import javax.swing.*;
23 import java.util.*;
24 import org.altusmetrum.altoslib_11.*;
25
26 public class AltosFlightStatsTable extends JComponent implements AltosFontListener {
27         GridBagLayout   layout;
28
29         LinkedList<FlightStat> flight_stats = new LinkedList<FlightStat>();
30
31         class FlightStat implements AltosFontListener {
32                 JLabel          label;
33                 JTextField[]    value;
34
35                 public void font_size_changed(int font_size) {
36                         label.setFont(AltosUILib.label_font);
37                         for (int i = 0; i < value.length; i++)
38                                 value[i].setFont(AltosUILib.value_font);
39                 }
40
41                 public FlightStat(GridBagLayout layout, int y, String label_text, String ... values) {
42                         GridBagConstraints      c = new GridBagConstraints();
43                         c.insets = new Insets(AltosUILib.tab_elt_pad, AltosUILib.tab_elt_pad, AltosUILib.tab_elt_pad, AltosUILib.tab_elt_pad);
44                         c.weighty = 1;
45
46                         label = new JLabel(label_text);
47                         label.setFont(AltosUILib.label_font);
48                         label.setHorizontalAlignment(SwingConstants.LEFT);
49                         c.gridx = 0; c.gridy = y;
50                         c.anchor = GridBagConstraints.WEST;
51                         c.fill = GridBagConstraints.VERTICAL;
52                         c.weightx = 0;
53                         layout.setConstraints(label, c);
54                         add(label);
55
56                         value = new JTextField[values.length];
57                         for (int j = 0; j < values.length; j++) {
58                                 value[j] = new JTextField(values[j]);
59                                 value[j].setEditable(false);
60                                 value[j].setFont(AltosUILib.value_font);
61                                 value[j].setHorizontalAlignment(SwingConstants.RIGHT);
62                                 c.gridx = j+1; c.gridy = y;
63                                 c.anchor = GridBagConstraints.EAST;
64                                 c.fill = GridBagConstraints.BOTH;
65                                 c.weightx = 1;
66                                 layout.setConstraints(value[j], c);
67                                 add(value[j]);
68                         }
69                         flight_stats.add(this);
70                 }
71
72         }
73
74         public void font_size_changed(int font_size) {
75                 for (FlightStat f : flight_stats)
76                         f.font_size_changed(font_size);
77         }
78
79         static String pos(double p, String pos, String neg) {
80                 String  h = pos;
81                 if (p < 0) {
82                         h = neg;
83                         p = -p;
84                 }
85                 int deg = (int) Math.floor(p);
86                 double min = (p - Math.floor(p)) * 60.0;
87                 return String.format("%s %4d° %9.6f'", h, deg, min);
88         }
89
90         public void set_stats(AltosFlightStats stats) {
91                 int y = 0;
92                 if (stats.serial != AltosLib.MISSING) {
93                         if (stats.product != null && stats.firmware_version != null)
94                                 new FlightStat(layout, y++, "Device",
95                                                stats.product,
96                                                String.format("version %s", stats.firmware_version),
97                                                String.format("serial %d", stats.serial));
98                         else
99                                 new FlightStat(layout, y++, "Serial", String.format("%d", stats.serial));
100                 }
101                 if (stats.flight != AltosLib.MISSING)
102                         new FlightStat(layout, y++, "Flight", String.format("%d", stats.flight));
103                 if (stats.year != AltosLib.MISSING && stats.hour != AltosLib.MISSING)
104                         new FlightStat(layout, y++, "Date/Time",
105                                        String.format("%04d-%02d-%02d", stats.year, stats.month, stats.day),
106                                        String.format("%02d:%02d:%02d UTC", stats.hour, stats.minute, stats.second));
107                 else {
108                         if (stats.year != AltosLib.MISSING)
109                                 new FlightStat(layout, y++, "Date",
110                                                String.format("%04d-%02d-%02d", stats.year, stats.month, stats.day));
111                         if (stats.hour != AltosLib.MISSING)
112                                 new FlightStat(layout, y++, "Time",
113                                                String.format("%02d:%02d:%02d UTC", stats.hour, stats.minute, stats.second));
114                 }
115                 if (stats.max_height != AltosLib.MISSING) {
116                         new FlightStat(layout, y++, "Maximum height",
117                                        String.format("%6.1f m", stats.max_height),
118                                        String.format("%5.0f ft", AltosConvert.meters_to_feet(stats.max_height)));
119                 }
120                 if (stats.max_gps_height != AltosLib.MISSING) {
121                         new FlightStat(layout, y++, "Maximum GPS height",
122                                        String.format("%6.1f m", stats.max_gps_height),
123                                        String.format("%5.0f ft", AltosConvert.meters_to_feet(stats.max_gps_height)));
124                 }
125                 if (stats.max_speed != AltosLib.MISSING) {
126                         new FlightStat(layout, y++, "Maximum speed",
127                                        String.format("%6.1f m/s", stats.max_speed),
128                                        String.format("%5.0f fps", AltosConvert.mps_to_fps(stats.max_speed)),
129                                        String.format("Mach %4.1f", AltosConvert.meters_to_mach(stats.max_speed)));
130                 }
131                 if (stats.max_acceleration != AltosLib.MISSING)
132                         new FlightStat(layout, y++, "Maximum boost acceleration",
133                                        String.format("%6.1f m/s²", stats.max_acceleration),
134                                        String.format("%5.0f ft/s²", AltosConvert.meters_to_feet(stats.max_acceleration)),
135                                        String.format("%6.2f G", AltosConvert.meters_to_g(stats.max_acceleration)));
136                 if (stats.state_accel[AltosLib.ao_flight_boost] != AltosLib.MISSING)
137                         new FlightStat(layout, y++, "Average boost acceleration",
138                                        String.format("%6.1f m/s²", stats.state_accel[AltosLib.ao_flight_boost]),
139                                        String.format("%5.0f ft/s²", AltosConvert.meters_to_feet(stats.state_accel[AltosLib.ao_flight_boost])),
140                                        String.format("%6.2f G", AltosConvert.meters_to_g(stats.state_accel[AltosLib.ao_flight_boost])));
141                 if (stats.state_start[AltosLib.ao_flight_boost] < stats.state_end[AltosLib.ao_flight_coast]) {
142
143                         double  boost_time = stats.state_end[AltosLib.ao_flight_boost] - stats.state_start[AltosLib.ao_flight_boost];
144                         double  fast_time = stats.state_end[AltosLib.ao_flight_fast] - stats.state_start[AltosLib.ao_flight_fast];
145                         double  coast_time = stats.state_end[AltosLib.ao_flight_coast] - stats.state_start[AltosLib.ao_flight_coast];
146
147                         if (fast_time > 0) {
148                                 new FlightStat(layout, y++, "Ascent time",
149                                                String.format("%6.1f s %s", boost_time,
150                                                              AltosLib.state_name(AltosLib.ao_flight_boost)),
151                                                String.format("%6.1f s %s", fast_time,
152                                                              AltosLib.state_name(AltosLib.ao_flight_fast)),
153                                                String.format("%6.1f s %s", coast_time,
154                                                              AltosLib.state_name(AltosLib.ao_flight_coast)));
155                         } else {
156                                 new FlightStat(layout, y++, "Ascent time",
157                                                String.format("%6.1f s %s", boost_time,
158                                                              AltosLib.state_name(AltosLib.ao_flight_boost)),
159                                                String.format("%6.1f s %s", coast_time,
160                                                              AltosLib.state_name(AltosLib.ao_flight_coast)));
161                         }
162                 }
163                 if (stats.state_speed[AltosLib.ao_flight_drogue] != AltosLib.MISSING) {
164                         String  label;
165
166                         if (stats.state_speed[AltosLib.ao_flight_main] == AltosLib.MISSING)
167                                 label = "Descent rate";
168                         else
169                                 label = "Drogue descent rate";
170                         new FlightStat(layout, y++, label,
171                                        String.format("%6.1f m/s", -stats.state_speed[AltosLib.ao_flight_drogue]),
172                                        String.format("%5.0f ft/s", -AltosConvert.meters_to_feet(stats.state_speed[AltosLib.ao_flight_drogue])));
173                 }
174                 if (stats.state_speed[AltosLib.ao_flight_main] != AltosLib.MISSING)
175                         new FlightStat(layout, y++, "Main descent rate",
176                                        String.format("%6.1f m/s", -stats.state_speed[AltosLib.ao_flight_main]),
177                                        String.format("%5.0f ft/s", -AltosConvert.meters_to_feet(stats.state_speed[AltosLib.ao_flight_main])));
178                 if (stats.state_start[AltosLib.ao_flight_drogue] < stats.state_end[AltosLib.ao_flight_main]) {
179                         double  drogue_duration = stats.state_end[AltosLib.ao_flight_drogue] - stats.state_start[AltosLib.ao_flight_drogue];
180                         double  main_duration = stats.landed_time - stats.state_start[AltosLib.ao_flight_main];
181                         double  duration = stats.landed_time - stats.state_start[AltosLib.ao_flight_drogue];
182
183                         if (drogue_duration > 0 && main_duration > 0) {
184                                 new FlightStat(layout, y++, "Descent time",
185                                                String.format("%6.1f s %s", drogue_duration,
186                                                              AltosLib.state_name(AltosLib.ao_flight_drogue)),
187                                                String.format("%6.1f s %s", main_duration,
188                                                              AltosLib.state_name(AltosLib.ao_flight_main)));
189                         } else if (duration > 0) {
190                                 new FlightStat(layout, y++, "Descent time",
191                                                String.format("%6.1f s", duration));
192                         }
193                 }
194                 if (stats.state_start[AltosLib.ao_flight_boost] < stats.state_start[AltosLib.ao_flight_landed])
195                         new FlightStat(layout, y++, "Flight time",
196                                        String.format("%6.1f s", stats.landed_time - stats.boost_time));
197                 if (stats.pad_lat != AltosLib.MISSING) {
198                         new FlightStat(layout, y++, "Pad location",
199                                        pos(stats.pad_lat,"N","S"),
200                                        pos(stats.pad_lon,"E","W"));
201                 }
202                 if (stats.lat != AltosLib.MISSING) {
203                         new FlightStat(layout, y++, "Last reported location",
204                                        pos(stats.lat,"N","S"),
205                                        pos(stats.lon,"E","W"));
206                 }
207         }
208
209         public void tell_closing() {
210                 AltosUIPreferences.unregister_font_listener(this);
211         }
212
213         public AltosFlightStatsTable() {
214                 layout = new GridBagLayout();
215
216                 setLayout(layout);
217
218                 AltosUIPreferences.register_font_listener(this);
219         }
220
221         public AltosFlightStatsTable(AltosFlightStats stats) {
222                 this();
223                 set_stats(stats);
224         }
225 }