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