816ffa2374f1c9679344f8ea66c85960627d2cff
[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     AltosSiteMap    sitemap;
48
49         private AltosStatusTable flightStatus;
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         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         public int width() {
70                 return flightInfo.width();
71         }
72
73         public int height() {
74                 return flightStatus.height() + flightInfo.height();
75         }
76
77         void stop_display() {
78                 if (thread != null && thread.isAlive()) {
79                         thread.interrupt();
80                         try {
81                                 thread.join();
82                         } catch (InterruptedException ie) {}
83                 }
84                 thread = null;
85         }
86
87         void disconnect() {
88                 stop_display();
89         }
90
91         public void reset() {
92                 pad.reset();
93                 ascent.reset();
94                 descent.reset();
95                 landed.reset();
96                 flightInfo.clear();
97                 sitemap.reset();
98         }
99
100         public void show(AltosState state, int crc_errors) {
101                 int     tab = which_tab(state);
102                 pad.show(state, crc_errors);
103                 ascent.show(state, crc_errors);
104                 descent.show(state, crc_errors);
105                 landed.show(state, crc_errors);
106                 if (tab != cur_tab) {
107                         switch (tab) {
108                         case tab_pad:
109                                 pane.setSelectedComponent(pad);
110                                 break;
111                         case tab_ascent:
112                                 pane.setSelectedComponent(ascent);
113                                 break;
114                         case tab_descent:
115                                 pane.setSelectedComponent(descent);
116                                 break;
117                         case tab_landed:
118                                 pane.setSelectedComponent(landed);
119                         }
120                         cur_tab = tab;
121                 }
122                 flightStatus.set(state);
123                 flightInfo.show(state, crc_errors);
124                 sitemap.show(state, crc_errors);
125         }
126
127         public AltosFlightUI(AltosVoice in_voice, AltosFlightReader in_reader, final int serial) {
128         AltosPreferences.init(this);
129
130                 voice = in_voice;
131                 reader = in_reader;
132
133                 java.net.URL imgURL = AltosUI.class.getResource("/altus-metrum-16x16.jpg");
134                 if (imgURL != null)
135                         setIconImage(new ImageIcon(imgURL).getImage());
136
137                 setTitle(String.format("AltOS %s", reader.name));
138
139                 flightStatus = new AltosStatusTable();
140
141                 vbox = new Box (BoxLayout.Y_AXIS);
142                 vbox.add(flightStatus);
143
144                 pane = new JTabbedPane();
145
146                 pad = new AltosPad();
147                 pane.add("Launch Pad", pad);
148
149                 ascent = new AltosAscent();
150                 pane.add("Ascent", ascent);
151
152                 descent = new AltosDescent();
153                 pane.add("Descent", descent);
154
155                 landed = new AltosLanded();
156                 pane.add("Landed", landed);
157
158                 flightInfo = new AltosInfoTable();
159                 pane.add("Table", flightInfo.box());
160
161         sitemap = new AltosSiteMap();
162         pane.add("Site Map", sitemap);
163
164                 vbox.add(pane);
165
166                 this.add(vbox);
167
168                 if (serial >= 0) {
169                         JMenuBar menubar = new JMenuBar();
170
171                         // Channel menu
172                         {
173                                 JMenu menu = new AltosChannelMenu(AltosPreferences.channel(serial));
174                                 menu.addActionListener(new ActionListener() {
175                                                 public void actionPerformed(ActionEvent e) {
176                                                         int channel = Integer.parseInt(e.getActionCommand());
177                                                         reader.set_channel(channel);
178                                                         AltosPreferences.set_channel(serial, channel);
179                                                 }
180                                         });
181                                 menu.setMnemonic(KeyEvent.VK_C);
182                                 menubar.add(menu);
183                         }
184
185                         this.setJMenuBar(menubar);
186                 }
187
188                 this.setSize(new Dimension (width(), height()));
189                 this.validate();
190
191                 setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
192                 addWindowListener(new WindowAdapter() {
193                         @Override
194                         public void windowClosing(WindowEvent e) {
195                                 disconnect();
196                                 setVisible(false);
197                                 dispose();
198                         }
199                 });
200
201                 this.setVisible(true);
202
203                 thread = new AltosDisplayThread(this, voice, this, reader);
204
205                 thread.start();
206         }
207
208         public AltosFlightUI (AltosVoice in_voice, AltosFlightReader in_reader) {
209                 this(in_voice, in_reader, -1);
210         }
211 }