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