pull up maps for arbitrary locations
[fw/altos] / ao-tools / altosui / AltosLanded.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 AltosLanded extends JComponent implements AltosFlightDisplay {
32         GridBagLayout   layout;
33         Font            label_font;
34         Font            value_font;
35
36         public class LandedValue {
37                 JLabel          label;
38                 JTextField      value;
39                 void show(AltosState state, int crc_errors) {}
40
41                 void reset() {
42                         value.setText("");
43                 }
44
45                 void show(String format, double v) {
46                         value.setText(String.format(format, v));
47                 }
48
49                 public LandedValue (GridBagLayout layout, int y, String text) {
50                         GridBagConstraints      c = new GridBagConstraints();
51
52                         label = new JLabel(text);
53                         label.setFont(label_font);
54                         label.setHorizontalAlignment(SwingConstants.LEFT);
55                         c.gridx = 0; c.gridy = y;
56                         c.insets = new Insets(10, 10, 10, 10);
57                         c.anchor = GridBagConstraints.WEST;
58                         layout.setConstraints(label, c);
59                         add(label);
60
61                         value = new JTextField(30);
62                         value.setFont(value_font);
63                         value.setHorizontalAlignment(SwingConstants.RIGHT);
64                         c.gridx = 1; c.gridy = y;
65                         c.anchor = GridBagConstraints.WEST;
66                         c.fill = GridBagConstraints.HORIZONTAL;
67                         layout.setConstraints(value, c);
68                         add(value);
69                 }
70         }
71
72         String pos(double p, String pos, String neg) {
73                 String  h = pos;
74                 if (p < 0) {
75                         h = neg;
76                         p = -p;
77                 }
78                 int deg = (int) Math.floor(p);
79                 double min = (p - Math.floor(p)) * 60.0;
80                 return String.format("%s %4d° %9.6f", h, deg, min);
81         }
82
83         class Lat extends LandedValue {
84                 void show (AltosState state, int crc_errors) {
85                         if (state.gps != null)
86                                 value.setText(pos(state.gps.lat,"N", "S"));
87                         else
88                                 value.setText("???");
89                 }
90                 public Lat (GridBagLayout layout, int y) {
91                         super (layout, y, "Latitude");
92                 }
93         }
94
95         Lat lat;
96
97         class Lon extends LandedValue {
98                 void show (AltosState state, int crc_errors) {
99                         if (state.gps != null)
100                                 value.setText(pos(state.gps.lon,"E", "W"));
101                         else
102                                 value.setText("???");
103                 }
104                 public Lon (GridBagLayout layout, int y) {
105                         super (layout, y, "Longitude");
106                 }
107         }
108
109         Lon lon;
110
111         class Bearing extends LandedValue {
112                 void show (AltosState state, int crc_errors) {
113                         if (state.from_pad != null)
114                                 show("%3.0f°", state.from_pad.bearing);
115                         else
116                                 value.setText("???");
117                 }
118                 public Bearing (GridBagLayout layout, int y) {
119                         super (layout, y, "Bearing");
120                 }
121         }
122
123         Bearing bearing;
124
125         class Distance extends LandedValue {
126                 void show (AltosState state, int crc_errors) {
127                         if (state.from_pad != null)
128                                 show("%6.0f m", state.from_pad.distance);
129                         else
130                                 value.setText("???");
131                 }
132                 public Distance (GridBagLayout layout, int y) {
133                         super (layout, y, "Distance");
134                 }
135         }
136
137         Distance distance;
138
139         class Height extends LandedValue {
140                 void show (AltosState state, int crc_errors) {
141                         show("%6.0f m", state.max_height);
142                 }
143                 public Height (GridBagLayout layout, int y) {
144                         super (layout, y, "Maximum Height");
145                 }
146         }
147
148         Height  height;
149
150         class Speed extends LandedValue {
151                 void show (AltosState state, int crc_errors) {
152                         show("%6.0f m/s", state.max_speed);
153                 }
154                 public Speed (GridBagLayout layout, int y) {
155                         super (layout, y, "Maximum Speed");
156                 }
157         }
158
159         Speed   speed;
160
161         class Accel extends LandedValue {
162                 void show (AltosState state, int crc_errors) {
163                         show("%6.0f m/s²", state.max_acceleration);
164                 }
165                 public Accel (GridBagLayout layout, int y) {
166                         super (layout, y, "Maximum Acceleration");
167                 }
168         }
169
170         Accel   accel;
171
172         public void reset() {
173                 lat.reset();
174                 lon.reset();
175                 bearing.reset();
176                 distance.reset();
177                 height.reset();
178                 speed.reset();
179                 accel.reset();
180         }
181
182         public void show(AltosState state, int crc_errors) {
183                 bearing.show(state, crc_errors);
184                 distance.show(state, crc_errors);
185                 lat.show(state, crc_errors);
186                 lon.show(state, crc_errors);
187                 height.show(state, crc_errors);
188                 speed.show(state, crc_errors);
189                 accel.show(state, crc_errors);
190         }
191
192         public AltosLanded() {
193                 layout = new GridBagLayout();
194
195                 label_font = new Font("Dialog", Font.PLAIN, 24);
196                 value_font = new Font("Monospaced", Font.PLAIN, 24);
197                 setLayout(layout);
198
199                 /* Elements in descent display */
200                 bearing = new Bearing(layout, 0);
201                 distance = new Distance(layout, 1);
202                 lat = new Lat(layout, 2);
203                 lon = new Lon(layout, 3);
204                 height = new Height(layout, 4);
205                 speed = new Speed(layout, 5);
206                 accel = new Accel(layout, 6);
207         }
208 }