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