telegps: Add scan UI
[fw/altos] / telegps / TeleGPSStatus.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 org.altusmetrum.telegps;
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 TeleGPSStatus 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(AltosUILib.status_font);
40                         value.setFont(AltosUILib.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(AltosUILib.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(AltosUILib.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 RSSI extends FlightValue {
102                 void show(AltosState state, AltosListenerState listener_state) {
103                         value.setText(String.format("%d", state.rssi()));
104                         if (state.rssi == AltosLib.MISSING)
105                                 setVisible(false);
106                         else
107                                 setVisible(true);
108                 }
109                 public RSSI (GridBagLayout layout, int x) {
110                         super (layout, x, "RSSI");
111                 }
112         }
113
114         RSSI rssi;
115
116         class LastPacket extends FlightValue {
117                 void show(AltosState state, AltosListenerState listener_state) {
118                         long secs = (System.currentTimeMillis() - state.received_time + 500) / 1000;
119                         value.setText(String.format("%d", secs));
120                 }
121                 public LastPacket(GridBagLayout layout, int x) {
122                         super (layout, x, "Age");
123                 }
124         }
125
126         LastPacket last_packet;
127
128         public void reset () {
129                 call.reset();
130                 serial.reset();
131                 rssi.reset();
132                 last_packet.reset();
133         }
134
135         public void set_font () {
136                 call.set_font();
137                 serial.set_font();
138                 rssi.set_font();
139                 last_packet.set_font();
140         }
141
142         public void show (AltosState state, AltosListenerState listener_state) {
143                 call.show(state, listener_state);
144                 serial.show(state, listener_state);
145                 rssi.show(state, listener_state);
146                 last_packet.show(state, listener_state);
147         }
148
149         public int height() {
150                 Dimension d = layout.preferredLayoutSize(this);
151                 return d.height;
152         }
153
154         public TeleGPSStatus() {
155                 layout = new GridBagLayout();
156
157                 setLayout(layout);
158
159                 call = new Call(layout, 0);
160                 serial = new Serial(layout, 1);
161                 rssi = new RSSI(layout, 4);
162                 last_packet = new LastPacket(layout, 5);
163         }
164 }