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