altosdroid: Add map types and map preloading UIs
[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_7;
19
20 import java.awt.*;
21 import javax.swing.*;
22 import java.util.*;
23 import org.altusmetrum.altoslib_7.*;
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].setEditable(false);
59                                 value[j].setFont(AltosUILib.value_font);
60                                 value[j].setHorizontalAlignment(SwingConstants.RIGHT);
61                                 c.gridx = j+1; c.gridy = y;
62                                 c.anchor = GridBagConstraints.EAST;
63                                 c.fill = GridBagConstraints.BOTH;
64                                 c.weightx = 1;
65                                 layout.setConstraints(value[j], c);
66                                 add(value[j]);
67                         }
68                         flight_stats.add(this);
69                 }
70
71         }
72
73         public void font_size_changed(int font_size) {
74                 for (FlightStat f : flight_stats)
75                         f.font_size_changed(font_size);
76         }
77
78         static String pos(double p, String pos, String neg) {
79                 String  h = pos;
80                 if (p < 0) {
81                         h = neg;
82                         p = -p;
83                 }
84                 int deg = (int) Math.floor(p);
85                 double min = (p - Math.floor(p)) * 60.0;
86                 return String.format("%s %4d° %9.6f'", h, deg, min);
87         }
88
89         public AltosFlightStatsTable(AltosFlightStats stats) {
90                 layout = new GridBagLayout();
91
92                 setLayout(layout);
93                 int y = 0;
94                 new FlightStat(layout, y++, "Serial", String.format("%d", stats.serial));
95                 new FlightStat(layout, y++, "Flight", String.format("%d", stats.flight));
96                 if (stats.year != AltosLib.MISSING && stats.hour != AltosLib.MISSING)
97                         new FlightStat(layout, y++, "Date/Time",
98                                        String.format("%04d-%02d-%02d", stats.year, stats.month, stats.day),
99                                        String.format("%02d:%02d:%02d UTC", stats.hour, stats.minute, stats.second));
100                 else {
101                         if (stats.year != AltosLib.MISSING)
102                                 new FlightStat(layout, y++, "Date",
103                                                String.format("%04d-%02d-%02d", stats.year, stats.month, stats.day));
104                         if (stats.hour != AltosLib.MISSING)
105                                 new FlightStat(layout, y++, "Time",
106                                                String.format("%02d:%02d:%02d UTC", stats.hour, stats.minute, stats.second));
107                 }
108                 if (stats.max_height != AltosLib.MISSING) {
109                         new FlightStat(layout, y++, "Maximum height",
110                                        String.format("%5.0f m", stats.max_height),
111                                        String.format("%5.0f ft", AltosConvert.meters_to_feet(stats.max_height)));
112                 }
113                 if (stats.max_gps_height != AltosLib.MISSING) {
114                         new FlightStat(layout, y++, "Maximum GPS height",
115                                        String.format("%5.0f m", stats.max_gps_height),
116                                        String.format("%5.0f ft", AltosConvert.meters_to_feet(stats.max_gps_height)));
117                 }
118                 new FlightStat(layout, y++, "Maximum speed",
119                                String.format("%5.0f m/s", stats.max_speed),
120                                String.format("%5.0f mph", AltosConvert.meters_to_mph(stats.max_speed)),
121                                String.format("Mach %4.1f", AltosConvert.meters_to_mach(stats.max_speed)));
122                 if (stats.max_acceleration != AltosLib.MISSING) {
123                         new FlightStat(layout, y++, "Maximum boost acceleration",
124                                        String.format("%5.0f m/s²", stats.max_acceleration),
125                                        String.format("%5.0f ft/s²", AltosConvert.meters_to_feet(stats.max_acceleration)),
126                                        String.format("%5.0f G", AltosConvert.meters_to_g(stats.max_acceleration)));
127                         new FlightStat(layout, y++, "Average boost acceleration",
128                                        String.format("%5.0f m/s²", stats.state_accel[AltosLib.ao_flight_boost]),
129                                        String.format("%5.0f ft/s²", AltosConvert.meters_to_feet(stats.state_accel[AltosLib.ao_flight_boost])),
130                                        String.format("%5.0f G", AltosConvert.meters_to_g(stats.state_accel[AltosLib.ao_flight_boost])));
131                 }
132                 if (stats.state_speed[AltosLib.ao_flight_drogue] != AltosLib.MISSING)
133                         new FlightStat(layout, y++, "Drogue descent rate",
134                                        String.format("%5.0f m/s", stats.state_speed[AltosLib.ao_flight_drogue]),
135                                        String.format("%5.0f ft/s", AltosConvert.meters_to_feet(stats.state_speed[AltosLib.ao_flight_drogue])));
136                 if (stats.state_speed[AltosLib.ao_flight_main] != AltosLib.MISSING)
137                         new FlightStat(layout, y++, "Main descent rate",
138                                        String.format("%5.0f m/s", stats.state_speed[AltosLib.ao_flight_main]),
139                                        String.format("%5.0f ft/s", AltosConvert.meters_to_feet(stats.state_speed[AltosLib.ao_flight_main])));
140                 if (stats.state_start[AltosLib.ao_flight_boost] < stats.state_end[AltosLib.ao_flight_coast])
141                         new FlightStat(layout, y++, "Ascent time",
142                                        String.format("%6.1f s %s", stats.state_end[AltosLib.ao_flight_boost] - stats.state_start[AltosLib.ao_flight_boost],
143                                                      AltosLib.state_name(AltosLib.ao_flight_boost)),
144                                        String.format("%6.1f s %s", stats.state_end[AltosLib.ao_flight_fast] - stats.state_start[AltosLib.ao_flight_fast],
145                                                      AltosLib.state_name(AltosLib.ao_flight_fast)),
146                                        String.format("%6.1f s %s", stats.state_end[AltosLib.ao_flight_coast] - stats.state_start[AltosLib.ao_flight_coast],
147                                                      AltosLib.state_name(AltosLib.ao_flight_coast)));
148                 if (stats.state_start[AltosLib.ao_flight_drogue] < stats.state_end[AltosLib.ao_flight_main])
149                         new FlightStat(layout, y++, "Descent time",
150                                        String.format("%6.1f s %s", stats.state_end[AltosLib.ao_flight_drogue] - stats.state_start[AltosLib.ao_flight_drogue],
151                                                      AltosLib.state_name(AltosLib.ao_flight_drogue)),
152                                        String.format("%6.1f s %s", stats.state_end[AltosLib.ao_flight_main] - stats.state_start[AltosLib.ao_flight_main],
153                                                      AltosLib.state_name(AltosLib.ao_flight_main)));
154                 if (stats.state_start[AltosLib.ao_flight_boost] < stats.state_end[AltosLib.ao_flight_main])
155                         new FlightStat(layout, y++, "Flight time",
156                                        String.format("%6.1f s", stats.state_end[AltosLib.ao_flight_main] -
157                                                      stats.state_start[AltosLib.ao_flight_boost]));
158                 if (stats.has_gps) {
159                         new FlightStat(layout, y++, "Pad location",
160                                        pos(stats.pad_lat,"N","S"),
161                                        pos(stats.pad_lon,"E","W"));
162                         new FlightStat(layout, y++, "Last reported location",
163                                        pos(stats.lat,"N","S"),
164                                        pos(stats.lon,"E","W"));
165                 }
166         }
167 }