altosui: Fix Landed tab units
[fw/altos] / altosui / AltosLanded.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 import org.altusmetrum.AltosLib.*;
31
32 public class AltosLanded extends JComponent implements AltosFlightDisplay, ActionListener {
33         GridBagLayout   layout;
34
35         public class LandedValue {
36                 JLabel          label;
37                 JTextField      value;
38                 void show(AltosState state, int crc_errors) {}
39
40                 void reset() {
41                         value.setText("");
42                 }
43
44                 void show() {
45                         label.setVisible(true);
46                         value.setVisible(true);
47                 }
48
49                 void show(String s) {
50                         show();
51                         value.setText(s);
52                 }
53                 
54                 void show(AltosUnits units, double v) {
55                         show(units.show(8, v));
56                 }
57
58                 void show(String format, double v) {
59                         show(String.format(format, v));
60                 }
61
62                 public void set_font() {
63                         label.setFont(Altos.label_font);
64                         value.setFont(Altos.value_font);
65                 }
66
67                 void hide() {
68                         label.setVisible(false);
69                         value.setVisible(false);
70                 }
71
72                 public LandedValue (GridBagLayout layout, int y, String text) {
73                         GridBagConstraints      c = new GridBagConstraints();
74                         c.weighty = 1;
75
76                         label = new JLabel(text);
77                         label.setFont(Altos.label_font);
78                         label.setHorizontalAlignment(SwingConstants.LEFT);
79                         c.gridx = 0; c.gridy = y;
80                         c.insets = new Insets(10, 10, 10, 10);
81                         c.anchor = GridBagConstraints.WEST;
82                         c.weightx = 0;
83                         c.fill = GridBagConstraints.VERTICAL;
84                         layout.setConstraints(label, c);
85                         add(label);
86
87                         value = new JTextField(Altos.text_width);
88                         value.setFont(Altos.value_font);
89                         value.setHorizontalAlignment(SwingConstants.RIGHT);
90                         c.gridx = 1; c.gridy = y;
91                         c.anchor = GridBagConstraints.WEST;
92                         c.weightx = 1;
93                         c.fill = GridBagConstraints.BOTH;
94                         layout.setConstraints(value, c);
95                         add(value);
96                 }
97         }
98
99         String pos(double p, String pos, String neg) {
100                 String  h = pos;
101                 if (p < 0) {
102                         h = neg;
103                         p = -p;
104                 }
105                 int deg = (int) Math.floor(p);
106                 double min = (p - Math.floor(p)) * 60.0;
107                 return String.format("%s %4d° %9.6f", h, deg, min);
108         }
109
110         class Lat extends LandedValue {
111                 void show (AltosState state, int crc_errors) {
112                         if (state.gps != null && state.gps.connected)
113                                 show(pos(state.gps.lat,"N", "S"));
114                         else
115                                 show("???");
116                 }
117                 public Lat (GridBagLayout layout, int y) {
118                         super (layout, y, "Latitude");
119                 }
120         }
121
122         Lat lat;
123
124         class Lon extends LandedValue {
125                 void show (AltosState state, int crc_errors) {
126                         show();
127                         if (state.gps != null && state.gps.connected)
128                                 show(pos(state.gps.lon,"E", "W"));
129                         else
130                                 show("???");
131                 }
132                 public Lon (GridBagLayout layout, int y) {
133                         super (layout, y, "Longitude");
134                 }
135         }
136
137         Lon lon;
138
139         class Bearing extends LandedValue {
140                 void show (AltosState state, int crc_errors) {
141                         show();
142                         if (state.from_pad != null)
143                                 show("%3.0f°", state.from_pad.bearing);
144                         else
145                                 show("???");
146                 }
147                 public Bearing (GridBagLayout layout, int y) {
148                         super (layout, y, "Bearing");
149                 }
150         }
151
152         Bearing bearing;
153
154         class Distance extends LandedValue {
155                 void show (AltosState state, int crc_errors) {
156                         show();
157                         if (state.from_pad != null)
158                                 show(AltosConvert.distance, state.from_pad.distance);
159                         else
160                                 show("???");
161                 }
162                 public Distance (GridBagLayout layout, int y) {
163                         super (layout, y, "Distance");
164                 }
165         }
166
167         Distance distance;
168
169         class Height extends LandedValue {
170                 void show (AltosState state, int crc_errors) {
171                         show(AltosConvert.height, state.max_height);
172                 }
173                 public Height (GridBagLayout layout, int y) {
174                         super (layout, y, "Maximum Height");
175                 }
176         }
177
178         Height  height;
179
180         class Speed extends LandedValue {
181                 void show (AltosState state, int crc_errors) {
182                         show(AltosConvert.speed, state.max_speed);
183                 }
184                 public Speed (GridBagLayout layout, int y) {
185                         super (layout, y, "Maximum Speed");
186                 }
187         }
188
189         Speed   speed;
190
191         class Accel extends LandedValue {
192                 void show (AltosState state, int crc_errors) {
193                         show(AltosConvert.accel, state.max_acceleration);
194                 }
195                 public Accel (GridBagLayout layout, int y) {
196                         super (layout, y, "Maximum Acceleration");
197                 }
198         }
199
200         Accel   accel;
201
202         public void reset() {
203                 lat.reset();
204                 lon.reset();
205                 bearing.reset();
206                 distance.reset();
207                 height.reset();
208                 speed.reset();
209                 accel.reset();
210         }
211
212         public void set_font() {
213                 lat.set_font();
214                 lon.set_font();
215                 bearing.set_font();
216                 distance.set_font();
217                 height.set_font();
218                 speed.set_font();
219                 accel.set_font();
220         }
221
222         public void show(AltosState state, int crc_errors) {
223                 if (state.gps != null && state.gps.connected) {
224                         bearing.show(state, crc_errors);
225                         distance.show(state, crc_errors);
226                         lat.show(state, crc_errors);
227                         lon.show(state, crc_errors);
228                 } else {
229                         bearing.hide();
230                         distance.hide();
231                         lat.hide();
232                         lon.hide();
233                 }
234                 height.show(state, crc_errors);
235                 speed.show(state, crc_errors);
236                 accel.show(state, crc_errors);
237                 if (reader.backing_file() != null)
238                         graph.setEnabled(true);
239         }
240
241         JButton graph;
242         AltosFlightReader reader;
243
244         public void actionPerformed(ActionEvent e) {
245                 String  cmd = e.getActionCommand();
246
247                 if (cmd.equals("graph")) {
248                         File    file = reader.backing_file();
249                         if (file != null) {
250                                 String  filename = file.getName();
251                                 try {
252                                         AltosRecordIterable records = null;
253                                         if (filename.endsWith("eeprom")) {
254                                                 FileInputStream in = new FileInputStream(file);
255                                                 records = new AltosEepromIterable(in);
256                                         } else if (filename.endsWith("telem")) {
257                                                 FileInputStream in = new FileInputStream(file);
258                                                 records = new AltosTelemetryIterable(in);
259                                         } else {
260                                                 throw new FileNotFoundException(filename);
261                                         }
262                                         try {
263                                                 new AltosGraphUI(records, filename);
264                                         } catch (InterruptedException ie) {
265                                         } catch (IOException ie) {
266                                         }
267                                 } catch (FileNotFoundException fe) {
268                                         JOptionPane.showMessageDialog(null,
269                                                                       fe.getMessage(),
270                                                                       "Cannot open file",
271                                                                       JOptionPane.ERROR_MESSAGE);
272                                 }
273                         }
274                 }
275         }
276
277         public AltosLanded(AltosFlightReader in_reader) {
278                 layout = new GridBagLayout();
279
280                 reader = in_reader;
281
282                 setLayout(layout);
283
284                 /* Elements in descent display */
285                 bearing = new Bearing(layout, 0);
286                 distance = new Distance(layout, 1);
287                 lat = new Lat(layout, 2);
288                 lon = new Lon(layout, 3);
289                 height = new Height(layout, 4);
290                 speed = new Speed(layout, 5);
291                 accel = new Accel(layout, 6);
292
293                 graph = new JButton ("Graph Flight");
294                 graph.setActionCommand("graph");
295                 graph.addActionListener(this);
296                 graph.setEnabled(false);
297
298                 GridBagConstraints      c = new GridBagConstraints();
299
300                 c.gridx = 0; c.gridy = 7;
301                 c.insets = new Insets(10, 10, 10, 10);
302                 c.anchor = GridBagConstraints.WEST;
303                 c.weightx = 0;
304                 c.weighty = 0;
305                 c.fill = GridBagConstraints.VERTICAL;
306                 add(graph, c);
307         }
308 }