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