altosuilib/micropeak: Add state markers to micropeak graph
[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_1.*;
23 import org.altusmetrum.altosuilib_1.*;
24
25 public class MicroStatsTable extends JComponent implements AltosFontListener {
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 void set_font() {
39                         for (int j = 0; j < texts.length; j++)
40                                 texts[j].setFont(AltosUILib.value_font);
41                         label.setFont(AltosUILib.label_font);
42                 }
43
44                 public MicroStat(GridBagLayout layout, int y, String label_text, String ... values) {
45                         GridBagConstraints      c = new GridBagConstraints();
46                         c.insets = new Insets(AltosUILib.tab_elt_pad, AltosUILib.tab_elt_pad, AltosUILib.tab_elt_pad, AltosUILib.tab_elt_pad);
47                         c.weighty = 1;
48
49                         label = new JLabel(label_text);
50                         label.setFont(AltosUILib.label_font);
51                         label.setHorizontalAlignment(SwingConstants.LEFT);
52                         c.gridx = 0; c.gridy = y;
53                         c.anchor = GridBagConstraints.WEST;
54                         c.fill = GridBagConstraints.VERTICAL;
55                         c.weightx = 0;
56                         layout.setConstraints(label, c);
57                         add(label);
58
59                         texts = new JTextField[values.length];
60                         for (int j = 0; j < values.length; j++) {
61                                 JTextField value = new JTextField(values[j]);
62                                 value.setFont(AltosUILib.value_font);
63                                 value.setHorizontalAlignment(SwingConstants.RIGHT);
64                                 texts[j] = value;
65                                 c.gridx = j+1; c.gridy = y;
66                                 c.anchor = GridBagConstraints.EAST;
67                                 c.fill = GridBagConstraints.BOTH;
68                                 c.weightx = 1;
69                                 layout.setConstraints(value, c);
70                                 add(value);
71                         }
72                 }
73         }
74
75         MicroStat       max_height, max_speed;
76         MicroStat       max_accel, avg_accel;
77         MicroStat       boost_duration;
78         MicroStat       coast_duration;
79         MicroStat       descent_speed;
80         MicroStat       descent_duration;
81         MicroStat       flight_time;
82         
83         public void setStats(MicroStats stats) {
84                 max_height.set_values(String.format("%7.1f m", stats.apogee_height),
85                                       String.format("%7.1f ft", AltosConvert.meters_to_feet(stats.apogee_height)));
86                 max_speed.set_values(String.format("%7.1f m/s", stats.max_speed),
87                                      String.format("%7.1f mph", AltosConvert.meters_to_mph(stats.max_speed)),
88                                      String.format("Mach %7.3f", AltosConvert.meters_to_mach(stats.max_speed)));
89                 max_accel.set_values(String.format("%7.1f m/s²", stats.max_accel),
90                                      String.format("%7.1f ft/s²", AltosConvert.meters_to_feet(stats.max_accel)),
91                                      String.format("%7.3f G", AltosConvert.meters_to_g(stats.max_accel)));
92                 avg_accel.set_values(String.format("%7.1f m/s²", stats.boost_accel(),
93                                                    String.format("%7.1f ft/s²", AltosConvert.meters_to_feet(stats.boost_accel())),
94                                                    String.format("%7.3f G", AltosConvert.meters_to_g(stats.boost_accel()))));
95                 boost_duration.set_values(String.format("%6.1f s", stats.boost_duration()));
96                 coast_duration.set_values(String.format("%6.1f s", stats.coast_duration()));
97                 descent_speed.set_values(String.format("%7.1f m/s", stats.descent_speed()),
98                                          String.format("%7.1f ft/s", AltosConvert.meters_to_feet(stats.descent_speed())));
99                 descent_duration.set_values(String.format("%6.1f s", stats.descent_duration()));
100                 flight_time.set_values(String.format("%6.1f s", stats.landed_time));
101         }
102
103         public void set_font() {
104                 max_height.set_font();
105                 max_speed.set_font();
106                 max_accel.set_font();
107                 avg_accel.set_font();
108                 boost_duration.set_font();
109                 coast_duration.set_font();
110                 descent_speed.set_font();
111                 descent_duration.set_font();
112                 flight_time.set_font();
113         }
114
115         public void font_size_changed(int font_size) {
116                 set_font();
117         }
118
119         public MicroStatsTable(MicroStats stats) {
120                 layout = new GridBagLayout();
121
122                 setLayout(layout);
123                 int y = 0;
124                 max_height = new MicroStat(layout, y++, "Maximum height",
125                                            String.format("%7.1f m", stats.apogee_height),
126                                            String.format("%7.1f ft", AltosConvert.meters_to_feet(stats.apogee_height)));
127                 max_speed = new MicroStat(layout, y++, "Maximum speed",
128                                           String.format("%7.1f m/s", stats.max_speed),
129                                           String.format("%7.1f mph", AltosConvert.meters_to_mph(stats.max_speed)),
130                                           String.format("Mach %4.1f", AltosConvert.meters_to_mach(stats.max_speed)));
131                 max_accel = new MicroStat(layout, y++, "Maximum boost acceleration",
132                                           String.format("%7.1f m/s²", stats.max_accel),
133                                           String.format("%7.1f ft/s²", AltosConvert.meters_to_feet(stats.max_accel)),
134                                           String.format("%7.3f G", AltosConvert.meters_to_g(stats.max_accel)));
135                 avg_accel = new MicroStat(layout, y++, "Average boost acceleration",
136                                           String.format("%7.1f m/s²", stats.boost_accel(),
137                                                         String.format("%7.1f ft/s²", AltosConvert.meters_to_feet(stats.boost_accel())),
138                                                         String.format("%7.3f G", AltosConvert.meters_to_g(stats.boost_accel()))));
139                 boost_duration = new MicroStat(layout, y++, "Boost duration",
140                                                String.format("%6.1f s", stats.boost_duration()));
141                 coast_duration = new MicroStat(layout, y++, "Coast duration",
142                                                String.format("%6.1f s", stats.coast_duration()));
143                 descent_speed = new MicroStat(layout, y++, "Descent rate",
144                                               String.format("%7.1f m/s", stats.descent_speed()),
145                                               String.format("%7.1f ft/s", AltosConvert.meters_to_feet(stats.descent_speed())));
146                 descent_duration = new MicroStat(layout, y++, "Descent duration",
147                                                  String.format("%6.1f s", stats.descent_duration()));
148                 flight_time = new MicroStat(layout, y++, "Flight Time",
149                                             String.format("%6.1f s", stats.landed_time));
150                 set_font();
151
152                 AltosUIPreferences.register_font_listener(this);
153         }
154
155         public void tell_closing() {
156                 AltosUIPreferences.unregister_font_listener(this);
157         }
158
159         public MicroStatsTable() {
160                 this(new MicroStats());
161         }
162         
163 }