altosui: Add TeleMini v3.0 and EasyMini v2.0 firmware to windows package
[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    ignitor;
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_ignitor;
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();
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                         if (cur_tab == pane.getSelectedComponent()) {
119                                 pane.setSelectedComponent(tab);
120                         }
121                         cur_tab = tab;
122                 }
123
124                 if (ignitor.should_show(state)) {
125                         if (!has_ignitor) {
126                                 pane.add("Ignitor", ignitor);
127                                 has_ignitor = true;
128                         }
129                 } else {
130                         if (has_ignitor) {
131                                 pane.remove(ignitor);
132                                 has_ignitor = false;
133                         }
134                 }
135
136                 if (state.companion != null) {
137                         if (!has_companion) {
138                                 pane.add("Companion", companion);
139                                 has_companion= true;
140                         }
141                 } else {
142                         if (has_companion) {
143                                 pane.remove(companion);
144                                 has_companion = false;
145                         }
146                 }
147
148                 if (state.gps != null && state.gps.connected) {
149                         if (!has_map) {
150                                 pane.add("Site Map", sitemap);
151                                 has_map = true;
152                         }
153                 } else {
154                         if (has_map) {
155                                 pane.remove(sitemap);
156                                 has_map = false;
157                         }
158                 }
159
160                 for (AltosFlightDisplay d : displays) {
161                         try {
162                                 d.show(state, listener_state);
163                         } catch (Exception e) {
164                                 System.out.printf("Exception showing %s\n", d.getName());
165                                 e.printStackTrace();
166                         }
167                 }
168         }
169
170         public void set_exit_on_close() {
171                 exit_on_close = true;
172         }
173
174         Container               bag;
175         AltosUIFreqList         frequencies;
176         AltosUIRateList         rates;
177         AltosUITelemetryList    telemetries;
178         JLabel                  telemetry;
179
180         ActionListener  show_timer;
181
182         public AltosFlightUI(AltosVoice in_voice, AltosFlightReader in_reader, final int serial) {
183                 AltosUIPreferences.set_component(this);
184
185                 displays = new LinkedList<AltosFlightDisplay>();
186
187                 voice = in_voice;
188                 reader = in_reader;
189
190                 bag = getContentPane();
191                 bag.setLayout(new GridBagLayout());
192
193                 setTitle(String.format("AltOS %s", reader.name));
194
195                 /* Stick channel selector at top of table for telemetry monitoring */
196                 if (serial >= 0) {
197                         set_inset(3);
198
199                         // Frequency menu
200                         frequencies = new AltosUIFreqList(AltosUIPreferences.frequency(serial));
201                         frequencies.set_product("Monitor");
202                         frequencies.set_serial(serial);
203                         frequencies.addActionListener(new ActionListener() {
204                                         public void actionPerformed(ActionEvent e) {
205                                                 double frequency = frequencies.frequency();
206                                                 try {
207                                                         reader.set_frequency(frequency);
208                                                 } catch (TimeoutException te) {
209                                                 } catch (InterruptedException ie) {
210                                                 }
211                                                 reader.save_frequency();
212                                         }
213                         });
214                         bag.add (frequencies, constraints(0, 1));
215
216                         // Telemetry rate list
217                         rates = new AltosUIRateList(AltosUIPreferences.telemetry_rate(serial));
218                         rates.addActionListener(new ActionListener() {
219                                         public void actionPerformed(ActionEvent e) {
220                                                 int rate = rates.rate();
221                                                 try {
222                                                         reader.set_telemetry_rate(rate);
223                                                 } catch (TimeoutException te) {
224                                                 } catch (InterruptedException ie) {
225                                                 }
226                                                 reader.save_telemetry_rate();
227                                         }
228                                 });
229                         rates.setEnabled(reader.supports_telemetry_rate(AltosLib.ao_telemetry_rate_2400));
230                         bag.add (rates, constraints(1, 1));
231
232                         // Telemetry format list
233                         if (reader.supports_telemetry(Altos.ao_telemetry_standard)) {
234                                 telemetries = new AltosUITelemetryList(serial);
235                                 telemetries.addActionListener(new ActionListener() {
236                                                 public void actionPerformed(ActionEvent e) {
237                                                         int telemetry = telemetries.get_selected();
238                                                         reader.set_telemetry(telemetry);
239                                                         reader.save_telemetry();
240                                                 }
241                                         });
242                                 bag.add (telemetries, constraints(2, 1));
243                         } else {
244                                 String  version;
245
246                                 if (reader.supports_telemetry(Altos.ao_telemetry_0_9))
247                                         version = "Telemetry: 0.9";
248                                 else if (reader.supports_telemetry(Altos.ao_telemetry_0_8))
249                                         version = "Telemetry: 0.8";
250                                 else
251                                         version = "Telemetry: None";
252
253                                 telemetry = new JLabel(version);
254                                 bag.add (telemetry, constraints(2, 1));
255                         }
256                         next_row();
257                 }
258                 set_inset(0);
259
260                 /* Flight status is always visible */
261                 flightStatus = new AltosFlightStatus();
262                 displays.add(flightStatus);
263                 bag.add(flightStatus, constraints(0, 4, GridBagConstraints.HORIZONTAL));
264                 next_row();
265
266                 /* The rest of the window uses a tabbed pane to
267                  * show one of the alternate data views
268                  */
269                 pane = new JTabbedPane();
270
271                 pad = new AltosPad();
272                 displays.add(pad);
273                 pane.add("Status", pad);
274
275                 ignitor = new AltosIgnitor();
276                 displays.add(ignitor);
277                 ascent = new AltosAscent();
278                 displays.add(ascent);
279                 descent = new AltosDescent();
280                 displays.add(descent);
281                 landed = new AltosLanded(reader);
282                 displays.add(landed);
283
284                 flightInfo = new AltosInfoTable();
285                 displays.add(flightInfo);
286                 pane.add("Table", new JScrollPane(flightInfo));
287
288                 companion = new AltosCompanionInfo();
289                 displays.add(companion);
290                 has_companion = false;
291                 has_state = false;
292
293                 sitemap = new AltosUIMap();
294                 displays.add(sitemap);
295                 has_map = false;
296
297                 /* Make the tabbed pane use the rest of the window space */
298                 bag.add(pane, constraints(0, 4, GridBagConstraints.BOTH));
299
300                 setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
301
302                 AltosUIPreferences.register_font_listener(this);
303                 AltosPreferences.register_units_listener(this);
304
305                 status_update = new AltosFlightStatusUpdate(flightStatus);
306
307                 flightStatus.start(status_update);
308
309                 addWindowListener(new WindowAdapter() {
310                                 @Override
311                                 public void windowClosing(WindowEvent e) {
312                                         flightStatus.stop();
313                                         disconnect();
314                                         setVisible(false);
315                                         dispose();
316                                         AltosUIPreferences.unregister_font_listener(AltosFlightUI.this);
317                                         AltosPreferences.unregister_units_listener(AltosFlightUI.this);
318                                         if (exit_on_close)
319                                                 System.exit(0);
320                                 }
321                         });
322
323                 pack();
324                 setVisible(true);
325
326                 thread = new AltosDisplayThread(this, voice, this, reader);
327
328                 thread.start();
329         }
330
331         public AltosFlightUI (AltosVoice in_voice, AltosFlightReader in_reader) {
332                 this(in_voice, in_reader, -1);
333         }
334 }