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_5.*;
28 import org.altusmetrum.altosuilib_3.*;
30 public class AltosIdleMonitorUI extends AltosUIFrame implements AltosFlightDisplay, AltosIdleMonitorListener, DocumentListener {
34 AltosInfoTable flightInfo;
35 AltosFlightStatus flightStatus;
37 AltosIdleMonitor thread;
46 } catch (InterruptedException ie) {
61 public void font_size_changed(int font_size) {
62 pad.font_size_changed(font_size);
63 flightInfo.font_size_changed(font_size);
66 public void units_changed(boolean imperial_units) {
67 pad.units_changed(imperial_units);
68 flightInfo.units_changed(imperial_units);
71 AltosFlightStatusUpdate status_update;
73 public void show(AltosState state, AltosListenerState listener_state) {
74 status_update.saved_state = state;
75 if (ignitor.should_show(state)) {
77 pane.add("Ignitor", ignitor);
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);
96 public void update(final AltosState state, final AltosListenerState listener_state) {
97 Runnable r = new Runnable() {
99 show(state, listener_state);
102 SwingUtilities.invokeLater(r);
106 AltosUIFreqList frequencies;
107 JTextField callsign_value;
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);
118 public void insertUpdate(DocumentEvent e) {
122 public void removeUpdate(DocumentEvent e) {
128 public GridBagConstraints constraints (int x, int width, int fill) {
129 GridBagConstraints c = new GridBagConstraints();
130 Insets insets = new Insets(4, 4, 4, 4);
135 c.anchor = GridBagConstraints.CENTER;
137 c.anchor = GridBagConstraints.EAST;
139 c.anchor = GridBagConstraints.WEST;
146 public GridBagConstraints constraints(int x, int width) {
147 return constraints(x, width, GridBagConstraints.NONE);
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()),
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);
169 JOptionPane.showMessageDialog(owner,
170 String.format("Connection to \"%s\" failed",
171 device.toShortString()),
173 JOptionPane.ERROR_MESSAGE);
177 public AltosIdleMonitorUI(JFrame in_owner)
178 throws FileNotFoundException, TimeoutException, InterruptedException {
180 device = AltosDeviceUIDialog.show(in_owner, Altos.product_any);
182 if (!device.matchProduct(Altos.product_altimeter))
185 serial = device.getSerial();
189 link = new AltosSerial(device);
190 } catch (Exception ex) {
191 idle_exception(in_owner, ex);
195 bag = getContentPane();
196 bag.setLayout(new GridBagLayout());
198 setTitle(String.format("AltOS %s", device.toShortString()));
200 /* Stick frequency selector at top of table for telemetry monitoring */
201 if (remote && serial >= 0) {
203 frequencies = new AltosUIFreqList(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(),
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));
223 /* Flight status is always visible */
224 flightStatus = new AltosFlightStatus();
225 bag.add(flightStatus, constraints(0, 3, GridBagConstraints.HORIZONTAL));
228 /* The rest of the window uses a tabbed pane to
229 * show one of the alternate data views
231 pane = new JTabbedPane();
233 pad = new AltosPad();
234 pane.add("Launch Pad", pad);
236 flightInfo = new AltosInfoTable();
237 pane.add("Table", new JScrollPane(flightInfo));
239 ignitor = new AltosIgnitor();
241 /* Make the tabbed pane use the rest of the window space */
242 bag.add(pane, constraints(0, 3, GridBagConstraints.BOTH));
244 setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
246 AltosUIPreferences.register_font_listener(this);
248 addWindowListener(new WindowAdapter() {
250 public void windowClosing(WindowEvent e) {
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());
260 AltosUIPreferences.unregister_font_listener(AltosIdleMonitorUI.this);
267 thread = new AltosIdleMonitor((AltosIdleMonitorListener) this, link, (boolean) remote);
269 status_update = new AltosFlightStatusUpdate(flightStatus);
271 new javax.swing.Timer(100, status_update).start();