telegps: Show flight number in monitor window
[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; 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 org.altusmetrum.telegps;
19
20 import java.awt.*;
21 import javax.swing.*;
22 import org.altusmetrum.altoslib_4.*;
23 import org.altusmetrum.altosuilib_2.*;
24
25 public class TeleGPSStatus extends JComponent implements AltosFlightDisplay {
26         GridBagLayout   layout;
27
28         public class Value {
29                 JLabel          label;
30                 JTextField      value;
31
32                 void show(AltosState state, AltosListenerState listener_state) {}
33
34                 void reset() {
35                         value.setText("");
36                 }
37
38                 void set_font() {
39                         label.setFont(AltosUILib.status_font);
40                         value.setFont(AltosUILib.status_font);
41                 }
42
43                 void setVisible(boolean visible) {
44                         label.setVisible(visible);
45                         value.setVisible(visible);
46                 }
47
48                 public Value (GridBagLayout layout, int x, String text) {
49                         GridBagConstraints      c = new GridBagConstraints();
50                         c.insets = new Insets(5, 5, 5, 5);
51                         c.anchor = GridBagConstraints.CENTER;
52                         c.fill = GridBagConstraints.BOTH;
53                         c.weightx = 1;
54                         c.weighty = 1;
55
56                         label = new JLabel(text);
57                         label.setFont(AltosUILib.status_font);
58                         label.setHorizontalAlignment(SwingConstants.CENTER);
59                         c.gridx = x; c.gridy = 0;
60                         layout.setConstraints(label, c);
61                         add(label);
62
63                         value = new JTextField("");
64                         value.setEditable(false);
65                         value.setFont(AltosUILib.status_font);
66                         value.setHorizontalAlignment(SwingConstants.CENTER);
67                         c.gridx = x; c.gridy = 1;
68                         layout.setConstraints(value, c);
69                         add(value);
70                 }
71         }
72
73         class Call extends Value {
74                 String  call;
75
76                 void show(AltosState state, AltosListenerState listener_state) {
77                         if (state.callsign != call) {
78                                 value.setText(state.callsign);
79                                 call = state.callsign;
80                         }
81                         if (state.callsign == null)
82                                 setVisible(false);
83                         else
84                                 setVisible(true);
85                 }
86                 public Call (GridBagLayout layout, int x) {
87                         super (layout, x, "Callsign");
88                 }
89         }
90
91         Call call;
92
93         class Serial extends Value {
94                 int     serial = -1;
95                 void show(AltosState state, AltosListenerState listener_state) {
96                         if (state.serial != serial) {
97                                 if (state.serial == AltosLib.MISSING)
98                                         value.setText("none");
99                                 else
100                                         value.setText(String.format("%d", state.serial));
101                                 serial = state.serial;
102                         }
103                 }
104                 public Serial (GridBagLayout layout, int x) {
105                         super (layout, x, "Serial");
106                 }
107         }
108
109         Serial serial;
110
111         class Flight extends Value {
112
113                 int     last_flight = -1;
114
115                 void show(AltosState state, AltosListenerState listener_state) {
116                         if (state.flight != last_flight) {
117                                 if (state.flight == AltosLib.MISSING)
118                                         value.setText("none");
119                                 else
120                                         value.setText(String.format("%d", state.flight));
121                                 last_flight = state.flight;
122                         }
123                 }
124                 public Flight (GridBagLayout layout, int x) {
125                         super (layout, x, "Flight");
126                 }
127         }
128
129         Flight flight;
130
131         class RSSI extends Value {
132                 int     rssi = 10000;
133
134                 void show(AltosState state, AltosListenerState listener_state) {
135                         int     new_rssi = state.rssi();
136
137                         if (new_rssi != rssi) {
138                                 value.setText(String.format("%d", new_rssi));
139                                 if (state.rssi == AltosLib.MISSING)
140                                         setVisible(false);
141                                 else
142                                         setVisible(true);
143                                 rssi = new_rssi;
144                         }
145                 }
146                 public RSSI (GridBagLayout layout, int x) {
147                         super (layout, x, "RSSI");
148                 }
149         }
150
151         RSSI rssi;
152
153         class LastPacket extends Value {
154
155                 long    last_secs = -1;
156
157                 void show(AltosState state, AltosListenerState listener_state) {
158                         long secs = (System.currentTimeMillis() - state.received_time + 500) / 1000;
159
160                         if (secs != last_secs) {
161                                 value.setText(String.format("%d", secs));
162                                 last_secs = secs;
163                         }
164                 }
165                 public LastPacket(GridBagLayout layout, int x) {
166                         super (layout, x, "Age");
167                 }
168         }
169
170         LastPacket last_packet;
171
172         public void reset () {
173                 call.reset();
174                 serial.reset();
175                 flight.reset();
176                 rssi.reset();
177                 last_packet.reset();
178         }
179
180         public void font_size_changed(int font_size) {
181                 call.set_font();
182                 serial.set_font();
183                 flight.set_font();
184                 rssi.set_font();
185                 last_packet.set_font();
186         }
187
188         public void units_changed(boolean imperial_units) {
189         }
190
191         public void show (AltosState state, AltosListenerState listener_state) {
192                 call.show(state, listener_state);
193                 serial.show(state, listener_state);
194                 flight.show(state, listener_state);
195                 rssi.show(state, listener_state);
196                 last_packet.show(state, listener_state);
197         }
198
199         public int height() {
200                 Dimension d = layout.preferredLayoutSize(this);
201                 return d.height;
202         }
203
204         public TeleGPSStatus() {
205                 layout = new GridBagLayout();
206
207                 setLayout(layout);
208
209                 call = new Call(layout, 0);
210                 serial = new Serial(layout, 1);
211                 flight = new Flight(layout, 2);
212                 rssi = new RSSI(layout, 4);
213                 last_packet = new LastPacket(layout, 5);
214         }
215 }