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