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