altos: Remove pdclib bits from Makefile
[fw/altos] / altosuilib / AltosInfoTable.java
1 /*
2  * Copyright © 2010 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.altosuilib_13;
20
21 import java.awt.*;
22 import java.awt.event.*;
23 import javax.swing.*;
24 import javax.swing.table.*;
25 import org.altusmetrum.altoslib_13.*;
26
27 public class AltosInfoTable extends JTable implements AltosFlightDisplay, HierarchyListener {
28         private AltosFlightInfoTableModel model;
29
30         static final int info_columns = 3;
31         static final int info_rows = 17;
32
33         private AltosState              last_state;
34         private AltosListenerState      last_listener_state;
35
36         int desired_row_height() {
37                 FontMetrics     infoValueMetrics = getFontMetrics(AltosUILib.table_value_font);
38                 return (infoValueMetrics.getHeight() + infoValueMetrics.getLeading()) * 18 / 10;
39         }
40
41         int text_width(String t) {
42                 FontMetrics     infoValueMetrics = getFontMetrics(AltosUILib.table_value_font);
43
44                 return infoValueMetrics.stringWidth(t);
45         }
46
47         void set_layout() {
48                 setRowHeight(desired_row_height());
49                 for (int i = 0; i < info_columns * 2; i++)
50                 {
51                         TableColumn column = getColumnModel().getColumn(i);
52
53                         if ((i & 1) == 0)
54                                 column.setPreferredWidth(text_width(" Satellites Visible"));
55                         else
56                                 column.setPreferredWidth(text_width("W 179°59.99999' "));
57                 }
58         }
59
60         public AltosInfoTable() {
61                 super(new AltosFlightInfoTableModel(info_rows, info_columns));
62                 model = (AltosFlightInfoTableModel) getModel();
63                 setFont(AltosUILib.table_value_font);
64                 addHierarchyListener(this);
65                 setAutoResizeMode(AUTO_RESIZE_ALL_COLUMNS);
66                 setShowGrid(true);
67                 set_layout();
68                 doLayout();
69         }
70
71         public void font_size_changed(int font_size) {
72                 setFont(AltosUILib.table_value_font);
73                 set_layout();
74                 doLayout();
75         }
76
77         public void units_changed(boolean imperial_units) {
78         }
79
80         public void hierarchyChanged(HierarchyEvent e) {
81                 if (last_state != null && isShowing()) {
82                         AltosState              state = last_state;
83                         AltosListenerState      listener_state = last_listener_state;
84
85                         last_state = null;
86                         last_listener_state = null;
87                         show(state, listener_state);
88                 }
89         }
90
91         public Dimension getPreferredScrollableViewportSize() {
92                 return getPreferredSize();
93         }
94
95         public void reset() {
96                 model.reset();
97         }
98
99         void info_add_row(int col, String name, String value) {
100                 model.addRow(col, name, value);
101         }
102
103         void info_add_row(int col, String name, String format, Object... parameters) {
104                 info_add_row (col, name, String.format(format, parameters));
105         }
106
107         void info_add_deg(int col, String name, double v, int pos, int neg) {
108                 int     c = pos;
109                 if (v < 0) {
110                         c = neg;
111                         v = -v;
112                 }
113                 double  deg = Math.floor(v);
114                 double  min = (v - deg) * 60;
115
116                 info_add_row(col, name, String.format("%c %3.0f°%08.5f'", c, deg, min));
117         }
118
119         void info_finish() {
120                 model.finish();
121         }
122
123         public void clear() {
124                 model.clear();
125         }
126
127         public String getName() { return "Table"; }
128
129         public void show(AltosState state, AltosListenerState listener_state) {
130
131                 AltosCalData    cal_data = state.cal_data();
132
133                 if (!isShowing()) {
134                         last_state = state;
135                         last_listener_state = listener_state;
136                         return;
137                 }
138
139                 reset();
140                 if (state != null) {
141                         if (cal_data.device_type != AltosLib.MISSING)
142                                 info_add_row(0, "Device", "%s", AltosLib.product_name(cal_data.device_type));
143                         else if (cal_data.product != null)
144                                 info_add_row(0, "Device", "%s", cal_data.product);
145                         if (state.tick() != AltosLib.MISSING)
146                                 info_add_row(0, "Tick", "%6d", state.tick());
147                         if (state.altitude() != AltosLib.MISSING)
148                                 info_add_row(0, "Altitude", "%6.0f    m", state.altitude());
149                         if (cal_data.ground_altitude != AltosLib.MISSING)
150                                 info_add_row(0, "Pad altitude", "%6.0f    m", cal_data.ground_altitude);
151                         if (state.height() != AltosLib.MISSING)
152                                 info_add_row(0, "Height", "%6.0f    m", state.height());
153                         if (state.max_height() != AltosLib.MISSING)
154                                 info_add_row(0, "Max height", "%6.0f    m", state.max_height());
155                         if (state.acceleration() != AltosLib.MISSING)
156                                 info_add_row(0, "Acceleration", "%8.1f  m/s²", state.acceleration());
157                         if (state.max_acceleration() != AltosLib.MISSING)
158                                 info_add_row(0, "Max acceleration", "%8.1f  m/s²", state.max_acceleration());
159                         if (state.speed() != AltosLib.MISSING)
160                                 info_add_row(0, "Speed", "%8.1f  m/s", state.speed());
161                         if (state.max_speed() != AltosLib.MISSING)
162                                 info_add_row(0, "Max Speed", "%8.1f  m/s", state.max_speed());
163                         if (state.orient() != AltosLib.MISSING)
164                                 info_add_row(0, "Tilt", "%4.0f °", state.orient());
165                         if (state.max_orient() != AltosLib.MISSING)
166                                 info_add_row(0, "Max Tilt", "%4.0f °", state.max_orient());
167                         if (state.temperature != AltosLib.MISSING)
168                                 info_add_row(0, "Temperature", "%9.2f °C", state.temperature);
169                         if (state.battery_voltage != AltosLib.MISSING)
170                                 info_add_row(0, "Battery", "%9.2f V", state.battery_voltage);
171                         if (state.apogee_voltage != AltosLib.MISSING)
172                                 info_add_row(0, "Drogue", "%9.2f V", state.apogee_voltage);
173                         if (state.main_voltage != AltosLib.MISSING)
174                                 info_add_row(0, "Main", "%9.2f V", state.main_voltage);
175                 }
176                 if (listener_state != null) {
177                         info_add_row(0, "CRC Errors", "%6d", listener_state.crc_errors);
178
179                         if (listener_state.battery != AltosLib.MISSING)
180                                 info_add_row(0, "Receiver Battery", "%9.2f", listener_state.battery);
181                 }
182
183                 if (state != null) {
184                         if (state.gps == null || !state.gps.connected) {
185                                 info_add_row(1, "GPS", "not available");
186                         } else {
187                                 if (state.gps_ready)
188                                         info_add_row(1, "GPS state", "%s", "ready");
189                                 else
190                                         info_add_row(1, "GPS state", "wait (%d)",
191                                                      state.gps_waiting);
192                                 if (state.gps.locked)
193                                         info_add_row(1, "GPS", "   locked");
194                                 else if (state.gps.connected)
195                                         info_add_row(1, "GPS", " unlocked");
196                                 else
197                                         info_add_row(1, "GPS", "  missing");
198                                 info_add_row(1, "Satellites", "%6d", state.gps.nsat);
199                                 if (state.gps.lat != AltosLib.MISSING)
200                                         info_add_deg(1, "Latitude", state.gps.lat, 'N', 'S');
201                                 if (state.gps.lon != AltosLib.MISSING)
202                                         info_add_deg(1, "Longitude", state.gps.lon, 'E', 'W');
203                                 if (state.gps.alt != AltosLib.MISSING)
204                                         info_add_row(1, "GPS altitude", "%8.1f", state.gps.alt);
205                                 if (state.gps_height != AltosLib.MISSING)
206                                         info_add_row(1, "GPS height", "%8.1f", state.gps_height);
207
208                                 if (state.gps.ground_speed != AltosLib.MISSING && state.gps.course != AltosLib.MISSING)
209                                         info_add_row(1, "GPS ground speed", "%6.1f m/s %3d°",
210                                                      state.gps.ground_speed,
211                                                      state.gps.course);
212                                 if (state.gps.climb_rate != AltosLib.MISSING)
213                                         info_add_row(1, "GPS climb rate", "%6.1f m/s",
214                                                      state.gps.climb_rate);
215
216                                 if (state.gps.h_error != AltosLib.MISSING && state.gps.v_error != AltosLib.MISSING)
217                                         info_add_row(1, "GPS error", "%6.0f m(h)%6.0f m(v)",
218                                                      state.gps.h_error, state.gps.v_error);
219                                 if (state.gps.pdop != AltosLib.MISSING &&
220                                     state.gps.hdop != AltosLib.MISSING &&
221                                     state.gps.vdop != AltosLib.MISSING)
222                                         info_add_row(1, "GPS dop", "%3.1fp/%3.1fh/%3.1fv",
223                                                      state.gps.pdop,
224                                                      state.gps.hdop,
225                                                      state.gps.vdop);
226
227                                 if (state.npad > 0) {
228                                         if (state.from_pad != null) {
229                                                 info_add_row(1, "Ground pad dist", "%6d m",
230                                                              (int) (state.from_pad.distance + 0.5));
231                                                 info_add_row(1, "Direction from pad", "%6d°",
232                                                              (int) (state.from_pad.bearing + 0.5));
233                                                 info_add_row(1, "Elevation from pad", "%6d°",
234                                                              (int) (state.elevation + 0.5));
235                                                 info_add_row(1, "Range from pad", "%6d m",
236                                                              (int) (state.range + 0.5));
237                                         } else {
238                                                 info_add_row(1, "Distance from pad", "unknown");
239                                                 info_add_row(1, "Direction from pad", "unknown");
240                                                 info_add_row(1, "Elevation from pad", "unknown");
241                                                 info_add_row(1, "Range from pad", "unknown");
242                                         }
243                                         info_add_deg(1, "Pad latitude", state.pad_lat, 'N', 'S');
244                                         info_add_deg(1, "Pad longitude", state.pad_lon, 'E', 'W');
245                                         info_add_row(1, "Pad GPS alt", "%6.0f m", state.pad_alt);
246                                 }
247                                 if (state.gps.year != AltosLib.MISSING)
248                                         info_add_row(2, "GPS date", "%04d-%02d-%02d",
249                                                      state.gps.year,
250                                                      state.gps.month,
251                                                      state.gps.day);
252                                 if (state.gps.hour != AltosLib.MISSING)
253                                         info_add_row(2, "GPS time", "  %02d:%02d:%02d",
254                                                      state.gps.hour,
255                                                      state.gps.minute,
256                                                      state.gps.second);
257                                 //int   nsat_vis = 0;
258                                 int     c;
259
260                                 if (state.gps.cc_gps_sat == null)
261                                         info_add_row(2, "Satellites Visible", "%4d", 0);
262                                 else {
263                                         info_add_row(2, "Satellites Visible", "%4d", state.gps.cc_gps_sat.length);
264                                         for (c = 0; c < state.gps.cc_gps_sat.length; c++) {
265                                                 info_add_row(2, "Satellite id,C/N0",
266                                                              "%4d, %4d",
267                                                              state.gps.cc_gps_sat[c].svid,
268                                                              state.gps.cc_gps_sat[c].c_n0);
269                                         }
270                                 }
271                         }
272                 }
273                 info_finish();
274         }
275 }