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);
105 public void failed() {
106 Runnable r = new Runnable() {
111 SwingUtilities.invokeLater(r);
115 AltosUIFreqList frequencies;
116 JTextField callsign_value;
118 /* DocumentListener interface methods */
119 public void changedUpdate(DocumentEvent e) {
120 if (callsign_value != null) {
121 String callsign = callsign_value.getText();
122 thread.set_callsign(callsign);
123 AltosUIPreferences.set_callsign(callsign);
127 public void insertUpdate(DocumentEvent e) {
131 public void removeUpdate(DocumentEvent e) {
137 public GridBagConstraints constraints (int x, int width, int fill) {
138 GridBagConstraints c = new GridBagConstraints();
139 Insets insets = new Insets(4, 4, 4, 4);
144 c.anchor = GridBagConstraints.CENTER;
146 c.anchor = GridBagConstraints.EAST;
148 c.anchor = GridBagConstraints.WEST;
155 public GridBagConstraints constraints(int x, int width) {
156 return constraints(x, width, GridBagConstraints.NONE);
159 void idle_exception(JFrame owner, Exception e) {
160 if (e instanceof FileNotFoundException) {
161 JOptionPane.showMessageDialog(owner,
162 ((FileNotFoundException) e).getMessage(),
163 "Cannot open target device",
164 JOptionPane.ERROR_MESSAGE);
165 } else if (e instanceof AltosSerialInUseException) {
166 JOptionPane.showMessageDialog(owner,
167 String.format("Device \"%s\" already in use",
168 device.toShortString()),
170 JOptionPane.ERROR_MESSAGE);
171 } else if (e instanceof IOException) {
172 IOException ee = (IOException) e;
173 JOptionPane.showMessageDialog(owner,
174 device.toShortString(),
175 ee.getLocalizedMessage(),
176 JOptionPane.ERROR_MESSAGE);
178 JOptionPane.showMessageDialog(owner,
179 String.format("Connection to \"%s\" failed",
180 device.toShortString()),
182 JOptionPane.ERROR_MESSAGE);
186 private void close() {
189 } catch (Exception ex) {
190 System.out.printf("Exception %s\n", ex.toString());
191 for (StackTraceElement el : ex.getStackTrace())
192 System.out.printf("%s\n", el.toString());
196 AltosUIPreferences.unregister_font_listener(AltosIdleMonitorUI.this);
199 public AltosIdleMonitorUI(JFrame in_owner)
200 throws FileNotFoundException, TimeoutException, InterruptedException {
202 device = AltosDeviceUIDialog.show(in_owner, Altos.product_any);
204 if (!device.matchProduct(Altos.product_altimeter))
207 serial = device.getSerial();
211 link = new AltosSerial(device);
212 link.set_frame(this);
213 } catch (Exception ex) {
214 idle_exception(in_owner, ex);
218 bag = getContentPane();
219 bag.setLayout(new GridBagLayout());
221 setTitle(String.format("AltOS %s", device.toShortString()));
223 /* Stick frequency selector at top of table for telemetry monitoring */
224 if (remote && serial >= 0) {
226 frequencies = new AltosUIFreqList(AltosUIPreferences.frequency(serial));
227 frequencies.addActionListener(new ActionListener() {
228 public void actionPerformed(ActionEvent e) {
229 double frequency = frequencies.frequency();
230 thread.set_frequency(frequency);
231 AltosUIPreferences.set_frequency(device.getSerial(),
235 bag.add (frequencies, constraints(0, 1));
236 bag.add (new JLabel("Callsign:"), constraints(1, 1));
237 /* Add callsign configuration */
238 callsign_value = new JTextField(AltosUIPreferences.callsign());
239 callsign_value.getDocument().addDocumentListener(this);
240 callsign_value.setToolTipText("Callsign sent in packet mode");
241 bag.add(callsign_value, constraints(2, 1, GridBagConstraints.BOTH));
246 /* Flight status is always visible */
247 flightStatus = new AltosFlightStatus();
248 bag.add(flightStatus, constraints(0, 3, GridBagConstraints.HORIZONTAL));
251 /* The rest of the window uses a tabbed pane to
252 * show one of the alternate data views
254 pane = new JTabbedPane();
256 pad = new AltosPad();
257 pane.add("Launch Pad", pad);
259 flightInfo = new AltosInfoTable();
260 pane.add("Table", new JScrollPane(flightInfo));
262 ignitor = new AltosIgnitor();
264 /* Make the tabbed pane use the rest of the window space */
265 bag.add(pane, constraints(0, 3, GridBagConstraints.BOTH));
267 setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
269 AltosUIPreferences.register_font_listener(this);
271 addWindowListener(new WindowAdapter() {
273 public void windowClosing(WindowEvent e) {
281 thread = new AltosIdleMonitor((AltosIdleMonitorListener) this, link, (boolean) remote);
283 status_update = new AltosFlightStatusUpdate(flightStatus);
285 new javax.swing.Timer(100, status_update).start();