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_4.*;
28 import org.altusmetrum.altosuilib_2.*;
30 public class AltosIdleMonitorUI extends AltosUIFrame implements AltosFlightDisplay, AltosFontListener, AltosIdleMonitorListener, DocumentListener {
34 AltosInfoTable flightInfo;
35 AltosFlightStatus flightStatus;
36 AltosIdleMonitor thread;
44 } catch (InterruptedException ie) {
59 public void set_font() {
61 flightInfo.set_font();
64 public void font_size_changed(int font_size) {
68 AltosFlightStatusUpdate status_update;
70 public void show(AltosState state, AltosListenerState listener_state) {
71 status_update.saved_state = state;
73 pad.show(state, listener_state);
74 flightStatus.show(state, listener_state);
75 flightInfo.show(state, listener_state);
76 // } catch (Exception e) {
77 // System.out.print("Show exception " + e);
81 public void update(final AltosState state, final AltosListenerState listener_state) {
82 Runnable r = new Runnable() {
84 show(state, listener_state);
87 SwingUtilities.invokeLater(r);
91 AltosFreqList frequencies;
92 JTextField callsign_value;
94 /* DocumentListener interface methods */
95 public void changedUpdate(DocumentEvent e) {
96 if (callsign_value != null) {
97 String callsign = callsign_value.getText();
98 thread.set_callsign(callsign);
99 AltosUIPreferences.set_callsign(callsign);
103 public void insertUpdate(DocumentEvent e) {
107 public void removeUpdate(DocumentEvent e) {
113 public GridBagConstraints constraints (int x, int width, int fill) {
114 GridBagConstraints c = new GridBagConstraints();
115 Insets insets = new Insets(4, 4, 4, 4);
120 c.anchor = GridBagConstraints.CENTER;
122 c.anchor = GridBagConstraints.EAST;
124 c.anchor = GridBagConstraints.WEST;
131 public GridBagConstraints constraints(int x, int width) {
132 return constraints(x, width, GridBagConstraints.NONE);
135 void idle_exception(JFrame owner, Exception e) {
136 if (e instanceof FileNotFoundException) {
137 JOptionPane.showMessageDialog(owner,
138 ((FileNotFoundException) e).getMessage(),
139 "Cannot open target device",
140 JOptionPane.ERROR_MESSAGE);
141 } else if (e instanceof AltosSerialInUseException) {
142 JOptionPane.showMessageDialog(owner,
143 String.format("Device \"%s\" already in use",
144 device.toShortString()),
146 JOptionPane.ERROR_MESSAGE);
147 } else if (e instanceof IOException) {
148 IOException ee = (IOException) e;
149 JOptionPane.showMessageDialog(owner,
150 device.toShortString(),
151 ee.getLocalizedMessage(),
152 JOptionPane.ERROR_MESSAGE);
154 JOptionPane.showMessageDialog(owner,
155 String.format("Connection to \"%s\" failed",
156 device.toShortString()),
158 JOptionPane.ERROR_MESSAGE);
162 public AltosIdleMonitorUI(JFrame in_owner)
163 throws FileNotFoundException, TimeoutException, InterruptedException {
165 device = AltosDeviceUIDialog.show(in_owner, Altos.product_any);
167 if (!device.matchProduct(Altos.product_altimeter))
170 serial = device.getSerial();
174 link = new AltosSerial(device);
175 } catch (Exception ex) {
176 idle_exception(in_owner, ex);
180 bag = getContentPane();
181 bag.setLayout(new GridBagLayout());
183 setTitle(String.format("AltOS %s", device.toShortString()));
185 /* Stick frequency selector at top of table for telemetry monitoring */
186 if (remote && serial >= 0) {
188 frequencies = new AltosFreqList(AltosUIPreferences.frequency(serial));
189 frequencies.addActionListener(new ActionListener() {
190 public void actionPerformed(ActionEvent e) {
191 double frequency = frequencies.frequency();
192 thread.set_frequency(frequency);
193 AltosUIPreferences.set_frequency(device.getSerial(),
197 bag.add (frequencies, constraints(0, 1));
198 bag.add (new JLabel("Callsign:"), constraints(1, 1));
199 /* Add callsign configuration */
200 callsign_value = new JTextField(AltosUIPreferences.callsign());
201 callsign_value.getDocument().addDocumentListener(this);
202 callsign_value.setToolTipText("Callsign sent in packet mode");
203 bag.add(callsign_value, constraints(2, 1, GridBagConstraints.BOTH));
208 /* Flight status is always visible */
209 flightStatus = new AltosFlightStatus();
210 bag.add(flightStatus, constraints(0, 3, GridBagConstraints.HORIZONTAL));
213 /* The rest of the window uses a tabbed pane to
214 * show one of the alternate data views
216 pane = new JTabbedPane();
218 pad = new AltosPad();
219 pane.add("Launch Pad", pad);
221 flightInfo = new AltosInfoTable();
222 pane.add("Table", new JScrollPane(flightInfo));
224 /* Make the tabbed pane use the rest of the window space */
225 bag.add(pane, constraints(0, 3, GridBagConstraints.BOTH));
227 setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
229 AltosUIPreferences.register_font_listener(this);
231 addWindowListener(new WindowAdapter() {
233 public void windowClosing(WindowEvent e) {
236 } catch (Exception ex) {
237 System.out.println(Arrays.toString(ex.getStackTrace()));
241 AltosUIPreferences.unregister_font_listener(AltosIdleMonitorUI.this);
248 thread = new AltosIdleMonitor((AltosIdleMonitorListener) this, link, (boolean) remote);
250 status_update = new AltosFlightStatusUpdate(flightStatus);
252 new javax.swing.Timer(100, status_update).start();