ddc54cbd18a82958b4cefc1bd809a43cc7b80c1e
[fw/altos] / altosui / AltosFlightUI.java
1 /*
2  * Copyright © 2010 Keith Packard <keithp@keithp.com>
3  *
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.
7  *
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.
12  *
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.
16  */
17
18 package altosui;
19
20 import java.awt.*;
21 import java.awt.event.*;
22 import javax.swing.*;
23 import javax.swing.filechooser.FileNameExtensionFilter;
24 import javax.swing.table.*;
25 import java.io.*;
26 import java.util.*;
27 import java.text.*;
28 import java.util.prefs.*;
29 import java.util.concurrent.*;
30 import org.altusmetrum.AltosLib.*;
31
32 public class AltosFlightUI extends AltosFrame implements AltosFlightDisplay, AltosFontListener {
33         AltosVoice              voice;
34         AltosFlightReader       reader;
35         AltosDisplayThread      thread;
36
37         JTabbedPane     pane;
38
39         AltosPad        pad;
40         AltosAscent     ascent;
41         AltosDescent    descent;
42         AltosLanded     landed;
43         AltosCompanionInfo      companion;
44         AltosSiteMap    sitemap;
45         boolean         has_map;
46         boolean         has_companion;
47
48         private AltosFlightStatus flightStatus;
49         private AltosInfoTable flightInfo;
50
51         boolean exit_on_close = false;
52
53         JComponent cur_tab = null;
54         JComponent which_tab(AltosState state) {
55                 if (state.state < Altos.ao_flight_boost)
56                         return pad;
57                 if (state.state <= Altos.ao_flight_coast)
58                         return ascent;
59                 if (state.state <= Altos.ao_flight_main)
60                         return descent;
61                 return landed;
62         }
63
64         void stop_display() {
65                 if (thread != null && thread.isAlive()) {
66                         thread.interrupt();
67                         try {
68                                 thread.join();
69                         } catch (InterruptedException ie) {}
70                 }
71                 thread = null;
72         }
73
74         void disconnect() {
75                 stop_display();
76         }
77
78         public void reset() {
79                 pad.reset();
80                 ascent.reset();
81                 descent.reset();
82                 landed.reset();
83                 flightInfo.clear();
84                 sitemap.reset();
85         }
86
87         public void set_font() {
88                 pad.set_font();
89                 ascent.set_font();
90                 descent.set_font();
91                 landed.set_font();
92                 flightStatus.set_font();
93                 flightInfo.set_font();
94                 sitemap.set_font();
95                 companion.set_font();
96         }
97
98         public void font_size_changed(int font_size) {
99                 set_font();
100         }
101
102
103         AltosFlightStatusUpdate status_update;
104
105         public void show(AltosState state, int crc_errors) {
106                 status_update.saved_state = state;
107                 JComponent tab = which_tab(state);
108                 try {
109                 pad.show(state, crc_errors);
110                 ascent.show(state, crc_errors);
111                 descent.show(state, crc_errors);
112                 landed.show(state, crc_errors);
113                 if (tab != cur_tab) {
114                         if (cur_tab == pane.getSelectedComponent()) {
115                                 pane.setSelectedComponent(tab);
116                         }
117                         cur_tab = tab;
118                 }
119                 flightStatus.show(state, crc_errors);
120                 flightInfo.show(state, crc_errors);
121
122                 if (state.data.companion != null) {
123                         if (!has_companion) {
124                                 pane.add("Companion", companion);
125                                 has_companion= true;
126                         }
127                         companion.show(state, crc_errors);
128                 } else {
129                         if (has_companion) {
130                                 pane.remove(companion);
131                                 has_companion = false;
132                         }
133                 }
134                 if (state.gps != null && state.gps.connected) {
135                         if (!has_map) {
136                                 pane.add("Site Map", sitemap);
137                                 has_map = true;
138                         }
139                         sitemap.show(state, crc_errors);
140                 } else {
141                         if (has_map) {
142                                 pane.remove(sitemap);
143                                 has_map = false;
144                         }
145                 }
146                 } catch (Exception e) {
147                         System.out.print("Show exception" + e);
148                 }
149         }
150
151         public void set_exit_on_close() {
152                 exit_on_close = true;
153         }
154
155         Container       bag;
156         AltosFreqList   frequencies;
157         JComboBox       telemetries;
158         JLabel          telemetry;
159
160         ActionListener  show_timer;
161
162         public AltosFlightUI(AltosVoice in_voice, AltosFlightReader in_reader, final int serial) {
163                 AltosUIPreferences.set_component(this);
164
165                 voice = in_voice;
166                 reader = in_reader;
167
168                 bag = getContentPane();
169                 bag.setLayout(new GridBagLayout());
170
171                 GridBagConstraints c = new GridBagConstraints();
172
173                 java.net.URL imgURL = AltosUI.class.getResource("/altus-metrum-16x16.jpg");
174                 if (imgURL != null)
175                         setIconImage(new ImageIcon(imgURL).getImage());
176
177                 setTitle(String.format("AltOS %s", reader.name));
178
179                 /* Stick channel selector at top of table for telemetry monitoring */
180                 if (serial >= 0) {
181                         // Channel menu
182                         frequencies = new AltosFreqList(AltosUIPreferences.frequency(serial));
183                         frequencies.set_product("Monitor");
184                         frequencies.set_serial(serial);
185                         frequencies.addActionListener(new ActionListener() {
186                                         public void actionPerformed(ActionEvent e) {
187                                                 double frequency = frequencies.frequency();
188                                                 try {
189                                                         reader.set_frequency(frequency);
190                                                 } catch (TimeoutException te) {
191                                                 } catch (InterruptedException ie) {
192                                                 }
193                                                 reader.save_frequency();
194                                         }
195                         });
196                         c.gridx = 0;
197                         c.gridy = 0;
198                         c.weightx = 0;
199                         c.weighty = 0;
200                         c.insets = new Insets(3, 3, 3, 3);
201                         c.fill = GridBagConstraints.NONE;
202                         c.anchor = GridBagConstraints.WEST;
203                         bag.add (frequencies, c);
204
205                         // Telemetry format menu
206                         if (reader.supports_telemetry(Altos.ao_telemetry_standard)) {
207                                 telemetries = new JComboBox();
208                                 for (int i = 1; i <= Altos.ao_telemetry_max; i++) 
209                                         telemetries.addItem(Altos.telemetry_name(i));
210                                 int telemetry = AltosPreferences.telemetry(serial);
211                                 if (telemetry <= Altos.ao_telemetry_off ||
212                                     telemetry > Altos.ao_telemetry_max)
213                                         telemetry = Altos.ao_telemetry_standard;
214                                 telemetries.setSelectedIndex(telemetry - 1);
215                                 telemetries.setMaximumRowCount(Altos.ao_telemetry_max);
216                                 telemetries.setPreferredSize(null);
217                                 telemetries.revalidate();
218                                 telemetries.addActionListener(new ActionListener() {
219                                                 public void actionPerformed(ActionEvent e) {
220                                                         int telemetry = telemetries.getSelectedIndex() + 1;
221                                                         reader.set_telemetry(telemetry);
222                                                         reader.save_telemetry();
223                                                 }
224                                         });
225                                 c.gridx = 1;
226                                 c.gridy = 0;
227                                 c.weightx = 0;
228                                 c.weighty = 0;
229                                 c.fill = GridBagConstraints.NONE;
230                                 c.anchor = GridBagConstraints.WEST;
231                                 bag.add (telemetries, c);
232                                 c.insets = new Insets(0, 0, 0, 0);
233                         } else {
234                                 String  version;
235
236                                 if (reader.supports_telemetry(Altos.ao_telemetry_0_9))
237                                         version = "Telemetry: 0.9";
238                                 else if (reader.supports_telemetry(Altos.ao_telemetry_0_8))
239                                         version = "Telemetry: 0.8";
240                                 else
241                                         version = "Telemetry: None";
242
243                                 telemetry = new JLabel(version);
244                                 c.gridx = 1;
245                                 c.gridy = 0;
246                                 c.weightx = 0;
247                                 c.weighty = 0;
248                                 c.fill = GridBagConstraints.NONE;
249                                 c.anchor = GridBagConstraints.WEST;
250                                 bag.add (telemetry, c);
251                                 c.insets = new Insets(0, 0, 0, 0);
252                         }
253                 }
254
255                 /* Flight status is always visible */
256                 flightStatus = new AltosFlightStatus();
257                 c.gridx = 0;
258                 c.gridy = 1;
259                 c.fill = GridBagConstraints.HORIZONTAL;
260                 c.weightx = 1;
261                 c.gridwidth = 2;
262                 bag.add(flightStatus, c);
263                 c.gridwidth = 1;
264
265                 /* The rest of the window uses a tabbed pane to
266                  * show one of the alternate data views
267                  */
268                 pane = new JTabbedPane();
269
270                 pad = new AltosPad();
271                 pane.add("Launch Pad", pad);
272
273                 ascent = new AltosAscent();
274                 pane.add("Ascent", ascent);
275
276                 descent = new AltosDescent();
277                 pane.add("Descent", descent);
278
279                 landed = new AltosLanded(reader);
280                 pane.add("Landed", landed);
281
282                 flightInfo = new AltosInfoTable();
283                 pane.add("Table", new JScrollPane(flightInfo));
284
285                 companion = new AltosCompanionInfo();
286                 has_companion = false;
287
288                 sitemap = new AltosSiteMap();
289                 has_map = false;
290
291                 /* Make the tabbed pane use the rest of the window space */
292                 c.gridx = 0;
293                 c.gridy = 2;
294                 c.fill = GridBagConstraints.BOTH;
295                 c.weightx = 1;
296                 c.weighty = 1;
297                 c.gridwidth = 2;
298                 bag.add(pane, c);
299
300                 setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
301
302                 AltosUIPreferences.register_font_listener(this);
303
304                 addWindowListener(new WindowAdapter() {
305                                 @Override
306                                 public void windowClosing(WindowEvent e) {
307                                         disconnect();
308                                         setVisible(false);
309                                         dispose();
310                                         AltosUIPreferences.unregister_font_listener(AltosFlightUI.this);
311                                         if (exit_on_close)
312                                                 System.exit(0);
313                                 }
314                         });
315
316                 pack();
317                 setVisible(true);
318
319                 thread = new AltosDisplayThread(this, voice, this, reader);
320
321                 status_update = new AltosFlightStatusUpdate(flightStatus);
322
323                 new javax.swing.Timer(100, status_update).start();
324
325                 thread.start();
326         }
327
328         public AltosFlightUI (AltosVoice in_voice, AltosFlightReader in_reader) {
329                 this(in_voice, in_reader, -1);
330         }
331 }