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