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