Altosui: Add flight statistics tab to graph window
[fw/altos] / altosui / 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 altosui;
19
20 import java.awt.*;
21 import java.awt.event.*;
22 import javax.swing.*;
23 import javax.swing.filechooser.FileNameExtensionFilter;
24 import javax.swing.table.*;
25 import java.io.*;
26 import java.util.*;
27 import java.text.*;
28 import java.util.prefs.*;
29 import java.util.concurrent.*;
30
31 public class AltosFlightStatsTable extends JComponent {
32         GridBagLayout   layout;
33
34         class FlightStat {
35                 JLabel          label;
36                 JTextField      value;
37
38                 public FlightStat(GridBagLayout layout, int y, String label_text, String ... values) {
39                         GridBagConstraints      c = new GridBagConstraints();
40                         c.insets = new Insets(Altos.tab_elt_pad, Altos.tab_elt_pad, Altos.tab_elt_pad, Altos.tab_elt_pad);
41                         c.weighty = 1;
42
43                         label = new JLabel(label_text);
44                         label.setFont(Altos.label_font);
45                         label.setHorizontalAlignment(SwingConstants.LEFT);
46                         c.gridx = 0; c.gridy = y;
47                         c.anchor = GridBagConstraints.WEST;
48                         c.fill = GridBagConstraints.VERTICAL;
49                         c.weightx = 0;
50                         layout.setConstraints(label, c);
51                         add(label);
52
53                         for (int j = 0; j < values.length; j++) {
54                                 value = new JTextField(values[j]);
55                                 value.setFont(Altos.value_font);
56                                 value.setHorizontalAlignment(SwingConstants.RIGHT);
57                                 c.gridx = j+1; c.gridy = y;
58                                 c.anchor = GridBagConstraints.EAST;
59                                 c.fill = GridBagConstraints.BOTH;
60                                 c.weightx = 1;
61                                 layout.setConstraints(value, c);
62                                 add(value);
63                         }
64                 }
65
66         }
67
68         public AltosFlightStatsTable(AltosFlightStats stats) {
69                 layout = new GridBagLayout();
70
71                 setLayout(layout);
72                 int y = 0;
73                 new FlightStat(layout, y++, "Maximum height",
74                                String.format("%5.0f m", stats.max_height),
75                                String.format("%5.0f ft", stats.max_height * 100 / 2.54 / 12));
76                 new FlightStat(layout, y++, "Maximum speed",
77                                String.format("%5.0f m/s", stats.max_speed),
78                                String.format("%5.0f ft/s", stats.max_speed * 100 / 2.54 / 12),
79                                String.format("Mach %5.3f", stats.max_speed / 343.0));
80                 new FlightStat(layout, y++, "Maximum acceleration",
81                                String.format("%5.0f m/s²", stats.max_acceleration),
82                                String.format("%5.0f ft/s²", stats.max_acceleration * 100 / 2.54 /12),
83                                String.format("%5.2f G", stats.max_acceleration / 9.80665));
84                 new FlightStat(layout, y++, "Average boost acceleration",
85                                String.format("%5.0f m/s²", stats.state_accel[Altos.ao_flight_boost]),
86                                String.format("%5.0f ft/s²", stats.state_accel[Altos.ao_flight_boost] * 100 / 2.54 /12),
87                                String.format("%5.2f G", stats.state_accel[Altos.ao_flight_boost] / 9.80665));
88                 new FlightStat(layout, y++, "Drogue descent rate",
89                                String.format("%5.0f m/s", stats.state_baro_speed[Altos.ao_flight_drogue]),
90                                String.format("%5.0f ft/s", stats.state_baro_speed[Altos.ao_flight_drogue] * 100 / 2.54 / 12));
91                 new FlightStat(layout, y++, "Main descent rate",
92                                String.format("%5.0f m/s", stats.state_baro_speed[Altos.ao_flight_main]),
93                                String.format("%5.0f ft/s", stats.state_baro_speed[Altos.ao_flight_main] * 100 / 2.54 / 12));
94                 for (int s = Altos.ao_flight_boost; s <= Altos.ao_flight_main; s++) {
95                         new FlightStat(layout, y++, String.format("%s time", Altos.state_to_string_capital[s]),
96                                        String.format("%6.2f s", stats.state_end[s] - stats.state_start[s]));
97                 }
98                 new FlightStat(layout, y++, "Flight Time",
99                                String.format("%6.2f s", stats.state_end[Altos.ao_flight_main] -
100                                              stats.state_start[Altos.ao_flight_boost]));
101                 
102         }
103         
104 }