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 javax.swing.filechooser.FileNameExtensionFilter;
24 import javax.swing.table.*;
28 import java.util.prefs.*;
29 import java.util.concurrent.*;
31 public class AltosFlightUI extends AltosFrame implements AltosFlightDisplay, AltosFontListener {
33 AltosFlightReader reader;
34 AltosDisplayThread thread;
42 AltosCompanionInfo companion;
45 boolean has_companion;
47 private AltosFlightStatus flightStatus;
48 private AltosInfoTable flightInfo;
50 boolean exit_on_close = false;
52 JComponent cur_tab = null;
53 JComponent which_tab(AltosState state) {
54 if (state.state < Altos.ao_flight_boost)
56 if (state.state <= Altos.ao_flight_coast)
58 if (state.state <= Altos.ao_flight_main)
64 if (thread != null && thread.isAlive()) {
68 } catch (InterruptedException ie) {}
86 public void set_font() {
91 flightStatus.set_font();
92 flightInfo.set_font();
97 public void font_size_changed(int font_size) {
102 AltosFlightStatusUpdate status_update;
104 public void show(AltosState state, int crc_errors) {
105 status_update.saved_state = state;
106 JComponent tab = which_tab(state);
108 pad.show(state, crc_errors);
109 ascent.show(state, crc_errors);
110 descent.show(state, crc_errors);
111 landed.show(state, crc_errors);
112 if (tab != cur_tab) {
113 if (cur_tab == pane.getSelectedComponent()) {
114 pane.setSelectedComponent(tab);
118 flightStatus.show(state, crc_errors);
119 flightInfo.show(state, crc_errors);
121 if (state.data.companion != null) {
122 if (!has_companion) {
123 pane.add("Companion", companion);
126 companion.show(state, crc_errors);
129 pane.remove(companion);
130 has_companion = false;
133 if (state.gps != null && state.gps.connected) {
135 pane.add("Site Map", sitemap);
138 sitemap.show(state, crc_errors);
141 pane.remove(sitemap);
145 } catch (Exception e) {
146 System.out.print("Show exception" + e);
150 public void set_exit_on_close() {
151 exit_on_close = true;
155 AltosFreqList frequencies;
156 JComboBox telemetries;
159 ActionListener show_timer;
161 public AltosFlightUI(AltosVoice in_voice, AltosFlightReader in_reader, final int serial) {
162 AltosPreferences.set_component(this);
167 bag = getContentPane();
168 bag.setLayout(new GridBagLayout());
170 GridBagConstraints c = new GridBagConstraints();
172 java.net.URL imgURL = AltosUI.class.getResource("/altus-metrum-16x16.jpg");
174 setIconImage(new ImageIcon(imgURL).getImage());
176 setTitle(String.format("AltOS %s", reader.name));
178 /* Stick channel selector at top of table for telemetry monitoring */
181 frequencies = new AltosFreqList(AltosPreferences.frequency(serial));
182 frequencies.set_product("Monitor");
183 frequencies.set_serial(serial);
184 frequencies.addActionListener(new ActionListener() {
185 public void actionPerformed(ActionEvent e) {
186 double frequency = frequencies.frequency();
188 reader.set_frequency(frequency);
189 } catch (TimeoutException te) {
190 } catch (InterruptedException ie) {
192 reader.save_frequency();
199 c.insets = new Insets(3, 3, 3, 3);
200 c.fill = GridBagConstraints.NONE;
201 c.anchor = GridBagConstraints.WEST;
202 bag.add (frequencies, c);
204 // Telemetry format menu
205 if (reader.supports_telemetry(Altos.ao_telemetry_standard)) {
206 telemetries = new JComboBox();
207 for (int i = 1; i <= Altos.ao_telemetry_max; i++)
208 telemetries.addItem(Altos.telemetry_name(i));
209 int telemetry = AltosPreferences.telemetry(serial);
210 if (telemetry <= Altos.ao_telemetry_off ||
211 telemetry > Altos.ao_telemetry_max)
212 telemetry = Altos.ao_telemetry_standard;
213 telemetries.setSelectedIndex(telemetry - 1);
214 telemetries.setMaximumRowCount(Altos.ao_telemetry_max);
215 telemetries.setPreferredSize(null);
216 telemetries.revalidate();
217 telemetries.addActionListener(new ActionListener() {
218 public void actionPerformed(ActionEvent e) {
219 int telemetry = telemetries.getSelectedIndex() + 1;
220 reader.set_telemetry(telemetry);
221 reader.save_telemetry();
228 c.fill = GridBagConstraints.NONE;
229 c.anchor = GridBagConstraints.WEST;
230 bag.add (telemetries, c);
231 c.insets = new Insets(0, 0, 0, 0);
235 if (reader.supports_telemetry(Altos.ao_telemetry_0_9))
236 version = "Telemetry: 0.9";
237 else if (reader.supports_telemetry(Altos.ao_telemetry_0_8))
238 version = "Telemetry: 0.8";
240 version = "Telemetry: None";
242 telemetry = new JLabel(version);
247 c.fill = GridBagConstraints.NONE;
248 c.anchor = GridBagConstraints.WEST;
249 bag.add (telemetry, c);
250 c.insets = new Insets(0, 0, 0, 0);
254 /* Flight status is always visible */
255 flightStatus = new AltosFlightStatus();
258 c.fill = GridBagConstraints.HORIZONTAL;
261 bag.add(flightStatus, c);
264 /* The rest of the window uses a tabbed pane to
265 * show one of the alternate data views
267 pane = new JTabbedPane();
269 pad = new AltosPad();
270 pane.add("Launch Pad", pad);
272 ascent = new AltosAscent();
273 pane.add("Ascent", ascent);
275 descent = new AltosDescent();
276 pane.add("Descent", descent);
278 landed = new AltosLanded(reader);
279 pane.add("Landed", landed);
281 flightInfo = new AltosInfoTable();
282 pane.add("Table", new JScrollPane(flightInfo));
284 companion = new AltosCompanionInfo();
285 has_companion = false;
287 sitemap = new AltosSiteMap();
290 /* Make the tabbed pane use the rest of the window space */
293 c.fill = GridBagConstraints.BOTH;
299 setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
301 AltosPreferences.register_font_listener(this);
303 addWindowListener(new WindowAdapter() {
305 public void windowClosing(WindowEvent e) {
309 AltosPreferences.unregister_font_listener(AltosFlightUI.this);
318 thread = new AltosDisplayThread(this, voice, this, reader);
320 status_update = new AltosFlightStatusUpdate(flightStatus);
322 new javax.swing.Timer(100, status_update).start();
327 public AltosFlightUI (AltosVoice in_voice, AltosFlightReader in_reader) {
328 this(in_voice, in_reader, -1);