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