altoslib: Add a 'seconds' method to AltosGPS
[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_11.*;
26 import org.altusmetrum.altosuilib_11.*;
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                                         AltosStateIterable states = null;
127                                         if (filename.endsWith("eeprom")) {
128                                                 FileInputStream in = new FileInputStream(file);
129                                                 states = new AltosEepromFile(in);
130                                         } else if (filename.endsWith("telem")) {
131                                                 FileInputStream in = new FileInputStream(file);
132                                                 states = new AltosTelemetryFile(in);
133                                         } else {
134                                                 throw new FileNotFoundException(filename);
135                                         }
136                                         try {
137                                                 new AltosGraphUI(states, file);
138                                         } catch (InterruptedException ie) {
139                                         } catch (IOException ie) {
140                                         }
141                                 } catch (FileNotFoundException fe) {
142                                         JOptionPane.showMessageDialog(null,
143                                                                       fe.getMessage(),
144                                                                       "Cannot open file",
145                                                                       JOptionPane.ERROR_MESSAGE);
146                                 }
147                         }
148                 }
149         }
150
151         public String getName() {
152                 return "Landed";
153         }
154
155         public void show(AltosState state, AltosListenerState listener_state) {
156                 super.show(state, listener_state);
157                 if (reader.backing_file() != null)
158                         graph.setEnabled(true);
159         }
160
161         public AltosLanded(AltosFlightReader in_reader) {
162                 reader = in_reader;
163
164                 /* Elements in descent display */
165                 add(new Bearing(this, 0));
166                 add(new Distance(this, 1));
167                 add(new Lat(this, 2));
168                 add(new Lon(this, 3));
169                 add(new MaxHeight(this, 4));
170                 add(new MaxSpeed(this, 5));
171                 add(new MaxAccel(this, 6));
172
173                 graph = new JButton ("Graph Flight");
174                 graph.setActionCommand("graph");
175                 graph.addActionListener(this);
176                 graph.setEnabled(false);
177
178                 GridBagConstraints      c = new GridBagConstraints();
179
180                 c.gridx = 1; c.gridy = 7;
181                 c.insets = new Insets(10, 10, 10, 10);
182                 c.anchor = GridBagConstraints.WEST;
183                 c.weightx = 0;
184                 c.weighty = 0;
185                 c.fill = GridBagConstraints.VERTICAL;
186                 add(graph, c);
187                 addHierarchyListener(this);
188         }
189 }