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; either version 2 of the License, or
7 * (at your option) any later version.
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.
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.
23 import org.altusmetrum.altoslib_12.*;
24 import org.altusmetrum.altosuilib_12.*;
26 public class AltosFlightStatus extends JComponent implements AltosFlightDisplay {
29 public abstract class FlightValue {
34 label.setVisible(true);
35 value.setVisible(true);
39 label.setVisible(false);
40 value.setVisible(false);
43 abstract void show(AltosState state, AltosListenerState listener_state);
50 label.setFont(Altos.status_font);
51 value.setFont(Altos.status_font);
54 void setVisible(boolean visible) {
55 label.setVisible(visible);
56 value.setVisible(visible);
59 public FlightValue (GridBagLayout layout, int x, String text) {
60 GridBagConstraints c = new GridBagConstraints();
61 c.insets = new Insets(5, 5, 5, 5);
62 c.anchor = GridBagConstraints.CENTER;
63 c.fill = GridBagConstraints.BOTH;
67 label = new JLabel(text);
68 label.setFont(Altos.status_font);
69 label.setHorizontalAlignment(SwingConstants.CENTER);
70 c.gridx = x; c.gridy = 0;
71 layout.setConstraints(label, c);
74 value = new JTextField("");
75 value.setEditable(false);
76 value.setFont(Altos.status_font);
77 value.setHorizontalAlignment(SwingConstants.CENTER);
78 c.gridx = x; c.gridy = 1;
79 layout.setConstraints(value, c);
84 class Call extends FlightValue {
86 String last_call = "";
88 boolean same_call(String call) {
89 if (last_call == null)
92 return last_call.equals(call);
95 void show(AltosState state, AltosListenerState listener_state) {
96 if (!same_call(state.cal_data().callsign)) {
98 value.setText(state.cal_data().callsign);
99 if (state.cal_data().callsign == null)
103 last_call = state.cal_data().callsign;
107 public void reset() {
112 public Call (GridBagLayout layout, int x) {
113 super (layout, x, "Callsign");
119 class Serial extends FlightValue {
121 int last_serial = -1;
122 void show(AltosState state, AltosListenerState listener_state) {
123 AltosCalData cal_data = state.cal_data();
124 if (cal_data.serial != last_serial) {
126 if (cal_data.serial == AltosLib.MISSING)
127 value.setText("none");
129 value.setText(String.format("%d", cal_data.serial));
130 last_serial = cal_data.serial;
134 public void reset() {
139 public Serial (GridBagLayout layout, int x) {
140 super (layout, x, "Serial");
146 class Flight extends FlightValue {
148 int last_flight = -1;
150 void show(AltosState state, AltosListenerState listener_state) {
151 AltosCalData cal_data = state.cal_data();
152 if (cal_data.flight != last_flight) {
154 if (cal_data.flight == AltosLib.MISSING)
155 value.setText("none");
157 value.setText(String.format("%d", cal_data.flight));
158 last_flight = cal_data.flight;
162 public void reset() {
167 public Flight (GridBagLayout layout, int x) {
168 super (layout, x, "Flight");
174 class FlightState extends FlightValue {
178 void show(AltosState state, AltosListenerState listener_state) {
179 if (state.state() != last_state) {
180 if (state.state() == AltosLib.ao_flight_stateless)
184 value.setText(state.state_name());
186 last_state = state.state();
190 public void reset() {
195 public FlightState (GridBagLayout layout, int x) {
196 super (layout, x, "State");
200 FlightState flight_state;
202 class RSSI extends FlightValue {
204 int last_rssi = 10000;
206 void show(AltosState state, AltosListenerState listener_state) {
207 if (state.rssi() != last_rssi) {
209 value.setText(String.format("%d", state.rssi()));
210 if (state.rssi == AltosLib.MISSING)
214 last_rssi = state.rssi();
218 public void reset() {
223 public RSSI (GridBagLayout layout, int x) {
224 super (layout, x, "RSSI");
230 class LastPacket extends FlightValue {
234 void show(AltosState state, AltosListenerState listener_state) {
235 if (listener_state.running) {
236 long secs = (System.currentTimeMillis() - state.received_time + 500) / 1000;
237 if (secs != last_secs) {
238 value.setText(String.format("%d", secs));
242 value.setText("done");
246 public void reset() {
251 public LastPacket(GridBagLayout layout, int x) {
252 super (layout, x, "Age");
256 LastPacket last_packet;
258 public void reset () {
262 flight_state.reset();
267 public void font_size_changed(int font_size) {
271 flight_state.set_font();
273 last_packet.set_font();
276 public void units_changed(boolean imperial_units) {
279 public void show (AltosState state, AltosListenerState listener_state) {
280 call.show(state, listener_state);
281 serial.show(state, listener_state);
282 flight.show(state, listener_state);
283 flight_state.show(state, listener_state);
284 rssi.show(state, listener_state);
285 last_packet.show(state, listener_state);
286 if (!listener_state.running)
290 public int height() {
291 Dimension d = layout.preferredLayoutSize(this);
295 public String getName() { return "Flight Status"; }
297 AltosFlightStatusUpdate status_update;
298 javax.swing.Timer timer;
300 public void start(AltosFlightStatusUpdate status_update) {
301 this.status_update = status_update;
302 timer = new javax.swing.Timer(100, status_update);
313 public AltosFlightStatus() {
314 layout = new GridBagLayout();
318 call = new Call(layout, 0);
319 serial = new Serial(layout, 1);
320 flight = new Flight(layout, 2);
321 flight_state = new FlightState(layout, 3);
322 rssi = new RSSI(layout, 4);
323 last_packet = new LastPacket(layout, 5);