telegps: Add scan UI
[fw/altos] / altosui / AltosCompanionInfo.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 org.altusmetrum.altoslib_4.*;
23
24 public class AltosCompanionInfo extends JTable {
25         private AltosFlightInfoTableModel model;
26
27         static final int info_columns = 2;
28         static final int info_rows = 17;
29
30         int desired_row_height() {
31                 FontMetrics     infoValueMetrics = getFontMetrics(Altos.table_value_font);
32                 return (infoValueMetrics.getHeight() + infoValueMetrics.getLeading()) * 18 / 10;
33         }
34
35         public void set_font() {
36                 setFont(Altos.table_value_font);
37                 setRowHeight(desired_row_height());
38                 doLayout();
39         }
40
41         public AltosCompanionInfo() {
42                 super(new AltosFlightInfoTableModel(info_rows, info_columns));
43                 model = (AltosFlightInfoTableModel) getModel();
44                 setAutoResizeMode(AUTO_RESIZE_ALL_COLUMNS);
45                 setShowGrid(true);
46                 set_font();
47         }
48
49         public Dimension getPreferredScrollableViewportSize() {
50                 return getPreferredSize();
51         }
52
53         void info_reset() {
54                 model.reset();
55         }
56
57         void info_add_row(int col, String name, String value) {
58                 model.addRow(col, name, value);
59         }
60
61         void info_add_row(int col, String name, String format, Object... parameters) {
62                 info_add_row (col, name, String.format(format, parameters));
63         }
64
65         void info_finish() {
66                 model.finish();
67         }
68
69         public void clear() {
70                 model.clear();
71         }
72
73         AltosCompanion  companion;
74
75         public String board_name() {
76                 if (companion == null)
77                         return "None";
78                 switch (companion.board_id) {
79                 case AltosCompanion.board_id_telescience:
80                         return "TeleScience";
81                 default:
82                         return String.format("%02x\n", companion.board_id);
83                 }
84         }
85
86         public void show(AltosState state, AltosListenerState listener_state) {
87                 if (state == null)
88                         return;
89                 if (state.companion != null)
90                         companion = state.companion;
91                 info_reset();
92                 info_add_row(0, "Companion board", "%s", board_name());
93                 if (companion != null) {
94                         info_add_row(0, "Last Data", "%5d", companion.tick);
95                         info_add_row(0, "Update period", "%5.2f s",
96                                      companion.update_period / 100.0);
97                         info_add_row(0, "Channels", "%3d", companion.channels);
98
99                         for (int i = 0; i < companion.channels; i++)
100                                 info_add_row(1, String.format("Channel %2d", i),
101                                              "%6d", companion.companion_data[i]);
102                 }
103                 info_finish();
104         }
105 }