Merge branch 'master' of ssh://git.gag.com/scm/git/fw/altos
[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_12;
20
21 import java.awt.*;
22 import java.awt.event.*;
23 import javax.swing.*;
24 import javax.swing.table.*;
25 import org.altusmetrum.altoslib_12.*;
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.altitude() != AltosLib.MISSING)
146                                 info_add_row(0, "Altitude", "%6.0f    m", state.altitude());
147                         if (cal_data.ground_altitude != AltosLib.MISSING)
148                                 info_add_row(0, "Pad altitude", "%6.0f    m", cal_data.ground_altitude);
149                         if (state.height() != AltosLib.MISSING)
150                                 info_add_row(0, "Height", "%6.0f    m", state.height());
151                         if (state.max_height() != AltosLib.MISSING)
152                                 info_add_row(0, "Max height", "%6.0f    m", state.max_height());
153                         if (state.acceleration() != AltosLib.MISSING)
154                                 info_add_row(0, "Acceleration", "%8.1f  m/s²", state.acceleration());
155                         if (state.max_acceleration() != AltosLib.MISSING)
156                                 info_add_row(0, "Max acceleration", "%8.1f  m/s²", state.max_acceleration());
157                         if (state.speed() != AltosLib.MISSING)
158                                 info_add_row(0, "Speed", "%8.1f  m/s", state.speed());
159                         if (state.max_speed() != AltosLib.MISSING)
160                                 info_add_row(0, "Max Speed", "%8.1f  m/s", state.max_speed());
161                         if (state.orient() != AltosLib.MISSING)
162                                 info_add_row(0, "Tilt", "%4.0f °", state.orient());
163                         if (state.max_orient() != AltosLib.MISSING)
164                                 info_add_row(0, "Max Tilt", "%4.0f °", state.max_orient());
165                         if (state.temperature != AltosLib.MISSING)
166                                 info_add_row(0, "Temperature", "%9.2f °C", state.temperature);
167                         if (state.battery_voltage != AltosLib.MISSING)
168                                 info_add_row(0, "Battery", "%9.2f V", state.battery_voltage);
169                         if (state.apogee_voltage != AltosLib.MISSING)
170                                 info_add_row(0, "Drogue", "%9.2f V", state.apogee_voltage);
171                         if (state.main_voltage != AltosLib.MISSING)
172                                 info_add_row(0, "Main", "%9.2f V", state.main_voltage);
173                 }
174                 if (listener_state != null) {
175                         info_add_row(0, "CRC Errors", "%6d", listener_state.crc_errors);
176
177                         if (listener_state.battery != AltosLib.MISSING)
178                                 info_add_row(0, "Receiver Battery", "%9.2f", listener_state.battery);
179                 }
180
181                 if (state != null) {
182                         if (state.gps == null || !state.gps.connected) {
183                                 info_add_row(1, "GPS", "not available");
184                         } else {
185                                 if (state.gps_ready)
186                                         info_add_row(1, "GPS state", "%s", "ready");
187                                 else
188                                         info_add_row(1, "GPS state", "wait (%d)",
189                                                      state.gps_waiting);
190                                 if (state.gps.locked)
191                                         info_add_row(1, "GPS", "   locked");
192                                 else if (state.gps.connected)
193                                         info_add_row(1, "GPS", " unlocked");
194                                 else
195                                         info_add_row(1, "GPS", "  missing");
196                                 info_add_row(1, "Satellites", "%6d", state.gps.nsat);
197                                 if (state.gps.lat != AltosLib.MISSING)
198                                         info_add_deg(1, "Latitude", state.gps.lat, 'N', 'S');
199                                 if (state.gps.lon != AltosLib.MISSING)
200                                         info_add_deg(1, "Longitude", state.gps.lon, 'E', 'W');
201                                 if (state.gps.alt != AltosLib.MISSING)
202                                         info_add_row(1, "GPS altitude", "%8.1f", state.gps.alt);
203                                 if (state.gps_height != AltosLib.MISSING)
204                                         info_add_row(1, "GPS height", "%8.1f", state.gps_height);
205
206                                 if (state.gps.ground_speed != AltosLib.MISSING && state.gps.course != AltosLib.MISSING)
207                                         info_add_row(1, "GPS ground speed", "%6.1f m/s %3d°",
208                                                      state.gps.ground_speed,
209                                                      state.gps.course);
210                                 if (state.gps.climb_rate != AltosLib.MISSING)
211                                         info_add_row(1, "GPS climb rate", "%6.1f m/s",
212                                                      state.gps.climb_rate);
213
214                                 if (state.gps.h_error != AltosLib.MISSING && state.gps.v_error != AltosLib.MISSING)
215                                         info_add_row(1, "GPS error", "%6.0f m(h)%6.0f m(v)",
216                                                      state.gps.h_error, state.gps.v_error);
217                                 if (state.gps.pdop != AltosLib.MISSING &&
218                                     state.gps.hdop != AltosLib.MISSING &&
219                                     state.gps.vdop != AltosLib.MISSING)
220                                         info_add_row(1, "GPS dop", "%3.1fp/%3.1fh/%3.1fv",
221                                                      state.gps.pdop,
222                                                      state.gps.hdop,
223                                                      state.gps.vdop);
224
225                                 if (state.npad > 0) {
226                                         if (state.from_pad != null) {
227                                                 info_add_row(1, "Ground pad dist", "%6d m",
228                                                              (int) (state.from_pad.distance + 0.5));
229                                                 info_add_row(1, "Direction from pad", "%6d°",
230                                                              (int) (state.from_pad.bearing + 0.5));
231                                                 info_add_row(1, "Elevation from pad", "%6d°",
232                                                              (int) (state.elevation + 0.5));
233                                                 info_add_row(1, "Range from pad", "%6d m",
234                                                              (int) (state.range + 0.5));
235                                         } else {
236                                                 info_add_row(1, "Distance from pad", "unknown");
237                                                 info_add_row(1, "Direction from pad", "unknown");
238                                                 info_add_row(1, "Elevation from pad", "unknown");
239                                                 info_add_row(1, "Range from pad", "unknown");
240                                         }
241                                         info_add_deg(1, "Pad latitude", state.pad_lat, 'N', 'S');
242                                         info_add_deg(1, "Pad longitude", state.pad_lon, 'E', 'W');
243                                         info_add_row(1, "Pad GPS alt", "%6.0f m", state.pad_alt);
244                                 }
245                                 if (state.gps.year != AltosLib.MISSING)
246                                         info_add_row(2, "GPS date", "%04d-%02d-%02d",
247                                                      state.gps.year,
248                                                      state.gps.month,
249                                                      state.gps.day);
250                                 if (state.gps.hour != AltosLib.MISSING)
251                                         info_add_row(2, "GPS time", "  %02d:%02d:%02d",
252                                                      state.gps.hour,
253                                                      state.gps.minute,
254                                                      state.gps.second);
255                                 //int   nsat_vis = 0;
256                                 int     c;
257
258                                 if (state.gps.cc_gps_sat == null)
259                                         info_add_row(2, "Satellites Visible", "%4d", 0);
260                                 else {
261                                         info_add_row(2, "Satellites Visible", "%4d", state.gps.cc_gps_sat.length);
262                                         for (c = 0; c < state.gps.cc_gps_sat.length; c++) {
263                                                 info_add_row(2, "Satellite id,C/N0",
264                                                              "%4d, %4d",
265                                                              state.gps.cc_gps_sat[c].svid,
266                                                              state.gps.cc_gps_sat[c].c_n0);
267                                         }
268                                 }
269                         }
270                 }
271                 info_finish();
272         }
273 }