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_9.*;
26 import org.altusmetrum.altosuilib_9.*;
28 public class AltosFlightUI extends AltosUIFrame implements AltosFlightDisplay {
30 AltosFlightReader reader;
31 AltosDisplayThread thread;
33 LinkedList<AltosFlightDisplay> displays;
42 AltosCompanionInfo companion;
43 AltosUIMapNew sitemap;
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;
100 status_update.saved_listener_state = listener_state;
103 state = new AltosState();
105 if (state.state() != Altos.ao_flight_startup) {
107 pane.setTitleAt(0, "Launch Pad");
109 pane.add(descent, 2);
115 JComponent tab = which_tab(state);
116 if (tab != cur_tab) {
117 if (cur_tab == pane.getSelectedComponent()) {
118 pane.setSelectedComponent(tab);
123 if (ignitor.should_show(state)) {
125 pane.add("Ignitor", ignitor);
130 pane.remove(ignitor);
135 if (state.companion != null) {
136 if (!has_companion) {
137 pane.add("Companion", companion);
142 pane.remove(companion);
143 has_companion = false;
147 if (state.gps != null && state.gps.connected) {
149 pane.add("Site Map", sitemap);
154 pane.remove(sitemap);
159 for (AltosFlightDisplay d : displays) {
161 d.show(state, listener_state);
162 } catch (Exception e) {
163 System.out.printf("Exception showing %s\n", d.getName());
169 public void set_exit_on_close() {
170 exit_on_close = true;
174 AltosUIFreqList frequencies;
175 AltosUIRateList rates;
176 AltosUITelemetryList telemetries;
179 ActionListener show_timer;
181 public AltosFlightUI(AltosVoice in_voice, AltosFlightReader in_reader, final int serial) {
182 AltosUIPreferences.set_component(this);
184 displays = new LinkedList<AltosFlightDisplay>();
189 bag = getContentPane();
190 bag.setLayout(new GridBagLayout());
192 setTitle(String.format("AltOS %s", reader.name));
194 /* Stick channel selector at top of table for telemetry monitoring */
199 frequencies = new AltosUIFreqList(AltosUIPreferences.frequency(serial));
200 frequencies.set_product("Monitor");
201 frequencies.set_serial(serial);
202 frequencies.addActionListener(new ActionListener() {
203 public void actionPerformed(ActionEvent e) {
204 double frequency = frequencies.frequency();
206 reader.set_frequency(frequency);
207 } catch (TimeoutException te) {
208 } catch (InterruptedException ie) {
210 reader.save_frequency();
213 bag.add (frequencies, constraints(0, 1));
215 // Telemetry rate list
216 rates = new AltosUIRateList(AltosUIPreferences.telemetry_rate(serial));
217 rates.addActionListener(new ActionListener() {
218 public void actionPerformed(ActionEvent e) {
219 int rate = rates.rate();
221 reader.set_telemetry_rate(rate);
222 } catch (TimeoutException te) {
223 } catch (InterruptedException ie) {
225 reader.save_telemetry_rate();
228 rates.setEnabled(reader.supports_telemetry_rate(AltosLib.ao_telemetry_rate_2400));
229 bag.add (rates, constraints(1, 1));
231 // Telemetry format list
232 if (reader.supports_telemetry(Altos.ao_telemetry_standard)) {
233 telemetries = new AltosUITelemetryList(serial);
234 telemetries.addActionListener(new ActionListener() {
235 public void actionPerformed(ActionEvent e) {
236 int telemetry = telemetries.get_selected();
237 reader.set_telemetry(telemetry);
238 reader.save_telemetry();
241 bag.add (telemetries, constraints(2, 1));
245 if (reader.supports_telemetry(Altos.ao_telemetry_0_9))
246 version = "Telemetry: 0.9";
247 else if (reader.supports_telemetry(Altos.ao_telemetry_0_8))
248 version = "Telemetry: 0.8";
250 version = "Telemetry: None";
252 telemetry = new JLabel(version);
253 bag.add (telemetry, constraints(2, 1));
259 /* Flight status is always visible */
260 flightStatus = new AltosFlightStatus();
261 displays.add(flightStatus);
262 bag.add(flightStatus, constraints(0, 4, GridBagConstraints.HORIZONTAL));
265 /* The rest of the window uses a tabbed pane to
266 * show one of the alternate data views
268 pane = new JTabbedPane();
270 pad = new AltosPad();
272 pane.add("Status", pad);
274 ignitor = new AltosIgnitor();
275 displays.add(ignitor);
276 ascent = new AltosAscent();
277 displays.add(ascent);
278 descent = new AltosDescent();
279 displays.add(descent);
280 landed = new AltosLanded(reader);
281 displays.add(landed);
283 flightInfo = new AltosInfoTable();
284 displays.add(flightInfo);
285 pane.add("Table", new JScrollPane(flightInfo));
287 companion = new AltosCompanionInfo();
288 displays.add(companion);
289 has_companion = false;
292 sitemap = new AltosUIMapNew();
293 displays.add(sitemap);
296 /* Make the tabbed pane use the rest of the window space */
297 bag.add(pane, constraints(0, 4, GridBagConstraints.BOTH));
299 setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
301 AltosUIPreferences.register_font_listener(this);
302 AltosPreferences.register_units_listener(this);
304 status_update = new AltosFlightStatusUpdate(flightStatus);
306 flightStatus.start(status_update);
308 addWindowListener(new WindowAdapter() {
310 public void windowClosing(WindowEvent e) {
315 AltosUIPreferences.unregister_font_listener(AltosFlightUI.this);
316 AltosPreferences.unregister_units_listener(AltosFlightUI.this);
325 thread = new AltosDisplayThread(this, voice, this, reader);
330 public AltosFlightUI (AltosVoice in_voice, AltosFlightReader in_reader) {
331 this(in_voice, in_reader, -1);