Merge branch 'master' of ssh://git.gag.com/scm/git/fw/altos
[fw/altos] / micropeak / MicroStatsTable.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.micropeak;
19
20 import java.awt.*;
21 import javax.swing.*;
22 import org.altusmetrum.AltosLib.*;
23 import org.altusmetrum.altosuilib.*;
24
25 public class MicroStatsTable extends JComponent {
26         GridBagLayout   layout;
27
28         class MicroStat {
29                 JLabel          label;
30                 JTextField[]    texts;
31
32                 public void set_values(String ... values) {
33                         for (int j = 0; j < values.length; j++) {
34                                 texts[j].setText(values[j]);
35                         }
36                 }
37
38                 public MicroStat(GridBagLayout layout, int y, String label_text, String ... values) {
39                         GridBagConstraints      c = new GridBagConstraints();
40                         c.insets = new Insets(AltosUILib.tab_elt_pad, AltosUILib.tab_elt_pad, AltosUILib.tab_elt_pad, AltosUILib.tab_elt_pad);
41                         c.weighty = 1;
42
43                         label = new JLabel(label_text);
44                         label.setFont(AltosUILib.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                         texts = new JTextField[values.length];
54                         for (int j = 0; j < values.length; j++) {
55                                 JTextField value = new JTextField(values[j]);
56                                 value.setFont(AltosUILib.value_font);
57                                 value.setHorizontalAlignment(SwingConstants.RIGHT);
58                                 texts[j] = value;
59                                 c.gridx = j+1; c.gridy = y;
60                                 c.anchor = GridBagConstraints.EAST;
61                                 c.fill = GridBagConstraints.BOTH;
62                                 c.weightx = 1;
63                                 layout.setConstraints(value, c);
64                                 add(value);
65                         }
66                 }
67         }
68
69         MicroStat       max_height, max_speed;
70         MicroStat       max_accel, avg_accel;
71         MicroStat       boost_duration;
72         MicroStat       coast_duration;
73         MicroStat       descent_speed;
74         MicroStat       descent_duration;
75         MicroStat       flight_time;
76         
77         public void setStats(MicroStats stats) {
78                 max_height.set_values(String.format("%5.0f m", stats.apogee_height),
79                                       String.format("%5.0f ft", AltosConvert.meters_to_feet(stats.apogee_height)));
80                 max_speed.set_values(String.format("%5.0f m/s", stats.max_speed),
81                                      String.format("%5.0f mph", AltosConvert.meters_to_mph(stats.max_speed)),
82                                      String.format("Mach %4.1f", AltosConvert.meters_to_mach(stats.max_speed)));
83                 max_accel.set_values(String.format("%5.0f m/s²", stats.max_accel),
84                                      String.format("%5.0f ft/s²", AltosConvert.meters_to_feet(stats.max_accel)),
85                                      String.format("%5.0f G", AltosConvert.meters_to_g(stats.max_accel)));
86                 avg_accel.set_values(String.format("%5.0f m/s²", stats.boost_accel(),
87                                                    String.format("%5.0f ft/s²", AltosConvert.meters_to_feet(stats.boost_accel())),
88                                                    String.format("%5.0f G", AltosConvert.meters_to_g(stats.boost_accel()))));
89                 boost_duration.set_values(String.format("%6.1f s", stats.boost_duration()));
90                 coast_duration.set_values(String.format("%6.1f s", stats.coast_duration()));
91                 descent_speed.set_values(String.format("%5.0f m/s", stats.descent_speed()),
92                                          String.format("%5.0f ft/s", AltosConvert.meters_to_feet(stats.descent_speed())));
93                 descent_duration.set_values(String.format("%6.1f s", stats.descent_duration()));
94                 flight_time.set_values(String.format("%6.1f s", stats.landed_time));
95         }
96
97         public void setData(MicroData data) {
98                 setStats(new MicroStats(data));
99         }
100
101         public MicroStatsTable(MicroStats stats) {
102                 layout = new GridBagLayout();
103
104                 setLayout(layout);
105                 int y = 0;
106                 max_height = new MicroStat(layout, y++, "Maximum height",
107                                            String.format("%5.0f m", stats.apogee_height),
108                                            String.format("%5.0f ft", AltosConvert.meters_to_feet(stats.apogee_height)));
109                 max_speed = new MicroStat(layout, y++, "Maximum speed",
110                                           String.format("%5.0f m/s", stats.max_speed),
111                                           String.format("%5.0f mph", AltosConvert.meters_to_mph(stats.max_speed)),
112                                           String.format("Mach %4.1f", AltosConvert.meters_to_mach(stats.max_speed)));
113                 max_accel = new MicroStat(layout, y++, "Maximum boost acceleration",
114                                           String.format("%5.0f m/s²", stats.max_accel),
115                                           String.format("%5.0f ft/s²", AltosConvert.meters_to_feet(stats.max_accel)),
116                                           String.format("%5.0f G", AltosConvert.meters_to_g(stats.max_accel)));
117                 avg_accel = new MicroStat(layout, y++, "Average boost acceleration",
118                                           String.format("%5.0f m/s²", stats.boost_accel(),
119                                                         String.format("%5.0f ft/s²", AltosConvert.meters_to_feet(stats.boost_accel())),
120                                                         String.format("%5.0f G", AltosConvert.meters_to_g(stats.boost_accel()))));
121                 boost_duration = new MicroStat(layout, y++, "Boost duration",
122                                                String.format("%6.0f s", stats.boost_duration()));
123                 coast_duration = new MicroStat(layout, y++, "Coast duration",
124                                                String.format("%6.1f s", stats.coast_duration()));
125                 descent_speed = new MicroStat(layout, y++, "Descent rate",
126                                               String.format("%5.0f m/s", stats.descent_speed()),
127                                               String.format("%5.0f ft/s", AltosConvert.meters_to_feet(stats.descent_speed())));
128                 descent_duration = new MicroStat(layout, y++, "Descent duration",
129                                                  String.format("%6.1f s", stats.descent_duration()));
130                 flight_time = new MicroStat(layout, y++, "Flight Time",
131                                             String.format("%6.0f s", stats.landed_time));
132         }
133
134         public MicroStatsTable() {
135                 this(new MicroStats());
136         }
137         
138 }