lose the map related code
[fw/altos] / teststand / 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; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with this program; if not, write to the Free Software Foundation, Inc.,
16  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
17  */
18
19 package teststand;
20
21 import java.awt.*;
22 import java.awt.event.*;
23 import javax.swing.*;
24 import java.util.*;
25 import java.util.concurrent.*;
26 import org.altusmetrum.altoslib_12.*;
27 import org.altusmetrum.altosuilib_12.*;
28
29 public class AltosFlightUI extends AltosUIFrame implements AltosFlightDisplay {
30         AltosVoice              voice;
31         AltosFlightReader       reader;
32         AltosDisplayThread      thread;
33
34         LinkedList<AltosFlightDisplay> displays;
35
36         JTabbedPane     pane;
37
38         AltosPad        pad;
39         AltosIgnitor    igniter;
40         AltosLanded     landed;
41         AltosCompanionInfo      companion;
42         boolean         has_companion;
43         boolean         has_state;
44         boolean         has_igniter;
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                 return landed;
56         }
57
58         void stop_display() {
59                 if (thread != null && thread.isAlive()) {
60                         thread.interrupt();
61                         try {
62                                 thread.join();
63                         } catch (InterruptedException ie) {}
64                 }
65                 thread = null;
66         }
67
68         void disconnect() {
69                 stop_display();
70         }
71
72         public void reset() {
73                 for (AltosFlightDisplay d : displays)
74                         d.reset();
75         }
76
77         public void font_size_changed(int font_size) {
78                 for (AltosFlightDisplay d : displays)
79                         d.font_size_changed(font_size);
80         }
81
82         public void units_changed(boolean imperial_units) {
83                 for (AltosFlightDisplay d : displays)
84                         d.units_changed(imperial_units);
85         }
86
87         AltosFlightStatusUpdate status_update;
88
89         public void show(AltosState state, AltosListenerState listener_state) {
90                 status_update.saved_state = state;
91                 status_update.saved_listener_state = listener_state;
92
93                 if (state == null)
94                         state = new AltosState(new AltosCalData());
95
96                 if (state.state() != Altos.ao_flight_startup) {
97                         if (!has_state) {
98                                 pane.setTitleAt(0, "Launch Pad");
99                                 pane.add(landed, 1);
100                                 has_state = true;
101                         }
102                 }
103
104                 JComponent tab = which_tab(state);
105                 if (tab != cur_tab) {
106                         if (cur_tab == pane.getSelectedComponent())
107                                 pane.setSelectedComponent(tab);
108                         cur_tab = tab;
109                 }
110
111                 if (igniter.should_show(state)) {
112                         if (!has_igniter) {
113                                 pane.add("Ignitor", igniter);
114                                 has_igniter = true;
115                         }
116                 } else {
117                         if (has_igniter) {
118                                 pane.remove(igniter);
119                                 has_igniter = false;
120                         }
121                 }
122
123                 if (state.companion != null) {
124                         if (!has_companion) {
125                                 pane.add("Companion", companion);
126                                 has_companion= true;
127                         }
128                 } else {
129                         if (has_companion) {
130                                 pane.remove(companion);
131                                 has_companion = false;
132                         }
133                 }
134
135                 for (AltosFlightDisplay d : displays) {
136                         try {
137                                 d.show(state, listener_state);
138                         } catch (Exception e) {
139                                 System.out.printf("Exception showing %s\n", d.getName());
140                                 e.printStackTrace();
141                         }
142                 }
143         }
144
145         public void set_exit_on_close() {
146                 exit_on_close = true;
147         }
148
149         Container               bag;
150         AltosUIFreqList         frequencies;
151         AltosUIRateList         rates;
152         AltosUITelemetryList    telemetries;
153         JLabel                  telemetry;
154
155         ActionListener  show_timer;
156
157         public AltosFlightUI(AltosVoice in_voice, AltosFlightReader in_reader, final int serial) {
158                 AltosUIPreferences.set_component(this);
159
160                 displays = new LinkedList<AltosFlightDisplay>();
161
162                 voice = in_voice;
163                 reader = in_reader;
164
165                 bag = getContentPane();
166                 bag.setLayout(new GridBagLayout());
167
168                 setTitle(String.format("AltOS %s", reader.name));
169
170                 /* Stick channel selector at top of table for telemetry monitoring */
171                 if (serial >= 0) {
172                         set_inset(3);
173
174                         // Frequency menu
175                         frequencies = new AltosUIFreqList(AltosUIPreferences.frequency(serial));
176                         frequencies.set_product("Monitor");
177                         frequencies.set_serial(serial);
178                         frequencies.addActionListener(new ActionListener() {
179                                         public void actionPerformed(ActionEvent e) {
180                                                 double frequency = frequencies.frequency();
181                                                 try {
182                                                         reader.set_frequency(frequency);
183                                                 } catch (TimeoutException te) {
184                                                 } catch (InterruptedException ie) {
185                                                 }
186                                                 reader.save_frequency();
187                                         }
188                         });
189                         bag.add (frequencies, constraints(0, 1));
190
191                         // Telemetry rate list
192                         rates = new AltosUIRateList(AltosUIPreferences.telemetry_rate(serial));
193                         rates.addActionListener(new ActionListener() {
194                                         public void actionPerformed(ActionEvent e) {
195                                                 int rate = rates.rate();
196                                                 try {
197                                                         reader.set_telemetry_rate(rate);
198                                                 } catch (TimeoutException te) {
199                                                 } catch (InterruptedException ie) {
200                                                 }
201                                                 reader.save_telemetry_rate();
202                                         }
203                                 });
204                         rates.setEnabled(reader.supports_telemetry_rate(AltosLib.ao_telemetry_rate_2400));
205                         bag.add (rates, constraints(1, 1));
206
207                         // Telemetry format list
208                         if (reader.supports_telemetry(Altos.ao_telemetry_standard)) {
209                                 telemetries = new AltosUITelemetryList(serial);
210                                 telemetries.addActionListener(new ActionListener() {
211                                                 public void actionPerformed(ActionEvent e) {
212                                                         int telemetry = telemetries.get_selected();
213                                                         reader.set_telemetry(telemetry);
214                                                         reader.save_telemetry();
215                                                 }
216                                         });
217                                 bag.add (telemetries, constraints(2, 1));
218                         } else {
219                                 String  version;
220
221                                 if (reader.supports_telemetry(Altos.ao_telemetry_0_9))
222                                         version = "Telemetry: 0.9";
223                                 else if (reader.supports_telemetry(Altos.ao_telemetry_0_8))
224                                         version = "Telemetry: 0.8";
225                                 else
226                                         version = "Telemetry: None";
227
228                                 telemetry = new JLabel(version);
229                                 bag.add (telemetry, constraints(2, 1));
230                         }
231                         next_row();
232                 }
233                 set_inset(0);
234
235                 /* Flight status is always visible */
236                 flightStatus = new AltosFlightStatus();
237                 displays.add(flightStatus);
238                 bag.add(flightStatus, constraints(0, 4, GridBagConstraints.HORIZONTAL));
239                 next_row();
240
241                 /* The rest of the window uses a tabbed pane to
242                  * show one of the alternate data views
243                  */
244                 pane = new JTabbedPane();
245
246                 pad = new AltosPad();
247                 displays.add(pad);
248                 pane.add("Status", pad);
249
250                 igniter = new AltosIgnitor();
251                 displays.add(igniter);
252                 landed = new AltosLanded(reader);
253                 displays.add(landed);
254
255                 flightInfo = new AltosInfoTable();
256                 displays.add(flightInfo);
257                 pane.add("Table", new JScrollPane(flightInfo));
258
259                 companion = new AltosCompanionInfo();
260                 displays.add(companion);
261                 has_companion = false;
262                 has_state = false;
263
264                 /* Make the tabbed pane use the rest of the window space */
265                 bag.add(pane, constraints(0, 4, GridBagConstraints.BOTH));
266
267                 setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
268
269                 AltosUIPreferences.register_font_listener(this);
270                 AltosPreferences.register_units_listener(this);
271
272                 status_update = new AltosFlightStatusUpdate(flightStatus);
273
274                 flightStatus.start(status_update);
275
276                 addWindowListener(new WindowAdapter() {
277                                 @Override
278                                 public void windowClosing(WindowEvent e) {
279                                         flightStatus.stop();
280                                         disconnect();
281                                         setVisible(false);
282                                         dispose();
283                                         AltosUIPreferences.unregister_font_listener(AltosFlightUI.this);
284                                         AltosPreferences.unregister_units_listener(AltosFlightUI.this);
285                                         if (exit_on_close)
286                                                 System.exit(0);
287                                 }
288                         });
289
290                 pack();
291                 setVisible(true);
292
293                 thread = new AltosDisplayThread(this, voice, this, reader);
294
295                 thread.start();
296         }
297
298         public AltosFlightUI (AltosVoice in_voice, AltosFlightReader in_reader) {
299                 this(in_voice, in_reader, -1);
300         }
301 }