altoslib: Remove some debug printfs
[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_5.*;
26 import org.altusmetrum.altosuilib_3.*;
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
101                 if (state == null)
102                         state = new AltosState();
103
104                 if (state.state != Altos.ao_flight_startup) {
105                         if (!has_state) {
106                                 pane.setTitleAt(0, "Launch Pad");
107                                 pane.add(ascent, 1);
108                                 pane.add(descent, 2);
109                                 pane.add(landed, 3);
110                                 has_state = true;
111                         }
112                 }
113
114                 JComponent tab = which_tab(state);
115                 if (tab != cur_tab) {
116                         if (cur_tab == pane.getSelectedComponent()) {
117                                 pane.setSelectedComponent(tab);
118                         }
119                         cur_tab = tab;
120                 }
121
122                 if (ignitor.should_show(state)) {
123                         if (!has_ignitor) {
124                                 pane.add("Ignitor", ignitor);
125                                 has_ignitor = true;
126                         }
127                 } else {
128                         if (has_ignitor) {
129                                 pane.remove(ignitor);
130                                 has_ignitor = false;
131                         }
132                 }
133
134                 if (state.companion != null) {
135                         if (!has_companion) {
136                                 pane.add("Companion", companion);
137                                 has_companion= true;
138                         }
139                 } else {
140                         if (has_companion) {
141                                 pane.remove(companion);
142                                 has_companion = false;
143                         }
144                 }
145
146                 if (state.gps != null && state.gps.connected) {
147                         if (!has_map) {
148                                 pane.add("Site Map", sitemap);
149                                 has_map = true;
150                         }
151                 } else {
152                         if (has_map) {
153                                 pane.remove(sitemap);
154                                 has_map = false;
155                         }
156                 }
157
158                 for (AltosFlightDisplay d : displays) {
159                         try {
160                                 d.show(state, listener_state);
161                         } catch (Exception e) {
162                                 System.out.printf("Exception showing %s\n", d.getName());
163                                 e.printStackTrace();
164                         }
165                 }
166         }
167
168         public void set_exit_on_close() {
169                 exit_on_close = true;
170         }
171
172         Container               bag;
173         AltosUIFreqList         frequencies;
174         AltosUIRateList         rates;
175         AltosUITelemetryList    telemetries;
176         JLabel                  telemetry;
177
178         ActionListener  show_timer;
179
180         public AltosFlightUI(AltosVoice in_voice, AltosFlightReader in_reader, final int serial) {
181                 AltosUIPreferences.set_component(this);
182
183                 displays = new LinkedList<AltosFlightDisplay>();
184
185                 voice = in_voice;
186                 reader = in_reader;
187
188                 bag = getContentPane();
189                 bag.setLayout(new GridBagLayout());
190
191                 GridBagConstraints c = new GridBagConstraints();
192
193                 setTitle(String.format("AltOS %s", reader.name));
194
195                 /* Stick channel selector at top of table for telemetry monitoring */
196                 if (serial >= 0) {
197                         // Frequency menu
198                         frequencies = new AltosUIFreqList(AltosUIPreferences.frequency(serial));
199                         frequencies.set_product("Monitor");
200                         frequencies.set_serial(serial);
201                         frequencies.addActionListener(new ActionListener() {
202                                         public void actionPerformed(ActionEvent e) {
203                                                 double frequency = frequencies.frequency();
204                                                 try {
205                                                         reader.set_frequency(frequency);
206                                                 } catch (TimeoutException te) {
207                                                 } catch (InterruptedException ie) {
208                                                 }
209                                                 reader.save_frequency();
210                                         }
211                         });
212                         c.gridx = 0;
213                         c.gridy = 0;
214                         c.weightx = 0;
215                         c.weighty = 0;
216                         c.insets = new Insets(3, 3, 3, 3);
217                         c.fill = GridBagConstraints.NONE;
218                         c.anchor = GridBagConstraints.WEST;
219                         bag.add (frequencies, c);
220
221                         // Telemetry rate list
222                         rates = new AltosUIRateList(AltosUIPreferences.telemetry_rate(serial));
223                         rates.addActionListener(new ActionListener() {
224                                         public void actionPerformed(ActionEvent e) {
225                                                 int rate = rates.rate();
226                                                 try {
227                                                         reader.set_telemetry_rate(rate);
228                                                 } catch (TimeoutException te) {
229                                                 } catch (InterruptedException ie) {
230                                                 }
231                                                 reader.save_telemetry_rate();
232                                         }
233                                 });
234                         rates.setEnabled(reader.supports_telemetry_rate(AltosLib.ao_telemetry_rate_2400));
235                         c.gridx = 1;
236                         c.gridy = 0;
237                         c.weightx = 0;
238                         c.weighty = 0;
239                         c.insets = new Insets(3, 3, 3, 3);
240                         c.fill = GridBagConstraints.NONE;
241                         c.anchor = GridBagConstraints.WEST;
242                         bag.add (rates, c);
243
244                         // Telemetry format list
245                         if (reader.supports_telemetry(Altos.ao_telemetry_standard)) {
246                                 telemetries = new AltosUITelemetryList(serial);
247                                 telemetries.addActionListener(new ActionListener() {
248                                                 public void actionPerformed(ActionEvent e) {
249                                                         int telemetry = telemetries.get_selected();
250                                                         reader.set_telemetry(telemetry);
251                                                         reader.save_telemetry();
252                                                 }
253                                         });
254                                 c.gridx = 2;
255                                 c.gridy = 0;
256                                 c.weightx = 0;
257                                 c.weighty = 0;
258                                 c.fill = GridBagConstraints.NONE;
259                                 c.anchor = GridBagConstraints.WEST;
260                                 bag.add (telemetries, c);
261                                 c.insets = new Insets(0, 0, 0, 0);
262                         } else {
263                                 String  version;
264
265                                 if (reader.supports_telemetry(Altos.ao_telemetry_0_9))
266                                         version = "Telemetry: 0.9";
267                                 else if (reader.supports_telemetry(Altos.ao_telemetry_0_8))
268                                         version = "Telemetry: 0.8";
269                                 else
270                                         version = "Telemetry: None";
271
272                                 telemetry = new JLabel(version);
273                                 c.gridx = 2;
274                                 c.gridy = 0;
275                                 c.weightx = 0;
276                                 c.weighty = 0;
277                                 c.fill = GridBagConstraints.NONE;
278                                 c.anchor = GridBagConstraints.WEST;
279                                 bag.add (telemetry, c);
280                                 c.insets = new Insets(0, 0, 0, 0);
281                         }
282                 }
283
284                 /* Flight status is always visible */
285                 flightStatus = new AltosFlightStatus();
286                 displays.add(flightStatus);
287                 c.gridx = 0;
288                 c.gridy = 1;
289                 c.fill = GridBagConstraints.HORIZONTAL;
290                 c.weightx = 1;
291                 c.gridwidth = 3;
292                 bag.add(flightStatus, c);
293
294                 /* The rest of the window uses a tabbed pane to
295                  * show one of the alternate data views
296                  */
297                 pane = new JTabbedPane();
298
299                 pad = new AltosPad();
300                 displays.add(pad);
301                 pane.add("Status", pad);
302
303                 ignitor = new AltosIgnitor();
304                 displays.add(ignitor);
305                 ascent = new AltosAscent();
306                 displays.add(ascent);
307                 descent = new AltosDescent();
308                 displays.add(descent);
309                 landed = new AltosLanded(reader);
310                 displays.add(landed);
311
312                 flightInfo = new AltosInfoTable();
313                 displays.add(flightInfo);
314                 pane.add("Table", new JScrollPane(flightInfo));
315
316                 companion = new AltosCompanionInfo();
317                 displays.add(companion);
318                 has_companion = false;
319                 has_state = false;
320
321                 sitemap = new AltosUIMap();
322                 displays.add(sitemap);
323                 has_map = false;
324
325                 /* Make the tabbed pane use the rest of the window space */
326                 c.gridx = 0;
327                 c.gridy = 2;
328                 c.fill = GridBagConstraints.BOTH;
329                 c.weightx = 1;
330                 c.weighty = 1;
331                 bag.add(pane, c);
332
333                 setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
334
335                 AltosUIPreferences.register_font_listener(this);
336                 AltosPreferences.register_units_listener(this);
337
338                 addWindowListener(new WindowAdapter() {
339                                 @Override
340                                 public void windowClosing(WindowEvent e) {
341                                         disconnect();
342                                         setVisible(false);
343                                         dispose();
344                                         AltosUIPreferences.unregister_font_listener(AltosFlightUI.this);
345                                         AltosPreferences.unregister_units_listener(AltosFlightUI.this);
346                                         if (exit_on_close)
347                                                 System.exit(0);
348                                 }
349                         });
350
351                 pack();
352                 setVisible(true);
353
354                 thread = new AltosDisplayThread(this, voice, this, reader);
355
356                 status_update = new AltosFlightStatusUpdate(flightStatus);
357
358                 new javax.swing.Timer(100, status_update).start();
359
360                 thread.start();
361         }
362
363         public AltosFlightUI (AltosVoice in_voice, AltosFlightReader in_reader) {
364                 this(in_voice, in_reader, -1);
365         }
366 }