Merge remote-tracking branch 'origin/master'
[fw/altos] / 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 javax.swing.*;
22 import org.altusmetrum.altoslib_2.*;
23
24 public class AltosDescent extends JComponent implements AltosFlightDisplay {
25         GridBagLayout   layout;
26
27         public abstract class DescentStatus {
28                 JLabel          label;
29                 JTextField      value;
30                 AltosLights     lights;
31
32                 abstract void show(AltosState state, AltosListenerState listener_state);
33
34                 void show() {
35                         label.setVisible(true);
36                         value.setVisible(true);
37                         lights.setVisible(true);
38                 }
39
40                 void show(String s) {
41                         show();
42                         value.setText(s);
43                 }
44
45                 void show(String format, double value) {
46                         show(String.format(format, value));
47                 }
48
49                 void hide() {
50                         label.setVisible(false);
51                         value.setVisible(false);
52                         lights.setVisible(false);
53                 }
54
55                 void reset() {
56                         value.setText("");
57                         lights.set(false);
58                 }
59
60                 void set_font() {
61                         label.setFont(Altos.label_font);
62                         value.setFont(Altos.value_font);
63                 }
64
65                 public DescentStatus (GridBagLayout layout, int y, String text) {
66                         GridBagConstraints      c = new GridBagConstraints();
67                         c.weighty = 1;
68
69                         lights = new AltosLights();
70                         c.gridx = 0; c.gridy = y;
71                         c.anchor = GridBagConstraints.CENTER;
72                         c.fill = GridBagConstraints.VERTICAL;
73                         c.weightx = 0;
74                         layout.setConstraints(lights, c);
75                         add(lights);
76
77                         label = new JLabel(text);
78                         label.setFont(Altos.label_font);
79                         label.setHorizontalAlignment(SwingConstants.LEFT);
80                         c.gridx = 1; c.gridy = y;
81                         c.insets = new Insets(Altos.tab_elt_pad, Altos.tab_elt_pad, Altos.tab_elt_pad, Altos.tab_elt_pad);
82                         c.anchor = GridBagConstraints.WEST;
83                         c.fill = GridBagConstraints.VERTICAL;
84                         c.gridwidth = 3;
85                         c.weightx = 0;
86                         layout.setConstraints(label, c);
87                         add(label);
88
89                         value = new JTextField(Altos.text_width);
90                         value.setFont(Altos.value_font);
91                         value.setHorizontalAlignment(SwingConstants.RIGHT);
92                         c.gridx = 4; c.gridy = y;
93                         c.gridwidth = 1;
94                         c.anchor = GridBagConstraints.WEST;
95                         c.fill = GridBagConstraints.BOTH;
96                         c.weightx = 1;
97                         layout.setConstraints(value, c);
98                         add(value);
99
100                 }
101         }
102
103         public abstract class DescentValue {
104                 JLabel          label;
105                 JTextField      value;
106
107                 void reset() {
108                         value.setText("");
109                 }
110
111                 abstract void show(AltosState state, AltosListenerState listener_state);
112
113                 void show() {
114                         label.setVisible(true);
115                         value.setVisible(true);
116                 }
117
118                 void hide() {
119                         label.setVisible(false);
120                         value.setVisible(false);
121                 }
122
123                 void show(String v) {
124                         show();
125                         value.setText(v);
126                 }
127
128                 void show(AltosUnits units, double v) {
129                         show(units.show(8, v));
130                 }
131
132                 void show(String format, double v) {
133                         show(String.format(format, v));
134                 }
135
136                 void set_font() {
137                         label.setFont(Altos.label_font);
138                         value.setFont(Altos.value_font);
139                 }
140
141                 public DescentValue (GridBagLayout layout, int x, int y, String text) {
142                         GridBagConstraints      c = new GridBagConstraints();
143                         c.weighty = 1;
144
145                         label = new JLabel(text);
146                         label.setFont(Altos.label_font);
147                         label.setHorizontalAlignment(SwingConstants.LEFT);
148                         c.gridx = x + 1; c.gridy = y;
149                         c.insets = new Insets(Altos.tab_elt_pad, Altos.tab_elt_pad, Altos.tab_elt_pad, Altos.tab_elt_pad);
150                         c.anchor = GridBagConstraints.WEST;
151                         c.fill = GridBagConstraints.VERTICAL;
152                         c.weightx = 0;
153                         add(label, c);
154
155                         value = new JTextField(Altos.text_width);
156                         value.setFont(Altos.value_font);
157                         value.setHorizontalAlignment(SwingConstants.RIGHT);
158                         c.gridx = x + 2; c.gridy = y;
159                         c.gridwidth = 1;
160                         c.anchor = GridBagConstraints.WEST;
161                         c.fill = GridBagConstraints.BOTH;
162                         c.weightx = 1;
163                         add(value, c);
164                 }
165         }
166
167         public abstract class DescentDualValue {
168                 JLabel          label;
169                 JTextField      value1;
170                 JTextField      value2;
171
172                 void reset() {
173                         value1.setText("");
174                         value2.setText("");
175                 }
176
177                 void show() {
178                         label.setVisible(true);
179                         value1.setVisible(true);
180                         value2.setVisible(true);
181                 }
182
183                 void hide() {
184                         label.setVisible(false);
185                         value1.setVisible(false);
186                         value2.setVisible(false);
187                 }
188
189                 void set_font() {
190                         label.setFont(Altos.label_font);
191                         value1.setFont(Altos.value_font);
192                         value2.setFont(Altos.value_font);
193                 }
194
195                 abstract void show(AltosState state, AltosListenerState listener_state);
196
197                 void show(String v1, String v2) {
198                         show();
199                         value1.setText(v1);
200                         value2.setText(v2);
201                 }
202                 void show(String f1, double v1, String f2, double v2) {
203                         show();
204                         value1.setText(String.format(f1, v1));
205                         value2.setText(String.format(f2, v2));
206                 }
207
208                 public DescentDualValue (GridBagLayout layout, int x, int y, String text) {
209                         GridBagConstraints      c = new GridBagConstraints();
210                         c.weighty = 1;
211
212                         label = new JLabel(text);
213                         label.setFont(Altos.label_font);
214                         label.setHorizontalAlignment(SwingConstants.LEFT);
215                         c.gridx = x + 1; c.gridy = y;
216                         c.insets = new Insets(Altos.tab_elt_pad, Altos.tab_elt_pad, Altos.tab_elt_pad, Altos.tab_elt_pad);
217                         c.anchor = GridBagConstraints.WEST;
218                         c.fill = GridBagConstraints.VERTICAL;
219                         c.weightx = 0;
220                         layout.setConstraints(label, c);
221                         add(label);
222
223                         value1 = new JTextField(Altos.text_width);
224                         value1.setFont(Altos.value_font);
225                         value1.setHorizontalAlignment(SwingConstants.RIGHT);
226                         c.gridx = x + 2; c.gridy = y;
227                         c.anchor = GridBagConstraints.WEST;
228                         c.fill = GridBagConstraints.BOTH;
229                         c.weightx = 1;
230                         layout.setConstraints(value1, c);
231                         add(value1);
232
233                         value2 = new JTextField(Altos.text_width);
234                         value2.setFont(Altos.value_font);
235                         value2.setHorizontalAlignment(SwingConstants.RIGHT);
236                         c.gridx = x + 4; c.gridy = y;
237                         c.anchor = GridBagConstraints.WEST;
238                         c.fill = GridBagConstraints.BOTH;
239                         c.weightx = 1;
240                         c.gridwidth = 1;
241                         layout.setConstraints(value2, c);
242                         add(value2);
243                 }
244         }
245
246         class Height extends DescentValue {
247                 void show (AltosState state, AltosListenerState listener_state) {
248                         show(AltosConvert.height, state.height());
249                 }
250                 public Height (GridBagLayout layout, int x, int y) {
251                         super (layout, x, y, "Height");
252                 }
253         }
254
255         Height  height;
256
257         class Speed extends DescentValue {
258                 void show (AltosState state, AltosListenerState listener_state) {
259                         show(AltosConvert.speed, state.speed());
260                 }
261                 public Speed (GridBagLayout layout, int x, int y) {
262                         super (layout, x, y, "Speed");
263                 }
264         }
265
266         Speed   speed;
267
268         String pos(double p, String pos, String neg) {
269                 String  h = pos;
270                 if (p < 0) {
271                         h = neg;
272                         p = -p;
273                 }
274                 int deg = (int) Math.floor(p);
275                 double min = (p - Math.floor(p)) * 60.0;
276                 return String.format("%s %d° %9.6f", h, deg, min);
277         }
278
279         class Lat extends DescentValue {
280                 void show (AltosState state, AltosListenerState listener_state) {
281                         if (state.gps != null && state.gps.connected && state.gps.lat != AltosLib.MISSING)
282                                 show(pos(state.gps.lat,"N", "S"));
283                         else
284                                 show("???");
285                 }
286                 public Lat (GridBagLayout layout, int x, int y) {
287                         super (layout, x, y, "Latitude");
288                 }
289         }
290
291         Lat lat;
292
293         class Lon extends DescentValue {
294                 void show (AltosState state, AltosListenerState listener_state) {
295                         if (state.gps != null && state.gps.connected && state.gps.lon != AltosLib.MISSING)
296                                 show(pos(state.gps.lon,"W", "E"));
297                         else
298                                 show("???");
299                 }
300                 public Lon (GridBagLayout layout, int x, int y) {
301                         super (layout, x, y, "Longitude");
302                 }
303         }
304
305         Lon lon;
306
307         class Distance extends DescentValue {
308                 void show(AltosState state, AltosListenerState listener_state) {
309                         if (state.from_pad != null)
310                                 show(AltosConvert.distance, state.from_pad.distance);
311                         else
312                                 show("???");
313                 }
314
315                 public Distance (GridBagLayout layout, int x, int y) {
316                         super(layout, x, y, "Ground Distance");
317                 }
318         }
319
320         Distance distance;
321                 
322
323         class Apogee extends DescentStatus {
324                 void show (AltosState state, AltosListenerState listener_state) {
325                         show("%4.2f V", state.apogee_voltage);
326                         lights.set(state.apogee_voltage > 3.7);
327                 }
328                 public Apogee (GridBagLayout layout, int y) {
329                         super(layout, y, "Apogee Igniter Voltage");
330                 }
331         }
332
333         Apogee apogee;
334
335         class Main extends DescentStatus {
336                 void show (AltosState state, AltosListenerState listener_state) {
337                         show("%4.2f V", state.main_voltage);
338                         lights.set(state.main_voltage > 3.7);
339                 }
340                 public Main (GridBagLayout layout, int y) {
341                         super(layout, y, "Main Igniter Voltage");
342                 }
343         }
344
345         Main main;
346
347         class Bearing extends DescentDualValue {
348                 void show (AltosState state, AltosListenerState listener_state) {
349                         if (state.from_pad != null) {
350                                 show( String.format("%3.0f°", state.from_pad.bearing),
351                                       state.from_pad.bearing_words(
352                                               AltosGreatCircle.BEARING_LONG));
353                         } else {
354                                 show("???", "???");
355                         }
356                 }
357                 public Bearing (GridBagLayout layout, int x, int y) {
358                         super (layout, x, y, "Bearing");
359                 }
360         }
361
362         Bearing bearing;
363
364         class Range extends DescentValue {
365                 void show (AltosState state, AltosListenerState listener_state) {
366                         show(AltosConvert.distance, state.range);
367                 }
368                 public Range (GridBagLayout layout, int x, int y) {
369                         super (layout, x, y, "Range");
370                 }
371         }
372
373         Range range;
374
375         class Elevation extends DescentValue {
376                 void show (AltosState state, AltosListenerState listener_state) {
377                         show("%3.0f°", state.elevation);
378                 }
379                 public Elevation (GridBagLayout layout, int x, int y) {
380                         super (layout, x, y, "Elevation");
381                 }
382         }
383
384         Elevation elevation;
385
386         public void reset() {
387                 lat.reset();
388                 lon.reset();
389                 height.reset();
390                 speed.reset();
391                 bearing.reset();
392                 range.reset();
393                 distance.reset();
394                 elevation.reset();
395                 main.reset();
396                 apogee.reset();
397         }
398
399         public void set_font() {
400                 lat.set_font();
401                 lon.set_font();
402                 height.set_font();
403                 speed.set_font();
404                 bearing.set_font();
405                 range.set_font();
406                 distance.set_font();
407                 elevation.set_font();
408                 main.set_font();
409                 apogee.set_font();
410         }
411
412         public void show(AltosState state, AltosListenerState listener_state) {
413                 height.show(state, listener_state);
414                 speed.show(state, listener_state);
415                 if (state.gps != null && state.gps.connected) {
416                         bearing.show(state, listener_state);
417                         range.show(state, listener_state);
418                         distance.show(state, listener_state);
419                         elevation.show(state, listener_state);
420                         lat.show(state, listener_state);
421                         lon.show(state, listener_state);
422                 } else {
423                         bearing.hide();
424                         range.hide();
425                         distance.hide();
426                         elevation.hide();
427                         lat.hide();
428                         lon.hide();
429                 }
430                 if (state.main_voltage != AltosLib.MISSING)
431                         main.show(state, listener_state);
432                 else
433                         main.hide();
434                 if (state.apogee_voltage != AltosLib.MISSING)
435                         apogee.show(state, listener_state);
436                 else
437                         apogee.hide();
438         }
439
440         public String getName() {
441                 return "Descent";
442         }
443
444         public AltosDescent() {
445                 layout = new GridBagLayout();
446
447                 setLayout(layout);
448
449                 /* Elements in descent display */
450                 speed = new Speed(layout, 0, 0);
451                 height = new Height(layout, 2, 0);
452                 elevation = new Elevation(layout, 0, 1);
453                 range = new Range(layout, 2, 1);
454                 bearing = new Bearing(layout, 0, 2);
455                 distance = new Distance(layout, 0, 3);
456                 lat = new Lat(layout, 0, 4);
457                 lon = new Lon(layout, 2, 4);
458
459                 apogee = new Apogee(layout, 5);
460                 main = new Main(layout, 6);
461         }
462 }