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