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_2;
21 import java.awt.event.*;
23 import javax.swing.event.*;
27 import java.util.concurrent.*;
28 import org.altusmetrum.altoslib_4.*;
30 class AltosScanResult {
34 AltosFrequency frequency;
37 boolean interrupted = false;
39 public String toString() {
40 return String.format("%-9.9s serial %-4d flight %-4d (%s %s)",
41 callsign, serial, flight, frequency.toShortString(), AltosLib.telemetry_name(telemetry));
44 public String toShortString() {
45 return String.format("%s %d %d %7.3f %d",
46 callsign, serial, flight, frequency, telemetry);
49 public AltosScanResult(String in_callsign, int in_serial,
50 int in_flight, AltosFrequency in_frequency, int in_telemetry) {
51 callsign = in_callsign;
54 frequency = in_frequency;
55 telemetry = in_telemetry;
58 public boolean equals(AltosScanResult other) {
59 return (serial == other.serial &&
60 frequency.frequency == other.frequency.frequency &&
61 telemetry == other.telemetry);
64 public boolean up_to_date(AltosScanResult other) {
65 if (flight == 0 && other.flight != 0) {
66 flight = other.flight;
69 if (callsign.equals("N0CALL") && !other.callsign.equals("N0CALL")) {
70 callsign = other.callsign;
77 class AltosScanResults extends LinkedList<AltosScanResult> implements ListModel<AltosScanResult> {
79 LinkedList<ListDataListener> listeners = new LinkedList<ListDataListener>();
81 void changed(ListDataEvent de) {
82 for (ListDataListener l : listeners)
83 l.contentsChanged(de);
86 public boolean add(AltosScanResult r) {
88 for (AltosScanResult old : this) {
90 if (!old.up_to_date(r))
91 changed (new ListDataEvent(this,
92 ListDataEvent.CONTENTS_CHANGED,
100 changed(new ListDataEvent(this,
101 ListDataEvent.INTERVAL_ADDED,
102 this.size() - 2, this.size() - 1));
106 public void addListDataListener(ListDataListener l) {
110 public void removeListDataListener(ListDataListener l) {
114 public AltosScanResult getElementAt(int i) {
118 public int getSize() {
123 public class AltosScanUI
124 extends AltosUIDialog
125 implements ActionListener
129 AltosConfigData config_data;
130 AltosTelemetryReader reader;
131 private JList<AltosScanResult> list;
132 private JLabel scanning_label;
133 private JLabel frequency_label;
134 private JLabel telemetry_label;
135 private JButton cancel_button;
136 private JButton monitor_button;
137 private JCheckBox[] telemetry_boxes;
138 javax.swing.Timer timer;
139 AltosScanResults results = new AltosScanResults();
142 boolean select_telemetry = false;
144 final static int timeout = 1200;
145 TelemetryHandler handler;
147 AltosFrequency[] frequencies;
150 void scan_exception(Exception e) {
151 if (e instanceof FileNotFoundException) {
152 JOptionPane.showMessageDialog(owner,
153 ((FileNotFoundException) e).getMessage(),
154 "Cannot open target device",
155 JOptionPane.ERROR_MESSAGE);
156 } else if (e instanceof AltosSerialInUseException) {
157 JOptionPane.showMessageDialog(owner,
158 String.format("Device \"%s\" already in use",
159 device.toShortString()),
161 JOptionPane.ERROR_MESSAGE);
162 } else if (e instanceof IOException) {
163 IOException ee = (IOException) e;
164 JOptionPane.showMessageDialog(owner,
165 device.toShortString(),
166 ee.getLocalizedMessage(),
167 JOptionPane.ERROR_MESSAGE);
169 JOptionPane.showMessageDialog(owner,
170 String.format("Connection to \"%s\" failed",
171 device.toShortString()),
173 JOptionPane.ERROR_MESSAGE);
178 class TelemetryHandler implements Runnable {
182 boolean interrupted = false;
187 AltosState state = reader.read();
190 if (state.flight != AltosLib.MISSING) {
191 final AltosScanResult result = new AltosScanResult(state.callsign,
194 frequencies[frequency_index],
196 Runnable r = new Runnable() {
201 SwingUtilities.invokeLater(r);
203 } catch (ParseException pp) {
204 } catch (AltosCRCException ce) {
207 } catch (InterruptedException ee) {
209 } catch (IOException ie) {
211 reader.close(interrupted);
217 frequency_label.setText(String.format("Frequency: %s", frequencies[frequency_index].toString()));
218 if (select_telemetry)
219 telemetry_label.setText(String.format("Telemetry: %s", AltosLib.telemetry_name(telemetry)));
222 void set_telemetry() {
223 reader.set_telemetry(telemetry);
226 void set_frequency() throws InterruptedException, TimeoutException {
227 reader.set_frequency(frequencies[frequency_index].frequency);
231 void next() throws InterruptedException, TimeoutException {
232 reader.set_monitor(false);
235 if (select_telemetry) {
236 if (frequency_index >= frequencies.length ||
237 !telemetry_boxes[telemetry - AltosLib.ao_telemetry_min].isSelected())
242 if (telemetry > AltosLib.ao_telemetry_max)
243 telemetry = AltosLib.ao_telemetry_min;
244 } while (!telemetry_boxes[telemetry - AltosLib.ao_telemetry_min].isSelected());
248 if (frequency_index >= frequencies.length)
253 reader.set_monitor(true);
258 if (thread != null && thread.isAlive()) {
262 } catch (InterruptedException ie) {}
271 void tick_timer() throws InterruptedException, TimeoutException {
275 public void actionPerformed(ActionEvent e) {
276 String cmd = e.getActionCommand();
279 if (cmd.equals("cancel"))
282 if (cmd.equals("tick"))
285 if (cmd.equals("telemetry")) {
287 int scanning_telemetry = 0;
288 for (k = AltosLib.ao_telemetry_min; k <= AltosLib.ao_telemetry_max; k++) {
289 int j = k - AltosLib.ao_telemetry_min;
290 if (telemetry_boxes[j].isSelected())
291 scanning_telemetry |= (1 << k);
293 if (scanning_telemetry == 0) {
294 scanning_telemetry |= (1 << AltosLib.ao_telemetry_standard);
295 telemetry_boxes[AltosLib.ao_telemetry_standard - AltosLib.ao_telemetry_min].setSelected(true);
297 AltosUIPreferences.set_scanning_telemetry(scanning_telemetry);
300 if (cmd.equals("monitor")) {
302 AltosScanResult r = (AltosScanResult) (list.getSelectedValue());
304 if (device != null) {
305 if (reader != null) {
306 reader.set_telemetry(r.telemetry);
307 reader.set_frequency(r.frequency.frequency);
308 reader.save_frequency();
309 owner.scan_device_selected(device);
314 } catch (TimeoutException te) {
316 } catch (InterruptedException ie) {
321 /* A window listener to catch closing events and tell the config code */
322 class ConfigListener extends WindowAdapter {
325 public ConfigListener(AltosScanUI this_ui) {
329 public void windowClosing(WindowEvent e) {
330 ui.actionPerformed(new ActionEvent(e.getSource(),
331 ActionEvent.ACTION_PERFORMED,
336 private boolean open() {
337 device = AltosDeviceUIDialog.show(owner, AltosLib.product_basestation);
341 reader = new AltosTelemetryReader(new AltosSerial(device));
346 } catch (InterruptedException ie) {
349 handler = new TelemetryHandler();
350 thread = new Thread(handler);
353 } catch (FileNotFoundException ee) {
354 JOptionPane.showMessageDialog(owner,
356 "Cannot open target device",
357 JOptionPane.ERROR_MESSAGE);
358 } catch (AltosSerialInUseException si) {
359 JOptionPane.showMessageDialog(owner,
360 String.format("Device \"%s\" already in use",
361 device.toShortString()),
363 JOptionPane.ERROR_MESSAGE);
364 } catch (IOException ee) {
365 JOptionPane.showMessageDialog(owner,
366 device.toShortString(),
368 JOptionPane.ERROR_MESSAGE);
369 } catch (TimeoutException te) {
370 JOptionPane.showMessageDialog(owner,
371 device.toShortString(),
373 JOptionPane.ERROR_MESSAGE);
374 } catch (InterruptedException ie) {
375 JOptionPane.showMessageDialog(owner,
376 device.toShortString(),
377 "Interrupted exception",
378 JOptionPane.ERROR_MESSAGE);
385 public AltosScanUI(AltosUIFrame in_owner, boolean in_select_telemetry) {
388 select_telemetry = in_select_telemetry;
390 frequencies = AltosUIPreferences.common_frequencies();
393 telemetry = AltosLib.ao_telemetry_standard;
398 Container pane = getContentPane();
399 GridBagConstraints c = new GridBagConstraints();
400 Insets i = new Insets(4,4,4,4);
402 timer = new javax.swing.Timer(timeout, this);
403 timer.setActionCommand("tick");
408 pane.setLayout(new GridBagLayout());
410 scanning_label = new JLabel("Scanning:");
411 frequency_label = new JLabel("");
413 if (select_telemetry) {
414 telemetry_label = new JLabel("");
415 telemetry = AltosLib.ao_telemetry_min;
417 telemetry = AltosLib.ao_telemetry_standard;
422 c.fill = GridBagConstraints.HORIZONTAL;
423 c.anchor = GridBagConstraints.WEST;
432 pane.add(scanning_label, c);
434 pane.add(frequency_label, c);
438 if (select_telemetry) {
440 pane.add(telemetry_label, c);
442 int scanning_telemetry = AltosUIPreferences.scanning_telemetry();
443 telemetry_boxes = new JCheckBox[AltosLib.ao_telemetry_max - AltosLib.ao_telemetry_min + 1];
444 for (int k = AltosLib.ao_telemetry_min; k <= AltosLib.ao_telemetry_max; k++) {
445 int j = k - AltosLib.ao_telemetry_min;
446 telemetry_boxes[j] = new JCheckBox(AltosLib.telemetry_name(k));
448 pane.add(telemetry_boxes[j], c);
449 telemetry_boxes[j].setActionCommand("telemetry");
450 telemetry_boxes[j].addActionListener(this);
451 telemetry_boxes[j].setSelected((scanning_telemetry & (1 << k)) != 0);
453 y_offset += (AltosLib.ao_telemetry_max - AltosLib.ao_telemetry_min + 1);
456 list = new JList<AltosScanResult>(results) {
457 //Subclass JList to workaround bug 4832765, which can cause the
458 //scroll pane to not let the user easily scroll up to the beginning
459 //of the list. An alternative would be to set the unitIncrement
460 //of the JScrollBar to a fixed value. You wouldn't get the nice
461 //aligned scrolling, but it should work.
462 public int getScrollableUnitIncrement(Rectangle visibleRect,
466 if (orientation == SwingConstants.VERTICAL &&
467 direction < 0 && (row = getFirstVisibleIndex()) != -1) {
468 Rectangle r = getCellBounds(row, row);
469 if ((r.y == visibleRect.y) && (row != 0)) {
470 Point loc = r.getLocation();
472 int prevIndex = locationToIndex(loc);
473 Rectangle prevR = getCellBounds(prevIndex, prevIndex);
475 if (prevR == null || prevR.y >= r.y) {
481 return super.getScrollableUnitIncrement(
482 visibleRect, orientation, direction);
486 list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
487 list.setLayoutOrientation(JList.HORIZONTAL_WRAP);
488 list.setVisibleRowCount(-1);
490 list.addMouseListener(new MouseAdapter() {
491 public void mouseClicked(MouseEvent e) {
492 if (e.getClickCount() == 2) {
493 monitor_button.doClick(); //emulate button click
497 JScrollPane listScroller = new JScrollPane(list);
498 listScroller.setPreferredSize(new Dimension(400, 80));
499 listScroller.setAlignmentX(LEFT_ALIGNMENT);
501 //Create a container so that we can add a title around
502 //the scroll pane. Can't add a title directly to the
503 //scroll pane because its background would be white.
504 //Lay out the label and scroll pane from top to bottom.
505 JPanel listPane = new JPanel();
506 listPane.setLayout(new BoxLayout(listPane, BoxLayout.PAGE_AXIS));
508 JLabel label = new JLabel("Select Device");
509 label.setLabelFor(list);
511 listPane.add(Box.createRigidArea(new Dimension(0,5)));
512 listPane.add(listScroller);
513 listPane.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));
515 c.fill = GridBagConstraints.BOTH;
516 c.anchor = GridBagConstraints.CENTER;
524 c.anchor = GridBagConstraints.CENTER;
526 pane.add(listPane, c);
528 cancel_button = new JButton("Cancel");
529 cancel_button.addActionListener(this);
530 cancel_button.setActionCommand("cancel");
532 c.fill = GridBagConstraints.NONE;
533 c.anchor = GridBagConstraints.CENTER;
539 c.gridy = y_offset + 1;
541 c.anchor = GridBagConstraints.CENTER;
543 pane.add(cancel_button, c);
545 monitor_button = new JButton("Monitor");
546 monitor_button.addActionListener(this);
547 monitor_button.setActionCommand("monitor");
549 c.fill = GridBagConstraints.NONE;
550 c.anchor = GridBagConstraints.CENTER;
556 c.gridy = y_offset + 1;
558 c.anchor = GridBagConstraints.CENTER;
560 pane.add(monitor_button, c);
563 setLocationRelativeTo(owner);
565 addWindowListener(new ConfigListener(this));