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.
18 package org.altusmetrum.altosuilib_3;
21 import java.awt.event.*;
23 import javax.swing.event.*;
27 import java.util.concurrent.*;
28 import org.altusmetrum.altoslib_5.*;
30 class AltosScanResult {
34 AltosFrequency frequency;
38 boolean interrupted = false;
40 public String toString() {
41 return String.format("%-9.9s serial %-4d flight %-4d (%s %s %d)",
42 callsign, serial, flight,
43 frequency.toShortString(),
44 AltosLib.telemetry_name(telemetry),
45 AltosLib.ao_telemetry_rate_values[rate]);
48 public String toShortString() {
49 return String.format("%s %d %d %7.3f %d %d",
50 callsign, serial, flight, frequency, telemetry, rate);
53 public AltosScanResult(String in_callsign, int in_serial,
54 int in_flight, AltosFrequency in_frequency,
57 callsign = in_callsign;
60 frequency = in_frequency;
61 telemetry = in_telemetry;
65 public boolean equals(AltosScanResult other) {
66 return (serial == other.serial &&
67 frequency.frequency == other.frequency.frequency &&
68 telemetry == other.telemetry &&
72 public boolean up_to_date(AltosScanResult other) {
73 if (flight == 0 && other.flight != 0) {
74 flight = other.flight;
77 if (callsign.equals("N0CALL") && !other.callsign.equals("N0CALL")) {
78 callsign = other.callsign;
85 class AltosScanResults extends LinkedList<AltosScanResult> implements ListModel<AltosScanResult> {
87 LinkedList<ListDataListener> listeners = new LinkedList<ListDataListener>();
89 void changed(ListDataEvent de) {
90 for (ListDataListener l : listeners)
91 l.contentsChanged(de);
94 public boolean add(AltosScanResult r) {
96 for (AltosScanResult old : this) {
98 if (!old.up_to_date(r))
99 changed (new ListDataEvent(this,
100 ListDataEvent.CONTENTS_CHANGED,
108 changed(new ListDataEvent(this,
109 ListDataEvent.INTERVAL_ADDED,
110 this.size() - 2, this.size() - 1));
114 public void addListDataListener(ListDataListener l) {
118 public void removeListDataListener(ListDataListener l) {
122 public AltosScanResult getElementAt(int i) {
126 public int getSize() {
131 public class AltosScanUI
132 extends AltosUIDialog
133 implements ActionListener
137 AltosConfigData config_data;
138 AltosTelemetryReader reader;
139 private JList<AltosScanResult> list;
140 private JLabel scanning_label;
141 private JLabel frequency_label;
142 private JLabel telemetry_label;
143 private JLabel rate_label;
144 private JButton cancel_button;
145 private JButton monitor_button;
146 private JCheckBox[] telemetry_boxes;
147 private JCheckBox[] rate_boxes;
148 javax.swing.Timer timer;
149 AltosScanResults results = new AltosScanResults();
152 boolean select_telemetry = false;
155 boolean select_rate = false;
157 final static int timeout = 1200;
158 TelemetryHandler handler;
160 AltosFrequency[] frequencies;
163 void scan_exception(Exception e) {
164 if (e instanceof FileNotFoundException) {
165 JOptionPane.showMessageDialog(owner,
166 ((FileNotFoundException) e).getMessage(),
167 "Cannot open target device",
168 JOptionPane.ERROR_MESSAGE);
169 } else if (e instanceof AltosSerialInUseException) {
170 JOptionPane.showMessageDialog(owner,
171 String.format("Device \"%s\" already in use",
172 device.toShortString()),
174 JOptionPane.ERROR_MESSAGE);
175 } else if (e instanceof IOException) {
176 IOException ee = (IOException) e;
177 JOptionPane.showMessageDialog(owner,
178 device.toShortString(),
179 ee.getLocalizedMessage(),
180 JOptionPane.ERROR_MESSAGE);
182 JOptionPane.showMessageDialog(owner,
183 String.format("Connection to \"%s\" failed",
184 device.toShortString()),
186 JOptionPane.ERROR_MESSAGE);
191 class TelemetryHandler implements Runnable {
195 boolean interrupted = false;
200 AltosState state = reader.read();
203 if (state.flight != AltosLib.MISSING) {
204 final AltosScanResult result = new AltosScanResult(state.callsign,
207 frequencies[frequency_index],
210 Runnable r = new Runnable() {
215 SwingUtilities.invokeLater(r);
217 } catch (ParseException pp) {
218 } catch (AltosCRCException ce) {
221 } catch (InterruptedException ee) {
223 } catch (IOException ie) {
225 reader.close(interrupted);
231 frequency_label.setText(String.format("Frequency: %s", frequencies[frequency_index].toString()));
232 if (select_telemetry)
233 telemetry_label.setText(String.format("Telemetry: %s", AltosLib.telemetry_name(telemetry)));
235 rate_label.setText(String.format("Rate: %d baud", AltosLib.ao_telemetry_rate_values[rate]));
238 void set_telemetry() {
239 reader.set_telemetry(telemetry);
243 reader.set_telemetry_rate(rate);
246 void set_frequency() throws InterruptedException, TimeoutException {
247 reader.set_frequency(frequencies[frequency_index].frequency);
251 void next() throws InterruptedException, TimeoutException {
252 reader.set_monitor(false);
254 /* Let any pending input from the last configuration drain out */
258 boolean wrapped = false;
261 if (rate > AltosLib.ao_telemetry_rate_max) {
265 } while (!rate_boxes[rate].isSelected());
272 if (select_telemetry) {
273 boolean wrapped = false;
276 if (telemetry > AltosLib.ao_telemetry_max) {
278 telemetry = AltosLib.ao_telemetry_min;
280 } while (!telemetry_boxes[telemetry - AltosLib.ao_telemetry_min].isSelected());
288 if (frequency_index >= frequencies.length)
292 reader.set_monitor(true);
297 if (thread != null && thread.isAlive()) {
301 } catch (InterruptedException ie) {}
310 void tick_timer() throws InterruptedException, TimeoutException {
314 public void actionPerformed(ActionEvent e) {
315 String cmd = e.getActionCommand();
318 if (cmd.equals("cancel"))
321 if (cmd.equals("tick"))
324 if (cmd.equals("telemetry")) {
326 int scanning_telemetry = 0;
327 for (k = AltosLib.ao_telemetry_min; k <= AltosLib.ao_telemetry_max; k++) {
328 int j = k - AltosLib.ao_telemetry_min;
329 if (telemetry_boxes[j].isSelected())
330 scanning_telemetry |= (1 << k);
332 if (scanning_telemetry == 0) {
333 scanning_telemetry |= (1 << AltosLib.ao_telemetry_standard);
334 telemetry_boxes[AltosLib.ao_telemetry_standard - AltosLib.ao_telemetry_min].setSelected(true);
336 AltosUIPreferences.set_scanning_telemetry(scanning_telemetry);
339 if (cmd.equals("rate")) {
341 int scanning_rate = 0;
342 for (k = 0; k <= AltosLib.ao_telemetry_rate_max; k++) {
343 if (rate_boxes[k].isSelected())
344 scanning_rate |= (1 << k);
346 if (scanning_rate == 0) {
347 scanning_rate = (1 << 0);
348 rate_boxes[0].setSelected(true);
350 AltosUIPreferences.set_scanning_telemetry_rate(scanning_rate);
353 if (cmd.equals("monitor")) {
355 AltosScanResult r = (AltosScanResult) (list.getSelectedValue());
357 if (device != null) {
358 if (reader != null) {
359 System.out.printf("frequency %g rate %d\n", r.frequency.frequency, r.rate);
360 reader.set_telemetry(r.telemetry);
361 reader.set_telemetry_rate(r.rate);
362 reader.set_frequency(r.frequency.frequency);
363 reader.save_frequency();
364 reader.save_telemetry();
365 reader.save_telemetry_rate();
366 owner.scan_device_selected(device);
371 } catch (TimeoutException te) {
373 } catch (InterruptedException ie) {
378 /* A window listener to catch closing events and tell the config code */
379 class ConfigListener extends WindowAdapter {
382 public ConfigListener(AltosScanUI this_ui) {
386 public void windowClosing(WindowEvent e) {
387 ui.actionPerformed(new ActionEvent(e.getSource(),
388 ActionEvent.ACTION_PERFORMED,
393 private boolean open() {
394 device = AltosDeviceUIDialog.show(owner, AltosLib.product_basestation);
398 reader = new AltosTelemetryReader(new AltosSerial(device));
404 } catch (InterruptedException ie) {
407 handler = new TelemetryHandler();
408 thread = new Thread(handler);
411 } catch (FileNotFoundException ee) {
412 JOptionPane.showMessageDialog(owner,
414 "Cannot open target device",
415 JOptionPane.ERROR_MESSAGE);
416 } catch (AltosSerialInUseException si) {
417 JOptionPane.showMessageDialog(owner,
418 String.format("Device \"%s\" already in use",
419 device.toShortString()),
421 JOptionPane.ERROR_MESSAGE);
422 } catch (IOException ee) {
423 JOptionPane.showMessageDialog(owner,
424 device.toShortString(),
426 JOptionPane.ERROR_MESSAGE);
427 } catch (TimeoutException te) {
428 JOptionPane.showMessageDialog(owner,
429 device.toShortString(),
431 JOptionPane.ERROR_MESSAGE);
432 } catch (InterruptedException ie) {
433 JOptionPane.showMessageDialog(owner,
434 device.toShortString(),
435 "Interrupted exception",
436 JOptionPane.ERROR_MESSAGE);
443 public AltosScanUI(AltosUIFrame in_owner, boolean in_select_telemetry) {
446 select_telemetry = in_select_telemetry;
449 frequencies = AltosUIPreferences.common_frequencies();
452 telemetry = AltosLib.ao_telemetry_standard;
458 Container pane = getContentPane();
459 GridBagConstraints c = new GridBagConstraints();
460 Insets i = new Insets(4,4,4,4);
462 timer = new javax.swing.Timer(timeout, this);
463 timer.setActionCommand("tick");
468 pane.setLayout(new GridBagLayout());
470 scanning_label = new JLabel("Scanning:");
471 frequency_label = new JLabel("");
473 if (select_telemetry) {
474 telemetry_label = new JLabel("");
475 telemetry_label.setPreferredSize(new Dimension(100, 16));
476 telemetry = AltosLib.ao_telemetry_min;
478 telemetry = AltosLib.ao_telemetry_standard;
482 rate_label = new JLabel("");
483 rate_label.setPreferredSize(new Dimension(100, 16));
488 c.fill = GridBagConstraints.HORIZONTAL;
489 c.anchor = GridBagConstraints.LINE_START;
498 pane.add(scanning_label, c);
500 pane.add(frequency_label, c);
502 int y_offset_rate = 3;
503 int y_offset_telem = 3;
507 if (select_rate && select_telemetry)
513 pane.add(rate_label, c);
515 int scanning_rate = AltosUIPreferences.scanning_telemetry_rate();
516 rate_boxes = new JCheckBox[AltosLib.ao_telemetry_rate_max + 1];
517 for (int k = 0; k <= AltosLib.ao_telemetry_rate_max; k++) {
518 rate_boxes[k] = new JCheckBox(String.format("%d baud", AltosLib.ao_telemetry_rate_values[k]));
519 c.gridy = y_offset_rate + k;
520 pane.add(rate_boxes[k], c);
521 rate_boxes[k].setActionCommand("rate");
522 rate_boxes[k].addActionListener(this);
523 rate_boxes[k].setSelected((scanning_rate & (1 << k)) != 0);
525 y_offset_rate += AltosLib.ao_telemetry_rate_max + 1;
528 if (select_telemetry) {
531 pane.add(telemetry_label, c);
533 int scanning_telemetry = AltosUIPreferences.scanning_telemetry();
534 telemetry_boxes = new JCheckBox[AltosLib.ao_telemetry_max - AltosLib.ao_telemetry_min + 1];
535 for (int k = AltosLib.ao_telemetry_min; k <= AltosLib.ao_telemetry_max; k++) {
536 int j = k - AltosLib.ao_telemetry_min;
537 telemetry_boxes[j] = new JCheckBox(AltosLib.telemetry_name(k));
538 c.gridy = y_offset_telem + j;
539 pane.add(telemetry_boxes[j], c);
540 telemetry_boxes[j].setActionCommand("telemetry");
541 telemetry_boxes[j].addActionListener(this);
542 telemetry_boxes[j].setSelected((scanning_telemetry & (1 << k)) != 0);
544 y_offset_telem += (AltosLib.ao_telemetry_max - AltosLib.ao_telemetry_min + 1);
547 int y_offset = Math.max(y_offset_rate, y_offset_telem);
549 list = new JList<AltosScanResult>(results) {
550 //Subclass JList to workaround bug 4832765, which can cause the
551 //scroll pane to not let the user easily scroll up to the beginning
552 //of the list. An alternative would be to set the unitIncrement
553 //of the JScrollBar to a fixed value. You wouldn't get the nice
554 //aligned scrolling, but it should work.
555 public int getScrollableUnitIncrement(Rectangle visibleRect,
559 if (orientation == SwingConstants.VERTICAL &&
560 direction < 0 && (row = getFirstVisibleIndex()) != -1) {
561 Rectangle r = getCellBounds(row, row);
562 if ((r.y == visibleRect.y) && (row != 0)) {
563 Point loc = r.getLocation();
565 int prevIndex = locationToIndex(loc);
566 Rectangle prevR = getCellBounds(prevIndex, prevIndex);
568 if (prevR == null || prevR.y >= r.y) {
574 return super.getScrollableUnitIncrement(
575 visibleRect, orientation, direction);
579 list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
580 list.setLayoutOrientation(JList.HORIZONTAL_WRAP);
581 list.setVisibleRowCount(-1);
583 list.addMouseListener(new MouseAdapter() {
584 public void mouseClicked(MouseEvent e) {
585 if (e.getClickCount() == 2) {
586 monitor_button.doClick(); //emulate button click
590 JScrollPane listScroller = new JScrollPane(list);
591 listScroller.setPreferredSize(new Dimension(400, 80));
592 listScroller.setAlignmentX(LEFT_ALIGNMENT);
594 //Create a container so that we can add a title around
595 //the scroll pane. Can't add a title directly to the
596 //scroll pane because its background would be white.
597 //Lay out the label and scroll pane from top to bottom.
598 JPanel listPane = new JPanel();
599 listPane.setLayout(new BoxLayout(listPane, BoxLayout.PAGE_AXIS));
601 JLabel label = new JLabel("Select Device");
602 label.setLabelFor(list);
604 listPane.add(Box.createRigidArea(new Dimension(0,5)));
605 listPane.add(listScroller);
606 listPane.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));
608 c.fill = GridBagConstraints.BOTH;
609 c.anchor = GridBagConstraints.CENTER;
617 c.anchor = GridBagConstraints.CENTER;
619 pane.add(listPane, c);
621 cancel_button = new JButton("Cancel");
622 cancel_button.addActionListener(this);
623 cancel_button.setActionCommand("cancel");
625 c.fill = GridBagConstraints.NONE;
626 c.anchor = GridBagConstraints.CENTER;
632 c.gridy = y_offset + 1;
634 c.anchor = GridBagConstraints.CENTER;
636 pane.add(cancel_button, c);
638 monitor_button = new JButton("Monitor");
639 monitor_button.addActionListener(this);
640 monitor_button.setActionCommand("monitor");
642 c.fill = GridBagConstraints.NONE;
643 c.anchor = GridBagConstraints.CENTER;
649 c.gridy = y_offset + 1;
651 c.anchor = GridBagConstraints.CENTER;
653 pane.add(monitor_button, c);
656 setLocationRelativeTo(owner);
658 addWindowListener(new ConfigListener(this));