altosui: Don't ship TeleMetrum v3.0 firmware (yet)
[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; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with this program; if not, write to the Free Software Foundation, Inc.,
16  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
17  */
18
19 package altosui;
20
21 import java.awt.*;
22 import javax.swing.*;
23 import org.altusmetrum.altoslib_11.*;
24 import org.altusmetrum.altosuilib_11.*;
25
26 public class AltosFlightStatus extends JComponent implements AltosFlightDisplay {
27         GridBagLayout   layout;
28
29         public abstract class FlightValue {
30                 JLabel          label;
31                 JTextField      value;
32
33                 void show() {
34                         label.setVisible(true);
35                         value.setVisible(true);
36                 }
37
38                 void hide() {
39                         label.setVisible(false);
40                         value.setVisible(false);
41                 }
42
43                 abstract void show(AltosState state, AltosListenerState listener_state);
44
45                 void reset() {
46                         value.setText("");
47                 }
48
49                 void set_font() {
50                         label.setFont(Altos.status_font);
51                         value.setFont(Altos.status_font);
52                 }
53
54                 void setVisible(boolean visible) {
55                         label.setVisible(visible);
56                         value.setVisible(visible);
57                 }
58
59                 public FlightValue (GridBagLayout layout, int x, String text) {
60                         GridBagConstraints      c = new GridBagConstraints();
61                         c.insets = new Insets(5, 5, 5, 5);
62                         c.anchor = GridBagConstraints.CENTER;
63                         c.fill = GridBagConstraints.BOTH;
64                         c.weightx = 1;
65                         c.weighty = 1;
66
67                         label = new JLabel(text);
68                         label.setFont(Altos.status_font);
69                         label.setHorizontalAlignment(SwingConstants.CENTER);
70                         c.gridx = x; c.gridy = 0;
71                         layout.setConstraints(label, c);
72                         add(label);
73
74                         value = new JTextField("");
75                         value.setEditable(false);
76                         value.setFont(Altos.status_font);
77                         value.setHorizontalAlignment(SwingConstants.CENTER);
78                         c.gridx = x; c.gridy = 1;
79                         layout.setConstraints(value, c);
80                         add(value);
81                 }
82         }
83
84         class Call extends FlightValue {
85
86                 String last_call = "";
87
88                 boolean same_call(String call) {
89                         if (last_call == null)
90                                 return call == null;
91                         else
92                                 return last_call.equals(call);
93                 }
94
95                 void show(AltosState state, AltosListenerState listener_state) {
96                         if (!same_call(state.callsign)) {
97                                 show();
98                                 value.setText(state.callsign);
99                                 if (state.callsign == null)
100                                         setVisible(false);
101                                 else
102                                         setVisible(true);
103                                 last_call = state.callsign;
104                         }
105                 }
106
107                 public void reset() {
108                         super.reset();
109                         last_call = "";
110                 }
111
112                 public Call (GridBagLayout layout, int x) {
113                         super (layout, x, "Callsign");
114                 }
115         }
116
117         Call call;
118
119         class Serial extends FlightValue {
120
121                 int     last_serial = -1;
122                 void show(AltosState state, AltosListenerState listener_state) {
123                         if (state.serial != last_serial) {
124                                 show();
125                                 if (state.serial == AltosLib.MISSING)
126                                         value.setText("none");
127                                 else
128                                         value.setText(String.format("%d", state.serial));
129                                 last_serial = state.serial;
130                         }
131                 }
132
133                 public void reset() {
134                         super.reset();
135                         last_serial = -1;
136                 }
137
138                 public Serial (GridBagLayout layout, int x) {
139                         super (layout, x, "Serial");
140                 }
141         }
142
143         Serial serial;
144
145         class Flight extends FlightValue {
146
147                 int     last_flight = -1;
148
149                 void show(AltosState state, AltosListenerState listener_state) {
150                         if (state.flight != last_flight) {
151                                 show();
152                                 if (state.flight == AltosLib.MISSING)
153                                         value.setText("none");
154                                 else
155                                         value.setText(String.format("%d", state.flight));
156                                 last_flight = state.flight;
157                         }
158                 }
159
160                 public void reset() {
161                         super.reset();
162                         last_flight = -1;
163                 }
164
165                 public Flight (GridBagLayout layout, int x) {
166                         super (layout, x, "Flight");
167                 }
168         }
169
170         Flight flight;
171
172         class FlightState extends FlightValue {
173
174                 int     last_state = -1;
175
176                 void show(AltosState state, AltosListenerState listener_state) {
177                         if (state.state() != last_state) {
178                                 if (state.state() == AltosLib.ao_flight_stateless)
179                                         hide();
180                                 else {
181                                         show();
182                                         value.setText(state.state_name());
183                                 }
184                                 last_state = state.state();
185                         }
186                 }
187
188                 public void reset() {
189                         super.reset();
190                         last_state = -1;
191                 }
192
193                 public FlightState (GridBagLayout layout, int x) {
194                         super (layout, x, "State");
195                 }
196         }
197
198         FlightState flight_state;
199
200         class RSSI extends FlightValue {
201
202                 int     last_rssi = 10000;
203
204                 void show(AltosState state, AltosListenerState listener_state) {
205                         if (state.rssi() != last_rssi) {
206                                 show();
207                                 value.setText(String.format("%d", state.rssi()));
208                                 if (state.rssi == AltosLib.MISSING)
209                                         setVisible(false);
210                                 else
211                                         setVisible(true);
212                                 last_rssi = state.rssi();
213                         }
214                 }
215
216                 public void reset() {
217                         super.reset();
218                         last_rssi = 10000;
219                 }
220
221                 public RSSI (GridBagLayout layout, int x) {
222                         super (layout, x, "RSSI");
223                 }
224         }
225
226         RSSI rssi;
227
228         class LastPacket extends FlightValue {
229
230                 long    last_secs = -1;
231
232                 void show(AltosState state, AltosListenerState listener_state) {
233                         if (listener_state.running) {
234                                 long secs = (System.currentTimeMillis() - state.received_time + 500) / 1000;
235                                 if (secs != last_secs) {
236                                         value.setText(String.format("%d", secs));
237                                         last_secs = secs;
238                                 }
239                         } else {
240                                 value.setText("done");
241                         }
242                 }
243
244                 public void reset() {
245                         super.reset();
246                         last_secs = -1;
247                 }
248
249                 public LastPacket(GridBagLayout layout, int x) {
250                         super (layout, x, "Age");
251                 }
252         }
253
254         LastPacket last_packet;
255
256         public void reset () {
257                 call.reset();
258                 serial.reset();
259                 flight.reset();
260                 flight_state.reset();
261                 rssi.reset();
262                 last_packet.reset();
263         }
264
265         public void font_size_changed(int font_size) {
266                 call.set_font();
267                 serial.set_font();
268                 flight.set_font();
269                 flight_state.set_font();
270                 rssi.set_font();
271                 last_packet.set_font();
272         }
273
274         public void units_changed(boolean imperial_units) {
275         }
276
277         public void show (AltosState state, AltosListenerState listener_state) {
278                 call.show(state, listener_state);
279                 serial.show(state, listener_state);
280                 flight.show(state, listener_state);
281                 flight_state.show(state, listener_state);
282                 rssi.show(state, listener_state);
283                 last_packet.show(state, listener_state);
284                 if (!listener_state.running)
285                         stop();
286         }
287
288         public int height() {
289                 Dimension d = layout.preferredLayoutSize(this);
290                 return d.height;
291         }
292
293         public String getName() { return "Flight Status"; }
294
295         AltosFlightStatusUpdate status_update;
296         javax.swing.Timer       timer;
297
298         public void start(AltosFlightStatusUpdate status_update) {
299                 this.status_update = status_update;
300                 timer = new javax.swing.Timer(100, status_update);
301                 timer.start();
302         }
303
304         public void stop() {
305                 if (timer != null) {
306                         timer.stop();
307                         timer = null;
308                 }
309         }
310
311         public AltosFlightStatus() {
312                 layout = new GridBagLayout();
313
314                 setLayout(layout);
315
316                 call = new Call(layout, 0);
317                 serial = new Serial(layout, 1);
318                 flight = new Flight(layout, 2);
319                 flight_state = new FlightState(layout, 3);
320                 rssi = new RSSI(layout, 4);
321                 last_packet = new LastPacket(layout, 5);
322         }
323 }