micropeak: Update Makefile to versioned Java libraries
[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 void setData(MicroData data) {
120                 setStats(new MicroStats(data));
121         }
122
123         public MicroStatsTable(MicroStats stats) {
124                 layout = new GridBagLayout();
125
126                 setLayout(layout);
127                 int y = 0;
128                 max_height = new MicroStat(layout, y++, "Maximum height",
129                                            String.format("%7.1f m", stats.apogee_height),
130                                            String.format("%7.1f ft", AltosConvert.meters_to_feet(stats.apogee_height)));
131                 max_speed = new MicroStat(layout, y++, "Maximum speed",
132                                           String.format("%7.1f m/s", stats.max_speed),
133                                           String.format("%7.1f mph", AltosConvert.meters_to_mph(stats.max_speed)),
134                                           String.format("Mach %4.1f", AltosConvert.meters_to_mach(stats.max_speed)));
135                 max_accel = new MicroStat(layout, y++, "Maximum boost acceleration",
136                                           String.format("%7.1f m/s²", stats.max_accel),
137                                           String.format("%7.1f ft/s²", AltosConvert.meters_to_feet(stats.max_accel)),
138                                           String.format("%7.3f G", AltosConvert.meters_to_g(stats.max_accel)));
139                 avg_accel = new MicroStat(layout, y++, "Average boost acceleration",
140                                           String.format("%7.1f m/s²", stats.boost_accel(),
141                                                         String.format("%7.1f ft/s²", AltosConvert.meters_to_feet(stats.boost_accel())),
142                                                         String.format("%7.3f G", AltosConvert.meters_to_g(stats.boost_accel()))));
143                 boost_duration = new MicroStat(layout, y++, "Boost duration",
144                                                String.format("%6.1f s", stats.boost_duration()));
145                 coast_duration = new MicroStat(layout, y++, "Coast duration",
146                                                String.format("%6.1f s", stats.coast_duration()));
147                 descent_speed = new MicroStat(layout, y++, "Descent rate",
148                                               String.format("%7.1f m/s", stats.descent_speed()),
149                                               String.format("%7.1f ft/s", AltosConvert.meters_to_feet(stats.descent_speed())));
150                 descent_duration = new MicroStat(layout, y++, "Descent duration",
151                                                  String.format("%6.1f s", stats.descent_duration()));
152                 flight_time = new MicroStat(layout, y++, "Flight Time",
153                                             String.format("%6.1f s", stats.landed_time));
154                 set_font();
155
156                 AltosUIPreferences.register_font_listener(this);
157         }
158
159         public void tell_closing() {
160                 AltosUIPreferences.unregister_font_listener(this);
161         }
162
163         public MicroStatsTable() {
164                 this(new MicroStats());
165         }
166         
167 }