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_4.*;
26 import org.altusmetrum.altosuilib_2.*;
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 AltosFreqList frequencies;
174 JComboBox<String> telemetries;
177 ActionListener show_timer;
179 public AltosFlightUI(AltosVoice in_voice, AltosFlightReader in_reader, final int serial) {
180 AltosUIPreferences.set_component(this);
182 displays = new LinkedList<AltosFlightDisplay>();
187 bag = getContentPane();
188 bag.setLayout(new GridBagLayout());
190 GridBagConstraints c = new GridBagConstraints();
192 setTitle(String.format("AltOS %s", reader.name));
194 /* Stick channel selector at top of table for telemetry monitoring */
197 frequencies = new AltosFreqList(AltosUIPreferences.frequency(serial));
198 frequencies.set_product("Monitor");
199 frequencies.set_serial(serial);
200 frequencies.addActionListener(new ActionListener() {
201 public void actionPerformed(ActionEvent e) {
202 double frequency = frequencies.frequency();
204 reader.set_frequency(frequency);
205 } catch (TimeoutException te) {
206 } catch (InterruptedException ie) {
208 reader.save_frequency();
215 c.insets = new Insets(3, 3, 3, 3);
216 c.fill = GridBagConstraints.NONE;
217 c.anchor = GridBagConstraints.WEST;
218 bag.add (frequencies, c);
220 // Telemetry format menu
221 if (reader.supports_telemetry(Altos.ao_telemetry_standard)) {
222 telemetries = new JComboBox<String>();
223 for (int i = 1; i <= Altos.ao_telemetry_max; i++)
224 telemetries.addItem(Altos.telemetry_name(i));
225 int telemetry = AltosPreferences.telemetry(serial);
226 if (telemetry <= Altos.ao_telemetry_off ||
227 telemetry > Altos.ao_telemetry_max)
228 telemetry = Altos.ao_telemetry_standard;
229 telemetries.setSelectedIndex(telemetry - 1);
230 telemetries.setMaximumRowCount(Altos.ao_telemetry_max);
231 telemetries.setPreferredSize(null);
232 telemetries.revalidate();
233 telemetries.addActionListener(new ActionListener() {
234 public void actionPerformed(ActionEvent e) {
235 int telemetry = telemetries.getSelectedIndex() + 1;
236 reader.set_telemetry(telemetry);
237 reader.save_telemetry();
244 c.fill = GridBagConstraints.NONE;
245 c.anchor = GridBagConstraints.WEST;
246 bag.add (telemetries, c);
247 c.insets = new Insets(0, 0, 0, 0);
251 if (reader.supports_telemetry(Altos.ao_telemetry_0_9))
252 version = "Telemetry: 0.9";
253 else if (reader.supports_telemetry(Altos.ao_telemetry_0_8))
254 version = "Telemetry: 0.8";
256 version = "Telemetry: None";
258 telemetry = new JLabel(version);
263 c.fill = GridBagConstraints.NONE;
264 c.anchor = GridBagConstraints.WEST;
265 bag.add (telemetry, c);
266 c.insets = new Insets(0, 0, 0, 0);
270 /* Flight status is always visible */
271 flightStatus = new AltosFlightStatus();
272 displays.add(flightStatus);
275 c.fill = GridBagConstraints.HORIZONTAL;
278 bag.add(flightStatus, c);
281 /* The rest of the window uses a tabbed pane to
282 * show one of the alternate data views
284 pane = new JTabbedPane();
286 pad = new AltosPad();
288 pane.add("Status", pad);
290 ignitor = new AltosIgnitor();
291 displays.add(ignitor);
292 ascent = new AltosAscent();
293 displays.add(ascent);
294 descent = new AltosDescent();
295 displays.add(descent);
296 landed = new AltosLanded(reader);
297 displays.add(landed);
299 flightInfo = new AltosInfoTable();
300 displays.add(flightInfo);
301 pane.add("Table", new JScrollPane(flightInfo));
303 companion = new AltosCompanionInfo();
304 displays.add(companion);
305 has_companion = false;
308 sitemap = new AltosUIMap();
309 displays.add(sitemap);
312 /* Make the tabbed pane use the rest of the window space */
315 c.fill = GridBagConstraints.BOTH;
321 setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
323 AltosUIPreferences.register_font_listener(this);
324 AltosPreferences.register_units_listener(this);
326 addWindowListener(new WindowAdapter() {
328 public void windowClosing(WindowEvent e) {
332 AltosUIPreferences.unregister_font_listener(AltosFlightUI.this);
333 AltosPreferences.unregister_units_listener(AltosFlightUI.this);
342 thread = new AltosDisplayThread(this, voice, this, reader);
344 status_update = new AltosFlightStatusUpdate(flightStatus);
346 new javax.swing.Timer(100, status_update).start();
351 public AltosFlightUI (AltosVoice in_voice, AltosFlightReader in_reader) {
352 this(in_voice, in_reader, -1);