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 thread.set_callsign(callsign);
140 AltosUIPreferences.set_callsign(callsign);
144 public void insertUpdate(DocumentEvent e) {
148 public void removeUpdate(DocumentEvent e) {
154 public GridBagConstraints constraints (int x, int width, int fill) {
155 GridBagConstraints c = new GridBagConstraints();
156 Insets insets = new Insets(4, 4, 4, 4);
161 c.anchor = GridBagConstraints.CENTER;
163 c.anchor = GridBagConstraints.EAST;
165 c.anchor = GridBagConstraints.WEST;
172 public GridBagConstraints constraints(int x, int width) {
173 return constraints(x, width, GridBagConstraints.NONE);
176 void idle_exception(JFrame owner, Exception e) {
177 if (e instanceof FileNotFoundException) {
178 JOptionPane.showMessageDialog(owner,
179 ((FileNotFoundException) e).getMessage(),
180 "Cannot open target device",
181 JOptionPane.ERROR_MESSAGE);
182 } else if (e instanceof AltosSerialInUseException) {
183 JOptionPane.showMessageDialog(owner,
184 String.format("Device \"%s\" already in use",
185 device.toShortString()),
187 JOptionPane.ERROR_MESSAGE);
188 } else if (e instanceof IOException) {
189 IOException ee = (IOException) e;
190 JOptionPane.showMessageDialog(owner,
191 device.toShortString(),
192 ee.getLocalizedMessage(),
193 JOptionPane.ERROR_MESSAGE);
195 JOptionPane.showMessageDialog(owner,
196 String.format("Connection to \"%s\" failed",
197 device.toShortString()),
199 JOptionPane.ERROR_MESSAGE);
203 private void close() {
206 } catch (Exception ex) {
207 System.out.printf("Exception %s\n", ex.toString());
208 for (StackTraceElement el : ex.getStackTrace())
209 System.out.printf("%s\n", el.toString());
213 AltosUIPreferences.unregister_font_listener(AltosIdleMonitorUI.this);
216 public AltosIdleMonitorUI(JFrame in_owner)
217 throws FileNotFoundException, TimeoutException, InterruptedException {
219 device = AltosDeviceUIDialog.show(in_owner, Altos.product_any);
221 if (!device.matchProduct(Altos.product_altimeter))
224 serial = device.getSerial();
228 link = new AltosSerial(device);
229 link.set_frame(this);
230 } catch (Exception ex) {
231 idle_exception(in_owner, ex);
235 bag = getContentPane();
236 bag.setLayout(new GridBagLayout());
238 setTitle(String.format("AltOS %s", device.toShortString()));
240 /* Stick frequency selector at top of table for telemetry monitoring */
241 if (remote && serial >= 0) {
243 frequencies = new AltosUIFreqList(AltosUIPreferences.frequency(serial));
244 frequencies.addActionListener(new ActionListener() {
245 public void actionPerformed(ActionEvent e) {
246 double frequency = frequencies.frequency();
247 thread.set_frequency(frequency);
248 AltosUIPreferences.set_frequency(device.getSerial(),
252 bag.add (frequencies, constraints(0, 1));
253 bag.add (new JLabel("Callsign:"), constraints(1, 1));
254 /* Add callsign configuration */
255 callsign_value = new JTextField(AltosUIPreferences.callsign());
256 callsign_value.getDocument().addDocumentListener(this);
257 callsign_value.setToolTipText("Callsign sent in packet mode");
258 bag.add(callsign_value, constraints(2, 1, GridBagConstraints.BOTH));
263 /* Flight status is always visible */
264 flightStatus = new AltosFlightStatus();
265 bag.add(flightStatus, constraints(0, 3, GridBagConstraints.HORIZONTAL));
268 /* The rest of the window uses a tabbed pane to
269 * show one of the alternate data views
271 pane = new JTabbedPane();
273 pad = new AltosPad();
274 pane.add("Launch Pad", pad);
276 flightInfo = new AltosInfoTable();
277 pane.add("Table", new JScrollPane(flightInfo));
279 ignitor = new AltosIgnitor();
281 sitemap = new AltosUIMapNew();
283 /* Make the tabbed pane use the rest of the window space */
284 bag.add(pane, constraints(0, 3, GridBagConstraints.BOTH));
286 setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
288 AltosUIPreferences.register_font_listener(this);
290 addWindowListener(new WindowAdapter() {
292 public void windowClosing(WindowEvent e) {
300 thread = new AltosIdleMonitor((AltosIdleMonitorListener) this, link, (boolean) remote);
302 status_update = new AltosFlightStatusUpdate(flightStatus);
304 new javax.swing.Timer(100, status_update).start();