altosui: remove un-used imports
[fw/altos] / 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 java.util.concurrent.*;
24 import org.altusmetrum.AltosLib.*;
25
26 public class AltosFlightUI extends AltosFrame implements AltosFlightDisplay, AltosFontListener {
27         AltosVoice              voice;
28         AltosFlightReader       reader;
29         AltosDisplayThread      thread;
30
31         JTabbedPane     pane;
32
33         AltosPad        pad;
34         AltosAscent     ascent;
35         AltosDescent    descent;
36         AltosLanded     landed;
37         AltosCompanionInfo      companion;
38         AltosSiteMap    sitemap;
39         boolean         has_map;
40         boolean         has_companion;
41
42         private AltosFlightStatus flightStatus;
43         private AltosInfoTable flightInfo;
44
45         boolean exit_on_close = false;
46
47         JComponent cur_tab = null;
48         JComponent which_tab(AltosState state) {
49                 if (state.state < Altos.ao_flight_boost)
50                         return pad;
51                 if (state.state <= Altos.ao_flight_coast)
52                         return ascent;
53                 if (state.state <= Altos.ao_flight_main)
54                         return descent;
55                 return landed;
56         }
57
58         void stop_display() {
59                 if (thread != null && thread.isAlive()) {
60                         thread.interrupt();
61                         try {
62                                 thread.join();
63                         } catch (InterruptedException ie) {}
64                 }
65                 thread = null;
66         }
67
68         void disconnect() {
69                 stop_display();
70         }
71
72         public void reset() {
73                 pad.reset();
74                 ascent.reset();
75                 descent.reset();
76                 landed.reset();
77                 flightInfo.clear();
78                 sitemap.reset();
79         }
80
81         public void set_font() {
82                 pad.set_font();
83                 ascent.set_font();
84                 descent.set_font();
85                 landed.set_font();
86                 flightStatus.set_font();
87                 flightInfo.set_font();
88                 sitemap.set_font();
89                 companion.set_font();
90         }
91
92         public void font_size_changed(int font_size) {
93                 set_font();
94         }
95
96
97         AltosFlightStatusUpdate status_update;
98
99         public void show(AltosState state, int crc_errors) {
100                 status_update.saved_state = state;
101                 JComponent tab = which_tab(state);
102                 try {
103                 pad.show(state, crc_errors);
104                 ascent.show(state, crc_errors);
105                 descent.show(state, crc_errors);
106                 landed.show(state, crc_errors);
107                 if (tab != cur_tab) {
108                         if (cur_tab == pane.getSelectedComponent()) {
109                                 pane.setSelectedComponent(tab);
110                         }
111                         cur_tab = tab;
112                 }
113                 flightStatus.show(state, crc_errors);
114                 flightInfo.show(state, crc_errors);
115
116                 if (state.data.companion != null) {
117                         if (!has_companion) {
118                                 pane.add("Companion", companion);
119                                 has_companion= true;
120                         }
121                         companion.show(state, crc_errors);
122                 } else {
123                         if (has_companion) {
124                                 pane.remove(companion);
125                                 has_companion = false;
126                         }
127                 }
128                 if (state.gps != null && state.gps.connected) {
129                         if (!has_map) {
130                                 pane.add("Site Map", sitemap);
131                                 has_map = true;
132                         }
133                         sitemap.show(state, crc_errors);
134                 } else {
135                         if (has_map) {
136                                 pane.remove(sitemap);
137                                 has_map = false;
138                         }
139                 }
140                 } catch (Exception e) {
141                         System.out.print("Show exception" + e);
142                 }
143         }
144
145         public void set_exit_on_close() {
146                 exit_on_close = true;
147         }
148
149         Container       bag;
150         AltosFreqList   frequencies;
151         JComboBox       telemetries;
152         JLabel          telemetry;
153
154         ActionListener  show_timer;
155
156         public AltosFlightUI(AltosVoice in_voice, AltosFlightReader in_reader, final int serial) {
157                 AltosUIPreferences.set_component(this);
158
159                 voice = in_voice;
160                 reader = in_reader;
161
162                 bag = getContentPane();
163                 bag.setLayout(new GridBagLayout());
164
165                 GridBagConstraints c = new GridBagConstraints();
166
167                 setTitle(String.format("AltOS %s", reader.name));
168
169                 /* Stick channel selector at top of table for telemetry monitoring */
170                 if (serial >= 0) {
171                         // Channel menu
172                         frequencies = new AltosFreqList(AltosUIPreferences.frequency(serial));
173                         frequencies.set_product("Monitor");
174                         frequencies.set_serial(serial);
175                         frequencies.addActionListener(new ActionListener() {
176                                         public void actionPerformed(ActionEvent e) {
177                                                 double frequency = frequencies.frequency();
178                                                 try {
179                                                         reader.set_frequency(frequency);
180                                                 } catch (TimeoutException te) {
181                                                 } catch (InterruptedException ie) {
182                                                 }
183                                                 reader.save_frequency();
184                                         }
185                         });
186                         c.gridx = 0;
187                         c.gridy = 0;
188                         c.weightx = 0;
189                         c.weighty = 0;
190                         c.insets = new Insets(3, 3, 3, 3);
191                         c.fill = GridBagConstraints.NONE;
192                         c.anchor = GridBagConstraints.WEST;
193                         bag.add (frequencies, c);
194
195                         // Telemetry format menu
196                         if (reader.supports_telemetry(Altos.ao_telemetry_standard)) {
197                                 telemetries = new JComboBox();
198                                 for (int i = 1; i <= Altos.ao_telemetry_max; i++) 
199                                         telemetries.addItem(Altos.telemetry_name(i));
200                                 int telemetry = AltosPreferences.telemetry(serial);
201                                 if (telemetry <= Altos.ao_telemetry_off ||
202                                     telemetry > Altos.ao_telemetry_max)
203                                         telemetry = Altos.ao_telemetry_standard;
204                                 telemetries.setSelectedIndex(telemetry - 1);
205                                 telemetries.setMaximumRowCount(Altos.ao_telemetry_max);
206                                 telemetries.setPreferredSize(null);
207                                 telemetries.revalidate();
208                                 telemetries.addActionListener(new ActionListener() {
209                                                 public void actionPerformed(ActionEvent e) {
210                                                         int telemetry = telemetries.getSelectedIndex() + 1;
211                                                         reader.set_telemetry(telemetry);
212                                                         reader.save_telemetry();
213                                                 }
214                                         });
215                                 c.gridx = 1;
216                                 c.gridy = 0;
217                                 c.weightx = 0;
218                                 c.weighty = 0;
219                                 c.fill = GridBagConstraints.NONE;
220                                 c.anchor = GridBagConstraints.WEST;
221                                 bag.add (telemetries, c);
222                                 c.insets = new Insets(0, 0, 0, 0);
223                         } else {
224                                 String  version;
225
226                                 if (reader.supports_telemetry(Altos.ao_telemetry_0_9))
227                                         version = "Telemetry: 0.9";
228                                 else if (reader.supports_telemetry(Altos.ao_telemetry_0_8))
229                                         version = "Telemetry: 0.8";
230                                 else
231                                         version = "Telemetry: None";
232
233                                 telemetry = new JLabel(version);
234                                 c.gridx = 1;
235                                 c.gridy = 0;
236                                 c.weightx = 0;
237                                 c.weighty = 0;
238                                 c.fill = GridBagConstraints.NONE;
239                                 c.anchor = GridBagConstraints.WEST;
240                                 bag.add (telemetry, c);
241                                 c.insets = new Insets(0, 0, 0, 0);
242                         }
243                 }
244
245                 /* Flight status is always visible */
246                 flightStatus = new AltosFlightStatus();
247                 c.gridx = 0;
248                 c.gridy = 1;
249                 c.fill = GridBagConstraints.HORIZONTAL;
250                 c.weightx = 1;
251                 c.gridwidth = 2;
252                 bag.add(flightStatus, c);
253                 c.gridwidth = 1;
254
255                 /* The rest of the window uses a tabbed pane to
256                  * show one of the alternate data views
257                  */
258                 pane = new JTabbedPane();
259
260                 pad = new AltosPad();
261                 pane.add("Launch Pad", pad);
262
263                 ascent = new AltosAscent();
264                 pane.add("Ascent", ascent);
265
266                 descent = new AltosDescent();
267                 pane.add("Descent", descent);
268
269                 landed = new AltosLanded(reader);
270                 pane.add("Landed", landed);
271
272                 flightInfo = new AltosInfoTable();
273                 pane.add("Table", new JScrollPane(flightInfo));
274
275                 companion = new AltosCompanionInfo();
276                 has_companion = false;
277
278                 sitemap = new AltosSiteMap();
279                 has_map = false;
280
281                 /* Make the tabbed pane use the rest of the window space */
282                 c.gridx = 0;
283                 c.gridy = 2;
284                 c.fill = GridBagConstraints.BOTH;
285                 c.weightx = 1;
286                 c.weighty = 1;
287                 c.gridwidth = 2;
288                 bag.add(pane, c);
289
290                 setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
291
292                 AltosUIPreferences.register_font_listener(this);
293
294                 addWindowListener(new WindowAdapter() {
295                                 @Override
296                                 public void windowClosing(WindowEvent e) {
297                                         disconnect();
298                                         setVisible(false);
299                                         dispose();
300                                         AltosUIPreferences.unregister_font_listener(AltosFlightUI.this);
301                                         if (exit_on_close)
302                                                 System.exit(0);
303                                 }
304                         });
305
306                 pack();
307                 setVisible(true);
308
309                 thread = new AltosDisplayThread(this, voice, this, reader);
310
311                 status_update = new AltosFlightStatusUpdate(flightStatus);
312
313                 new javax.swing.Timer(100, status_update).start();
314
315                 thread.start();
316         }
317
318         public AltosFlightUI (AltosVoice in_voice, AltosFlightReader in_reader) {
319                 this(in_voice, in_reader, -1);
320         }
321 }