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