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