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