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