altosui: Add support for telemetry version 4
[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.LinkedBlockingQueue;
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         AltosSiteMap    sitemap;
43         boolean         has_map;
44
45         private AltosFlightStatus flightStatus;
46         private AltosInfoTable flightInfo;
47
48         boolean exit_on_close = false;
49
50         JComponent cur_tab = null;
51         JComponent which_tab(AltosState state) {
52                 if (state.state < Altos.ao_flight_boost)
53                         return pad;
54                 if (state.state <= Altos.ao_flight_coast)
55                         return ascent;
56                 if (state.state <= Altos.ao_flight_main)
57                         return descent;
58                 return landed;
59         }
60
61         void stop_display() {
62                 if (thread != null && thread.isAlive()) {
63                         thread.interrupt();
64                         try {
65                                 thread.join();
66                         } catch (InterruptedException ie) {}
67                 }
68                 thread = null;
69         }
70
71         void disconnect() {
72                 stop_display();
73         }
74
75         public void reset() {
76                 pad.reset();
77                 ascent.reset();
78                 descent.reset();
79                 landed.reset();
80                 flightInfo.clear();
81                 sitemap.reset();
82         }
83
84         public void show(AltosState state, int crc_errors) {
85                 JComponent tab = which_tab(state);
86                 try {
87                 pad.show(state, crc_errors);
88                 ascent.show(state, crc_errors);
89                 descent.show(state, crc_errors);
90                 landed.show(state, crc_errors);
91                 if (tab != cur_tab) {
92                         if (cur_tab == pane.getSelectedComponent()) {
93                                 pane.setSelectedComponent(tab);
94                         }
95                         cur_tab = tab;
96                 }
97                 flightStatus.show(state, crc_errors);
98                 flightInfo.show(state, crc_errors);
99                 if (state.gps != null) {
100                         if (!has_map) {
101                                 pane.add("Site Map", sitemap);
102                                 has_map = true;
103                         }
104                         sitemap.show(state, crc_errors);
105                 } else {
106                         if (has_map) {
107                                 pane.remove(sitemap);
108                                 has_map = false;
109                         }
110                 }
111                 } catch (Exception e) {
112                         System.out.print("Show exception" + e);
113                 }
114         }
115
116         public void set_exit_on_close() {
117                 exit_on_close = true;
118         }
119
120         Container       bag;
121         JComboBox       channels;
122
123         public AltosFlightUI(AltosVoice in_voice, AltosFlightReader in_reader, final int serial) {
124                 AltosPreferences.init(this);
125
126                 voice = in_voice;
127                 reader = in_reader;
128
129                 bag = getContentPane();
130                 bag.setLayout(new GridBagLayout());
131
132                 GridBagConstraints c = new GridBagConstraints();
133
134                 java.net.URL imgURL = AltosUI.class.getResource("/altus-metrum-16x16.jpg");
135                 if (imgURL != null)
136                         setIconImage(new ImageIcon(imgURL).getImage());
137
138                 setTitle(String.format("AltOS %s", reader.name));
139
140                 /* Stick channel selector at top of table for telemetry monitoring */
141                 if (serial >= 0) {
142                         // Channel menu
143                         channels = new AltosChannelMenu(AltosPreferences.channel(serial));
144                         channels.addActionListener(new ActionListener() {
145                                 public void actionPerformed(ActionEvent e) {
146                                         int channel = channels.getSelectedIndex();
147                                         reader.set_channel(channel);
148                                 }
149                         });
150                         c.gridx = 0;
151                         c.gridy = 0;
152                         c.anchor = GridBagConstraints.WEST;
153                         bag.add (channels, c);
154                 }
155
156                 /* Flight status is always visible */
157                 flightStatus = new AltosFlightStatus();
158                 c.gridx = 0;
159                 c.gridy = 1;
160                 c.fill = GridBagConstraints.HORIZONTAL;
161                 c.weightx = 1;
162                 bag.add(flightStatus, c);
163
164                 /* The rest of the window uses a tabbed pane to
165                  * show one of the alternate data views
166                  */
167                 pane = new JTabbedPane();
168
169                 pad = new AltosPad();
170                 pane.add("Launch Pad", pad);
171
172                 ascent = new AltosAscent();
173                 pane.add("Ascent", ascent);
174
175                 descent = new AltosDescent();
176                 pane.add("Descent", descent);
177
178                 landed = new AltosLanded();
179                 pane.add("Landed", landed);
180
181                 flightInfo = new AltosInfoTable();
182                 pane.add("Table", new JScrollPane(flightInfo));
183
184                 sitemap = new AltosSiteMap();
185                 has_map = false;
186
187                 /* Make the tabbed pane use the rest of the window space */
188                 c.gridx = 0;
189                 c.gridy = 2;
190                 c.fill = GridBagConstraints.BOTH;
191                 c.weightx = 1;
192                 c.weighty = 1;
193                 bag.add(pane, c);
194
195                 setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
196                 addWindowListener(new WindowAdapter() {
197                                 @Override
198                                 public void windowClosing(WindowEvent e) {
199                                         disconnect();
200                                         setVisible(false);
201                                         dispose();
202                                         if (exit_on_close)
203                                                 System.exit(0);
204                                 }
205                         });
206
207                 pack();
208                 setVisible(true);
209
210                 thread = new AltosDisplayThread(this, voice, this, reader);
211
212                 thread.start();
213         }
214
215         public AltosFlightUI (AltosVoice in_voice, AltosFlightReader in_reader) {
216                 this(in_voice, in_reader, -1);
217         }
218 }