73b84f8d51cdd2dc59705cffafbc1a5a0e329284
[fw/altos] / altosui / AltosFlightStatus.java
1 /*
2  * Copyright © 2010 Keith Packard <keithp@keithp.com>
3  *
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.
7  *
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.
12  *
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.
16  */
17
18 package altosui;
19
20 import java.awt.*;
21 import javax.swing.*;
22 import org.altusmetrum.altoslib_4.*;
23 import org.altusmetrum.altosuilib_2.*;
24
25 public class AltosFlightStatus extends JComponent implements AltosFlightDisplay {
26         GridBagLayout   layout;
27
28         public class FlightValue {
29                 JLabel          label;
30                 JTextField      value;
31
32                 void show(AltosState state, AltosListenerState listener_state) {}
33
34                 void reset() {
35                         value.setText("");
36                 }
37
38                 void set_font() {
39                         label.setFont(Altos.status_font);
40                         value.setFont(Altos.status_font);
41                 }
42
43                 void setVisible(boolean visible) {
44                         label.setVisible(visible);
45                         value.setVisible(visible);
46                 }
47
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;
53                         c.weightx = 1;
54                         c.weighty = 1;
55
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);
61                         add(label);
62
63                         value = new JTextField("");
64                         value.setFont(Altos.status_font);
65                         value.setHorizontalAlignment(SwingConstants.CENTER);
66                         c.gridx = x; c.gridy = 1;
67                         layout.setConstraints(value, c);
68                         add(value);
69                 }
70         }
71
72         class Call extends FlightValue {
73                 void show(AltosState state, AltosListenerState listener_state) {
74                         value.setText(state.callsign);
75                         if (state.callsign == null)
76                                 setVisible(false);
77                         else
78                                 setVisible(true);
79                 }
80                 public Call (GridBagLayout layout, int x) {
81                         super (layout, x, "Callsign");
82                 }
83         }
84
85         Call call;
86
87         class Serial extends FlightValue {
88                 void show(AltosState state, AltosListenerState listener_state) {
89                         if (state.serial == AltosLib.MISSING)
90                                 value.setText("none");
91                         else
92                                 value.setText(String.format("%d", state.serial));
93                 }
94                 public Serial (GridBagLayout layout, int x) {
95                         super (layout, x, "Serial");
96                 }
97         }
98
99         Serial serial;
100
101         class Flight extends FlightValue {
102                 void show(AltosState state, AltosListenerState listener_state) {
103                         if (state.flight == AltosLib.MISSING)
104                                 value.setText("none");
105                         else
106                                 value.setText(String.format("%d", state.flight));
107                 }
108                 public Flight (GridBagLayout layout, int x) {
109                         super (layout, x, "Flight");
110                 }
111         }
112
113         Flight flight;
114
115         class FlightState extends FlightValue {
116                 void show(AltosState state, AltosListenerState listener_state) {
117                         value.setText(state.state_name());
118                 }
119                 public FlightState (GridBagLayout layout, int x) {
120                         super (layout, x, "State");
121                 }
122         }
123
124         FlightState flight_state;
125
126         class RSSI extends FlightValue {
127                 void show(AltosState state, AltosListenerState listener_state) {
128                         value.setText(String.format("%d", state.rssi()));
129                         if (state.rssi == AltosLib.MISSING)
130                                 setVisible(false);
131                         else
132                                 setVisible(true);
133                 }
134                 public RSSI (GridBagLayout layout, int x) {
135                         super (layout, x, "RSSI");
136                 }
137         }
138
139         RSSI rssi;
140
141         class LastPacket extends FlightValue {
142                 void show(AltosState state, AltosListenerState listener_state) {
143                         long secs = (System.currentTimeMillis() - state.received_time + 500) / 1000;
144                         value.setText(String.format("%d", secs));
145                 }
146                 public LastPacket(GridBagLayout layout, int x) {
147                         super (layout, x, "Age");
148                 }
149         }
150
151         LastPacket last_packet;
152
153         public void reset () {
154                 call.reset();
155                 serial.reset();
156                 flight.reset();
157                 flight_state.reset();
158                 rssi.reset();
159                 last_packet.reset();
160         }
161
162         public void set_font () {
163                 call.set_font();
164                 serial.set_font();
165                 flight.set_font();
166                 flight_state.set_font();
167                 rssi.set_font();
168                 last_packet.set_font();
169         }
170
171         public void show (AltosState state, AltosListenerState listener_state) {
172                 call.show(state, listener_state);
173                 serial.show(state, listener_state);
174                 flight.show(state, listener_state);
175                 flight_state.show(state, listener_state);
176                 rssi.show(state, listener_state);
177                 last_packet.show(state, listener_state);
178         }
179
180         public int height() {
181                 Dimension d = layout.preferredLayoutSize(this);
182                 return d.height;
183         }
184
185         public AltosFlightStatus() {
186                 layout = new GridBagLayout();
187
188                 setLayout(layout);
189
190                 call = new Call(layout, 0);
191                 serial = new Serial(layout, 1);
192                 flight = new Flight(layout, 2);
193                 flight_state = new FlightState(layout, 3);
194                 rssi = new RSSI(layout, 4);
195                 last_packet = new LastPacket(layout, 5);
196         }
197 }