2 * Copyright © 2010 Keith Packard <keithp@keithp.com>
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.
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.
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.
22 import org.altusmetrum.altoslib_8.*;
23 import org.altusmetrum.altosuilib_8.*;
25 public class AltosFlightStatus extends JComponent implements AltosFlightDisplay {
28 public abstract class FlightValue {
33 label.setVisible(true);
34 value.setVisible(true);
38 label.setVisible(false);
39 value.setVisible(false);
42 abstract void show(AltosState state, AltosListenerState listener_state);
49 label.setFont(Altos.status_font);
50 value.setFont(Altos.status_font);
53 void setVisible(boolean visible) {
54 label.setVisible(visible);
55 value.setVisible(visible);
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;
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);
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);
83 class Call extends FlightValue {
85 String last_call = "";
87 boolean same_call(String call) {
88 if (last_call == null)
91 return last_call.equals(call);
94 void show(AltosState state, AltosListenerState listener_state) {
95 if (!same_call(state.callsign)) {
97 value.setText(state.callsign);
98 if (state.callsign == null)
102 last_call = state.callsign;
106 public void reset() {
111 public Call (GridBagLayout layout, int x) {
112 super (layout, x, "Callsign");
118 class Serial extends FlightValue {
120 int last_serial = -1;
121 void show(AltosState state, AltosListenerState listener_state) {
122 if (state.serial != last_serial) {
124 if (state.serial == AltosLib.MISSING)
125 value.setText("none");
127 value.setText(String.format("%d", state.serial));
128 last_serial = state.serial;
132 public void reset() {
137 public Serial (GridBagLayout layout, int x) {
138 super (layout, x, "Serial");
144 class Flight extends FlightValue {
146 int last_flight = -1;
148 void show(AltosState state, AltosListenerState listener_state) {
149 if (state.flight != last_flight) {
151 if (state.flight == AltosLib.MISSING)
152 value.setText("none");
154 value.setText(String.format("%d", state.flight));
155 last_flight = state.flight;
159 public void reset() {
164 public Flight (GridBagLayout layout, int x) {
165 super (layout, x, "Flight");
171 class FlightState extends FlightValue {
175 void show(AltosState state, AltosListenerState listener_state) {
176 if (state.state() != last_state) {
177 if (state.state() == AltosLib.ao_flight_stateless)
181 value.setText(state.state_name());
183 last_state = state.state();
187 public void reset() {
192 public FlightState (GridBagLayout layout, int x) {
193 super (layout, x, "State");
197 FlightState flight_state;
199 class RSSI extends FlightValue {
201 int last_rssi = 10000;
203 void show(AltosState state, AltosListenerState listener_state) {
204 if (state.rssi() != last_rssi) {
206 value.setText(String.format("%d", state.rssi()));
207 if (state.rssi == AltosLib.MISSING)
211 last_rssi = state.rssi();
215 public void reset() {
220 public RSSI (GridBagLayout layout, int x) {
221 super (layout, x, "RSSI");
227 class LastPacket extends FlightValue {
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));
239 value.setText("done");
243 public void reset() {
248 public LastPacket(GridBagLayout layout, int x) {
249 super (layout, x, "Age");
253 LastPacket last_packet;
255 public void reset () {
259 flight_state.reset();
264 public void font_size_changed(int font_size) {
268 flight_state.set_font();
270 last_packet.set_font();
273 public void units_changed(boolean imperial_units) {
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)
287 public int height() {
288 Dimension d = layout.preferredLayoutSize(this);
292 public String getName() { return "Flight Status"; }
294 AltosFlightStatusUpdate status_update;
295 javax.swing.Timer timer;
297 public void start(AltosFlightStatusUpdate status_update) {
298 this.status_update = status_update;
299 timer = new javax.swing.Timer(100, status_update);
310 public AltosFlightStatus() {
311 layout = new GridBagLayout();
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);