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