AltosAscent/Descent: tidy up layout
[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 abstract class DescentStatus {
35                 JLabel          label;
36                 JTextField      value;
37                 AltosLights     lights;
38
39                 abstract void show(AltosState state, int crc_errors);
40                 void reset() {
41                         value.setText("");
42                         lights.set(false);
43                 }
44
45                 public DescentStatus (GridBagLayout layout, int y, String text) {
46                         GridBagConstraints      c = new GridBagConstraints();
47                         c.weighty = 1;
48
49                         lights = new AltosLights();
50                         c.gridx = 0; c.gridy = y;
51                         c.anchor = GridBagConstraints.CENTER;
52                         c.fill = GridBagConstraints.VERTICAL;
53                         c.weightx = 0;
54                         layout.setConstraints(lights, c);
55                         add(lights);
56
57                         label = new JLabel(text);
58                         label.setFont(Altos.label_font);
59                         label.setHorizontalAlignment(SwingConstants.LEFT);
60                         c.gridx = 1; c.gridy = y;
61                         c.insets = new Insets(Altos.tab_elt_pad, Altos.tab_elt_pad, Altos.tab_elt_pad, Altos.tab_elt_pad);
62                         c.anchor = GridBagConstraints.WEST;
63                         c.fill = GridBagConstraints.VERTICAL;
64                         c.weightx = 0;
65                         layout.setConstraints(label, c);
66                         add(label);
67
68                         value = new JTextField(15);
69                         value.setFont(Altos.value_font);
70                         value.setHorizontalAlignment(SwingConstants.RIGHT);
71                         c.gridx = 2; c.gridy = y;
72             c.gridwidth = 2;
73                         c.anchor = GridBagConstraints.WEST;
74                         c.fill = GridBagConstraints.BOTH;
75                         c.weightx = 1;
76                         layout.setConstraints(value, c);
77                         add(value);
78
79                 }
80         }
81
82         public abstract class DescentValue {
83                 JLabel          label;
84                 JTextField      value;
85
86                 void reset() {
87                         value.setText("");
88                 }
89
90                 abstract void show(AltosState state, int crc_errors);
91
92                 void show(String format, double v) {
93                         value.setText(String.format(format, v));
94                 }
95
96                 public DescentValue (GridBagLayout layout, int x, int y, String text) {
97                         GridBagConstraints      c = new GridBagConstraints();
98                         c.weighty = 1;
99
100                         label = new JLabel(text);
101                         label.setFont(Altos.label_font);
102                         label.setHorizontalAlignment(SwingConstants.LEFT);
103                         c.gridx = x + 1; c.gridy = y;
104                         c.insets = new Insets(Altos.tab_elt_pad, Altos.tab_elt_pad, Altos.tab_elt_pad, Altos.tab_elt_pad);
105                         c.anchor = GridBagConstraints.WEST;
106                         c.fill = GridBagConstraints.VERTICAL;
107                         c.weightx = 0;
108                         layout.setConstraints(label, c);
109                         add(label);
110
111                         value = new JTextField(17);
112                         value.setFont(Altos.value_font);
113                         value.setHorizontalAlignment(SwingConstants.RIGHT);
114                         c.gridx = x + 2; c.gridy = y;
115             c.gridwidth = 2;
116                         c.anchor = GridBagConstraints.WEST;
117                         c.fill = GridBagConstraints.BOTH;
118                         c.weightx = 1;
119                         layout.setConstraints(value, c);
120                         add(value);
121                 }
122         }
123
124         public abstract class DescentDualValue {
125                 JLabel          label;
126                 JTextField      value1;
127                 JTextField      value2;
128
129                 void reset() {
130                         value1.setText("");
131                         value2.setText("");
132                 }
133
134                 abstract void show(AltosState state, int crc_errors);
135                 void show(String v1, String v2) {
136             value1.setText(v1);
137             value2.setText(v2);
138         }
139                 void show(String f1, double v1, String f2, double v2) {
140                         value1.setText(String.format(f1, v1));
141                         value2.setText(String.format(f2, v2));
142                 }
143
144                 public DescentDualValue (GridBagLayout layout, int x, int y, String text) {
145                         GridBagConstraints      c = new GridBagConstraints();
146                         c.weighty = 1;
147
148                         label = new JLabel(text);
149                         label.setFont(Altos.label_font);
150                         label.setHorizontalAlignment(SwingConstants.LEFT);
151                         c.gridx = x + 1; c.gridy = y;
152                         c.insets = new Insets(Altos.tab_elt_pad, Altos.tab_elt_pad, Altos.tab_elt_pad, Altos.tab_elt_pad);
153                         c.anchor = GridBagConstraints.WEST;
154                         c.fill = GridBagConstraints.VERTICAL;
155                         c.weightx = 0;
156                         layout.setConstraints(label, c);
157                         add(label);
158
159                         value1 = new JTextField(17);
160                         value1.setFont(Altos.value_font);
161                         value1.setHorizontalAlignment(SwingConstants.RIGHT);
162                         c.gridx = x + 2; c.gridy = y;
163                         c.anchor = GridBagConstraints.WEST;
164                         c.fill = GridBagConstraints.BOTH;
165                         c.weightx = 1;
166                         layout.setConstraints(value1, c);
167                         add(value1);
168
169                         value2 = new JTextField(17);
170                         value2.setFont(Altos.value_font);
171                         value2.setHorizontalAlignment(SwingConstants.RIGHT);
172                         c.gridx = x + 3; c.gridy = y;
173                         c.anchor = GridBagConstraints.WEST;
174                         c.fill = GridBagConstraints.BOTH;
175                         c.weightx = 1;
176                         layout.setConstraints(value2, c);
177                         add(value2);
178                 }
179         }
180
181         class Height extends DescentDualValue {
182                 void show (AltosState state, int crc_errors) {
183                         show("%6.0f m", state.height,
184                                  "%3.0f°", state.elevation);
185                 }
186                 public Height (GridBagLayout layout, int x, int y) {
187                         super (layout, x, y, "Height/Elevation");
188                 }
189         }
190
191         Height  height;
192
193         class Speed extends DescentValue {
194                 void show (AltosState state, int crc_errors) {
195                         double speed = state.speed;
196                         if (!state.ascent)
197                                 speed = state.baro_speed;
198                         show("%6.0f m/s", speed);
199                 }
200                 public Speed (GridBagLayout layout, int x, int y) {
201                         super (layout, x, y, "Speed");
202                 }
203         }
204
205         Speed   speed;
206
207         String pos(double p, String pos, String neg) {
208                 String  h = pos;
209                 if (p < 0) {
210                         h = neg;
211                         p = -p;
212                 }
213                 int deg = (int) Math.floor(p);
214                 double min = (p - Math.floor(p)) * 60.0;
215                 return String.format("%s %4d° %9.6f", h, deg, min);
216         }
217
218         class LatLon extends DescentDualValue {
219                 void show (AltosState state, int crc_errors) {
220                         if (state.gps != null)
221                 show(pos(state.gps.lat,"N", "S"),
222                         pos(state.gps.lon,"W", "E"));
223                         else
224                                 show("???", "???");
225                 }
226                 public LatLon (GridBagLayout layout, int x, int y) {
227                         super (layout, x, y, "Lat/Long");
228                 }
229         }
230
231         LatLon latlon;
232
233         class Apogee extends DescentStatus {
234                 void show (AltosState state, int crc_errors) {
235                         value.setText(String.format("%4.2f V", state.drogue_sense));
236                         lights.set(state.drogue_sense > 3.2);
237                 }
238                 public Apogee (GridBagLayout layout, int y) {
239                         super(layout, y, "Apogee Igniter Voltage");
240                 }
241         }
242
243         Apogee apogee;
244
245         class Main extends DescentStatus {
246                 void show (AltosState state, int crc_errors) {
247                         value.setText(String.format("%4.2f V", state.main_sense));
248                         lights.set(state.main_sense > 3.2);
249                 }
250                 public Main (GridBagLayout layout, int y) {
251                         super(layout, y, "Main Igniter Voltage");
252                 }
253         }
254
255         Main main;
256
257         class Bearing extends DescentDualValue {
258                 void show (AltosState state, int crc_errors) {
259                         if (state.from_pad != null) {
260                 show( state.from_pad.bearing_words(
261                             AltosGreatCircle.BEARING_LONG),
262                                       String.format("%3.0f°", state.from_pad.bearing));
263                         } else {
264                                 show("???", "???");
265                         }
266                 }
267                 public Bearing (GridBagLayout layout, int x, int y) {
268                         super (layout, x, y, "Bearing");
269         }
270         }
271
272         Bearing bearing;
273
274         class Range extends DescentValue {
275                 void show (AltosState state, int crc_errors) {
276                         show("%6.0f m", state.range);
277                 }
278                 public Range (GridBagLayout layout, int x, int y) {
279                         super (layout, x, y, "Range");
280                 }
281         }
282
283         Range range;
284
285         public void reset() {
286                 latlon.reset();
287                 height.reset();
288                 speed.reset();
289                 bearing.reset();
290                 range.reset();
291                 main.reset();
292                 apogee.reset();
293         }
294
295         public void show(AltosState state, int crc_errors) {
296                 height.show(state, crc_errors);
297                 speed.show(state, crc_errors);
298                 bearing.show(state, crc_errors);
299                 range.show(state, crc_errors);
300                 latlon.show(state, crc_errors);
301                 main.show(state, crc_errors);
302                 apogee.show(state, crc_errors);
303         }
304
305         public AltosDescent() {
306                 layout = new GridBagLayout();
307
308                 setLayout(layout);
309
310                 /* Elements in descent display */
311         speed = new Speed(layout, 0, 0);    
312         height = new Height(layout, 0, 1);
313         range = new Range(layout, 0, 2);
314         bearing = new Bearing(layout, 0, 3);
315         latlon = new LatLon(layout, 0, 4);
316
317                 apogee = new Apogee(layout, 5);
318                 main = new Main(layout, 6);
319         }
320 }