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