altosui: Print exception stack trace when tracking flights
[fw/altos] / altosui / AltosFlightUI.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.util.concurrent.*;
24 import org.altusmetrum.AltosLib.*;
25
26 public class AltosFlightUI extends AltosFrame implements AltosFlightDisplay, AltosFontListener {
27         AltosVoice              voice;
28         AltosFlightReader       reader;
29         AltosDisplayThread      thread;
30
31         JTabbedPane     pane;
32
33         AltosPad        pad;
34         AltosAscent     ascent;
35         AltosDescent    descent;
36         AltosLanded     landed;
37         AltosCompanionInfo      companion;
38         AltosSiteMap    sitemap;
39         boolean         has_map;
40         boolean         has_companion;
41
42         private AltosFlightStatus flightStatus;
43         private AltosInfoTable flightInfo;
44
45         boolean exit_on_close = false;
46
47         JComponent cur_tab = null;
48         JComponent which_tab(AltosState state) {
49                 if (state.state < Altos.ao_flight_boost)
50                         return pad;
51                 if (state.state <= Altos.ao_flight_coast)
52                         return ascent;
53                 if (state.state <= Altos.ao_flight_main)
54                         return descent;
55                 return landed;
56         }
57
58         void stop_display() {
59                 if (thread != null && thread.isAlive()) {
60                         thread.interrupt();
61                         try {
62                                 thread.join();
63                         } catch (InterruptedException ie) {}
64                 }
65                 thread = null;
66         }
67
68         void disconnect() {
69                 stop_display();
70         }
71
72         public void reset() {
73                 pad.reset();
74                 ascent.reset();
75                 descent.reset();
76                 landed.reset();
77                 flightInfo.clear();
78                 sitemap.reset();
79         }
80
81         public void set_font() {
82                 pad.set_font();
83                 ascent.set_font();
84                 descent.set_font();
85                 landed.set_font();
86                 flightStatus.set_font();
87                 flightInfo.set_font();
88                 sitemap.set_font();
89                 companion.set_font();
90         }
91
92         public void font_size_changed(int font_size) {
93                 set_font();
94         }
95
96
97         AltosFlightStatusUpdate status_update;
98
99         public void show(AltosState state, int crc_errors) {
100                 status_update.saved_state = state;
101                 JComponent tab = which_tab(state);
102                 try {
103                 pad.show(state, crc_errors);
104                 ascent.show(state, crc_errors);
105                 descent.show(state, crc_errors);
106                 landed.show(state, crc_errors);
107                 if (tab != cur_tab) {
108                         if (cur_tab == pane.getSelectedComponent()) {
109                                 pane.setSelectedComponent(tab);
110                         }
111                         cur_tab = tab;
112                 }
113                 flightStatus.show(state, crc_errors);
114                 flightInfo.show(state, crc_errors);
115
116                 if (state.data.companion != null) {
117                         if (!has_companion) {
118                                 pane.add("Companion", companion);
119                                 has_companion= true;
120                         }
121                         companion.show(state, crc_errors);
122                 } else {
123                         if (has_companion) {
124                                 pane.remove(companion);
125                                 has_companion = false;
126                         }
127                 }
128                 if (state.gps != null && state.gps.connected) {
129                         if (!has_map) {
130                                 pane.add("Site Map", sitemap);
131                                 has_map = true;
132                         }
133                         sitemap.show(state, crc_errors);
134                 } else {
135                         if (has_map) {
136                                 pane.remove(sitemap);
137                                 has_map = false;
138                         }
139                 }
140                 } catch (Exception e) {
141                         System.out.print("Show exception " + e + "\n");
142                         e.printStackTrace();
143                 }
144         }
145
146         public void set_exit_on_close() {
147                 exit_on_close = true;
148         }
149
150         Container       bag;
151         AltosFreqList   frequencies;
152         JComboBox       telemetries;
153         JLabel          telemetry;
154
155         ActionListener  show_timer;
156
157         public AltosFlightUI(AltosVoice in_voice, AltosFlightReader in_reader, final int serial) {
158                 AltosUIPreferences.set_component(this);
159
160                 voice = in_voice;
161                 reader = in_reader;
162
163                 bag = getContentPane();
164                 bag.setLayout(new GridBagLayout());
165
166                 GridBagConstraints c = new GridBagConstraints();
167
168                 setTitle(String.format("AltOS %s", reader.name));
169
170                 /* Stick channel selector at top of table for telemetry monitoring */
171                 if (serial >= 0) {
172                         // Channel menu
173                         frequencies = new AltosFreqList(AltosUIPreferences.frequency(serial));
174                         frequencies.set_product("Monitor");
175                         frequencies.set_serial(serial);
176                         frequencies.addActionListener(new ActionListener() {
177                                         public void actionPerformed(ActionEvent e) {
178                                                 double frequency = frequencies.frequency();
179                                                 try {
180                                                         reader.set_frequency(frequency);
181                                                 } catch (TimeoutException te) {
182                                                 } catch (InterruptedException ie) {
183                                                 }
184                                                 reader.save_frequency();
185                                         }
186                         });
187                         c.gridx = 0;
188                         c.gridy = 0;
189                         c.weightx = 0;
190                         c.weighty = 0;
191                         c.insets = new Insets(3, 3, 3, 3);
192                         c.fill = GridBagConstraints.NONE;
193                         c.anchor = GridBagConstraints.WEST;
194                         bag.add (frequencies, c);
195
196                         // Telemetry format menu
197                         if (reader.supports_telemetry(Altos.ao_telemetry_standard)) {
198                                 telemetries = new JComboBox();
199                                 for (int i = 1; i <= Altos.ao_telemetry_max; i++) 
200                                         telemetries.addItem(Altos.telemetry_name(i));
201                                 int telemetry = AltosPreferences.telemetry(serial);
202                                 if (telemetry <= Altos.ao_telemetry_off ||
203                                     telemetry > Altos.ao_telemetry_max)
204                                         telemetry = Altos.ao_telemetry_standard;
205                                 telemetries.setSelectedIndex(telemetry - 1);
206                                 telemetries.setMaximumRowCount(Altos.ao_telemetry_max);
207                                 telemetries.setPreferredSize(null);
208                                 telemetries.revalidate();
209                                 telemetries.addActionListener(new ActionListener() {
210                                                 public void actionPerformed(ActionEvent e) {
211                                                         int telemetry = telemetries.getSelectedIndex() + 1;
212                                                         reader.set_telemetry(telemetry);
213                                                         reader.save_telemetry();
214                                                 }
215                                         });
216                                 c.gridx = 1;
217                                 c.gridy = 0;
218                                 c.weightx = 0;
219                                 c.weighty = 0;
220                                 c.fill = GridBagConstraints.NONE;
221                                 c.anchor = GridBagConstraints.WEST;
222                                 bag.add (telemetries, c);
223                                 c.insets = new Insets(0, 0, 0, 0);
224                         } else {
225                                 String  version;
226
227                                 if (reader.supports_telemetry(Altos.ao_telemetry_0_9))
228                                         version = "Telemetry: 0.9";
229                                 else if (reader.supports_telemetry(Altos.ao_telemetry_0_8))
230                                         version = "Telemetry: 0.8";
231                                 else
232                                         version = "Telemetry: None";
233
234                                 telemetry = new JLabel(version);
235                                 c.gridx = 1;
236                                 c.gridy = 0;
237                                 c.weightx = 0;
238                                 c.weighty = 0;
239                                 c.fill = GridBagConstraints.NONE;
240                                 c.anchor = GridBagConstraints.WEST;
241                                 bag.add (telemetry, c);
242                                 c.insets = new Insets(0, 0, 0, 0);
243                         }
244                 }
245
246                 /* Flight status is always visible */
247                 flightStatus = new AltosFlightStatus();
248                 c.gridx = 0;
249                 c.gridy = 1;
250                 c.fill = GridBagConstraints.HORIZONTAL;
251                 c.weightx = 1;
252                 c.gridwidth = 2;
253                 bag.add(flightStatus, c);
254                 c.gridwidth = 1;
255
256                 /* The rest of the window uses a tabbed pane to
257                  * show one of the alternate data views
258                  */
259                 pane = new JTabbedPane();
260
261                 pad = new AltosPad();
262                 pane.add("Launch Pad", pad);
263
264                 ascent = new AltosAscent();
265                 pane.add("Ascent", ascent);
266
267                 descent = new AltosDescent();
268                 pane.add("Descent", descent);
269
270                 landed = new AltosLanded(reader);
271                 pane.add("Landed", landed);
272
273                 flightInfo = new AltosInfoTable();
274                 pane.add("Table", new JScrollPane(flightInfo));
275
276                 companion = new AltosCompanionInfo();
277                 has_companion = false;
278
279                 sitemap = new AltosSiteMap();
280                 has_map = false;
281
282                 /* Make the tabbed pane use the rest of the window space */
283                 c.gridx = 0;
284                 c.gridy = 2;
285                 c.fill = GridBagConstraints.BOTH;
286                 c.weightx = 1;
287                 c.weighty = 1;
288                 c.gridwidth = 2;
289                 bag.add(pane, c);
290
291                 setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
292
293                 AltosUIPreferences.register_font_listener(this);
294
295                 addWindowListener(new WindowAdapter() {
296                                 @Override
297                                 public void windowClosing(WindowEvent e) {
298                                         disconnect();
299                                         setVisible(false);
300                                         dispose();
301                                         AltosUIPreferences.unregister_font_listener(AltosFlightUI.this);
302                                         if (exit_on_close)
303                                                 System.exit(0);
304                                 }
305                         });
306
307                 pack();
308                 setVisible(true);
309
310                 thread = new AltosDisplayThread(this, voice, this, reader);
311
312                 status_update = new AltosFlightStatusUpdate(flightStatus);
313
314                 new javax.swing.Timer(100, status_update).start();
315
316                 thread.start();
317         }
318
319         public AltosFlightUI (AltosVoice in_voice, AltosFlightReader in_reader) {
320                 this(in_voice, in_reader, -1);
321         }
322 }