altoslib: Switch distance from m/ft to km/miles for large values
[fw/altos] / altosui / AltosIdleMonitorUI.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.event.*;
24 import java.io.*;
25 import java.util.concurrent.*;
26 import java.util.Arrays;
27 import org.altusmetrum.altoslib_10.*;
28 import org.altusmetrum.altosuilib_10.*;
29
30 public class AltosIdleMonitorUI extends AltosUIFrame implements AltosFlightDisplay, AltosIdleMonitorListener, DocumentListener {
31         AltosDevice             device;
32         JTabbedPane             pane;
33         AltosPad                pad;
34         AltosInfoTable          flightInfo;
35         AltosFlightStatus       flightStatus;
36         AltosIgnitor            ignitor;
37         AltosIdleMonitor        thread;
38         AltosUIMapNew           sitemap;
39         int                     serial;
40         boolean                 remote;
41         boolean                 has_ignitor;
42         boolean                 has_map;
43
44         void stop_display() {
45                 if (thread != null) {
46                         try {
47                                 thread.abort();
48                         } catch (InterruptedException ie) {
49                         }
50                 }
51                 thread = null;
52         }
53
54         void disconnect() {
55                 stop_display();
56         }
57
58         public void reset() {
59                 pad.reset();
60                 flightInfo.clear();
61         }
62
63         public void font_size_changed(int font_size) {
64                 pad.font_size_changed(font_size);
65                 flightInfo.font_size_changed(font_size);
66         }
67
68         public void units_changed(boolean imperial_units) {
69                 pad.units_changed(imperial_units);
70                 flightInfo.units_changed(imperial_units);
71         }
72
73         AltosFlightStatusUpdate status_update;
74
75         public void show(AltosState state, AltosListenerState listener_state) {
76                 status_update.saved_state = state;
77                 if (ignitor.should_show(state)) {
78                         if (!has_ignitor) {
79                                 pane.add("Ignitor", ignitor);
80                                 has_ignitor = true;
81                         }
82                 } else {
83                         if (has_ignitor) {
84                                 pane.remove(ignitor);
85                                 has_ignitor = false;
86                         }
87                 }
88                 if (state.gps != null && state.gps.connected) {
89                         if (!has_map) {
90                                 pane.add("Site Map", sitemap);
91                                 has_map = true;
92                         }
93                 } else {
94                         if (has_map) {
95                                 pane.remove(sitemap);
96                                 has_map = false;
97                         }
98                 }
99
100 //              try {
101                         pad.show(state, listener_state);
102                         flightStatus.show(state, listener_state);
103                         flightInfo.show(state, listener_state);
104                         if (has_ignitor)
105                                 ignitor.show(state, listener_state);
106                         if (has_map)
107                                 sitemap.show(state, listener_state);
108 //              } catch (Exception e) {
109 //                      System.out.print("Show exception " + e);
110 //              }
111         }
112
113         public void update(final AltosState state, final AltosListenerState listener_state) {
114                 Runnable r = new Runnable() {
115                                 public void run() {
116                                         show(state, listener_state);
117                                 }
118                         };
119                 SwingUtilities.invokeLater(r);
120         }
121
122         public void failed() {
123                 Runnable r = new Runnable() {
124                                 public void run() {
125                                         close();
126                                 }
127                         };
128                 SwingUtilities.invokeLater(r);
129         }
130
131         public void error(final String reason) {
132                 Runnable r = new Runnable() {
133                                 public void run() {
134                                         disconnect();
135                                         JOptionPane.showMessageDialog(AltosIdleMonitorUI.this,
136                                                                       reason,
137                                                                       "Error fetching data",
138                                                                       JOptionPane.ERROR_MESSAGE);
139                                 }
140                         };
141                 SwingUtilities.invokeLater(r);
142         }
143
144         Container       bag;
145         AltosUIFreqList frequencies;
146         JTextField      callsign_value;
147
148         /* DocumentListener interface methods */
149         public void changedUpdate(DocumentEvent e) {
150                 if (callsign_value != null) {
151                         String  callsign = callsign_value.getText();
152                         System.out.printf("callsign set to %s\n", callsign);
153                         thread.set_callsign(callsign);
154                         AltosUIPreferences.set_callsign(callsign);
155                 }
156         }
157
158         public void insertUpdate(DocumentEvent e) {
159                 changedUpdate(e);
160         }
161
162         public void removeUpdate(DocumentEvent e) {
163                 changedUpdate(e);
164         }
165
166         void idle_exception(JFrame owner, Exception e) {
167                 if (e instanceof FileNotFoundException) {
168                         JOptionPane.showMessageDialog(owner,
169                                                       ((FileNotFoundException) e).getMessage(),
170                                                       "Cannot open target device",
171                                                       JOptionPane.ERROR_MESSAGE);
172                 } else if (e instanceof AltosSerialInUseException) {
173                         JOptionPane.showMessageDialog(owner,
174                                                       String.format("Device \"%s\" already in use",
175                                                                     device.toShortString()),
176                                                       "Device in use",
177                                                       JOptionPane.ERROR_MESSAGE);
178                 } else if (e instanceof IOException) {
179                         IOException ee = (IOException) e;
180                         JOptionPane.showMessageDialog(owner,
181                                                       device.toShortString(),
182                                                       ee.getLocalizedMessage(),
183                                                       JOptionPane.ERROR_MESSAGE);
184                 } else {
185                         JOptionPane.showMessageDialog(owner,
186                                                       String.format("Connection to \"%s\" failed",
187                                                                     device.toShortString()),
188                                                       "Connection Failed",
189                                                       JOptionPane.ERROR_MESSAGE);
190                 }
191         }
192
193         private void close() {
194                 try {
195                         disconnect();
196                 } catch (Exception ex) {
197                         System.out.printf("Exception %s\n", ex.toString());
198                         for (StackTraceElement el : ex.getStackTrace())
199                                 System.out.printf("%s\n", el.toString());
200                 }
201                 setVisible(false);
202                 dispose();
203                 AltosUIPreferences.unregister_font_listener(AltosIdleMonitorUI.this);
204         }
205
206         public AltosIdleMonitorUI(JFrame in_owner)
207                 throws FileNotFoundException, TimeoutException, InterruptedException {
208
209                 device = AltosDeviceUIDialog.show(in_owner, Altos.product_any);
210                 remote = false;
211                 if (!device.matchProduct(Altos.product_altimeter))
212                         remote = true;
213
214                 serial = device.getSerial();
215
216                 AltosSerial link;
217                 try {
218                         link = new AltosSerial(device);
219                 } catch (Exception ex) {
220                         idle_exception(in_owner, ex);
221                         return;
222                 }
223                 link.set_frame(this);
224
225                 /* We let the user set the freq/callsign, so don't bother with the cancel dialog */
226                 link.set_cancel_enable(false);
227
228                 bag = getContentPane();
229                 bag.setLayout(new GridBagLayout());
230
231                 setTitle(String.format("AltOS %s", device.toShortString()));
232
233                 /* Stick frequency selector at top of table for telemetry monitoring */
234                 if (remote && serial >= 0) {
235                         set_inset(3);
236
237                         // Frequency menu
238                         frequencies = new AltosUIFreqList(AltosUIPreferences.frequency(serial));
239                         frequencies.addActionListener(new ActionListener() {
240                                         public void actionPerformed(ActionEvent e) {
241                                                 double frequency = frequencies.frequency();
242                                                 thread.set_frequency(frequency);
243                                                 AltosUIPreferences.set_frequency(device.getSerial(),
244                                                                                frequency);
245                                         }
246                         });
247                         bag.add (frequencies, constraints(0, 1));
248                         bag.add (new JLabel("Callsign:"), constraints(1, 1));
249                         /* Add callsign configuration */
250                         callsign_value = new JTextField(AltosUIPreferences.callsign());
251                         callsign_value.getDocument().addDocumentListener(this);
252                         callsign_value.setToolTipText("Callsign sent in packet mode");
253                         bag.add(callsign_value, constraints(2, 1, GridBagConstraints.HORIZONTAL));
254                         next_row();
255                 }
256
257                 set_inset(0);
258
259                 /* Flight status is always visible */
260                 flightStatus = new AltosFlightStatus();
261                 bag.add(flightStatus, constraints(0, 4, GridBagConstraints.HORIZONTAL));
262
263                 next_row();
264
265                 /* The rest of the window uses a tabbed pane to
266                  * show one of the alternate data views
267                  */
268                 pane = new JTabbedPane();
269
270                 pad = new AltosPad();
271                 pane.add("Launch Pad", pad);
272
273                 flightInfo = new AltosInfoTable();
274                 pane.add("Table", new JScrollPane(flightInfo));
275
276                 ignitor = new AltosIgnitor();
277
278                 sitemap = new AltosUIMapNew();
279
280                 /* Make the tabbed pane use the rest of the window space */
281                 bag.add(pane, constraints(0, 4, GridBagConstraints.BOTH));
282
283                 setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
284
285                 AltosUIPreferences.register_font_listener(this);
286
287                 addWindowListener(new WindowAdapter() {
288                                 @Override
289                                 public void windowClosing(WindowEvent e) {
290                                         close();
291                                 }
292                         });
293
294                 pack();
295                 setVisible(true);
296
297                 thread = new AltosIdleMonitor((AltosIdleMonitorListener) this, link, (boolean) remote);
298
299                 status_update = new AltosFlightStatusUpdate(flightStatus);
300
301                 new javax.swing.Timer(100, status_update).start();
302
303                 thread.start();
304         }
305 }