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