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