altoslib: Improve EEprom download
[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; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with this program; if not, write to the Free Software Foundation, Inc.,
16  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
17  */
18
19 package altosui;
20
21 import java.awt.*;
22 import java.awt.event.*;
23 import javax.swing.*;
24 import java.io.*;
25 import org.altusmetrum.altoslib_12.*;
26 import org.altusmetrum.altosuilib_12.*;
27
28 public class AltosLanded extends AltosUIFlightTab implements ActionListener {
29
30         class Bearing extends AltosUIIndicator {
31                 public void show (AltosState state, AltosListenerState listener_state) {
32                         if (state.from_pad != null && state.from_pad.bearing != AltosLib.MISSING) {
33                                 show( String.format("%3.0f°", state.from_pad.bearing),
34                                       state.from_pad.bearing_words(
35                                               AltosGreatCircle.BEARING_LONG));
36                         } else {
37                                 show("Missing", "Missing");
38                         }
39                 }
40                 public Bearing (Container container, int y) {
41                         super (container, y, "Bearing", 2);
42                 }
43         }
44
45         class Distance extends AltosUIUnitsIndicator {
46                 public double value(AltosState state, int i) {
47                         if (state.from_pad != null)
48                                 return state.from_pad.distance;
49                         else
50                                 return AltosLib.MISSING;
51                 }
52
53                 public Distance(Container container, int y) {
54                         super(container, y, AltosConvert.distance, "Ground Distance", 2);
55                 }
56         }
57
58         class Lat extends AltosUIUnitsIndicator {
59
60                 public boolean hide (AltosState state, int i) { return state.gps == null || !state.gps.connected; }
61
62                 public double value(AltosState state, int i) {
63                         if (state.gps == null)
64                                 return AltosLib.MISSING;
65                         if (!state.gps.connected)
66                                 return AltosLib.MISSING;
67                         return state.gps.lat;
68                 }
69
70                 public Lat (Container container, int y) {
71                         super (container, y, AltosConvert.latitude, "Latitude", 2);
72                 }
73         }
74
75         class Lon extends AltosUIUnitsIndicator {
76                 public boolean hide (AltosState state, int i) { return state.gps == null || !state.gps.connected; }
77
78                 public double value(AltosState state, int i) {
79                         if (state.gps == null)
80                                 return AltosLib.MISSING;
81                         if (!state.gps.connected)
82                                 return AltosLib.MISSING;
83                         return state.gps.lon;
84                 }
85
86                 public Lon (Container container, int y) {
87                         super (container, y, AltosConvert.longitude, "Longitude", 2);
88                 }
89         }
90
91         class MaxHeight extends AltosUIUnitsIndicator {
92                 public double value(AltosState state, int i) { return state.max_height(); }
93
94                 public MaxHeight (Container container, int y) {
95                         super (container, y, AltosConvert.height, "Maximum Height", 2);
96                 }
97         }
98
99         class MaxSpeed extends AltosUIUnitsIndicator {
100                 public double value(AltosState state, int i) { return state.max_speed(); }
101
102                 public MaxSpeed (Container container, int y) {
103                         super (container, y, AltosConvert.speed, "Maximum Speed", 2);
104                 }
105         }
106
107         class MaxAccel extends AltosUIUnitsIndicator {
108                 public double value(AltosState state, int i) { return state.max_acceleration(); }
109
110                 public MaxAccel (Container container, int y) {
111                         super (container, y, AltosConvert.speed, "Maximum acceleration", 2);
112                 }
113         }
114
115         JButton graph;
116         AltosFlightReader reader;
117
118         public void actionPerformed(ActionEvent e) {
119                 String  cmd = e.getActionCommand();
120
121                 if (cmd.equals("graph")) {
122                         File    file = reader.backing_file();
123                         if (file != null) {
124                                 String  filename = file.getName();
125                                 try {
126                                         AltosRecordSet record_set = null;
127                                         FileInputStream in = new FileInputStream(file);
128                                         if (filename.endsWith("eeprom")) {
129                                                 record_set = new AltosEepromRecordSet(in);
130                                         } else if (filename.endsWith("telem")) {
131                                                 record_set = new AltosTelemetryFile(in);
132                                         } else {
133                                                 throw new FileNotFoundException(filename);
134                                         }
135                                         try {
136                                                 new AltosGraphUI(record_set, file);
137                                         } catch (InterruptedException ie) {
138                                         } catch (IOException ie) {
139                                         }
140                                 } catch (FileNotFoundException fe) {
141                                         JOptionPane.showMessageDialog(null,
142                                                                       fe.getMessage(),
143                                                                       "Cannot open file",
144                                                                       JOptionPane.ERROR_MESSAGE);
145                                 } catch (IOException ie) {
146                                         JOptionPane.showMessageDialog(null,
147                                                                       ie.getMessage(),
148                                                                       "Error reading file file",
149                                                                       JOptionPane.ERROR_MESSAGE);
150                                 }
151                         }
152                 }
153         }
154
155         public String getName() {
156                 return "Landed";
157         }
158
159         public void show(AltosState state, AltosListenerState listener_state) {
160                 super.show(state, listener_state);
161                 if (reader.backing_file() != null)
162                         graph.setEnabled(true);
163         }
164
165         public AltosLanded(AltosFlightReader in_reader) {
166                 reader = in_reader;
167
168                 /* Elements in descent display */
169                 add(new Bearing(this, 0));
170                 add(new Distance(this, 1));
171                 add(new Lat(this, 2));
172                 add(new Lon(this, 3));
173                 add(new MaxHeight(this, 4));
174                 add(new MaxSpeed(this, 5));
175                 add(new MaxAccel(this, 6));
176
177                 graph = new JButton ("Graph Flight");
178                 graph.setActionCommand("graph");
179                 graph.addActionListener(this);
180                 graph.setEnabled(false);
181
182                 GridBagConstraints      c = new GridBagConstraints();
183
184                 c.gridx = 1; c.gridy = 7;
185                 c.insets = new Insets(10, 10, 10, 10);
186                 c.anchor = GridBagConstraints.WEST;
187                 c.weightx = 0;
188                 c.weighty = 0;
189                 c.fill = GridBagConstraints.VERTICAL;
190                 add(graph, c);
191                 addHierarchyListener(this);
192         }
193 }