Merge branch 'buttonbox' of git://git.gag.com/fw/altos into buttonbox
[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         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         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                 pad.show(state, crc_errors);
89                 ascent.show(state, crc_errors);
90                 descent.show(state, crc_errors);
91                 landed.show(state, crc_errors);
92                 if (tab != cur_tab) {
93                         if (cur_tab == pane.getSelectedComponent()) {
94                                 pane.setSelectedComponent(tab);
95                         }
96                         cur_tab = tab;
97                 }
98                 flightStatus.show(state, crc_errors);
99                 flightInfo.show(state, crc_errors);
100                 sitemap.show(state, crc_errors);
101         }
102
103         public void set_exit_on_close() {
104                 exit_on_close = true;
105         }
106
107         Container       bag;
108         JComboBox       channels;
109
110         public AltosFlightUI(AltosVoice in_voice, AltosFlightReader in_reader, final int serial) {
111                 AltosPreferences.init(this);
112
113                 voice = in_voice;
114                 reader = in_reader;
115
116                 bag = getContentPane();
117                 bag.setLayout(new GridBagLayout());
118
119                 GridBagConstraints c = new GridBagConstraints();
120
121                 java.net.URL imgURL = AltosUI.class.getResource("/altus-metrum-16x16.jpg");
122                 if (imgURL != null)
123                         setIconImage(new ImageIcon(imgURL).getImage());
124
125                 setTitle(String.format("AltOS %s", reader.name));
126
127                 /* Stick channel selector at top of table for telemetry monitoring */
128                 if (serial >= 0) {
129                         // Channel menu
130                         channels = new AltosChannelMenu(AltosPreferences.channel(serial));
131                         channels.addActionListener(new ActionListener() {
132                                 public void actionPerformed(ActionEvent e) {
133                                         int channel = channels.getSelectedIndex();
134                                         reader.set_channel(channel);
135                                 }
136                         });
137                         c.gridx = 0;
138                         c.gridy = 0;
139                         c.anchor = GridBagConstraints.WEST;
140                         bag.add (channels, c);
141                 }
142
143                 /* Flight status is always visible */
144                 flightStatus = new AltosFlightStatus();
145                 c.gridx = 0;
146                 c.gridy = 1;
147                 c.fill = GridBagConstraints.HORIZONTAL;
148                 c.weightx = 1;
149                 bag.add(flightStatus, c);
150
151                 /* The rest of the window uses a tabbed pane to
152                  * show one of the alternate data views
153                  */
154                 pane = new JTabbedPane();
155
156                 pad = new AltosPad();
157                 pane.add("Launch Pad", pad);
158
159                 ascent = new AltosAscent();
160                 pane.add("Ascent", ascent);
161
162                 descent = new AltosDescent();
163                 pane.add("Descent", descent);
164
165                 landed = new AltosLanded();
166                 pane.add("Landed", landed);
167
168                 flightInfo = new AltosInfoTable();
169                 pane.add("Table", new JScrollPane(flightInfo));
170
171                 sitemap = new AltosSiteMap();
172                 pane.add("Site Map", sitemap);
173
174                 /* Make the tabbed pane use the rest of the window space */
175                 c.gridx = 0;
176                 c.gridy = 2;
177                 c.fill = GridBagConstraints.BOTH;
178                 c.weightx = 1;
179                 c.weighty = 1;
180                 bag.add(pane, c);
181
182                 setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
183                 addWindowListener(new WindowAdapter() {
184                                 @Override
185                                 public void windowClosing(WindowEvent e) {
186                                         disconnect();
187                                         setVisible(false);
188                                         dispose();
189                                         if (exit_on_close)
190                                                 System.exit(0);
191                                 }
192                         });
193
194                 pack();
195                 setVisible(true);
196
197                 thread = new AltosDisplayThread(this, voice, this, reader);
198
199                 thread.start();
200         }
201
202         public AltosFlightUI (AltosVoice in_voice, AltosFlightReader in_reader) {
203                 this(in_voice, in_reader, -1);
204         }
205 }