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