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