altosui: Rewrite info table to mix with scroll pane well. Fix startup size
[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         JTabbedPane     pane;
40
41         AltosPad        pad;
42         AltosAscent     ascent;
43         AltosDescent    descent;
44         AltosLanded     landed;
45
46         private AltosFlightStatus flightStatus;
47         private AltosInfoTable flightInfo;
48
49         static final int tab_pad = 1;
50         static final int tab_ascent = 2;
51         static final int tab_descent = 3;
52         static final int tab_landed = 4;
53
54         int cur_tab = 0;
55
56         boolean exit_on_close = false;
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         void stop_display() {
69                 if (thread != null && thread.isAlive()) {
70                         thread.interrupt();
71                         try {
72                                 thread.join();
73                         } catch (InterruptedException ie) {}
74                 }
75                 thread = null;
76         }
77
78         void disconnect() {
79                 stop_display();
80         }
81
82         public void reset() {
83                 pad.reset();
84                 ascent.reset();
85                 descent.reset();
86                 landed.reset();
87                 flightInfo.clear();
88         }
89
90         public void show(AltosState state, int crc_errors) {
91                 int     tab = which_tab(state);
92                 pad.show(state, crc_errors);
93                 ascent.show(state, crc_errors);
94                 descent.show(state, crc_errors);
95                 landed.show(state, crc_errors);
96                 if (tab != cur_tab) {
97                         switch (tab) {
98                         case tab_pad:
99                                 pane.setSelectedComponent(pad);
100                                 break;
101                         case tab_ascent:
102                                 pane.setSelectedComponent(ascent);
103                                 break;
104                         case tab_descent:
105                                 pane.setSelectedComponent(descent);
106                                 break;
107                         case tab_landed:
108                                 pane.setSelectedComponent(landed);
109                         }
110                         cur_tab = tab;
111                 }
112                 flightStatus.show(state, crc_errors);
113                 flightInfo.show(state, crc_errors);
114         }
115
116         public void set_exit_on_close() {
117                 exit_on_close = true;
118         }
119
120         Container       bag;
121         JComboBox       channels;
122
123         public AltosFlightUI(AltosVoice in_voice, AltosFlightReader in_reader, final int serial) {
124                 AltosPreferences.init(this);
125
126                 voice = in_voice;
127                 reader = in_reader;
128
129                 bag = getContentPane();
130                 bag.setLayout(new GridBagLayout());
131
132                 GridBagConstraints c = new GridBagConstraints();
133
134                 java.net.URL imgURL = AltosUI.class.getResource("/altus-metrum-16x16.jpg");
135                 if (imgURL != null)
136                         setIconImage(new ImageIcon(imgURL).getImage());
137
138                 setTitle(String.format("AltOS %s", reader.name));
139
140                 /* Stick channel selector at top of table for telemetry monitoring */
141                 if (serial >= 0) {
142                         // Channel menu
143                         channels = new AltosChannelMenu(AltosPreferences.channel(serial));
144                         channels.addActionListener(new ActionListener() {
145                                         public void actionPerformed(ActionEvent e) {
146                                                 int channel = channels.getSelectedIndex();
147                                                 reader.set_channel(channel);
148                                                 AltosPreferences.set_channel(serial, channel);
149                                         }
150                                 });
151                         c.gridx = 0;
152                         c.gridy = 0;
153                         c.anchor = GridBagConstraints.WEST;
154                         bag.add (channels, c);
155                 }
156
157                 /* Flight status is always visible */
158                 flightStatus = new AltosFlightStatus();
159                 c.gridx = 0;
160                 c.gridy = 1;
161                 c.fill = GridBagConstraints.HORIZONTAL;
162                 c.weightx = 1;
163                 bag.add(flightStatus, c);
164
165                 /* The rest of the window uses a tabbed pane to
166                  * show one of the alternate data views
167                  */
168                 pane = new JTabbedPane();
169
170                 pad = new AltosPad();
171                 pane.add("Launch Pad", pad);
172
173                 ascent = new AltosAscent();
174                 pane.add("Ascent", ascent);
175
176                 descent = new AltosDescent();
177                 pane.add("Descent", descent);
178
179                 landed = new AltosLanded();
180                 pane.add("Landed", landed);
181
182                 flightInfo = new AltosInfoTable();
183                 pane.add("Table", new JScrollPane(flightInfo));
184
185                 /* Make the tabbed pane use the rest of the window space */
186                 c.gridx = 0;
187                 c.gridy = 2;
188                 c.fill = GridBagConstraints.BOTH;
189                 c.weightx = 1;
190                 c.weighty = 1;
191                 bag.add(pane, c);
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                 pack();
206                 setVisible(true);
207
208                 thread = new AltosDisplayThread(this, voice, this, reader);
209
210                 thread.start();
211         }
212
213         public AltosFlightUI (AltosVoice in_voice, AltosFlightReader in_reader) {
214                 this(in_voice, in_reader, -1);
215         }
216 }