altosui: With --replay option, exit when replay window is closed
[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         private Box vbox;
40
41         JTabbedPane     pane;
42
43         AltosPad        pad;
44         AltosAscent     ascent;
45         AltosDescent    descent;
46         AltosLanded     landed;
47
48         private AltosFlightStatus flightStatus;
49         private JScrollPane flightInfoPane;
50         private AltosInfoTable flightInfo;
51
52         static final int tab_pad = 1;
53         static final int tab_ascent = 2;
54         static final int tab_descent = 3;
55         static final int tab_landed = 4;
56
57         int cur_tab = 0;
58
59         boolean exit_on_close = false;
60
61         int which_tab(AltosState state) {
62                 if (state.state < Altos.ao_flight_boost)
63                         return tab_pad;
64                 if (state.state <= Altos.ao_flight_coast)
65                         return tab_ascent;
66                 if (state.state <= Altos.ao_flight_main)
67                         return tab_descent;
68                 return tab_landed;
69         }
70
71         public int width() {
72                 return flightInfo.width();
73         }
74
75         public int height() {
76                 return flightStatus.height() + flightInfo.height();
77         }
78
79         void stop_display() {
80                 if (thread != null && thread.isAlive()) {
81                         thread.interrupt();
82                         try {
83                                 thread.join();
84                         } catch (InterruptedException ie) {}
85                 }
86                 thread = null;
87         }
88
89         void disconnect() {
90                 stop_display();
91         }
92
93         public void reset() {
94                 pad.reset();
95                 ascent.reset();
96                 descent.reset();
97                 landed.reset();
98                 flightInfo.clear();
99         }
100
101         public void show(AltosState state, int crc_errors) {
102                 int     tab = which_tab(state);
103                 pad.show(state, crc_errors);
104                 ascent.show(state, crc_errors);
105                 descent.show(state, crc_errors);
106                 landed.show(state, crc_errors);
107                 if (tab != cur_tab) {
108                         switch (tab) {
109                         case tab_pad:
110                                 pane.setSelectedComponent(pad);
111                                 break;
112                         case tab_ascent:
113                                 pane.setSelectedComponent(ascent);
114                                 break;
115                         case tab_descent:
116                                 pane.setSelectedComponent(descent);
117                                 break;
118                         case tab_landed:
119                                 pane.setSelectedComponent(landed);
120                         }
121                         cur_tab = tab;
122                 }
123                 flightStatus.show(state, crc_errors);
124                 flightInfo.show(state, crc_errors);
125         }
126
127         public void set_exit_on_close() {
128                 exit_on_close = true;
129         }
130
131         public AltosFlightUI(AltosVoice in_voice, AltosFlightReader in_reader, final int serial) {
132                 AltosPreferences.init(this);
133
134                 voice = in_voice;
135                 reader = in_reader;
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                 flightStatus = new AltosFlightStatus();
144
145                 vbox = new Box (BoxLayout.Y_AXIS);
146                 vbox.add(flightStatus);
147
148                 pane = new JTabbedPane();
149
150                 pad = new AltosPad();
151                 pane.add("Launch Pad", pad);
152
153                 ascent = new AltosAscent();
154                 pane.add("Ascent", ascent);
155
156                 descent = new AltosDescent();
157                 pane.add("Descent", descent);
158
159                 landed = new AltosLanded();
160                 pane.add("Landed", landed);
161
162                 flightInfo = new AltosInfoTable();
163                 flightInfoPane = new JScrollPane(flightInfo.box());
164                 pane.add("Table", flightInfoPane);
165
166                 vbox.add(pane);
167
168                 this.add(vbox);
169
170                 if (serial >= 0) {
171                         JMenuBar menubar = new JMenuBar();
172
173                         // Channel menu
174                         {
175                                 JMenu menu = new AltosChannelMenu(AltosPreferences.channel(serial));
176                                 menu.addActionListener(new ActionListener() {
177                                                 public void actionPerformed(ActionEvent e) {
178                                                         int channel = Integer.parseInt(e.getActionCommand());
179                                                         reader.set_channel(channel);
180                                                         AltosPreferences.set_channel(serial, channel);
181                                                 }
182                                         });
183                                 menu.setMnemonic(KeyEvent.VK_C);
184                                 menubar.add(menu);
185                         }
186
187                         this.setJMenuBar(menubar);
188                 }
189
190                 this.setSize(new Dimension (width(), height()));
191                 this.validate();
192
193                 setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
194                 addWindowListener(new WindowAdapter() {
195                         @Override
196                         public void windowClosing(WindowEvent e) {
197                                 disconnect();
198                                 setVisible(false);
199                                 dispose();
200                                 if (exit_on_close)
201                                         System.exit(0);
202                         }
203                 });
204
205                 this.setVisible(true);
206
207                 thread = new AltosDisplayThread(this, voice, this, reader);
208
209                 thread.start();
210         }
211
212         public AltosFlightUI (AltosVoice in_voice, AltosFlightReader in_reader) {
213                 this(in_voice, in_reader, -1);
214         }
215 }