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; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with this program; if not, write to the Free Software Foundation, Inc.,
16  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
17  */
18
19 package org.altusmetrum.micropeak;
20
21 import java.awt.*;
22 import javax.swing.*;
23 import org.altusmetrum.altoslib_11.*;
24 import org.altusmetrum.altosuilib_11.*;
25
26 public class MicroStatsTable extends JComponent implements AltosFontListener {
27         GridBagLayout   layout;
28
29         class MicroStat {
30                 JLabel          label;
31                 JTextField[]    texts;
32
33                 public void set_values(String ... values) {
34                         for (int j = 0; j < values.length; j++) {
35                                 texts[j].setText(values[j]);
36                         }
37                 }
38
39                 public void set_font() {
40                         for (int j = 0; j < texts.length; j++)
41                                 texts[j].setFont(AltosUILib.value_font);
42                         label.setFont(AltosUILib.label_font);
43                 }
44
45                 public MicroStat(GridBagLayout layout, int y, String label_text, String ... values) {
46                         GridBagConstraints      c = new GridBagConstraints();
47                         c.insets = new Insets(AltosUILib.tab_elt_pad, AltosUILib.tab_elt_pad, AltosUILib.tab_elt_pad, AltosUILib.tab_elt_pad);
48                         c.weighty = 1;
49
50                         label = new JLabel(label_text);
51                         label.setFont(AltosUILib.label_font);
52                         label.setHorizontalAlignment(SwingConstants.LEFT);
53                         c.gridx = 0; c.gridy = y;
54                         c.anchor = GridBagConstraints.WEST;
55                         c.fill = GridBagConstraints.VERTICAL;
56                         c.weightx = 0;
57                         layout.setConstraints(label, c);
58                         add(label);
59
60                         texts = new JTextField[values.length];
61                         for (int j = 0; j < values.length; j++) {
62                                 JTextField value = new JTextField(values[j]);
63                                 value.setEditable(false);
64                                 value.setFont(AltosUILib.value_font);
65                                 value.setHorizontalAlignment(SwingConstants.RIGHT);
66                                 texts[j] = value;
67                                 c.gridx = j+1; c.gridy = y;
68                                 c.anchor = GridBagConstraints.EAST;
69                                 c.fill = GridBagConstraints.BOTH;
70                                 c.weightx = 1;
71                                 layout.setConstraints(value, c);
72                                 add(value);
73                         }
74                 }
75         }
76
77         MicroStat       max_height, max_speed;
78         MicroStat       max_accel, avg_accel;
79         MicroStat       boost_duration;
80         MicroStat       coast_duration;
81         MicroStat       descent_speed;
82         MicroStat       descent_duration;
83         MicroStat       flight_time;
84         
85         public void setStats(MicroStats stats) {
86                 max_height.set_values(String.format("%7.1f m", stats.apogee_height),
87                                       String.format("%7.1f ft", AltosConvert.meters_to_feet(stats.apogee_height)));
88                 max_speed.set_values(String.format("%7.1f m/s", stats.max_speed),
89                                      String.format("%7.1f mph", AltosConvert.meters_to_mph(stats.max_speed)),
90                                      String.format("Mach %7.3f", AltosConvert.meters_to_mach(stats.max_speed)));
91                 max_accel.set_values(String.format("%7.1f m/s²", stats.max_accel),
92                                      String.format("%7.1f ft/s²", AltosConvert.meters_to_feet(stats.max_accel)),
93                                      String.format("%7.3f G", AltosConvert.meters_to_g(stats.max_accel)));
94                 avg_accel.set_values(String.format("%7.1f m/s²", stats.boost_accel(),
95                                                    String.format("%7.1f ft/s²", AltosConvert.meters_to_feet(stats.boost_accel())),
96                                                    String.format("%7.3f G", AltosConvert.meters_to_g(stats.boost_accel()))));
97                 boost_duration.set_values(String.format("%6.1f s", stats.boost_duration()));
98                 coast_duration.set_values(String.format("%6.1f s", stats.coast_duration()));
99                 descent_speed.set_values(String.format("%7.1f m/s", stats.descent_speed()),
100                                          String.format("%7.1f ft/s", AltosConvert.meters_to_feet(stats.descent_speed())));
101                 descent_duration.set_values(String.format("%6.1f s", stats.descent_duration()));
102                 flight_time.set_values(String.format("%6.1f s", stats.landed_time));
103         }
104
105         public void set_font() {
106                 max_height.set_font();
107                 max_speed.set_font();
108                 max_accel.set_font();
109                 avg_accel.set_font();
110                 boost_duration.set_font();
111                 coast_duration.set_font();
112                 descent_speed.set_font();
113                 descent_duration.set_font();
114                 flight_time.set_font();
115         }
116
117         public void font_size_changed(int font_size) {
118                 set_font();
119         }
120
121         public MicroStatsTable(MicroStats stats) {
122                 layout = new GridBagLayout();
123
124                 setLayout(layout);
125                 int y = 0;
126                 max_height = new MicroStat(layout, y++, "Maximum height",
127                                            String.format("%7.1f m", stats.apogee_height),
128                                            String.format("%7.1f ft", AltosConvert.meters_to_feet(stats.apogee_height)));
129                 max_speed = new MicroStat(layout, y++, "Maximum speed",
130                                           String.format("%7.1f m/s", stats.max_speed),
131                                           String.format("%7.1f mph", AltosConvert.meters_to_mph(stats.max_speed)),
132                                           String.format("Mach %4.1f", AltosConvert.meters_to_mach(stats.max_speed)));
133                 max_accel = new MicroStat(layout, y++, "Maximum boost acceleration",
134                                           String.format("%7.1f m/s²", stats.max_accel),
135                                           String.format("%7.1f ft/s²", AltosConvert.meters_to_feet(stats.max_accel)),
136                                           String.format("%7.3f G", AltosConvert.meters_to_g(stats.max_accel)));
137                 avg_accel = new MicroStat(layout, y++, "Average boost acceleration",
138                                           String.format("%7.1f m/s²", stats.boost_accel(),
139                                                         String.format("%7.1f ft/s²", AltosConvert.meters_to_feet(stats.boost_accel())),
140                                                         String.format("%7.3f G", AltosConvert.meters_to_g(stats.boost_accel()))));
141                 boost_duration = new MicroStat(layout, y++, "Boost duration",
142                                                String.format("%6.1f s", stats.boost_duration()));
143                 coast_duration = new MicroStat(layout, y++, "Coast duration",
144                                                String.format("%6.1f s", stats.coast_duration()));
145                 descent_speed = new MicroStat(layout, y++, "Descent rate",
146                                               String.format("%7.1f m/s", stats.descent_speed()),
147                                               String.format("%7.1f ft/s", AltosConvert.meters_to_feet(stats.descent_speed())));
148                 descent_duration = new MicroStat(layout, y++, "Descent duration",
149                                                  String.format("%6.1f s", stats.descent_duration()));
150                 flight_time = new MicroStat(layout, y++, "Flight Time",
151                                             String.format("%6.1f s", stats.landed_time));
152                 set_font();
153
154                 AltosUIPreferences.register_font_listener(this);
155         }
156
157         public void tell_closing() {
158                 AltosUIPreferences.unregister_font_listener(this);
159         }
160
161         public MicroStatsTable() {
162                 this(new MicroStats());
163         }
164         
165 }