2 * Copyright © 2010 Keith Packard <keithp@keithp.com>
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.
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.
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.
19 package org.altusmetrum.altosuilib_13;
22 import java.awt.event.*;
24 import javax.swing.table.*;
25 import org.altusmetrum.altoslib_13.*;
27 public class AltosInfoTable extends JTable implements AltosFlightDisplay, HierarchyListener {
28 private AltosFlightInfoTableModel model;
30 static final int info_columns = 3;
31 static final int info_rows = 17;
33 private AltosState last_state;
34 private AltosListenerState last_listener_state;
36 int desired_row_height() {
37 FontMetrics infoValueMetrics = getFontMetrics(AltosUILib.table_value_font);
38 return (infoValueMetrics.getHeight() + infoValueMetrics.getLeading()) * 18 / 10;
41 int text_width(String t) {
42 FontMetrics infoValueMetrics = getFontMetrics(AltosUILib.table_value_font);
44 return infoValueMetrics.stringWidth(t);
48 setRowHeight(desired_row_height());
49 for (int i = 0; i < info_columns * 2; i++)
51 TableColumn column = getColumnModel().getColumn(i);
54 column.setPreferredWidth(text_width(" Satellites Visible"));
56 column.setPreferredWidth(text_width("W 179°59.99999' "));
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);
71 public void font_size_changed(int font_size) {
72 setFont(AltosUILib.table_value_font);
77 public void units_changed(boolean imperial_units) {
80 public void hierarchyChanged(HierarchyEvent e) {
81 if (last_state != null && isShowing()) {
82 AltosState state = last_state;
83 AltosListenerState listener_state = last_listener_state;
86 last_listener_state = null;
87 show(state, listener_state);
91 public Dimension getPreferredScrollableViewportSize() {
92 return getPreferredSize();
99 void info_add_row(int col, String name, String value) {
100 model.addRow(col, name, value);
103 void info_add_row(int col, String name, String format, Object... parameters) {
104 info_add_row (col, name, String.format(format, parameters));
107 void info_add_deg(int col, String name, double v, int pos, int neg) {
113 double deg = Math.floor(v);
114 double min = (v - deg) * 60;
116 info_add_row(col, name, String.format("%c %3.0f°%08.5f'", c, deg, min));
123 public void clear() {
127 public String getName() { return "Table"; }
129 public void show(AltosState state, AltosListenerState listener_state) {
131 AltosCalData cal_data = state.cal_data();
135 last_listener_state = listener_state;
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);
176 if (listener_state != null) {
177 info_add_row(0, "CRC Errors", "%6d", listener_state.crc_errors);
179 if (listener_state.battery != AltosLib.MISSING)
180 info_add_row(0, "Receiver Battery", "%9.2f", listener_state.battery);
184 if (state.gps == null || !state.gps.connected) {
185 info_add_row(1, "GPS", "not available");
188 info_add_row(1, "GPS state", "%s", "ready");
190 info_add_row(1, "GPS state", "wait (%d)",
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");
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);
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,
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);
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",
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));
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");
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);
247 if (state.gps.year != AltosLib.MISSING)
248 info_add_row(2, "GPS date", "%04d-%02d-%02d",
252 if (state.gps.hour != AltosLib.MISSING)
253 info_add_row(2, "GPS time", " %02d:%02d:%02d",
260 if (state.gps.cc_gps_sat == null)
261 info_add_row(2, "Satellites Visible", "%4d", 0);
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",
267 state.gps.cc_gps_sat[c].svid,
268 state.gps.cc_gps_sat[c].c_n0);