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_5.*;
23 import org.altusmetrum.altosuilib_3.*;
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 long secs = (System.currentTimeMillis() - state.received_time + 500) / 1000;
233 if (secs != last_secs) {
234 value.setText(String.format("%d", secs));
239 public void reset() {
244 public LastPacket(GridBagLayout layout, int x) {
245 super (layout, x, "Age");
249 LastPacket last_packet;
251 public void reset () {
255 flight_state.reset();
260 public void font_size_changed(int font_size) {
264 flight_state.set_font();
266 last_packet.set_font();
269 public void units_changed(boolean imperial_units) {
272 public void show (AltosState state, AltosListenerState listener_state) {
273 call.show(state, listener_state);
274 serial.show(state, listener_state);
275 flight.show(state, listener_state);
276 flight_state.show(state, listener_state);
277 rssi.show(state, listener_state);
278 last_packet.show(state, listener_state);
281 public int height() {
282 Dimension d = layout.preferredLayoutSize(this);
286 public String getName() { return "Flight Status"; }
288 public AltosFlightStatus() {
289 layout = new GridBagLayout();
293 call = new Call(layout, 0);
294 serial = new Serial(layout, 1);
295 flight = new Flight(layout, 2);
296 flight_state = new FlightState(layout, 3);
297 rssi = new RSSI(layout, 4);
298 last_packet = new LastPacket(layout, 5);