Switch from GPLv2 to GPLv2+
[fw/altos] / telegps / TeleGPSStatus.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 org.altusmetrum.telegps;
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 TeleGPSStatus extends JComponent implements AltosFlightDisplay {
27         GridBagLayout   layout;
28
29         public class Value {
30                 JLabel          label;
31                 JTextField      value;
32
33                 void show(AltosState state, AltosListenerState listener_state) {}
34
35                 void reset() {
36                         value.setText("");
37                 }
38
39                 void set_font() {
40                         label.setFont(AltosUILib.status_font);
41                         value.setFont(AltosUILib.status_font);
42                 }
43
44                 void setVisible(boolean visible) {
45                         label.setVisible(visible);
46                         value.setVisible(visible);
47                 }
48
49                 public Value (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(AltosUILib.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.setEditable(false);
66                         value.setFont(AltosUILib.status_font);
67                         value.setHorizontalAlignment(SwingConstants.CENTER);
68                         c.gridx = x; c.gridy = 1;
69                         layout.setConstraints(value, c);
70                         add(value);
71                 }
72         }
73
74         class Call extends Value {
75                 String  call;
76
77                 void show(AltosState state, AltosListenerState listener_state) {
78                         if (state.callsign != call) {
79                                 value.setText(state.callsign);
80                                 call = state.callsign;
81                         }
82                         if (state.callsign == null)
83                                 setVisible(false);
84                         else
85                                 setVisible(true);
86                 }
87
88                 public void reset() {
89                         super.reset();
90                         call = "";
91                 }
92
93                 public Call (GridBagLayout layout, int x) {
94                         super (layout, x, "Callsign");
95                 }
96         }
97
98         Call call;
99
100         class Serial extends Value {
101                 int     serial = -1;
102                 void show(AltosState state, AltosListenerState listener_state) {
103                         if (state.serial != serial) {
104                                 if (state.serial == AltosLib.MISSING)
105                                         value.setText("none");
106                                 else
107                                         value.setText(String.format("%d", state.serial));
108                                 serial = state.serial;
109                         }
110                 }
111
112                 public void reset() {
113                         super.reset();
114                         serial = -1;
115                 }
116
117                 public Serial (GridBagLayout layout, int x) {
118                         super (layout, x, "Serial");
119                 }
120         }
121
122         Serial serial;
123
124         class Flight extends Value {
125
126                 int     last_flight = -1;
127
128                 void show(AltosState state, AltosListenerState listener_state) {
129                         if (state.flight != last_flight) {
130                                 if (state.flight == AltosLib.MISSING)
131                                         value.setText("none");
132                                 else
133                                         value.setText(String.format("%d", state.flight));
134                                 last_flight = state.flight;
135                         }
136                 }
137
138                 public void reset() {
139                         super.reset();
140                         last_flight = -1;
141                 }
142
143                 public Flight (GridBagLayout layout, int x) {
144                         super (layout, x, "Flight");
145                 }
146         }
147
148         Flight flight;
149
150         class RSSI extends Value {
151                 int     rssi = 10000;
152
153                 void show(AltosState state, AltosListenerState listener_state) {
154                         int     new_rssi = state.rssi();
155
156                         if (new_rssi != rssi) {
157                                 value.setText(String.format("%d", new_rssi));
158                                 if (state.rssi == AltosLib.MISSING)
159                                         setVisible(false);
160                                 else
161                                         setVisible(true);
162                                 rssi = new_rssi;
163                         }
164                 }
165
166                 public void reset() {
167                         super.reset();
168                         rssi = 10000;
169                 }
170
171                 public RSSI (GridBagLayout layout, int x) {
172                         super (layout, x, "RSSI");
173                 }
174         }
175
176         RSSI rssi;
177
178         class LastPacket extends Value {
179
180                 long    last_secs = -1;
181
182                 void show(AltosState state, AltosListenerState listener_state) {
183                         if (listener_state.running) {
184                                 long secs = (System.currentTimeMillis() - state.received_time + 500) / 1000;
185
186                                 if (secs != last_secs) {
187                                         value.setText(String.format("%d", secs));
188                                         last_secs = secs;
189                                 }
190                         } else {
191                                 value.setText("done");
192                         }
193                 }
194
195                 void reset() {
196                         super.reset();
197                         last_secs = -1;
198                 }
199
200                 void disable() {
201                         value.setText("");
202                 }
203
204                 public LastPacket(GridBagLayout layout, int x) {
205                         super (layout, x, "Age");
206                 }
207         }
208
209         LastPacket last_packet;
210
211         public void disable_receive() {
212                 last_packet.disable();
213         }
214
215         public void reset () {
216                 call.reset();
217                 serial.reset();
218                 flight.reset();
219                 rssi.reset();
220                 last_packet.reset();
221         }
222
223         public void font_size_changed(int font_size) {
224                 call.set_font();
225                 serial.set_font();
226                 flight.set_font();
227                 rssi.set_font();
228                 last_packet.set_font();
229         }
230
231         public void units_changed(boolean imperial_units) {
232         }
233
234         public void show (AltosState state, AltosListenerState listener_state) {
235                 call.show(state, listener_state);
236                 serial.show(state, listener_state);
237                 flight.show(state, listener_state);
238                 rssi.show(state, listener_state);
239                 last_packet.show(state, listener_state);
240                 if (!listener_state.running)
241                         stop();
242         }
243
244         public int height() {
245                 Dimension d = layout.preferredLayoutSize(this);
246                 return d.height;
247         }
248
249         TeleGPSStatusUpdate     status_update;
250         javax.swing.Timer       timer;
251
252         public void start(TeleGPSStatusUpdate status_update) {
253                 this.status_update = status_update;
254                 timer = new javax.swing.Timer(100, status_update);
255                 timer.start();
256         }
257
258         public void stop() {
259                 if (timer != null) {
260                         timer.stop();
261                         timer = null;
262                 }
263         }
264
265         public TeleGPSStatus() {
266                 layout = new GridBagLayout();
267
268                 setLayout(layout);
269
270                 call = new Call(layout, 0);
271                 serial = new Serial(layout, 1);
272                 flight = new Flight(layout, 2);
273                 rssi = new RSSI(layout, 4);
274                 last_packet = new LastPacket(layout, 5);
275         }
276 }