altosui: Show time since last packet in flight status window
[fw/altos] / altosui / AltosFlightStatus.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
31 public class AltosFlightStatus extends JComponent implements AltosFlightDisplay {
32         GridBagLayout   layout;
33
34         public class FlightValue {
35                 JLabel          label;
36                 JTextField      value;
37
38                 void show(AltosState state, int crc_errors) {}
39
40                 void reset() {
41                         value.setText("");
42                 }
43
44                 void set_font() {
45                         label.setFont(Altos.status_font);
46                         value.setFont(Altos.status_font);
47                 }
48
49                 public FlightValue (GridBagLayout layout, int x, String text) {
50                         GridBagConstraints      c = new GridBagConstraints();
51                         c.insets = new Insets(5, 5, 5, 5);
52                         c.anchor = GridBagConstraints.CENTER;
53                         c.fill = GridBagConstraints.BOTH;
54                         c.weightx = 1;
55                         c.weighty = 1;
56
57                         label = new JLabel(text);
58                         label.setFont(Altos.status_font);
59                         label.setHorizontalAlignment(SwingConstants.CENTER);
60                         c.gridx = x; c.gridy = 0;
61                         layout.setConstraints(label, c);
62                         add(label);
63
64                         value = new JTextField("");
65                         value.setFont(Altos.status_font);
66                         value.setHorizontalAlignment(SwingConstants.CENTER);
67                         c.gridx = x; c.gridy = 1;
68                         layout.setConstraints(value, c);
69                         add(value);
70                 }
71         }
72
73         class Call extends FlightValue {
74                 void show(AltosState state, int crc_errors) {
75                         value.setText(state.data.callsign);
76                 }
77                 public Call (GridBagLayout layout, int x) {
78                         super (layout, x, "Callsign");
79                 }
80         }
81
82         Call call;
83
84         class Serial extends FlightValue {
85                 void show(AltosState state, int crc_errors) {
86                         value.setText(String.format("%d", state.data.serial));
87                 }
88                 public Serial (GridBagLayout layout, int x) {
89                         super (layout, x, "Serial");
90                 }
91         }
92
93         Serial serial;
94
95         class Flight extends FlightValue {
96                 void show(AltosState state, int crc_errors) {
97                         value.setText(String.format("%d", state.data.flight));
98                 }
99                 public Flight (GridBagLayout layout, int x) {
100                         super (layout, x, "Flight");
101                 }
102         }
103
104         Flight flight;
105
106         class FlightState extends FlightValue {
107                 void show(AltosState state, int crc_errors) {
108                         value.setText(state.data.state());
109                 }
110                 public FlightState (GridBagLayout layout, int x) {
111                         super (layout, x, "State");
112                 }
113         }
114
115         FlightState flight_state;
116
117         class RSSI extends FlightValue {
118                 void show(AltosState state, int crc_errors) {
119                         value.setText(String.format("%d", state.data.rssi));
120                 }
121                 public RSSI (GridBagLayout layout, int x) {
122                         super (layout, x, "RSSI");
123                 }
124         }
125
126         RSSI rssi;
127
128         class LastPacket extends FlightValue {
129                 void show(AltosState state, int crc_errors) {
130                         long secs = (System.currentTimeMillis() - state.report_time + 500) / 1000;
131                         value.setText(String.format("%d", secs));
132                 }
133                 public LastPacket(GridBagLayout layout, int x) {
134                         super (layout, x, "Age");
135                 }
136         }
137
138         LastPacket last_packet;
139
140         public void reset () {
141                 call.reset();
142                 serial.reset();
143                 flight.reset();
144                 flight_state.reset();
145                 rssi.reset();
146                 last_packet.reset();
147         }
148
149         public void set_font () {
150                 call.set_font();
151                 serial.set_font();
152                 flight.set_font();
153                 flight_state.set_font();
154                 rssi.set_font();
155                 last_packet.set_font();
156         }
157
158         public void show (AltosState state, int crc_errors) {
159                 call.show(state, crc_errors);
160                 serial.show(state, crc_errors);
161                 flight.show(state, crc_errors);
162                 flight_state.show(state, crc_errors);
163                 rssi.show(state, crc_errors);
164                 last_packet.show(state, crc_errors);
165         }
166
167         public int height() {
168                 Dimension d = layout.preferredLayoutSize(this);
169                 return d.height;
170         }
171
172         public AltosFlightStatus() {
173                 layout = new GridBagLayout();
174
175                 setLayout(layout);
176
177                 call = new Call(layout, 0);
178                 serial = new Serial(layout, 1);
179                 flight = new Flight(layout, 2);
180                 flight_state = new FlightState(layout, 3);
181                 rssi = new RSSI(layout, 4);
182                 last_packet = new LastPacket(layout, 5);
183         }
184 }