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