Merge branch 'buttonbox' of git://git.gag.com/fw/altos into buttonbox
[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     AltosSiteMap    sitemap;
46
47         private AltosFlightStatus flightStatus;
48         private JScrollPane flightInfoPane;
49         private JScrollPane sitemapPane;
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                 sitemap.reset();
100         }
101
102         public void show(AltosState state, int crc_errors) {
103                 int     tab = which_tab(state);
104                 pad.show(state, crc_errors);
105                 ascent.show(state, crc_errors);
106                 descent.show(state, crc_errors);
107                 landed.show(state, crc_errors);
108                 if (tab != cur_tab) {
109                         switch (tab) {
110                         case tab_pad:
111                                 pane.setSelectedComponent(pad);
112                                 break;
113                         case tab_ascent:
114                                 pane.setSelectedComponent(ascent);
115                                 break;
116                         case tab_descent:
117                                 pane.setSelectedComponent(descent);
118                                 break;
119                         case tab_landed:
120                                 pane.setSelectedComponent(landed);
121                         }
122                         cur_tab = tab;
123                 }
124                 flightStatus.show(state, crc_errors);
125                 flightInfo.show(state, crc_errors);
126                 sitemap.show(state, crc_errors);
127         }
128
129         public void set_exit_on_close() {
130                 exit_on_close = true;
131         }
132
133         Container       bag;
134         JComboBox       channels;
135
136         public AltosFlightUI(AltosVoice in_voice, AltosFlightReader in_reader, final int serial) {
137                 AltosPreferences.init(this);
138
139                 voice = in_voice;
140                 reader = in_reader;
141
142                 bag = getContentPane();
143                 bag.setLayout(new GridBagLayout());
144
145                 GridBagConstraints c = new GridBagConstraints();
146
147                 java.net.URL imgURL = AltosUI.class.getResource("/altus-metrum-16x16.jpg");
148                 if (imgURL != null)
149                         setIconImage(new ImageIcon(imgURL).getImage());
150
151                 setTitle(String.format("AltOS %s", reader.name));
152
153                 if (serial >= 0) {
154                         // Channel menu
155                         channels = new AltosChannelMenu(AltosPreferences.channel(serial));
156                         channels.addActionListener(new ActionListener() {
157                                         public void actionPerformed(ActionEvent e) {
158                                                 int channel = channels.getSelectedIndex();
159                                                 reader.set_channel(channel);
160                                                 AltosPreferences.set_channel(serial, channel);
161                                         }
162                                 });
163                         c.gridx = 0;
164                         c.gridy = 0;
165                         c.anchor = GridBagConstraints.WEST;
166                         bag.add (channels, c);
167                 }
168
169                 flightStatus = new AltosFlightStatus();
170                 c.gridx = 0;
171                 c.gridy = 1;
172                 c.fill = GridBagConstraints.HORIZONTAL;
173                 c.weightx = 1;
174                 bag.add(flightStatus, c);
175
176                 pane = new JTabbedPane();
177
178                 pad = new AltosPad();
179                 pane.add("Launch Pad", pad);
180
181                 ascent = new AltosAscent();
182                 pane.add("Ascent", ascent);
183
184                 descent = new AltosDescent();
185                 pane.add("Descent", descent);
186
187                 landed = new AltosLanded();
188                 pane.add("Landed", landed);
189
190                 flightInfo = new AltosInfoTable();
191                 flightInfoPane = new JScrollPane(flightInfo.box());
192                 pane.add("Table", flightInfoPane);
193
194         sitemap = new AltosSiteMap();
195                 sitemapPane = new JScrollPane(sitemap);
196         pane.add("Site Map", sitemapPane);
197
198                 c.gridx = 0;
199                 c.gridy = 2;
200                 c.fill = GridBagConstraints.BOTH;
201                 c.weightx = 1;
202                 c.weighty = 1;
203                 bag.add(pane, c);
204
205                 this.setSize(this.getPreferredSize());
206                 this.validate();
207
208                 setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
209                 addWindowListener(new WindowAdapter() {
210                         @Override
211                         public void windowClosing(WindowEvent e) {
212                                 disconnect();
213                                 setVisible(false);
214                                 dispose();
215                                 if (exit_on_close)
216                                         System.exit(0);
217                         }
218                 });
219
220                 this.setVisible(true);
221
222                 thread = new AltosDisplayThread(this, voice, this, reader);
223
224                 thread.start();
225         }
226
227         public AltosFlightUI (AltosVoice in_voice, AltosFlightReader in_reader) {
228                 this(in_voice, in_reader, -1);
229         }
230 }