altoslib: Make cal_data private in AltosDataListener
[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_12.*;
24 import org.altusmetrum.altosuilib_12.*;
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.cal_data().callsign)) {
97                                 show();
98                                 value.setText(state.cal_data().callsign);
99                                 if (state.cal_data().callsign == null)
100                                         setVisible(false);
101                                 else
102                                         setVisible(true);
103                                 last_call = state.cal_data().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                         AltosCalData    cal_data = state.cal_data();
124                         if (cal_data.serial != last_serial) {
125                                 show();
126                                 if (cal_data.serial == AltosLib.MISSING)
127                                         value.setText("none");
128                                 else
129                                         value.setText(String.format("%d", cal_data.serial));
130                                 last_serial = cal_data.serial;
131                         }
132                 }
133
134                 public void reset() {
135                         super.reset();
136                         last_serial = -1;
137                 }
138
139                 public Serial (GridBagLayout layout, int x) {
140                         super (layout, x, "Serial");
141                 }
142         }
143
144         Serial serial;
145
146         class Flight extends FlightValue {
147
148                 int     last_flight = -1;
149
150                 void show(AltosState state, AltosListenerState listener_state) {
151                         AltosCalData cal_data = state.cal_data();
152                         if (cal_data.flight != last_flight) {
153                                 show();
154                                 if (cal_data.flight == AltosLib.MISSING)
155                                         value.setText("none");
156                                 else
157                                         value.setText(String.format("%d", cal_data.flight));
158                                 last_flight = cal_data.flight;
159                         }
160                 }
161
162                 public void reset() {
163                         super.reset();
164                         last_flight = -1;
165                 }
166
167                 public Flight (GridBagLayout layout, int x) {
168                         super (layout, x, "Flight");
169                 }
170         }
171
172         Flight flight;
173
174         class FlightState extends FlightValue {
175
176                 int     last_state = -1;
177
178                 void show(AltosState state, AltosListenerState listener_state) {
179                         if (state.state() != last_state) {
180                                 if (state.state() == AltosLib.ao_flight_stateless)
181                                         hide();
182                                 else {
183                                         show();
184                                         value.setText(state.state_name());
185                                 }
186                                 last_state = state.state();
187                         }
188                 }
189
190                 public void reset() {
191                         super.reset();
192                         last_state = -1;
193                 }
194
195                 public FlightState (GridBagLayout layout, int x) {
196                         super (layout, x, "State");
197                 }
198         }
199
200         FlightState flight_state;
201
202         class RSSI extends FlightValue {
203
204                 int     last_rssi = 10000;
205
206                 void show(AltosState state, AltosListenerState listener_state) {
207                         if (state.rssi() != last_rssi) {
208                                 show();
209                                 value.setText(String.format("%d", state.rssi()));
210                                 if (state.rssi == AltosLib.MISSING)
211                                         setVisible(false);
212                                 else
213                                         setVisible(true);
214                                 last_rssi = state.rssi();
215                         }
216                 }
217
218                 public void reset() {
219                         super.reset();
220                         last_rssi = 10000;
221                 }
222
223                 public RSSI (GridBagLayout layout, int x) {
224                         super (layout, x, "RSSI");
225                 }
226         }
227
228         RSSI rssi;
229
230         class LastPacket extends FlightValue {
231
232                 long    last_secs = -1;
233
234                 void show(AltosState state, AltosListenerState listener_state) {
235                         if (listener_state.running) {
236                                 long secs = (System.currentTimeMillis() - state.received_time + 500) / 1000;
237                                 if (secs != last_secs) {
238                                         value.setText(String.format("%d", secs));
239                                         last_secs = secs;
240                                 }
241                         } else {
242                                 value.setText("done");
243                         }
244                 }
245
246                 public void reset() {
247                         super.reset();
248                         last_secs = -1;
249                 }
250
251                 public LastPacket(GridBagLayout layout, int x) {
252                         super (layout, x, "Age");
253                 }
254         }
255
256         LastPacket last_packet;
257
258         public void reset () {
259                 call.reset();
260                 serial.reset();
261                 flight.reset();
262                 flight_state.reset();
263                 rssi.reset();
264                 last_packet.reset();
265         }
266
267         public void font_size_changed(int font_size) {
268                 call.set_font();
269                 serial.set_font();
270                 flight.set_font();
271                 flight_state.set_font();
272                 rssi.set_font();
273                 last_packet.set_font();
274         }
275
276         public void units_changed(boolean imperial_units) {
277         }
278
279         public void show (AltosState state, AltosListenerState listener_state) {
280                 call.show(state, listener_state);
281                 serial.show(state, listener_state);
282                 flight.show(state, listener_state);
283                 flight_state.show(state, listener_state);
284                 rssi.show(state, listener_state);
285                 last_packet.show(state, listener_state);
286                 if (!listener_state.running)
287                         stop();
288         }
289
290         public int height() {
291                 Dimension d = layout.preferredLayoutSize(this);
292                 return d.height;
293         }
294
295         public String getName() { return "Flight Status"; }
296
297         AltosFlightStatusUpdate status_update;
298         javax.swing.Timer       timer;
299
300         public void start(AltosFlightStatusUpdate status_update) {
301                 this.status_update = status_update;
302                 timer = new javax.swing.Timer(100, status_update);
303                 timer.start();
304         }
305
306         public void stop() {
307                 if (timer != null) {
308                         timer.stop();
309                         timer = null;
310                 }
311         }
312
313         public AltosFlightStatus() {
314                 layout = new GridBagLayout();
315
316                 setLayout(layout);
317
318                 call = new Call(layout, 0);
319                 serial = new Serial(layout, 1);
320                 flight = new Flight(layout, 2);
321                 flight_state = new FlightState(layout, 3);
322                 rssi = new RSSI(layout, 4);
323                 last_packet = new LastPacket(layout, 5);
324         }
325 }