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_11.*;
24 import org.altusmetrum.altosuilib_11.*;
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.callsign)) {
98 value.setText(state.callsign);
99 if (state.callsign == null)
103 last_call = state.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 if (state.serial != last_serial) {
125 if (state.serial == AltosLib.MISSING)
126 value.setText("none");
128 value.setText(String.format("%d", state.serial));
129 last_serial = state.serial;
133 public void reset() {
138 public Serial (GridBagLayout layout, int x) {
139 super (layout, x, "Serial");
145 class Flight extends FlightValue {
147 int last_flight = -1;
149 void show(AltosState state, AltosListenerState listener_state) {
150 if (state.flight != last_flight) {
152 if (state.flight == AltosLib.MISSING)
153 value.setText("none");
155 value.setText(String.format("%d", state.flight));
156 last_flight = state.flight;
160 public void reset() {
165 public Flight (GridBagLayout layout, int x) {
166 super (layout, x, "Flight");
172 class FlightState extends FlightValue {
176 void show(AltosState state, AltosListenerState listener_state) {
177 if (state.state() != last_state) {
178 if (state.state() == AltosLib.ao_flight_stateless)
182 value.setText(state.state_name());
184 last_state = state.state();
188 public void reset() {
193 public FlightState (GridBagLayout layout, int x) {
194 super (layout, x, "State");
198 FlightState flight_state;
200 class RSSI extends FlightValue {
202 int last_rssi = 10000;
204 void show(AltosState state, AltosListenerState listener_state) {
205 if (state.rssi() != last_rssi) {
207 value.setText(String.format("%d", state.rssi()));
208 if (state.rssi == AltosLib.MISSING)
212 last_rssi = state.rssi();
216 public void reset() {
221 public RSSI (GridBagLayout layout, int x) {
222 super (layout, x, "RSSI");
228 class LastPacket extends FlightValue {
232 void show(AltosState state, AltosListenerState listener_state) {
233 if (listener_state.running) {
234 long secs = (System.currentTimeMillis() - state.received_time + 500) / 1000;
235 if (secs != last_secs) {
236 value.setText(String.format("%d", secs));
240 value.setText("done");
244 public void reset() {
249 public LastPacket(GridBagLayout layout, int x) {
250 super (layout, x, "Age");
254 LastPacket last_packet;
256 public void reset () {
260 flight_state.reset();
265 public void font_size_changed(int font_size) {
269 flight_state.set_font();
271 last_packet.set_font();
274 public void units_changed(boolean imperial_units) {
277 public void show (AltosState state, AltosListenerState listener_state) {
278 call.show(state, listener_state);
279 serial.show(state, listener_state);
280 flight.show(state, listener_state);
281 flight_state.show(state, listener_state);
282 rssi.show(state, listener_state);
283 last_packet.show(state, listener_state);
284 if (!listener_state.running)
288 public int height() {
289 Dimension d = layout.preferredLayoutSize(this);
293 public String getName() { return "Flight Status"; }
295 AltosFlightStatusUpdate status_update;
296 javax.swing.Timer timer;
298 public void start(AltosFlightStatusUpdate status_update) {
299 this.status_update = status_update;
300 timer = new javax.swing.Timer(100, status_update);
311 public AltosFlightStatus() {
312 layout = new GridBagLayout();
316 call = new Call(layout, 0);
317 serial = new Serial(layout, 1);
318 flight = new Flight(layout, 2);
319 flight_state = new FlightState(layout, 3);
320 rssi = new RSSI(layout, 4);
321 last_packet = new LastPacket(layout, 5);