ceb78e57dbbc6254c5f13c2aceeae3afc9dbe2fc
[fw/altos] / ao-tools / altosui / AltosDescent.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 AltosDescent extends JComponent implements AltosFlightDisplay {
32         GridBagLayout   layout;
33
34         public class DescentValue {
35                 JLabel          label;
36                 JTextField      value;
37                 void show(AltosState state, int crc_errors) {}
38
39                 void reset() {
40                         value.setText("");
41                 }
42
43                 void show(String format, double v) {
44                         value.setText(String.format(format, v));
45                 }
46
47                 public DescentValue (GridBagLayout layout, int y, String text) {
48                         GridBagConstraints      c = new GridBagConstraints();
49                         c.weighty = 1;
50
51                         label = new JLabel(text);
52                         label.setFont(Altos.label_font);
53                         label.setHorizontalAlignment(SwingConstants.LEFT);
54                         c.gridx = 0; c.gridy = y;
55                         c.insets = new Insets(Altos.tab_elt_pad, Altos.tab_elt_pad, Altos.tab_elt_pad, Altos.tab_elt_pad);
56                         c.anchor = GridBagConstraints.WEST;
57                         c.fill = GridBagConstraints.VERTICAL;
58                         c.weightx = 0;
59                         layout.setConstraints(label, c);
60                         add(label);
61
62                         value = new JTextField(30);
63                         value.setFont(Altos.value_font);
64                         value.setHorizontalAlignment(SwingConstants.RIGHT);
65                         c.gridx = 1; c.gridy = y;
66                         c.gridwidth = 2;
67                         c.anchor = GridBagConstraints.WEST;
68                         c.fill = GridBagConstraints.BOTH;
69                         c.weightx = 1;
70                         layout.setConstraints(value, c);
71                         add(value);
72                 }
73         }
74
75         class Height extends DescentValue {
76                 void show (AltosState state, int crc_errors) {
77                         show("%6.0f m", state.height);
78                 }
79                 public Height (GridBagLayout layout, int y) {
80                         super (layout, y, "Height");
81                 }
82         }
83
84         Height  height;
85
86         class Speed extends DescentValue {
87                 void show (AltosState state, int crc_errors) {
88                         double speed = state.speed;
89                         if (!state.ascent)
90                                 speed = state.baro_speed;
91                         show("%6.0f m/s", speed);
92                 }
93                 public Speed (GridBagLayout layout, int y) {
94                         super (layout, y, "Speed");
95                 }
96         }
97
98         Speed   speed;
99
100         String pos(double p, String pos, String neg) {
101                 String  h = pos;
102                 if (p < 0) {
103                         h = neg;
104                         p = -p;
105                 }
106                 int deg = (int) Math.floor(p);
107                 double min = (p - Math.floor(p)) * 60.0;
108                 return String.format("%s %4d° %9.6f", h, deg, min);
109         }
110
111         class Lat extends DescentValue {
112                 void show (AltosState state, int crc_errors) {
113                         if (state.gps != null)
114                                 value.setText(pos(state.gps.lat,"N", "S"));
115                         else
116                                 value.setText("???");
117                 }
118                 public Lat (GridBagLayout layout, int y) {
119                         super (layout, y, "Latitude");
120                 }
121         }
122
123         Lat lat;
124
125         class Lon extends DescentValue {
126                 void show (AltosState state, int crc_errors) {
127                         if (state.gps != null)
128                                 value.setText(pos(state.gps.lon,"E", "W"));
129                         else
130                                 value.setText("???");
131                 }
132                 public Lon (GridBagLayout layout, int y) {
133                         super (layout, y, "Longitude");
134                 }
135         }
136
137         Lon lon;
138
139         class Bearing {
140                 JLabel          label;
141                 JTextField      value;
142                 JTextField      value_deg;
143                 void reset () {
144                         value.setText("");
145                         value_deg.setText("");
146                 }
147                 void show (AltosState state, int crc_errors) {
148                         if (state.from_pad != null) {
149                                 value.setText(state.from_pad.bearing_words(
150                                                       AltosGreatCircle.BEARING_LONG));
151                                 value_deg.setText(String.format("%3.0f°", state.from_pad.bearing));
152                         } else {
153                                 value.setText("???");
154                                 value_deg.setText("???");
155                         }
156                 }
157                 public Bearing (GridBagLayout layout, int y) {
158                         GridBagConstraints      c = new GridBagConstraints();
159                         c.weighty = 1;
160
161                         label = new JLabel("Bearing");
162                         label.setFont(Altos.label_font);
163                         label.setHorizontalAlignment(SwingConstants.LEFT);
164                         c.gridx = 0; c.gridy = y;
165                         c.insets = new Insets(Altos.tab_elt_pad, Altos.tab_elt_pad, Altos.tab_elt_pad, Altos.tab_elt_pad);
166                         c.anchor = GridBagConstraints.WEST;
167                         c.weightx = 0;
168                         c.fill = GridBagConstraints.VERTICAL;
169                         layout.setConstraints(label, c);
170                         add(label);
171
172                         value = new JTextField(30);
173                         value.setFont(Altos.value_font);
174                         value.setHorizontalAlignment(SwingConstants.RIGHT);
175                         c.gridx = 1; c.gridy = y;
176                         c.anchor = GridBagConstraints.EAST;
177                         c.weightx = 1;
178                         c.fill = GridBagConstraints.BOTH;
179                         layout.setConstraints(value, c);
180                         add(value);
181
182                         value_deg = new JTextField(5);
183                         value_deg.setFont(Altos.value_font);
184                         value_deg.setHorizontalAlignment(SwingConstants.RIGHT);
185                         c.gridx = 2; c.gridy = y;
186                         c.anchor = GridBagConstraints.EAST;
187                         c.weightx = 1;
188                         c.fill = GridBagConstraints.BOTH;
189                         layout.setConstraints(value_deg, c);
190                         add(value_deg);
191                 }
192         }
193
194         Bearing bearing;
195
196         class Elevation extends DescentValue {
197                 void show (AltosState state, int crc_errors) {
198                         if (state.from_pad != null)
199                                 show("%3.0f°", state.elevation);
200                         else
201                                 value.setText("???");
202                 }
203                 public Elevation (GridBagLayout layout, int y) {
204                         super (layout, y, "Elevation");
205                 }
206         }
207
208         Elevation elevation;
209
210         class Range extends DescentValue {
211                 void show (AltosState state, int crc_errors) {
212                         show("%6.0f m", state.range);
213                 }
214                 public Range (GridBagLayout layout, int y) {
215                         super (layout, y, "Range");
216                 }
217         }
218
219         Range range;
220
221         public void reset() {
222                 lat.reset();
223                 lon.reset();
224                 height.reset();
225                 speed.reset();
226                 bearing.reset();
227                 elevation.reset();
228                 range.reset();
229         }
230
231         public void show(AltosState state, int crc_errors) {
232                 height.show(state, crc_errors);
233                 speed.show(state, crc_errors);
234                 bearing.show(state, crc_errors);
235                 elevation.show(state, crc_errors);
236                 range.show(state, crc_errors);
237                 lat.show(state, crc_errors);
238                 lon.show(state, crc_errors);
239         }
240
241         public AltosDescent() {
242                 layout = new GridBagLayout();
243
244                 setLayout(layout);
245
246                 /* Elements in descent display */
247                 speed = new Speed(layout, 0);
248                 height = new Height(layout, 1);
249                 bearing = new Bearing(layout, 2);
250                 elevation = new Elevation(layout, 3);
251                 range = new Range(layout, 4);
252                 lat = new Lat(layout, 5);
253                 lon = new Lon(layout, 6);
254         }
255 }