Merge remote branch 'aj/sitemap' into buttonbox
[fw/altos] / ao-tools / 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         String[] statusNames = { "Height (m)", "State", "RSSI (dBm)", "Speed (m/s)" };
33         Object[][] statusData = { { "0", "pad", "-50", "0" } };
34
35         AltosVoice              voice;
36         AltosFlightReader       reader;
37         AltosDisplayThread      thread;
38
39         JTabbedPane     pane;
40
41         AltosPad        pad;
42         AltosAscent     ascent;
43         AltosDescent    descent;
44         AltosLanded     landed;
45         AltosSiteMap    sitemap;
46
47         private AltosFlightStatus flightStatus;
48         private AltosInfoTable flightInfo;
49
50         static final int tab_pad = 1;
51         static final int tab_ascent = 2;
52         static final int tab_descent = 3;
53         static final int tab_landed = 4;
54
55         int cur_tab = 0;
56
57         boolean exit_on_close = false;
58
59         int which_tab(AltosState state) {
60                 if (state.state < Altos.ao_flight_boost)
61                         return tab_pad;
62                 if (state.state <= Altos.ao_flight_coast)
63                         return tab_ascent;
64                 if (state.state <= Altos.ao_flight_main)
65                         return tab_descent;
66                 return tab_landed;
67         }
68
69         void stop_display() {
70                 if (thread != null && thread.isAlive()) {
71                         thread.interrupt();
72                         try {
73                                 thread.join();
74                         } catch (InterruptedException ie) {}
75                 }
76                 thread = null;
77         }
78
79         void disconnect() {
80                 stop_display();
81         }
82
83         public void reset() {
84                 pad.reset();
85                 ascent.reset();
86                 descent.reset();
87                 landed.reset();
88                 flightInfo.clear();
89                 sitemap.reset();
90         }
91
92         public void show(AltosState state, int crc_errors) {
93                 int     tab = which_tab(state);
94                 pad.show(state, crc_errors);
95                 ascent.show(state, crc_errors);
96                 descent.show(state, crc_errors);
97                 landed.show(state, crc_errors);
98                 if (tab != cur_tab) {
99                         switch (tab) {
100                         case tab_pad:
101                                 pane.setSelectedComponent(pad);
102                                 break;
103                         case tab_ascent:
104                                 pane.setSelectedComponent(ascent);
105                                 break;
106                         case tab_descent:
107                                 pane.setSelectedComponent(descent);
108                                 break;
109                         case tab_landed:
110                                 pane.setSelectedComponent(landed);
111                         }
112                         cur_tab = tab;
113                 }
114                 flightStatus.show(state, crc_errors);
115                 flightInfo.show(state, crc_errors);
116                 sitemap.show(state, crc_errors);
117         }
118
119         public void set_exit_on_close() {
120                 exit_on_close = true;
121         }
122
123         Container       bag;
124         JComboBox       channels;
125
126         public AltosFlightUI(AltosVoice in_voice, AltosFlightReader in_reader, final int serial) {
127                 AltosPreferences.init(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                                                 AltosPreferences.set_channel(serial, channel);
152                                         }
153                                 });
154                         c.gridx = 0;
155                         c.gridy = 0;
156                         c.anchor = GridBagConstraints.WEST;
157                         bag.add (channels, c);
158                 }
159
160                 /* Flight status is always visible */
161                 flightStatus = new AltosFlightStatus();
162                 c.gridx = 0;
163                 c.gridy = 1;
164                 c.fill = GridBagConstraints.HORIZONTAL;
165                 c.weightx = 1;
166                 bag.add(flightStatus, c);
167
168                 /* The rest of the window uses a tabbed pane to
169                  * show one of the alternate data views
170                  */
171                 pane = new JTabbedPane();
172
173                 pad = new AltosPad();
174                 pane.add("Launch Pad", pad);
175
176                 ascent = new AltosAscent();
177                 pane.add("Ascent", ascent);
178
179                 descent = new AltosDescent();
180                 pane.add("Descent", descent);
181
182                 landed = new AltosLanded();
183                 pane.add("Landed", landed);
184
185                 flightInfo = new AltosInfoTable();
186                 pane.add("Table", new JScrollPane(flightInfo));
187
188                 sitemap = new AltosSiteMap();
189                 pane.add("Site Map", sitemap);
190
191                 /* Make the tabbed pane use the rest of the window space */
192                 c.gridx = 0;
193                 c.gridy = 2;
194                 c.fill = GridBagConstraints.BOTH;
195                 c.weightx = 1;
196                 c.weighty = 1;
197                 bag.add(pane, c);
198
199                 setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
200                 addWindowListener(new WindowAdapter() {
201                                 @Override
202                                 public void windowClosing(WindowEvent e) {
203                                         disconnect();
204                                         setVisible(false);
205                                         dispose();
206                                         if (exit_on_close)
207                                                 System.exit(0);
208                                 }
209                         });
210
211                 pack();
212                 setVisible(true);
213
214                 thread = new AltosDisplayThread(this, voice, this, reader);
215
216                 thread.start();
217         }
218
219         public AltosFlightUI (AltosVoice in_voice, AltosFlightReader in_reader) {
220                 this(in_voice, in_reader, -1);
221         }
222 }