windows: Use new windows stub to launch applications
[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_6.*;
26 import org.altusmetrum.altosuilib_6.*;
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                 status_update.saved_listener_state = listener_state;
101
102                 if (state == null)
103                         state = new AltosState();
104
105                 if (state.state != Altos.ao_flight_startup) {
106                         if (!has_state) {
107                                 pane.setTitleAt(0, "Launch Pad");
108                                 pane.add(ascent, 1);
109                                 pane.add(descent, 2);
110                                 pane.add(landed, 3);
111                                 has_state = true;
112                         }
113                 }
114
115                 JComponent tab = which_tab(state);
116                 if (tab != cur_tab) {
117                         if (cur_tab == pane.getSelectedComponent()) {
118                                 pane.setSelectedComponent(tab);
119                         }
120                         cur_tab = tab;
121                 }
122
123                 if (ignitor.should_show(state)) {
124                         if (!has_ignitor) {
125                                 pane.add("Ignitor", ignitor);
126                                 has_ignitor = true;
127                         }
128                 } else {
129                         if (has_ignitor) {
130                                 pane.remove(ignitor);
131                                 has_ignitor = 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 && state.gps.connected) {
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                 GridBagConstraints c = new GridBagConstraints();
193
194                 setTitle(String.format("AltOS %s", reader.name));
195
196                 /* Stick channel selector at top of table for telemetry monitoring */
197                 if (serial >= 0) {
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                         c.gridx = 0;
214                         c.gridy = 0;
215                         c.weightx = 0;
216                         c.weighty = 0;
217                         c.insets = new Insets(3, 3, 3, 3);
218                         c.fill = GridBagConstraints.NONE;
219                         c.anchor = GridBagConstraints.WEST;
220                         bag.add (frequencies, c);
221
222                         // Telemetry rate list
223                         rates = new AltosUIRateList(AltosUIPreferences.telemetry_rate(serial));
224                         rates.addActionListener(new ActionListener() {
225                                         public void actionPerformed(ActionEvent e) {
226                                                 int rate = rates.rate();
227                                                 try {
228                                                         reader.set_telemetry_rate(rate);
229                                                 } catch (TimeoutException te) {
230                                                 } catch (InterruptedException ie) {
231                                                 }
232                                                 reader.save_telemetry_rate();
233                                         }
234                                 });
235                         rates.setEnabled(reader.supports_telemetry_rate(AltosLib.ao_telemetry_rate_2400));
236                         c.gridx = 1;
237                         c.gridy = 0;
238                         c.weightx = 0;
239                         c.weighty = 0;
240                         c.insets = new Insets(3, 3, 3, 3);
241                         c.fill = GridBagConstraints.NONE;
242                         c.anchor = GridBagConstraints.WEST;
243                         bag.add (rates, c);
244
245                         // Telemetry format list
246                         if (reader.supports_telemetry(Altos.ao_telemetry_standard)) {
247                                 telemetries = new AltosUITelemetryList(serial);
248                                 telemetries.addActionListener(new ActionListener() {
249                                                 public void actionPerformed(ActionEvent e) {
250                                                         int telemetry = telemetries.get_selected();
251                                                         reader.set_telemetry(telemetry);
252                                                         reader.save_telemetry();
253                                                 }
254                                         });
255                                 c.gridx = 2;
256                                 c.gridy = 0;
257                                 c.weightx = 0;
258                                 c.weighty = 0;
259                                 c.fill = GridBagConstraints.NONE;
260                                 c.anchor = GridBagConstraints.WEST;
261                                 bag.add (telemetries, c);
262                                 c.insets = new Insets(0, 0, 0, 0);
263                         } else {
264                                 String  version;
265
266                                 if (reader.supports_telemetry(Altos.ao_telemetry_0_9))
267                                         version = "Telemetry: 0.9";
268                                 else if (reader.supports_telemetry(Altos.ao_telemetry_0_8))
269                                         version = "Telemetry: 0.8";
270                                 else
271                                         version = "Telemetry: None";
272
273                                 telemetry = new JLabel(version);
274                                 c.gridx = 2;
275                                 c.gridy = 0;
276                                 c.weightx = 0;
277                                 c.weighty = 0;
278                                 c.fill = GridBagConstraints.NONE;
279                                 c.anchor = GridBagConstraints.WEST;
280                                 bag.add (telemetry, c);
281                                 c.insets = new Insets(0, 0, 0, 0);
282                         }
283                 }
284
285                 /* Flight status is always visible */
286                 flightStatus = new AltosFlightStatus();
287                 displays.add(flightStatus);
288                 c.gridx = 0;
289                 c.gridy = 1;
290                 c.fill = GridBagConstraints.HORIZONTAL;
291                 c.weightx = 1;
292                 c.gridwidth = 3;
293                 bag.add(flightStatus, c);
294
295                 /* The rest of the window uses a tabbed pane to
296                  * show one of the alternate data views
297                  */
298                 pane = new JTabbedPane();
299
300                 pad = new AltosPad();
301                 displays.add(pad);
302                 pane.add("Status", pad);
303
304                 ignitor = new AltosIgnitor();
305                 displays.add(ignitor);
306                 ascent = new AltosAscent();
307                 displays.add(ascent);
308                 descent = new AltosDescent();
309                 displays.add(descent);
310                 landed = new AltosLanded(reader);
311                 displays.add(landed);
312
313                 flightInfo = new AltosInfoTable();
314                 displays.add(flightInfo);
315                 pane.add("Table", new JScrollPane(flightInfo));
316
317                 companion = new AltosCompanionInfo();
318                 displays.add(companion);
319                 has_companion = false;
320                 has_state = false;
321
322                 sitemap = new AltosUIMap();
323                 displays.add(sitemap);
324                 has_map = false;
325
326                 /* Make the tabbed pane use the rest of the window space */
327                 c.gridx = 0;
328                 c.gridy = 2;
329                 c.fill = GridBagConstraints.BOTH;
330                 c.weightx = 1;
331                 c.weighty = 1;
332                 bag.add(pane, c);
333
334                 setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
335
336                 AltosUIPreferences.register_font_listener(this);
337                 AltosPreferences.register_units_listener(this);
338
339                 status_update = new AltosFlightStatusUpdate(flightStatus);
340
341                 flightStatus.start(status_update);
342
343                 addWindowListener(new WindowAdapter() {
344                                 @Override
345                                 public void windowClosing(WindowEvent e) {
346                                         flightStatus.stop();
347                                         disconnect();
348                                         setVisible(false);
349                                         dispose();
350                                         AltosUIPreferences.unregister_font_listener(AltosFlightUI.this);
351                                         AltosPreferences.unregister_units_listener(AltosFlightUI.this);
352                                         if (exit_on_close)
353                                                 System.exit(0);
354                                 }
355                         });
356
357                 pack();
358                 setVisible(true);
359
360                 thread = new AltosDisplayThread(this, voice, this, reader);
361
362                 thread.start();
363         }
364
365         public AltosFlightUI (AltosVoice in_voice, AltosFlightReader in_reader) {
366                 this(in_voice, in_reader, -1);
367         }
368 }