altos: Call ao_telemetry_reset_interval when telemetry rate changes
[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 javax.swing.*;
22 import org.altusmetrum.altoslib_5.*;
23 import org.altusmetrum.altosuilib_3.*;
24
25 public class AltosFlightStatus extends JComponent implements AltosFlightDisplay {
26         GridBagLayout   layout;
27
28         public abstract class FlightValue {
29                 JLabel          label;
30                 JTextField      value;
31
32                 void show() {
33                         label.setVisible(true);
34                         value.setVisible(true);
35                 }
36
37                 void hide() {
38                         label.setVisible(false);
39                         value.setVisible(false);
40                 }
41
42                 abstract void show(AltosState state, AltosListenerState listener_state);
43
44                 void reset() {
45                         value.setText("");
46                 }
47
48                 void set_font() {
49                         label.setFont(Altos.status_font);
50                         value.setFont(Altos.status_font);
51                 }
52
53                 void setVisible(boolean visible) {
54                         label.setVisible(visible);
55                         value.setVisible(visible);
56                 }
57
58                 public FlightValue (GridBagLayout layout, int x, String text) {
59                         GridBagConstraints      c = new GridBagConstraints();
60                         c.insets = new Insets(5, 5, 5, 5);
61                         c.anchor = GridBagConstraints.CENTER;
62                         c.fill = GridBagConstraints.BOTH;
63                         c.weightx = 1;
64                         c.weighty = 1;
65
66                         label = new JLabel(text);
67                         label.setFont(Altos.status_font);
68                         label.setHorizontalAlignment(SwingConstants.CENTER);
69                         c.gridx = x; c.gridy = 0;
70                         layout.setConstraints(label, c);
71                         add(label);
72
73                         value = new JTextField("");
74                         value.setEditable(false);
75                         value.setFont(Altos.status_font);
76                         value.setHorizontalAlignment(SwingConstants.CENTER);
77                         c.gridx = x; c.gridy = 1;
78                         layout.setConstraints(value, c);
79                         add(value);
80                 }
81         }
82
83         class Call extends FlightValue {
84
85                 String last_call = "";
86
87                 boolean same_call(String call) {
88                         if (last_call == null)
89                                 return call == null;
90                         else
91                                 return last_call.equals(call);
92                 }
93
94                 void show(AltosState state, AltosListenerState listener_state) {
95                         if (!same_call(state.callsign)) {
96                                 show();
97                                 value.setText(state.callsign);
98                                 if (state.callsign == null)
99                                         setVisible(false);
100                                 else
101                                         setVisible(true);
102                                 last_call = state.callsign;
103                         }
104                 }
105
106                 public void reset() {
107                         super.reset();
108                         last_call = "";
109                 }
110
111                 public Call (GridBagLayout layout, int x) {
112                         super (layout, x, "Callsign");
113                 }
114         }
115
116         Call call;
117
118         class Serial extends FlightValue {
119
120                 int     last_serial = -1;
121                 void show(AltosState state, AltosListenerState listener_state) {
122                         if (state.serial != last_serial) {
123                                 show();
124                                 if (state.serial == AltosLib.MISSING)
125                                         value.setText("none");
126                                 else
127                                         value.setText(String.format("%d", state.serial));
128                                 last_serial = state.serial;
129                         }
130                 }
131
132                 public void reset() {
133                         super.reset();
134                         last_serial = -1;
135                 }
136
137                 public Serial (GridBagLayout layout, int x) {
138                         super (layout, x, "Serial");
139                 }
140         }
141
142         Serial serial;
143
144         class Flight extends FlightValue {
145
146                 int     last_flight = -1;
147
148                 void show(AltosState state, AltosListenerState listener_state) {
149                         if (state.flight != last_flight) {
150                                 show();
151                                 if (state.flight == AltosLib.MISSING)
152                                         value.setText("none");
153                                 else
154                                         value.setText(String.format("%d", state.flight));
155                                 last_flight = state.flight;
156                         }
157                 }
158
159                 public void reset() {
160                         super.reset();
161                         last_flight = -1;
162                 }
163
164                 public Flight (GridBagLayout layout, int x) {
165                         super (layout, x, "Flight");
166                 }
167         }
168
169         Flight flight;
170
171         class FlightState extends FlightValue {
172
173                 int     last_state = -1;
174
175                 void show(AltosState state, AltosListenerState listener_state) {
176                         if (state.state != last_state) {
177                                 if (state.state == AltosLib.ao_flight_stateless)
178                                         hide();
179                                 else {
180                                         show();
181                                         value.setText(state.state_name());
182                                 }
183                                 last_state = state.state;
184                         }
185                 }
186
187                 public void reset() {
188                         super.reset();
189                         last_state = -1;
190                 }
191
192                 public FlightState (GridBagLayout layout, int x) {
193                         super (layout, x, "State");
194                 }
195         }
196
197         FlightState flight_state;
198
199         class RSSI extends FlightValue {
200
201                 int     last_rssi = 10000;
202
203                 void show(AltosState state, AltosListenerState listener_state) {
204                         if (state.rssi() != last_rssi) {
205                                 show();
206                                 value.setText(String.format("%d", state.rssi()));
207                                 if (state.rssi == AltosLib.MISSING)
208                                         setVisible(false);
209                                 else
210                                         setVisible(true);
211                                 last_rssi = state.rssi();
212                         }
213                 }
214
215                 public void reset() {
216                         super.reset();
217                         last_rssi = 10000;
218                 }
219
220                 public RSSI (GridBagLayout layout, int x) {
221                         super (layout, x, "RSSI");
222                 }
223         }
224
225         RSSI rssi;
226
227         class LastPacket extends FlightValue {
228
229                 long    last_secs = -1;
230
231                 void show(AltosState state, AltosListenerState listener_state) {
232                         long secs = (System.currentTimeMillis() - state.received_time + 500) / 1000;
233                         if (secs != last_secs) {
234                                 value.setText(String.format("%d", secs));
235                                 last_secs = secs;
236                         }
237                 }
238
239                 public void reset() {
240                         super.reset();
241                         last_secs = -1;
242                 }
243
244                 public LastPacket(GridBagLayout layout, int x) {
245                         super (layout, x, "Age");
246                 }
247         }
248
249         LastPacket last_packet;
250
251         public void reset () {
252                 call.reset();
253                 serial.reset();
254                 flight.reset();
255                 flight_state.reset();
256                 rssi.reset();
257                 last_packet.reset();
258         }
259
260         public void font_size_changed(int font_size) {
261                 call.set_font();
262                 serial.set_font();
263                 flight.set_font();
264                 flight_state.set_font();
265                 rssi.set_font();
266                 last_packet.set_font();
267         }
268
269         public void units_changed(boolean imperial_units) {
270         }
271
272         public void show (AltosState state, AltosListenerState listener_state) {
273                 call.show(state, listener_state);
274                 serial.show(state, listener_state);
275                 flight.show(state, listener_state);
276                 flight_state.show(state, listener_state);
277                 rssi.show(state, listener_state);
278                 last_packet.show(state, listener_state);
279         }
280
281         public int height() {
282                 Dimension d = layout.preferredLayoutSize(this);
283                 return d.height;
284         }
285
286         public String getName() { return "Flight Status"; }
287
288         public AltosFlightStatus() {
289                 layout = new GridBagLayout();
290
291                 setLayout(layout);
292
293                 call = new Call(layout, 0);
294                 serial = new Serial(layout, 1);
295                 flight = new Flight(layout, 2);
296                 flight_state = new FlightState(layout, 3);
297                 rssi = new RSSI(layout, 4);
298                 last_packet = new LastPacket(layout, 5);
299         }
300 }