2 * Copyright © 2010 Keith Packard <keithp@keithp.com>
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.
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.
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.
21 import java.awt.event.*;
24 import java.util.concurrent.*;
25 import org.altusmetrum.altoslib_5.*;
26 import org.altusmetrum.altosuilib_3.*;
28 public class AltosFlightUI extends AltosUIFrame implements AltosFlightDisplay {
30 AltosFlightReader reader;
31 AltosDisplayThread thread;
33 LinkedList<AltosFlightDisplay> displays;
42 AltosCompanionInfo companion;
45 boolean has_companion;
49 private AltosFlightStatus flightStatus;
50 private AltosInfoTable flightInfo;
52 boolean exit_on_close = false;
54 JComponent cur_tab = null;
55 JComponent which_tab(AltosState state) {
56 if (state.state < Altos.ao_flight_boost)
58 if (state.state <= Altos.ao_flight_coast)
60 if (state.state <= Altos.ao_flight_main)
62 if (state.state == AltosLib.ao_flight_stateless)
68 if (thread != null && thread.isAlive()) {
72 } catch (InterruptedException ie) {}
82 for (AltosFlightDisplay d : displays)
86 public void font_size_changed(int font_size) {
87 for (AltosFlightDisplay d : displays)
88 d.font_size_changed(font_size);
91 public void units_changed(boolean imperial_units) {
92 for (AltosFlightDisplay d : displays)
93 d.units_changed(imperial_units);
96 AltosFlightStatusUpdate status_update;
98 public void show(AltosState state, AltosListenerState listener_state) {
99 status_update.saved_state = state;
102 state = new AltosState();
104 if (state.state != Altos.ao_flight_startup) {
106 pane.setTitleAt(0, "Launch Pad");
108 pane.add(descent, 2);
114 JComponent tab = which_tab(state);
115 if (tab != cur_tab) {
116 if (cur_tab == pane.getSelectedComponent()) {
117 pane.setSelectedComponent(tab);
122 if (ignitor.should_show(state)) {
124 pane.add("Ignitor", ignitor);
129 pane.remove(ignitor);
134 if (state.companion != null) {
135 if (!has_companion) {
136 pane.add("Companion", companion);
141 pane.remove(companion);
142 has_companion = false;
146 if (state.gps != null && state.gps.connected) {
148 pane.add("Site Map", sitemap);
153 pane.remove(sitemap);
158 for (AltosFlightDisplay d : displays) {
160 d.show(state, listener_state);
161 } catch (Exception e) {
162 System.out.printf("Exception showing %s\n", d.getName());
168 public void set_exit_on_close() {
169 exit_on_close = true;
173 AltosUIFreqList frequencies;
174 AltosUIRateList rates;
175 AltosUITelemetryList telemetries;
178 ActionListener show_timer;
180 public AltosFlightUI(AltosVoice in_voice, AltosFlightReader in_reader, final int serial) {
181 AltosUIPreferences.set_component(this);
183 displays = new LinkedList<AltosFlightDisplay>();
188 bag = getContentPane();
189 bag.setLayout(new GridBagLayout());
191 GridBagConstraints c = new GridBagConstraints();
193 setTitle(String.format("AltOS %s", reader.name));
195 /* Stick channel selector at top of table for telemetry monitoring */
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();
205 reader.set_frequency(frequency);
206 } catch (TimeoutException te) {
207 } catch (InterruptedException ie) {
209 reader.save_frequency();
216 c.insets = new Insets(3, 3, 3, 3);
217 c.fill = GridBagConstraints.NONE;
218 c.anchor = GridBagConstraints.WEST;
219 bag.add (frequencies, c);
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();
227 reader.set_telemetry_rate(rate);
228 } catch (TimeoutException te) {
229 } catch (InterruptedException ie) {
231 reader.save_telemetry_rate();
234 rates.setEnabled(reader.supports_telemetry_rate(AltosLib.ao_telemetry_rate_2400));
239 c.insets = new Insets(3, 3, 3, 3);
240 c.fill = GridBagConstraints.NONE;
241 c.anchor = GridBagConstraints.WEST;
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();
258 c.fill = GridBagConstraints.NONE;
259 c.anchor = GridBagConstraints.WEST;
260 bag.add (telemetries, c);
261 c.insets = new Insets(0, 0, 0, 0);
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";
270 version = "Telemetry: None";
272 telemetry = new JLabel(version);
277 c.fill = GridBagConstraints.NONE;
278 c.anchor = GridBagConstraints.WEST;
279 bag.add (telemetry, c);
280 c.insets = new Insets(0, 0, 0, 0);
284 /* Flight status is always visible */
285 flightStatus = new AltosFlightStatus();
286 displays.add(flightStatus);
289 c.fill = GridBagConstraints.HORIZONTAL;
292 bag.add(flightStatus, c);
294 /* The rest of the window uses a tabbed pane to
295 * show one of the alternate data views
297 pane = new JTabbedPane();
299 pad = new AltosPad();
301 pane.add("Status", pad);
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);
312 flightInfo = new AltosInfoTable();
313 displays.add(flightInfo);
314 pane.add("Table", new JScrollPane(flightInfo));
316 companion = new AltosCompanionInfo();
317 displays.add(companion);
318 has_companion = false;
321 sitemap = new AltosUIMap();
322 displays.add(sitemap);
325 /* Make the tabbed pane use the rest of the window space */
328 c.fill = GridBagConstraints.BOTH;
333 setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
335 AltosUIPreferences.register_font_listener(this);
336 AltosPreferences.register_units_listener(this);
338 addWindowListener(new WindowAdapter() {
340 public void windowClosing(WindowEvent e) {
344 AltosUIPreferences.unregister_font_listener(AltosFlightUI.this);
345 AltosPreferences.unregister_units_listener(AltosFlightUI.this);
354 thread = new AltosDisplayThread(this, voice, this, reader);
356 status_update = new AltosFlightStatusUpdate(flightStatus);
358 new javax.swing.Timer(100, status_update).start();
363 public AltosFlightUI (AltosVoice in_voice, AltosFlightReader in_reader) {
364 this(in_voice, in_reader, -1);