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_4.*;
23 import org.altusmetrum.altosuilib_2.*;
25 public class AltosFlightStatus extends JComponent implements AltosFlightDisplay {
28 public class FlightValue {
32 void show(AltosState state, AltosListenerState listener_state) {}
39 label.setFont(Altos.status_font);
40 value.setFont(Altos.status_font);
43 void setVisible(boolean visible) {
44 label.setVisible(visible);
45 value.setVisible(visible);
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;
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);
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);
73 class Call extends FlightValue {
75 String last_call = "";
77 boolean same_call(String call) {
78 if (last_call == null)
81 return last_call.equals(call);
84 void show(AltosState state, AltosListenerState listener_state) {
85 if (!same_call(state.callsign)) {
86 value.setText(state.callsign);
87 if (state.callsign == null)
91 last_call = state.callsign;
94 public Call (GridBagLayout layout, int x) {
95 super (layout, x, "Callsign");
101 class Serial extends FlightValue {
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");
109 value.setText(String.format("%d", state.serial));
110 last_serial = state.serial;
113 public Serial (GridBagLayout layout, int x) {
114 super (layout, x, "Serial");
120 class Flight extends FlightValue {
122 int last_flight = -1;
124 void show(AltosState state, AltosListenerState listener_state) {
125 if (state.flight != last_flight) {
126 if (state.flight == AltosLib.MISSING)
127 value.setText("none");
129 value.setText(String.format("%d", state.flight));
130 last_flight = state.flight;
133 public Flight (GridBagLayout layout, int x) {
134 super (layout, x, "Flight");
140 class FlightState extends FlightValue {
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;
150 public FlightState (GridBagLayout layout, int x) {
151 super (layout, x, "State");
155 FlightState flight_state;
157 class RSSI extends FlightValue {
159 int last_rssi = 10000;
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)
168 last_rssi = state.rssi();
171 public RSSI (GridBagLayout layout, int x) {
172 super (layout, x, "RSSI");
178 class LastPacket extends FlightValue {
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));
189 public LastPacket(GridBagLayout layout, int x) {
190 super (layout, x, "Age");
194 LastPacket last_packet;
196 public void reset () {
200 flight_state.reset();
205 public void font_size_changed(int font_size) {
209 flight_state.set_font();
211 last_packet.set_font();
214 public void units_changed(boolean imperial_units) {
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);
226 public int height() {
227 Dimension d = layout.preferredLayoutSize(this);
231 public AltosFlightStatus() {
232 layout = new GridBagLayout();
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);