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