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.*;
23 import java.util.concurrent.*;
24 import org.altusmetrum.altoslib_4.*;
25 import org.altusmetrum.altosuilib_2.*;
27 public class AltosFlightUI extends AltosUIFrame implements AltosFlightDisplay {
29 AltosFlightReader reader;
30 AltosDisplayThread thread;
39 AltosCompanionInfo companion;
42 boolean has_companion;
46 private AltosFlightStatus flightStatus;
47 private AltosInfoTable flightInfo;
49 boolean exit_on_close = false;
51 JComponent cur_tab = null;
52 JComponent which_tab(AltosState state) {
53 if (state.state < Altos.ao_flight_boost)
55 if (state.state <= Altos.ao_flight_coast)
57 if (state.state <= Altos.ao_flight_main)
63 if (thread != null && thread.isAlive()) {
67 } catch (InterruptedException ie) {}
86 public void font_size_changed(int font_size) {
87 pad.font_size_changed(font_size);
88 ignitor.font_size_changed(font_size);
89 ascent.font_size_changed(font_size);
90 descent.font_size_changed(font_size);
91 landed.font_size_changed(font_size);
92 flightStatus.font_size_changed(font_size);
93 flightInfo.font_size_changed(font_size);
94 sitemap.font_size_changed(font_size);
95 companion.font_size_changed(font_size);
98 public void units_changed(boolean imperial_units) {
99 pad.units_changed(imperial_units);
100 ignitor.units_changed(imperial_units);
101 ascent.units_changed(imperial_units);
102 descent.units_changed(imperial_units);
103 landed.units_changed(imperial_units);
104 flightStatus.units_changed(imperial_units);
105 flightInfo.units_changed(imperial_units);
106 sitemap.units_changed(imperial_units);
107 companion.units_changed(imperial_units);
110 AltosFlightStatusUpdate status_update;
112 public void show(AltosState state, AltosListenerState listener_state) {
113 status_update.saved_state = state;
116 state = new AltosState();
118 pad.show(state, listener_state);
120 if (state.state != Altos.ao_flight_startup) {
122 pane.setTitleAt(0, "Launch Pad");
124 pane.add(descent, 2);
130 ascent.show(state, listener_state);
131 descent.show(state, listener_state);
132 landed.show(state, listener_state);
134 JComponent tab = which_tab(state);
135 if (tab != cur_tab) {
136 if (cur_tab == pane.getSelectedComponent()) {
137 pane.setSelectedComponent(tab);
141 flightStatus.show(state, listener_state);
142 flightInfo.show(state, listener_state);
144 if (ignitor.should_show(state)) {
146 pane.add("Ignitor", ignitor);
149 ignitor.show(state, listener_state);
152 pane.remove(ignitor);
157 if (state.companion != null) {
158 if (!has_companion) {
159 pane.add("Companion", companion);
162 companion.show(state, listener_state);
165 pane.remove(companion);
166 has_companion = false;
169 if (state.gps != null && state.gps.connected) {
171 pane.add("Site Map", sitemap);
174 sitemap.show(state, listener_state);
177 pane.remove(sitemap);
183 public void set_exit_on_close() {
184 exit_on_close = true;
188 AltosFreqList frequencies;
189 JComboBox<String> telemetries;
192 ActionListener show_timer;
194 public AltosFlightUI(AltosVoice in_voice, AltosFlightReader in_reader, final int serial) {
195 AltosUIPreferences.set_component(this);
200 bag = getContentPane();
201 bag.setLayout(new GridBagLayout());
203 GridBagConstraints c = new GridBagConstraints();
205 setTitle(String.format("AltOS %s", reader.name));
207 /* Stick channel selector at top of table for telemetry monitoring */
210 frequencies = new AltosFreqList(AltosUIPreferences.frequency(serial));
211 frequencies.set_product("Monitor");
212 frequencies.set_serial(serial);
213 frequencies.addActionListener(new ActionListener() {
214 public void actionPerformed(ActionEvent e) {
215 double frequency = frequencies.frequency();
217 reader.set_frequency(frequency);
218 } catch (TimeoutException te) {
219 } catch (InterruptedException ie) {
221 reader.save_frequency();
228 c.insets = new Insets(3, 3, 3, 3);
229 c.fill = GridBagConstraints.NONE;
230 c.anchor = GridBagConstraints.WEST;
231 bag.add (frequencies, c);
233 // Telemetry format menu
234 if (reader.supports_telemetry(Altos.ao_telemetry_standard)) {
235 telemetries = new JComboBox<String>();
236 for (int i = 1; i <= Altos.ao_telemetry_max; i++)
237 telemetries.addItem(Altos.telemetry_name(i));
238 int telemetry = AltosPreferences.telemetry(serial);
239 if (telemetry <= Altos.ao_telemetry_off ||
240 telemetry > Altos.ao_telemetry_max)
241 telemetry = Altos.ao_telemetry_standard;
242 telemetries.setSelectedIndex(telemetry - 1);
243 telemetries.setMaximumRowCount(Altos.ao_telemetry_max);
244 telemetries.setPreferredSize(null);
245 telemetries.revalidate();
246 telemetries.addActionListener(new ActionListener() {
247 public void actionPerformed(ActionEvent e) {
248 int telemetry = telemetries.getSelectedIndex() + 1;
249 reader.set_telemetry(telemetry);
250 reader.save_telemetry();
257 c.fill = GridBagConstraints.NONE;
258 c.anchor = GridBagConstraints.WEST;
259 bag.add (telemetries, c);
260 c.insets = new Insets(0, 0, 0, 0);
264 if (reader.supports_telemetry(Altos.ao_telemetry_0_9))
265 version = "Telemetry: 0.9";
266 else if (reader.supports_telemetry(Altos.ao_telemetry_0_8))
267 version = "Telemetry: 0.8";
269 version = "Telemetry: None";
271 telemetry = new JLabel(version);
276 c.fill = GridBagConstraints.NONE;
277 c.anchor = GridBagConstraints.WEST;
278 bag.add (telemetry, c);
279 c.insets = new Insets(0, 0, 0, 0);
283 /* Flight status is always visible */
284 flightStatus = new AltosFlightStatus();
287 c.fill = GridBagConstraints.HORIZONTAL;
290 bag.add(flightStatus, c);
293 /* The rest of the window uses a tabbed pane to
294 * show one of the alternate data views
296 pane = new JTabbedPane();
298 pad = new AltosPad();
299 pane.add("Status", pad);
301 ignitor = new AltosIgnitor();
302 ascent = new AltosAscent();
303 descent = new AltosDescent();
304 landed = new AltosLanded(reader);
306 flightInfo = new AltosInfoTable();
307 pane.add("Table", new JScrollPane(flightInfo));
309 companion = new AltosCompanionInfo();
310 has_companion = false;
313 sitemap = new AltosUIMap();
316 /* Make the tabbed pane use the rest of the window space */
319 c.fill = GridBagConstraints.BOTH;
325 setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
327 AltosUIPreferences.register_font_listener(this);
328 AltosPreferences.register_units_listener(this);
330 addWindowListener(new WindowAdapter() {
332 public void windowClosing(WindowEvent e) {
336 AltosUIPreferences.unregister_font_listener(AltosFlightUI.this);
337 AltosPreferences.unregister_units_listener(AltosFlightUI.this);
346 thread = new AltosDisplayThread(this, voice, this, reader);
348 status_update = new AltosFlightStatusUpdate(flightStatus);
350 new javax.swing.Timer(100, status_update).start();
355 public AltosFlightUI (AltosVoice in_voice, AltosFlightReader in_reader) {
356 this(in_voice, in_reader, -1);