altosui: Remove un-implemented --fetchmaps argument
[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_6.*;
28 import org.altusmetrum.altosuilib_6.*;
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         AltosUIMap              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         Container       bag;
132         AltosUIFreqList frequencies;
133         JTextField      callsign_value;
134
135         /* DocumentListener interface methods */
136         public void changedUpdate(DocumentEvent e) {
137                 if (callsign_value != null) {
138                         String  callsign = callsign_value.getText();
139                         thread.set_callsign(callsign);
140                         AltosUIPreferences.set_callsign(callsign);
141                 }
142         }
143
144         public void insertUpdate(DocumentEvent e) {
145                 changedUpdate(e);
146         }
147
148         public void removeUpdate(DocumentEvent e) {
149                 changedUpdate(e);
150         }
151
152         int row = 0;
153
154         public GridBagConstraints constraints (int x, int width, int fill) {
155                 GridBagConstraints c = new GridBagConstraints();
156                 Insets insets = new Insets(4, 4, 4, 4);
157
158                 c.insets = insets;
159                 c.fill = fill;
160                 if (width == 3)
161                         c.anchor = GridBagConstraints.CENTER;
162                 else if (x == 2)
163                         c.anchor = GridBagConstraints.EAST;
164                 else
165                         c.anchor = GridBagConstraints.WEST;
166                 c.gridx = x;
167                 c.gridwidth = width;
168                 c.gridy = row;
169                 return c;
170         }
171
172         public GridBagConstraints constraints(int x, int width) {
173                 return constraints(x, width, GridBagConstraints.NONE);
174         }
175
176         void idle_exception(JFrame owner, Exception e) {
177                 if (e instanceof FileNotFoundException) {
178                         JOptionPane.showMessageDialog(owner,
179                                                       ((FileNotFoundException) e).getMessage(),
180                                                       "Cannot open target device",
181                                                       JOptionPane.ERROR_MESSAGE);
182                 } else if (e instanceof AltosSerialInUseException) {
183                         JOptionPane.showMessageDialog(owner,
184                                                       String.format("Device \"%s\" already in use",
185                                                                     device.toShortString()),
186                                                       "Device in use",
187                                                       JOptionPane.ERROR_MESSAGE);
188                 } else if (e instanceof IOException) {
189                         IOException ee = (IOException) e;
190                         JOptionPane.showMessageDialog(owner,
191                                                       device.toShortString(),
192                                                       ee.getLocalizedMessage(),
193                                                       JOptionPane.ERROR_MESSAGE);
194                 } else {
195                         JOptionPane.showMessageDialog(owner,
196                                                       String.format("Connection to \"%s\" failed",
197                                                                     device.toShortString()),
198                                                       "Connection Failed",
199                                                       JOptionPane.ERROR_MESSAGE);
200                 }
201         }
202
203         private void close() {
204                 try {
205                         disconnect();
206                 } catch (Exception ex) {
207                         System.out.printf("Exception %s\n", ex.toString());
208                         for (StackTraceElement el : ex.getStackTrace())
209                                 System.out.printf("%s\n", el.toString());
210                 }
211                 setVisible(false);
212                 dispose();
213                 AltosUIPreferences.unregister_font_listener(AltosIdleMonitorUI.this);
214         }
215
216         public AltosIdleMonitorUI(JFrame in_owner)
217                 throws FileNotFoundException, TimeoutException, InterruptedException {
218
219                 device = AltosDeviceUIDialog.show(in_owner, Altos.product_any);
220                 remote = false;
221                 if (!device.matchProduct(Altos.product_altimeter))
222                         remote = true;
223
224                 serial = device.getSerial();
225
226                 AltosSerial link;
227                 try {
228                         link = new AltosSerial(device);
229                         link.set_frame(this);
230                 } catch (Exception ex) {
231                         idle_exception(in_owner, ex);
232                         return;
233                 }
234
235                 bag = getContentPane();
236                 bag.setLayout(new GridBagLayout());
237
238                 setTitle(String.format("AltOS %s", device.toShortString()));
239
240                 /* Stick frequency selector at top of table for telemetry monitoring */
241                 if (remote && serial >= 0) {
242                         // Frequency menu
243                         frequencies = new AltosUIFreqList(AltosUIPreferences.frequency(serial));
244                         frequencies.addActionListener(new ActionListener() {
245                                         public void actionPerformed(ActionEvent e) {
246                                                 double frequency = frequencies.frequency();
247                                                 thread.set_frequency(frequency);
248                                                 AltosUIPreferences.set_frequency(device.getSerial(),
249                                                                                frequency);
250                                         }
251                         });
252                         bag.add (frequencies, constraints(0, 1));
253                         bag.add (new JLabel("Callsign:"), constraints(1, 1));
254                         /* Add callsign configuration */
255                         callsign_value = new JTextField(AltosUIPreferences.callsign());
256                         callsign_value.getDocument().addDocumentListener(this);
257                         callsign_value.setToolTipText("Callsign sent in packet mode");
258                         bag.add(callsign_value, constraints(2, 1, GridBagConstraints.BOTH));
259                         row++;
260                 }
261
262
263                 /* Flight status is always visible */
264                 flightStatus = new AltosFlightStatus();
265                 bag.add(flightStatus, constraints(0, 3, GridBagConstraints.HORIZONTAL));
266                 row++;
267
268                 /* The rest of the window uses a tabbed pane to
269                  * show one of the alternate data views
270                  */
271                 pane = new JTabbedPane();
272
273                 pad = new AltosPad();
274                 pane.add("Launch Pad", pad);
275
276                 flightInfo = new AltosInfoTable();
277                 pane.add("Table", new JScrollPane(flightInfo));
278
279                 ignitor = new AltosIgnitor();
280
281                 sitemap = new AltosUIMap();
282
283                 /* Make the tabbed pane use the rest of the window space */
284                 bag.add(pane, constraints(0, 3, GridBagConstraints.BOTH));
285
286                 setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
287
288                 AltosUIPreferences.register_font_listener(this);
289
290                 addWindowListener(new WindowAdapter() {
291                                 @Override
292                                 public void windowClosing(WindowEvent e) {
293                                         close();
294                                 }
295                         });
296
297                 pack();
298                 setVisible(true);
299
300                 thread = new AltosIdleMonitor((AltosIdleMonitorListener) this, link, (boolean) remote);
301
302                 status_update = new AltosFlightStatusUpdate(flightStatus);
303
304                 new javax.swing.Timer(100, status_update).start();
305
306                 thread.start();
307         }
308 }