]> git.gag.com Git - fw/altos/blob - telegps/TeleGPSStatus.java
altos: Allow 'c a 0' without Syntax Error
[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_14.*;
24 import org.altusmetrum.altosuilib_14.*;
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                         AltosCalData cal_data = state.cal_data();
79                         if (cal_data == null)
80                                 System.out.printf("null cal data?\n");
81                         if (cal_data.callsign != call) {
82                                 value.setText(cal_data.callsign);
83                                 call = cal_data.callsign;
84                         }
85                         if (cal_data.callsign == null)
86                                 setVisible(false);
87                         else
88                                 setVisible(true);
89                 }
90
91                 public void reset() {
92                         super.reset();
93                         call = "";
94                 }
95
96                 public Call (GridBagLayout layout, int x) {
97                         super (layout, x, "Callsign");
98                 }
99         }
100
101         Call call;
102
103         class Serial extends Value {
104                 int     serial = -1;
105                 void show(AltosState state, AltosListenerState listener_state) {
106                         AltosCalData cal_data = state.cal_data();
107                         if (cal_data.serial != serial) {
108                                 if (cal_data.serial == AltosLib.MISSING)
109                                         value.setText("none");
110                                 else
111                                         value.setText(String.format("%d", cal_data.serial));
112                                 serial = cal_data.serial;
113                         }
114                 }
115
116                 public void reset() {
117                         super.reset();
118                         serial = -1;
119                 }
120
121                 public Serial (GridBagLayout layout, int x) {
122                         super (layout, x, "Serial");
123                 }
124         }
125
126         Serial serial;
127
128         class Flight extends Value {
129
130                 int     last_flight = -1;
131
132                 void show(AltosState state, AltosListenerState listener_state) {
133                         AltosCalData cal_data = state.cal_data();
134                         if (cal_data.flight != last_flight) {
135                                 if (cal_data.flight == AltosLib.MISSING)
136                                         value.setText("none");
137                                 else
138                                         value.setText(String.format("%d", cal_data.flight));
139                                 last_flight = cal_data.flight;
140                         }
141                 }
142
143                 public void reset() {
144                         super.reset();
145                         last_flight = -1;
146                 }
147
148                 public Flight (GridBagLayout layout, int x) {
149                         super (layout, x, "Flight");
150                 }
151         }
152
153         Flight flight;
154
155         class RSSI extends Value {
156                 int     rssi = 10000;
157
158                 void show(AltosState state, AltosListenerState listener_state) {
159                         int     new_rssi = state.rssi();
160
161                         if (new_rssi != rssi) {
162                                 value.setText(String.format("%d", new_rssi));
163                                 if (state.rssi == AltosLib.MISSING)
164                                         setVisible(false);
165                                 else
166                                         setVisible(true);
167                                 rssi = new_rssi;
168                         }
169                 }
170
171                 public void reset() {
172                         super.reset();
173                         rssi = 10000;
174                 }
175
176                 public RSSI (GridBagLayout layout, int x) {
177                         super (layout, x, "RSSI");
178                 }
179         }
180
181         RSSI rssi;
182
183         class LastPacket extends Value {
184
185                 long    last_secs = -1;
186
187                 void show(AltosState state, AltosListenerState listener_state) {
188                         if (listener_state.running) {
189                                 long secs = (System.currentTimeMillis() - state.received_time + 500) / 1000;
190
191                                 if (secs != last_secs) {
192                                         value.setText(String.format("%d", secs));
193                                         last_secs = secs;
194                                 }
195                         } else {
196                                 value.setText("done");
197                         }
198                 }
199
200                 void reset() {
201                         super.reset();
202                         last_secs = -1;
203                 }
204
205                 void disable() {
206                         value.setText("");
207                 }
208
209                 public LastPacket(GridBagLayout layout, int x) {
210                         super (layout, x, "Age");
211                 }
212         }
213
214         LastPacket last_packet;
215
216         public void disable_receive() {
217                 last_packet.disable();
218         }
219
220         public void reset () {
221                 call.reset();
222                 serial.reset();
223                 flight.reset();
224                 rssi.reset();
225                 last_packet.reset();
226         }
227
228         public void font_size_changed(int font_size) {
229                 call.set_font();
230                 serial.set_font();
231                 flight.set_font();
232                 rssi.set_font();
233                 last_packet.set_font();
234         }
235
236         public void units_changed(boolean imperial_units) {
237         }
238
239         public void show (AltosState state, AltosListenerState listener_state) {
240                 call.show(state, listener_state);
241                 serial.show(state, listener_state);
242                 flight.show(state, listener_state);
243                 rssi.show(state, listener_state);
244                 last_packet.show(state, listener_state);
245                 if (!listener_state.running)
246                         stop();
247         }
248
249         public int height() {
250                 Dimension d = layout.preferredLayoutSize(this);
251                 return d.height;
252         }
253
254         TeleGPSStatusUpdate     status_update;
255         javax.swing.Timer       timer;
256
257         public void start(TeleGPSStatusUpdate status_update) {
258                 this.status_update = status_update;
259                 timer = new javax.swing.Timer(100, status_update);
260                 timer.start();
261         }
262
263         public void stop() {
264                 if (timer != null) {
265                         timer.stop();
266                         timer = null;
267                 }
268         }
269
270         public TeleGPSStatus() {
271                 layout = new GridBagLayout();
272
273                 setLayout(layout);
274
275                 call = new Call(layout, 0);
276                 serial = new Serial(layout, 1);
277                 flight = new Flight(layout, 2);
278                 rssi = new RSSI(layout, 4);
279                 last_packet = new LastPacket(layout, 5);
280         }
281 }