]> git.gag.com Git - fw/altos/blob - altosui/AltosFlightStatus.java
doc: Add titles to all figures
[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_8.*;
23 import org.altusmetrum.altosuilib_8.*;
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                         if (listener_state.running) {
233                                 long secs = (System.currentTimeMillis() - state.received_time + 500) / 1000;
234                                 if (secs != last_secs) {
235                                         value.setText(String.format("%d", secs));
236                                         last_secs = secs;
237                                 }
238                         } else {
239                                 value.setText("done");
240                         }
241                 }
242
243                 public void reset() {
244                         super.reset();
245                         last_secs = -1;
246                 }
247
248                 public LastPacket(GridBagLayout layout, int x) {
249                         super (layout, x, "Age");
250                 }
251         }
252
253         LastPacket last_packet;
254
255         public void reset () {
256                 call.reset();
257                 serial.reset();
258                 flight.reset();
259                 flight_state.reset();
260                 rssi.reset();
261                 last_packet.reset();
262         }
263
264         public void font_size_changed(int font_size) {
265                 call.set_font();
266                 serial.set_font();
267                 flight.set_font();
268                 flight_state.set_font();
269                 rssi.set_font();
270                 last_packet.set_font();
271         }
272
273         public void units_changed(boolean imperial_units) {
274         }
275
276         public void show (AltosState state, AltosListenerState listener_state) {
277                 call.show(state, listener_state);
278                 serial.show(state, listener_state);
279                 flight.show(state, listener_state);
280                 flight_state.show(state, listener_state);
281                 rssi.show(state, listener_state);
282                 last_packet.show(state, listener_state);
283                 if (!listener_state.running)
284                         stop();
285         }
286
287         public int height() {
288                 Dimension d = layout.preferredLayoutSize(this);
289                 return d.height;
290         }
291
292         public String getName() { return "Flight Status"; }
293
294         AltosFlightStatusUpdate status_update;
295         javax.swing.Timer       timer;
296
297         public void start(AltosFlightStatusUpdate status_update) {
298                 this.status_update = status_update;
299                 timer = new javax.swing.Timer(100, status_update);
300                 timer.start();
301         }
302
303         public void stop() {
304                 if (timer != null) {
305                         timer.stop();
306                         timer = null;
307                 }
308         }
309
310         public AltosFlightStatus() {
311                 layout = new GridBagLayout();
312
313                 setLayout(layout);
314
315                 call = new Call(layout, 0);
316                 serial = new Serial(layout, 1);
317                 flight = new Flight(layout, 2);
318                 flight_state = new FlightState(layout, 3);
319                 rssi = new RSSI(layout, 4);
320                 last_packet = new LastPacket(layout, 5);
321         }
322 }