altoslib/altosui: Carry receiver status around in AltosListenerState
[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, AltosListenerState listener_state) {
102                 status_update.saved_state = state;
103
104                 if (state == null) {
105                         System.out.printf ("no state provided\n");
106                         state = new AltosState(new AltosRecord());
107                 }
108
109                 pad.show(state, listener_state);
110
111                 if (state.state != Altos.ao_flight_startup) {
112                         if (!has_state) {
113                                 pane.setTitleAt(0, "Launch Pad");
114                                 pane.add(ascent, 1);
115                                 pane.add(descent, 2);
116                                 pane.add(landed, 3);
117                                 has_state = true;
118                         }
119                 }
120
121                 ascent.show(state, listener_state);
122                 descent.show(state, listener_state);
123                 landed.show(state, listener_state);
124
125                 JComponent tab = which_tab(state);
126                 if (tab != cur_tab) {
127                         if (cur_tab == pane.getSelectedComponent()) {
128                                 pane.setSelectedComponent(tab);
129                         }
130                         cur_tab = tab;
131                 }
132                 flightStatus.show(state, listener_state);
133                 flightInfo.show(state, listener_state);
134
135                 if (state.data.companion != null) {
136                         if (!has_companion) {
137                                 pane.add("Companion", companion);
138                                 has_companion= true;
139                         }
140                         companion.show(state, listener_state);
141                 } else {
142                         if (has_companion) {
143                                 pane.remove(companion);
144                                 has_companion = false;
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                         sitemap.show(state, listener_state);
153                 } else {
154                         if (has_map) {
155                                 pane.remove(sitemap);
156                                 has_map = false;
157                         }
158                 }
159         }
160
161         public void set_exit_on_close() {
162                 exit_on_close = true;
163         }
164
165         Container       bag;
166         AltosFreqList   frequencies;
167         JComboBox       telemetries;
168         JLabel          telemetry;
169
170         ActionListener  show_timer;
171
172         public AltosFlightUI(AltosVoice in_voice, AltosFlightReader in_reader, final int serial) {
173                 AltosUIPreferences.set_component(this);
174
175                 voice = in_voice;
176                 reader = in_reader;
177
178                 bag = getContentPane();
179                 bag.setLayout(new GridBagLayout());
180
181                 GridBagConstraints c = new GridBagConstraints();
182
183                 setTitle(String.format("AltOS %s", reader.name));
184
185                 /* Stick channel selector at top of table for telemetry monitoring */
186                 if (serial >= 0) {
187                         // Channel menu
188                         frequencies = new AltosFreqList(AltosUIPreferences.frequency(serial));
189                         frequencies.set_product("Monitor");
190                         frequencies.set_serial(serial);
191                         frequencies.addActionListener(new ActionListener() {
192                                         public void actionPerformed(ActionEvent e) {
193                                                 double frequency = frequencies.frequency();
194                                                 try {
195                                                         reader.set_frequency(frequency);
196                                                 } catch (TimeoutException te) {
197                                                 } catch (InterruptedException ie) {
198                                                 }
199                                                 reader.save_frequency();
200                                         }
201                         });
202                         c.gridx = 0;
203                         c.gridy = 0;
204                         c.weightx = 0;
205                         c.weighty = 0;
206                         c.insets = new Insets(3, 3, 3, 3);
207                         c.fill = GridBagConstraints.NONE;
208                         c.anchor = GridBagConstraints.WEST;
209                         bag.add (frequencies, c);
210
211                         // Telemetry format menu
212                         if (reader.supports_telemetry(Altos.ao_telemetry_standard)) {
213                                 telemetries = new JComboBox();
214                                 for (int i = 1; i <= Altos.ao_telemetry_max; i++) 
215                                         telemetries.addItem(Altos.telemetry_name(i));
216                                 int telemetry = AltosPreferences.telemetry(serial);
217                                 if (telemetry <= Altos.ao_telemetry_off ||
218                                     telemetry > Altos.ao_telemetry_max)
219                                         telemetry = Altos.ao_telemetry_standard;
220                                 telemetries.setSelectedIndex(telemetry - 1);
221                                 telemetries.setMaximumRowCount(Altos.ao_telemetry_max);
222                                 telemetries.setPreferredSize(null);
223                                 telemetries.revalidate();
224                                 telemetries.addActionListener(new ActionListener() {
225                                                 public void actionPerformed(ActionEvent e) {
226                                                         int telemetry = telemetries.getSelectedIndex() + 1;
227                                                         reader.set_telemetry(telemetry);
228                                                         reader.save_telemetry();
229                                                 }
230                                         });
231                                 c.gridx = 1;
232                                 c.gridy = 0;
233                                 c.weightx = 0;
234                                 c.weighty = 0;
235                                 c.fill = GridBagConstraints.NONE;
236                                 c.anchor = GridBagConstraints.WEST;
237                                 bag.add (telemetries, c);
238                                 c.insets = new Insets(0, 0, 0, 0);
239                         } else {
240                                 String  version;
241
242                                 if (reader.supports_telemetry(Altos.ao_telemetry_0_9))
243                                         version = "Telemetry: 0.9";
244                                 else if (reader.supports_telemetry(Altos.ao_telemetry_0_8))
245                                         version = "Telemetry: 0.8";
246                                 else
247                                         version = "Telemetry: None";
248
249                                 telemetry = new JLabel(version);
250                                 c.gridx = 1;
251                                 c.gridy = 0;
252                                 c.weightx = 0;
253                                 c.weighty = 0;
254                                 c.fill = GridBagConstraints.NONE;
255                                 c.anchor = GridBagConstraints.WEST;
256                                 bag.add (telemetry, c);
257                                 c.insets = new Insets(0, 0, 0, 0);
258                         }
259                 }
260
261                 /* Flight status is always visible */
262                 flightStatus = new AltosFlightStatus();
263                 c.gridx = 0;
264                 c.gridy = 1;
265                 c.fill = GridBagConstraints.HORIZONTAL;
266                 c.weightx = 1;
267                 c.gridwidth = 2;
268                 bag.add(flightStatus, c);
269                 c.gridwidth = 1;
270
271                 /* The rest of the window uses a tabbed pane to
272                  * show one of the alternate data views
273                  */
274                 pane = new JTabbedPane();
275
276                 pad = new AltosPad();
277                 pane.add("Status", pad);
278
279                 ascent = new AltosAscent();
280                 descent = new AltosDescent();
281                 landed = new AltosLanded(reader);
282
283                 flightInfo = new AltosInfoTable();
284                 pane.add("Table", new JScrollPane(flightInfo));
285
286                 companion = new AltosCompanionInfo();
287                 has_companion = false;
288                 has_state = false;
289
290                 sitemap = new AltosSiteMap();
291                 has_map = false;
292
293                 /* Make the tabbed pane use the rest of the window space */
294                 c.gridx = 0;
295                 c.gridy = 2;
296                 c.fill = GridBagConstraints.BOTH;
297                 c.weightx = 1;
298                 c.weighty = 1;
299                 c.gridwidth = 2;
300                 bag.add(pane, c);
301
302                 setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
303
304                 AltosUIPreferences.register_font_listener(this);
305
306                 addWindowListener(new WindowAdapter() {
307                                 @Override
308                                 public void windowClosing(WindowEvent e) {
309                                         disconnect();
310                                         setVisible(false);
311                                         dispose();
312                                         AltosUIPreferences.unregister_font_listener(AltosFlightUI.this);
313                                         if (exit_on_close)
314                                                 System.exit(0);
315                                 }
316                         });
317
318                 pack();
319                 setVisible(true);
320
321                 thread = new AltosDisplayThread(this, voice, this, reader);
322
323                 status_update = new AltosFlightStatusUpdate(flightStatus);
324
325                 new javax.swing.Timer(100, status_update).start();
326
327                 thread.start();
328         }
329
330         public AltosFlightUI (AltosVoice in_voice, AltosFlightReader in_reader) {
331                 this(in_voice, in_reader, -1);
332         }
333 }