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