altosui: Make Windows java test "smarter"
[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_5.*;
23 import org.altusmetrum.altosuilib_3.*;
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.setEditable(false);
63                                 value.setFont(AltosUILib.value_font);
64                                 value.setHorizontalAlignment(SwingConstants.RIGHT);
65                                 texts[j] = value;
66                                 c.gridx = j+1; c.gridy = y;
67                                 c.anchor = GridBagConstraints.EAST;
68                                 c.fill = GridBagConstraints.BOTH;
69                                 c.weightx = 1;
70                                 layout.setConstraints(value, c);
71                                 add(value);
72                         }
73                 }
74         }
75
76         MicroStat       max_height, max_speed;
77         MicroStat       max_accel, avg_accel;
78         MicroStat       boost_duration;
79         MicroStat       coast_duration;
80         MicroStat       descent_speed;
81         MicroStat       descent_duration;
82         MicroStat       flight_time;
83         
84         public void setStats(MicroStats stats) {
85                 max_height.set_values(String.format("%7.1f m", stats.apogee_height),
86                                       String.format("%7.1f ft", AltosConvert.meters_to_feet(stats.apogee_height)));
87                 max_speed.set_values(String.format("%7.1f m/s", stats.max_speed),
88                                      String.format("%7.1f mph", AltosConvert.meters_to_mph(stats.max_speed)),
89                                      String.format("Mach %7.3f", AltosConvert.meters_to_mach(stats.max_speed)));
90                 max_accel.set_values(String.format("%7.1f m/s²", stats.max_accel),
91                                      String.format("%7.1f ft/s²", AltosConvert.meters_to_feet(stats.max_accel)),
92                                      String.format("%7.3f G", AltosConvert.meters_to_g(stats.max_accel)));
93                 avg_accel.set_values(String.format("%7.1f m/s²", stats.boost_accel(),
94                                                    String.format("%7.1f ft/s²", AltosConvert.meters_to_feet(stats.boost_accel())),
95                                                    String.format("%7.3f G", AltosConvert.meters_to_g(stats.boost_accel()))));
96                 boost_duration.set_values(String.format("%6.1f s", stats.boost_duration()));
97                 coast_duration.set_values(String.format("%6.1f s", stats.coast_duration()));
98                 descent_speed.set_values(String.format("%7.1f m/s", stats.descent_speed()),
99                                          String.format("%7.1f ft/s", AltosConvert.meters_to_feet(stats.descent_speed())));
100                 descent_duration.set_values(String.format("%6.1f s", stats.descent_duration()));
101                 flight_time.set_values(String.format("%6.1f s", stats.landed_time));
102         }
103
104         public void set_font() {
105                 max_height.set_font();
106                 max_speed.set_font();
107                 max_accel.set_font();
108                 avg_accel.set_font();
109                 boost_duration.set_font();
110                 coast_duration.set_font();
111                 descent_speed.set_font();
112                 descent_duration.set_font();
113                 flight_time.set_font();
114         }
115
116         public void font_size_changed(int font_size) {
117                 set_font();
118         }
119
120         public MicroStatsTable(MicroStats stats) {
121                 layout = new GridBagLayout();
122
123                 setLayout(layout);
124                 int y = 0;
125                 max_height = new MicroStat(layout, y++, "Maximum height",
126                                            String.format("%7.1f m", stats.apogee_height),
127                                            String.format("%7.1f ft", AltosConvert.meters_to_feet(stats.apogee_height)));
128                 max_speed = new MicroStat(layout, y++, "Maximum speed",
129                                           String.format("%7.1f m/s", stats.max_speed),
130                                           String.format("%7.1f mph", AltosConvert.meters_to_mph(stats.max_speed)),
131                                           String.format("Mach %4.1f", AltosConvert.meters_to_mach(stats.max_speed)));
132                 max_accel = new MicroStat(layout, y++, "Maximum boost acceleration",
133                                           String.format("%7.1f m/s²", stats.max_accel),
134                                           String.format("%7.1f ft/s²", AltosConvert.meters_to_feet(stats.max_accel)),
135                                           String.format("%7.3f G", AltosConvert.meters_to_g(stats.max_accel)));
136                 avg_accel = new MicroStat(layout, y++, "Average boost acceleration",
137                                           String.format("%7.1f m/s²", stats.boost_accel(),
138                                                         String.format("%7.1f ft/s²", AltosConvert.meters_to_feet(stats.boost_accel())),
139                                                         String.format("%7.3f G", AltosConvert.meters_to_g(stats.boost_accel()))));
140                 boost_duration = new MicroStat(layout, y++, "Boost duration",
141                                                String.format("%6.1f s", stats.boost_duration()));
142                 coast_duration = new MicroStat(layout, y++, "Coast duration",
143                                                String.format("%6.1f s", stats.coast_duration()));
144                 descent_speed = new MicroStat(layout, y++, "Descent rate",
145                                               String.format("%7.1f m/s", stats.descent_speed()),
146                                               String.format("%7.1f ft/s", AltosConvert.meters_to_feet(stats.descent_speed())));
147                 descent_duration = new MicroStat(layout, y++, "Descent duration",
148                                                  String.format("%6.1f s", stats.descent_duration()));
149                 flight_time = new MicroStat(layout, y++, "Flight Time",
150                                             String.format("%6.1f s", stats.landed_time));
151                 set_font();
152
153                 AltosUIPreferences.register_font_listener(this);
154         }
155
156         public void tell_closing() {
157                 AltosUIPreferences.unregister_font_listener(this);
158         }
159
160         public MicroStatsTable() {
161                 this(new MicroStats());
162         }
163         
164 }