altosui/telegps: Reduce CPU time needed for flight displays
[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_4.*;
23 import org.altusmetrum.altosuilib_2.*;
24
25 public class AltosFlightStatus extends JComponent implements AltosFlightDisplay {
26         GridBagLayout   layout;
27
28         public class FlightValue {
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(Altos.status_font);
40                         value.setFont(Altos.status_font);
41                 }
42
43                 void setVisible(boolean visible) {
44                         label.setVisible(visible);
45                         value.setVisible(visible);
46                 }
47
48                 public FlightValue (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(Altos.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(Altos.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 FlightValue {
74
75                 String last_call = "";
76
77                 boolean same_call(String call) {
78                         if (last_call == null)
79                                 return call == null;
80                         else
81                                 return last_call.equals(call);
82                 }
83
84                 void show(AltosState state, AltosListenerState listener_state) {
85                         if (!same_call(state.callsign)) {
86                                 value.setText(state.callsign);
87                                 if (state.callsign == null)
88                                         setVisible(false);
89                                 else
90                                         setVisible(true);
91                                 last_call = state.callsign;
92                         }
93                 }
94                 public Call (GridBagLayout layout, int x) {
95                         super (layout, x, "Callsign");
96                 }
97         }
98
99         Call call;
100
101         class Serial extends FlightValue {
102
103                 int     last_serial = -1;
104                 void show(AltosState state, AltosListenerState listener_state) {
105                         if (state.serial != last_serial) {
106                                 if (state.serial == AltosLib.MISSING)
107                                         value.setText("none");
108                                 else
109                                         value.setText(String.format("%d", state.serial));
110                                 last_serial = state.serial;
111                         }
112                 }
113                 public Serial (GridBagLayout layout, int x) {
114                         super (layout, x, "Serial");
115                 }
116         }
117
118         Serial serial;
119
120         class Flight extends FlightValue {
121
122                 int     last_flight = -1;
123
124                 void show(AltosState state, AltosListenerState listener_state) {
125                         if (state.flight != last_flight) {
126                                 if (state.flight == AltosLib.MISSING)
127                                         value.setText("none");
128                                 else
129                                         value.setText(String.format("%d", state.flight));
130                                 last_flight = state.flight;
131                         }
132                 }
133                 public Flight (GridBagLayout layout, int x) {
134                         super (layout, x, "Flight");
135                 }
136         }
137
138         Flight flight;
139
140         class FlightState extends FlightValue {
141
142                 int     last_state = -1;
143
144                 void show(AltosState state, AltosListenerState listener_state) {
145                         if (state.state != last_state) {
146                                 value.setText(state.state_name());
147                                 last_state = state.state;
148                         }
149                 }
150                 public FlightState (GridBagLayout layout, int x) {
151                         super (layout, x, "State");
152                 }
153         }
154
155         FlightState flight_state;
156
157         class RSSI extends FlightValue {
158
159                 int     last_rssi = 10000;
160
161                 void show(AltosState state, AltosListenerState listener_state) {
162                         if (state.rssi() != last_rssi) {
163                                 value.setText(String.format("%d", state.rssi()));
164                                 if (state.rssi == AltosLib.MISSING)
165                                         setVisible(false);
166                                 else
167                                         setVisible(true);
168                                 last_rssi = state.rssi();
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 FlightValue {
179
180                 long    last_secs = -1;
181
182                 void show(AltosState state, AltosListenerState listener_state) {
183                         long secs = (System.currentTimeMillis() - state.received_time + 500) / 1000;
184                         if (secs != last_secs) {
185                                 value.setText(String.format("%d", secs));
186                                 last_secs = secs;
187                         }
188                 }
189                 public LastPacket(GridBagLayout layout, int x) {
190                         super (layout, x, "Age");
191                 }
192         }
193
194         LastPacket last_packet;
195
196         public void reset () {
197                 call.reset();
198                 serial.reset();
199                 flight.reset();
200                 flight_state.reset();
201                 rssi.reset();
202                 last_packet.reset();
203         }
204
205         public void font_size_changed(int font_size) {
206                 call.set_font();
207                 serial.set_font();
208                 flight.set_font();
209                 flight_state.set_font();
210                 rssi.set_font();
211                 last_packet.set_font();
212         }
213
214         public void units_changed(boolean imperial_units) {
215         }
216
217         public void show (AltosState state, AltosListenerState listener_state) {
218                 call.show(state, listener_state);
219                 serial.show(state, listener_state);
220                 flight.show(state, listener_state);
221                 flight_state.show(state, listener_state);
222                 rssi.show(state, listener_state);
223                 last_packet.show(state, listener_state);
224         }
225
226         public int height() {
227                 Dimension d = layout.preferredLayoutSize(this);
228                 return d.height;
229         }
230
231         public AltosFlightStatus() {
232                 layout = new GridBagLayout();
233
234                 setLayout(layout);
235
236                 call = new Call(layout, 0);
237                 serial = new Serial(layout, 1);
238                 flight = new Flight(layout, 2);
239                 flight_state = new FlightState(layout, 3);
240                 rssi = new RSSI(layout, 4);
241                 last_packet = new LastPacket(layout, 5);
242         }
243 }