Merge branch 'master' of ssh://git.gag.com/scm/git/fw/altos
[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_7.*;
25 import org.altusmetrum.altosuilib_7.*;
26
27 public class AltosLanded extends AltosUIFlightTab implements ActionListener {
28
29         class Bearing extends AltosUIIndicator {
30                 public void show (AltosState state, AltosListenerState listener_state) {
31                         if (state.from_pad != null && state.from_pad.bearing != AltosLib.MISSING) {
32                                 show( String.format("%3.0f°", state.from_pad.bearing),
33                                       state.from_pad.bearing_words(
34                                               AltosGreatCircle.BEARING_LONG));
35                         } else {
36                                 show("Missing", "Missing");
37                         }
38                 }
39                 public Bearing (Container container, int y) {
40                         super (container, y, "Bearing", 2);
41                 }
42         }
43
44         class Distance extends AltosUIUnitsIndicator {
45                 public double value(AltosState state, int i) {
46                         if (state.from_pad != null)
47                                 return state.from_pad.distance;
48                         else
49                                 return AltosLib.MISSING;
50                 }
51
52                 public Distance(Container container, int y) {
53                         super(container, y, AltosConvert.distance, "Ground Distance", 2);
54                 }
55         }
56
57         class Lat extends AltosUIUnitsIndicator {
58
59                 public boolean hide (AltosState state, int i) { return state.gps == null || !state.gps.connected; }
60
61                 public double value(AltosState state, int i) {
62                         if (state.gps == null)
63                                 return AltosLib.MISSING;
64                         if (!state.gps.connected)
65                                 return AltosLib.MISSING;
66                         return state.gps.lat;
67                 }
68
69                 public Lat (Container container, int y) {
70                         super (container, y, AltosConvert.latitude, "Latitude", 2);
71                 }
72         }
73
74         class Lon extends AltosUIUnitsIndicator {
75                 public boolean hide (AltosState state, int i) { return state.gps == null || !state.gps.connected; }
76
77                 public double value(AltosState state, int i) {
78                         if (state.gps == null)
79                                 return AltosLib.MISSING;
80                         if (!state.gps.connected)
81                                 return AltosLib.MISSING;
82                         return state.gps.lon;
83                 }
84
85                 public Lon (Container container, int y) {
86                         super (container, y, AltosConvert.longitude, "Longitude", 2);
87                 }
88         }
89
90         class MaxHeight extends AltosUIUnitsIndicator {
91                 public double value(AltosState state, int i) { return state.max_height(); }
92
93                 public MaxHeight (Container container, int y) {
94                         super (container, y, AltosConvert.height, "Maximum Height", 2);
95                 }
96         }
97
98         class MaxSpeed extends AltosUIUnitsIndicator {
99                 public double value(AltosState state, int i) { return state.max_speed(); }
100
101                 public MaxSpeed (Container container, int y) {
102                         super (container, y, AltosConvert.speed, "Maximum Speed", 2);
103                 }
104         }
105
106         class MaxAccel extends AltosUIUnitsIndicator {
107                 public double value(AltosState state, int i) { return state.max_acceleration(); }
108
109                 public MaxAccel (Container container, int y) {
110                         super (container, y, AltosConvert.speed, "Maximum acceleration", 2);
111                 }
112         }
113
114         JButton graph;
115         AltosFlightReader reader;
116
117         public void actionPerformed(ActionEvent e) {
118                 String  cmd = e.getActionCommand();
119
120                 if (cmd.equals("graph")) {
121                         File    file = reader.backing_file();
122                         if (file != null) {
123                                 String  filename = file.getName();
124                                 try {
125                                         AltosStateIterable states = null;
126                                         if (filename.endsWith("eeprom")) {
127                                                 FileInputStream in = new FileInputStream(file);
128                                                 states = new AltosEepromFile(in);
129                                         } else if (filename.endsWith("telem")) {
130                                                 FileInputStream in = new FileInputStream(file);
131                                                 states = new AltosTelemetryFile(in);
132                                         } else {
133                                                 throw new FileNotFoundException(filename);
134                                         }
135                                         try {
136                                                 new AltosGraphUI(states, 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                                 }
146                         }
147                 }
148         }
149
150         public String getName() {
151                 return "Landed";
152         }
153
154         public void show(AltosState state, AltosListenerState listener_state) {
155                 super.show(state, listener_state);
156                 if (reader.backing_file() != null)
157                         graph.setEnabled(true);
158         }
159
160         public AltosLanded(AltosFlightReader in_reader) {
161                 reader = in_reader;
162
163                 /* Elements in descent display */
164                 add(new Bearing(this, 0));
165                 add(new Distance(this, 1));
166                 add(new Lat(this, 2));
167                 add(new Lon(this, 3));
168                 add(new MaxHeight(this, 4));
169                 add(new MaxSpeed(this, 5));
170                 add(new MaxAccel(this, 6));
171
172                 graph = new JButton ("Graph Flight");
173                 graph.setActionCommand("graph");
174                 graph.addActionListener(this);
175                 graph.setEnabled(false);
176
177                 GridBagConstraints      c = new GridBagConstraints();
178
179                 c.gridx = 1; c.gridy = 7;
180                 c.insets = new Insets(10, 10, 10, 10);
181                 c.anchor = GridBagConstraints.WEST;
182                 c.weightx = 0;
183                 c.weighty = 0;
184                 c.fill = GridBagConstraints.VERTICAL;
185                 add(graph, c);
186                 addHierarchyListener(this);
187         }
188 }