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