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