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