2 * Copyright © 2010 Keith Packard <keithp@keithp.com>
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.
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.
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.
21 import java.awt.event.*;
23 import javax.swing.event.*;
25 import java.util.concurrent.*;
26 import java.util.Arrays;
27 import org.altusmetrum.altoslib_7.*;
28 import org.altusmetrum.altosuilib_7.*;
30 public class AltosIdleMonitorUI extends AltosUIFrame implements AltosFlightDisplay, AltosIdleMonitorListener, DocumentListener {
34 AltosInfoTable flightInfo;
35 AltosFlightStatus flightStatus;
37 AltosIdleMonitor thread;
38 AltosUIMapNew sitemap;
48 } catch (InterruptedException ie) {
63 public void font_size_changed(int font_size) {
64 pad.font_size_changed(font_size);
65 flightInfo.font_size_changed(font_size);
68 public void units_changed(boolean imperial_units) {
69 pad.units_changed(imperial_units);
70 flightInfo.units_changed(imperial_units);
73 AltosFlightStatusUpdate status_update;
75 public void show(AltosState state, AltosListenerState listener_state) {
76 status_update.saved_state = state;
77 if (ignitor.should_show(state)) {
79 pane.add("Ignitor", ignitor);
88 if (state.gps != null && state.gps.connected) {
90 pane.add("Site Map", sitemap);
101 pad.show(state, listener_state);
102 flightStatus.show(state, listener_state);
103 flightInfo.show(state, listener_state);
105 ignitor.show(state, listener_state);
107 sitemap.show(state, listener_state);
108 // } catch (Exception e) {
109 // System.out.print("Show exception " + e);
113 public void update(final AltosState state, final AltosListenerState listener_state) {
114 Runnable r = new Runnable() {
116 show(state, listener_state);
119 SwingUtilities.invokeLater(r);
122 public void failed() {
123 Runnable r = new Runnable() {
128 SwingUtilities.invokeLater(r);
132 AltosUIFreqList frequencies;
133 JTextField callsign_value;
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);
145 public void insertUpdate(DocumentEvent e) {
149 public void removeUpdate(DocumentEvent e) {
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()),
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);
172 JOptionPane.showMessageDialog(owner,
173 String.format("Connection to \"%s\" failed",
174 device.toShortString()),
176 JOptionPane.ERROR_MESSAGE);
180 private void close() {
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());
190 AltosUIPreferences.unregister_font_listener(AltosIdleMonitorUI.this);
193 public AltosIdleMonitorUI(JFrame in_owner)
194 throws FileNotFoundException, TimeoutException, InterruptedException {
196 device = AltosDeviceUIDialog.show(in_owner, Altos.product_any);
198 if (!device.matchProduct(Altos.product_altimeter))
201 serial = device.getSerial();
205 link = new AltosSerial(device);
206 } catch (Exception ex) {
207 idle_exception(in_owner, ex);
210 link.set_frame(this);
212 /* We let the user set the freq/callsign, so don't bother with the cancel dialog */
213 link.set_cancel_enable(false);
215 bag = getContentPane();
216 bag.setLayout(new GridBagLayout());
218 setTitle(String.format("AltOS %s", device.toShortString()));
220 /* Stick frequency selector at top of table for telemetry monitoring */
221 if (remote && serial >= 0) {
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(),
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));
246 /* Flight status is always visible */
247 flightStatus = new AltosFlightStatus();
248 bag.add(flightStatus, constraints(0, 4, GridBagConstraints.HORIZONTAL));
252 /* The rest of the window uses a tabbed pane to
253 * show one of the alternate data views
255 pane = new JTabbedPane();
257 pad = new AltosPad();
258 pane.add("Launch Pad", pad);
260 flightInfo = new AltosInfoTable();
261 pane.add("Table", new JScrollPane(flightInfo));
263 ignitor = new AltosIgnitor();
265 sitemap = new AltosUIMapNew();
267 /* Make the tabbed pane use the rest of the window space */
268 bag.add(pane, constraints(0, 4, GridBagConstraints.BOTH));
270 setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
272 AltosUIPreferences.register_font_listener(this);
274 addWindowListener(new WindowAdapter() {
276 public void windowClosing(WindowEvent e) {
284 thread = new AltosIdleMonitor((AltosIdleMonitorListener) this, link, (boolean) remote);
286 status_update = new AltosFlightStatusUpdate(flightStatus);
288 new javax.swing.Timer(100, status_update).start();