b99a532537a92c18ce9ea1f2eadf8bbd810d12bf
[fw/altos] / ao-tools / 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
31 public class AltosFlightStatus extends JComponent implements AltosFlightDisplay {
32         GridBagLayout   layout;
33
34         private Font status_font;
35
36         public class FlightValue {
37                 JLabel          label;
38                 JTextField      value;
39
40                 void show(AltosState state, int crc_errors) {}
41
42                 void reset() {
43                         value.setText("");
44                 }
45                 public FlightValue (GridBagLayout layout, int x, String text) {
46                         GridBagConstraints      c = new GridBagConstraints();
47                         c.insets = new Insets(5, 5, 5, 5);
48                         c.anchor = GridBagConstraints.CENTER;
49                         c.fill = GridBagConstraints.BOTH;
50                         c.weightx = 1;
51                         c.weighty = 1;
52
53                         label = new JLabel(text);
54                         label.setFont(status_font);
55                         label.setHorizontalAlignment(SwingConstants.CENTER);
56                         c.gridx = x; c.gridy = 0;
57                         layout.setConstraints(label, c);
58                         add(label);
59
60                         value = new JTextField("");
61                         value.setFont(status_font);
62                         value.setHorizontalAlignment(SwingConstants.CENTER);
63                         c.gridx = x; c.gridy = 1;
64                         layout.setConstraints(value, c);
65                         add(value);
66                 }
67         }
68
69         class Call extends FlightValue {
70                 void show(AltosState state, int crc_errors) {
71                         value.setText(state.data.callsign);
72                 }
73                 public Call (GridBagLayout layout, int x) {
74                         super (layout, x, "Callsign");
75                 }
76         }
77
78         Call call;
79
80         class Serial extends FlightValue {
81                 void show(AltosState state, int crc_errors) {
82                         value.setText(String.format("%d", state.data.serial));
83                 }
84                 public Serial (GridBagLayout layout, int x) {
85                         super (layout, x, "Serial");
86                 }
87         }
88
89         Serial serial;
90
91         class Flight extends FlightValue {
92                 void show(AltosState state, int crc_errors) {
93                         value.setText(String.format("%d", state.data.flight));
94                 }
95                 public Flight (GridBagLayout layout, int x) {
96                         super (layout, x, "Flight");
97                 }
98         }
99
100         Flight flight;
101
102         class FlightState extends FlightValue {
103                 void show(AltosState state, int crc_errors) {
104                         value.setText(state.data.state());
105                 }
106                 public FlightState (GridBagLayout layout, int x) {
107                         super (layout, x, "State");
108                 }
109         }
110
111         FlightState flight_state;
112
113         class RSSI extends FlightValue {
114                 void show(AltosState state, int crc_errors) {
115                         value.setText(String.format("%d", state.data.rssi));
116                 }
117                 public RSSI (GridBagLayout layout, int x) {
118                         super (layout, x, "RSSI (dBm)");
119                 }
120         }
121
122         RSSI rssi;
123
124         public void reset () {
125                 call.reset();
126                 serial.reset();
127                 flight.reset();
128                 flight_state.reset();
129                 rssi.reset();
130         }
131
132         public void show (AltosState state, int crc_errors) {
133                 call.show(state, crc_errors);
134                 serial.show(state, crc_errors);
135                 flight.show(state, crc_errors);
136                 flight_state.show(state, crc_errors);
137                 rssi.show(state, crc_errors);
138         }
139
140         public int height() {
141                 Dimension d = layout.preferredLayoutSize(this);
142                 return d.height;
143         }
144
145         public AltosFlightStatus() {
146                 layout = new GridBagLayout();
147
148                 status_font = new Font("SansSerif", Font.BOLD, 24);
149                 setLayout(layout);
150
151                 call = new Call(layout, 0);
152                 serial = new Serial(layout, 1);
153                 flight = new Flight(layout, 2);
154                 flight_state = new FlightState(layout, 3);
155                 rssi = new RSSI(layout, 4);
156         }
157 }