2 * Copyright © 2011 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.filechooser.FileNameExtensionFilter;
24 import javax.swing.table.*;
25 import javax.swing.event.*;
29 import java.util.prefs.*;
30 import java.util.concurrent.*;
32 class AltosScanResult {
38 static final String[] short_monitor_names = { "Standard", "Original" };
40 boolean interrupted = false;
42 public String toString() {
43 return String.format("%-9.9s serial %-4d flight %-4d (channel %-2d telemetry %s)",
44 callsign, serial, flight, channel, short_monitor_names[telemetry]);
47 public String toShortString() {
48 return String.format("%s %d %d %d %d",
49 callsign, serial, flight, channel, telemetry);
52 public AltosScanResult(String in_callsign, int in_serial,
53 int in_flight, int in_channel, int in_telemetry) {
54 callsign = in_callsign;
58 telemetry = in_telemetry;
61 public boolean equals(AltosScanResult other) {
62 return (callsign.equals(other.callsign) &&
63 serial == other.serial &&
64 flight == other.flight &&
65 channel == other.channel &&
66 telemetry == other.telemetry);
70 class AltosScanResults extends LinkedList<AltosScanResult> implements ListModel {
72 LinkedList<ListDataListener> listeners = new LinkedList<ListDataListener>();
74 public boolean add(AltosScanResult r) {
75 for (AltosScanResult old : this)
80 ListDataEvent de = new ListDataEvent(this,
81 ListDataEvent.INTERVAL_ADDED,
82 this.size() - 2, this.size() - 1);
83 for (ListDataListener l : listeners)
84 l.contentsChanged(de);
88 public void addListDataListener(ListDataListener l) {
92 public void removeListDataListener(ListDataListener l) {
96 public AltosScanResult getElementAt(int i) {
100 public int getSize() {
105 public class AltosScanUI
107 implements ActionListener
111 AltosTelemetryReader reader;
113 private JLabel scanning_label;
114 private JButton cancel_button;
115 private JButton monitor_button;
116 javax.swing.Timer timer;
117 AltosScanResults results = new AltosScanResults();
119 static final String[] monitor_names = { "Standard AltOS Telemetry", "Original TeleMetrum Telemetry" };
120 static final int[] monitors = { 2, 1 };
124 final static int timeout = 1200;
125 TelemetryHandler handler;
128 void scan_exception(Exception e) {
129 if (e instanceof FileNotFoundException) {
130 JOptionPane.showMessageDialog(owner,
131 String.format("Cannot open device \"%s\"",
132 device.toShortString()),
133 "Cannot open target device",
134 JOptionPane.ERROR_MESSAGE);
135 } else if (e instanceof AltosSerialInUseException) {
136 JOptionPane.showMessageDialog(owner,
137 String.format("Device \"%s\" already in use",
138 device.toShortString()),
140 JOptionPane.ERROR_MESSAGE);
141 } else if (e instanceof IOException) {
142 IOException ee = (IOException) e;
143 JOptionPane.showMessageDialog(owner,
144 device.toShortString(),
145 ee.getLocalizedMessage(),
146 JOptionPane.ERROR_MESSAGE);
148 JOptionPane.showMessageDialog(owner,
149 String.format("Connection to \"%s\" failed",
150 device.toShortString()),
152 JOptionPane.ERROR_MESSAGE);
157 class TelemetryHandler implements Runnable {
161 boolean interrupted = false;
166 AltosRecord record = reader.read();
169 if ((record.seen & AltosRecord.seen_flight) != 0) {
170 final AltosScanResult result = new AltosScanResult(record.callsign,
175 Runnable r = new Runnable() {
180 SwingUtilities.invokeLater(r);
182 } catch (ParseException pp) {
183 } catch (AltosCRCException ce) {
186 } catch (InterruptedException ee) {
188 } catch (IOException ie) {
190 reader.close(interrupted);
196 scanning_label.setText(String.format("Scanning: channel %d %s",
198 monitor_names[monitor]));
202 reader.serial.set_monitor(false);
205 } catch (InterruptedException ie){
211 if (monitor == monitors.length)
213 reader.serial.set_telemetry(monitors[monitor]);
215 reader.serial.set_channel(channel);
217 reader.serial.set_monitor(true);
222 if (thread != null && thread.isAlive()) {
226 } catch (InterruptedException ie) {}
239 public void actionPerformed(ActionEvent e) {
240 String cmd = e.getActionCommand();
242 if (cmd.equals("cancel"))
245 if (cmd.equals("tick"))
248 if (cmd.equals("monitor")) {
250 AltosScanResult r = (AltosScanResult) (list.getSelectedValue());
252 if (device != null) {
253 if (reader != null) {
254 reader.set_telemetry(monitors[r.telemetry]);
255 reader.set_channel(r.channel);
256 owner.telemetry_window(device);
263 /* A window listener to catch closing events and tell the config code */
264 class ConfigListener extends WindowAdapter {
267 public ConfigListener(AltosScanUI this_ui) {
271 public void windowClosing(WindowEvent e) {
272 ui.actionPerformed(new ActionEvent(e.getSource(),
273 ActionEvent.ACTION_PERFORMED,
278 private boolean open() {
279 device = AltosDeviceDialog.show(owner, Altos.product_basestation);
283 reader = new AltosTelemetryReader(device);
284 reader.serial.set_channel(channel);
285 reader.serial.set_telemetry(monitors[monitor]);
286 handler = new TelemetryHandler();
287 thread = new Thread(handler);
290 } catch (FileNotFoundException ee) {
291 JOptionPane.showMessageDialog(owner,
292 String.format("Cannot open device \"%s\"",
293 device.toShortString()),
294 "Cannot open target device",
295 JOptionPane.ERROR_MESSAGE);
296 } catch (AltosSerialInUseException si) {
297 JOptionPane.showMessageDialog(owner,
298 String.format("Device \"%s\" already in use",
299 device.toShortString()),
301 JOptionPane.ERROR_MESSAGE);
302 } catch (IOException ee) {
303 JOptionPane.showMessageDialog(owner,
304 device.toShortString(),
306 JOptionPane.ERROR_MESSAGE);
313 public AltosScanUI(AltosUI in_owner) {
320 Container pane = getContentPane();
321 GridBagConstraints c = new GridBagConstraints();
322 Insets i = new Insets(4,4,4,4);
324 timer = new javax.swing.Timer(timeout, this);
325 timer.setActionCommand("tick");
330 pane.setLayout(new GridBagLayout());
332 scanning_label = new JLabel("Scanning:");
336 c.fill = GridBagConstraints.NONE;
337 c.anchor = GridBagConstraints.CENTER;
345 c.anchor = GridBagConstraints.CENTER;
347 pane.add(scanning_label, c);
349 list = new JList(results) {
350 //Subclass JList to workaround bug 4832765, which can cause the
351 //scroll pane to not let the user easily scroll up to the beginning
352 //of the list. An alternative would be to set the unitIncrement
353 //of the JScrollBar to a fixed value. You wouldn't get the nice
354 //aligned scrolling, but it should work.
355 public int getScrollableUnitIncrement(Rectangle visibleRect,
359 if (orientation == SwingConstants.VERTICAL &&
360 direction < 0 && (row = getFirstVisibleIndex()) != -1) {
361 Rectangle r = getCellBounds(row, row);
362 if ((r.y == visibleRect.y) && (row != 0)) {
363 Point loc = r.getLocation();
365 int prevIndex = locationToIndex(loc);
366 Rectangle prevR = getCellBounds(prevIndex, prevIndex);
368 if (prevR == null || prevR.y >= r.y) {
374 return super.getScrollableUnitIncrement(
375 visibleRect, orientation, direction);
379 list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
380 list.setLayoutOrientation(JList.HORIZONTAL_WRAP);
381 list.setVisibleRowCount(-1);
383 list.addMouseListener(new MouseAdapter() {
384 public void mouseClicked(MouseEvent e) {
385 if (e.getClickCount() == 2) {
386 monitor_button.doClick(); //emulate button click
390 JScrollPane listScroller = new JScrollPane(list);
391 listScroller.setPreferredSize(new Dimension(400, 80));
392 listScroller.setAlignmentX(LEFT_ALIGNMENT);
394 //Create a container so that we can add a title around
395 //the scroll pane. Can't add a title directly to the
396 //scroll pane because its background would be white.
397 //Lay out the label and scroll pane from top to bottom.
398 JPanel listPane = new JPanel();
399 listPane.setLayout(new BoxLayout(listPane, BoxLayout.PAGE_AXIS));
401 JLabel label = new JLabel("Select Device");
402 label.setLabelFor(list);
404 listPane.add(Box.createRigidArea(new Dimension(0,5)));
405 listPane.add(listScroller);
406 listPane.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));
408 c.fill = GridBagConstraints.BOTH;
409 c.anchor = GridBagConstraints.CENTER;
417 c.anchor = GridBagConstraints.CENTER;
419 pane.add(listPane, c);
421 cancel_button = new JButton("Cancel");
422 cancel_button.addActionListener(this);
423 cancel_button.setActionCommand("cancel");
425 c.fill = GridBagConstraints.NONE;
426 c.anchor = GridBagConstraints.CENTER;
434 c.anchor = GridBagConstraints.CENTER;
436 pane.add(cancel_button, c);
438 monitor_button = new JButton("Monitor");
439 monitor_button.addActionListener(this);
440 monitor_button.setActionCommand("monitor");
442 c.fill = GridBagConstraints.NONE;
443 c.anchor = GridBagConstraints.CENTER;
451 c.anchor = GridBagConstraints.CENTER;
453 pane.add(monitor_button, c);
456 setLocationRelativeTo(owner);
458 addWindowListener(new ConfigListener(this));