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