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.event.*;
27 import java.util.concurrent.*;
28 import org.altusmetrum.AltosLib.*;
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(), Altos.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 {
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
125 implements ActionListener
129 AltosConfigData config_data;
130 AltosTelemetryReader reader;
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();
143 final static int timeout = 1200;
144 TelemetryHandler handler;
146 AltosFrequency[] frequencies;
149 void scan_exception(Exception e) {
150 if (e instanceof FileNotFoundException) {
151 JOptionPane.showMessageDialog(owner,
152 ((FileNotFoundException) e).getMessage(),
153 "Cannot open target device",
154 JOptionPane.ERROR_MESSAGE);
155 } else if (e instanceof AltosSerialInUseException) {
156 JOptionPane.showMessageDialog(owner,
157 String.format("Device \"%s\" already in use",
158 device.toShortString()),
160 JOptionPane.ERROR_MESSAGE);
161 } else if (e instanceof IOException) {
162 IOException ee = (IOException) e;
163 JOptionPane.showMessageDialog(owner,
164 device.toShortString(),
165 ee.getLocalizedMessage(),
166 JOptionPane.ERROR_MESSAGE);
168 JOptionPane.showMessageDialog(owner,
169 String.format("Connection to \"%s\" failed",
170 device.toShortString()),
172 JOptionPane.ERROR_MESSAGE);
177 class TelemetryHandler implements Runnable {
181 boolean interrupted = false;
186 AltosRecord record = reader.read();
189 if ((record.seen & AltosRecord.seen_flight) != 0) {
190 final AltosScanResult result = new AltosScanResult(record.callsign,
193 frequencies[frequency_index],
195 Runnable r = new Runnable() {
200 SwingUtilities.invokeLater(r);
202 } catch (ParseException pp) {
203 } catch (AltosCRCException ce) {
206 } catch (InterruptedException ee) {
208 } catch (IOException ie) {
210 reader.close(interrupted);
216 frequency_label.setText(String.format("Frequency: %s", frequencies[frequency_index].toString()));
217 telemetry_label.setText(String.format("Telemetry: %s", Altos.telemetry_name(telemetry)));
220 void set_telemetry() {
221 reader.set_telemetry(telemetry);
224 void set_frequency() throws InterruptedException, TimeoutException {
225 reader.set_frequency(frequencies[frequency_index].frequency);
229 void next() throws InterruptedException, TimeoutException {
230 reader.set_monitor(false);
233 if (frequency_index >= frequencies.length ||
234 !telemetry_boxes[telemetry - Altos.ao_telemetry_min].isSelected())
239 if (telemetry > Altos.ao_telemetry_max)
240 telemetry = Altos.ao_telemetry_min;
241 } while (!telemetry_boxes[telemetry - Altos.ao_telemetry_min].isSelected());
246 reader.set_monitor(true);
251 if (thread != null && thread.isAlive()) {
255 } catch (InterruptedException ie) {}
264 void tick_timer() throws InterruptedException, TimeoutException {
268 public void actionPerformed(ActionEvent e) {
269 String cmd = e.getActionCommand();
272 if (cmd.equals("cancel"))
275 if (cmd.equals("tick"))
278 if (cmd.equals("telemetry")) {
280 int scanning_telemetry = 0;
281 for (k = Altos.ao_telemetry_min; k <= Altos.ao_telemetry_max; k++) {
282 int j = k - Altos.ao_telemetry_min;
283 if (telemetry_boxes[j].isSelected())
284 scanning_telemetry |= (1 << k);
286 if (scanning_telemetry == 0) {
287 scanning_telemetry |= (1 << Altos.ao_telemetry_standard);
288 telemetry_boxes[Altos.ao_telemetry_standard - Altos.ao_telemetry_min].setSelected(true);
290 AltosUIPreferences.set_scanning_telemetry(scanning_telemetry);
293 if (cmd.equals("monitor")) {
295 AltosScanResult r = (AltosScanResult) (list.getSelectedValue());
297 if (device != null) {
298 if (reader != null) {
299 reader.set_telemetry(r.telemetry);
300 reader.set_frequency(r.frequency.frequency);
301 reader.save_frequency();
302 owner.telemetry_window(device);
307 } catch (TimeoutException te) {
309 } catch (InterruptedException ie) {
314 /* A window listener to catch closing events and tell the config code */
315 class ConfigListener extends WindowAdapter {
318 public ConfigListener(AltosScanUI this_ui) {
322 public void windowClosing(WindowEvent e) {
323 ui.actionPerformed(new ActionEvent(e.getSource(),
324 ActionEvent.ACTION_PERFORMED,
329 private boolean open() {
330 device = AltosDeviceDialog.show(owner, Altos.product_basestation);
334 reader = new AltosTelemetryReader(new AltosSerial(device));
339 } catch (InterruptedException ie) {
342 handler = new TelemetryHandler();
343 thread = new Thread(handler);
346 } catch (FileNotFoundException ee) {
347 JOptionPane.showMessageDialog(owner,
349 "Cannot open target device",
350 JOptionPane.ERROR_MESSAGE);
351 } catch (AltosSerialInUseException si) {
352 JOptionPane.showMessageDialog(owner,
353 String.format("Device \"%s\" already in use",
354 device.toShortString()),
356 JOptionPane.ERROR_MESSAGE);
357 } catch (IOException ee) {
358 JOptionPane.showMessageDialog(owner,
359 device.toShortString(),
361 JOptionPane.ERROR_MESSAGE);
362 } catch (TimeoutException te) {
363 JOptionPane.showMessageDialog(owner,
364 device.toShortString(),
366 JOptionPane.ERROR_MESSAGE);
367 } catch (InterruptedException ie) {
368 JOptionPane.showMessageDialog(owner,
369 device.toShortString(),
370 "Interrupted exception",
371 JOptionPane.ERROR_MESSAGE);
378 public AltosScanUI(AltosUI in_owner) {
382 frequencies = AltosUIPreferences.common_frequencies();
384 telemetry = Altos.ao_telemetry_min;
389 Container pane = getContentPane();
390 GridBagConstraints c = new GridBagConstraints();
391 Insets i = new Insets(4,4,4,4);
393 timer = new javax.swing.Timer(timeout, this);
394 timer.setActionCommand("tick");
399 pane.setLayout(new GridBagLayout());
401 scanning_label = new JLabel("Scanning:");
402 frequency_label = new JLabel("");
403 telemetry_label = new JLabel("");
407 c.fill = GridBagConstraints.HORIZONTAL;
408 c.anchor = GridBagConstraints.WEST;
417 pane.add(scanning_label, c);
419 pane.add(frequency_label, c);
421 pane.add(telemetry_label, c);
423 int scanning_telemetry = AltosUIPreferences.scanning_telemetry();
424 telemetry_boxes = new JCheckBox[Altos.ao_telemetry_max - Altos.ao_telemetry_min + 1];
425 for (int k = Altos.ao_telemetry_min; k <= Altos.ao_telemetry_max; k++) {
426 int j = k - Altos.ao_telemetry_min;
427 telemetry_boxes[j] = new JCheckBox(AltosLib.telemetry_name(k));
429 pane.add(telemetry_boxes[j], c);
430 telemetry_boxes[j].setActionCommand("telemetry");
431 telemetry_boxes[j].addActionListener(this);
432 telemetry_boxes[j].setSelected((scanning_telemetry & (1 << k)) != 0);
435 int y_offset = 3 + (Altos.ao_telemetry_max - Altos.ao_telemetry_min + 1);
437 list = new JList(results) {
438 //Subclass JList to workaround bug 4832765, which can cause the
439 //scroll pane to not let the user easily scroll up to the beginning
440 //of the list. An alternative would be to set the unitIncrement
441 //of the JScrollBar to a fixed value. You wouldn't get the nice
442 //aligned scrolling, but it should work.
443 public int getScrollableUnitIncrement(Rectangle visibleRect,
447 if (orientation == SwingConstants.VERTICAL &&
448 direction < 0 && (row = getFirstVisibleIndex()) != -1) {
449 Rectangle r = getCellBounds(row, row);
450 if ((r.y == visibleRect.y) && (row != 0)) {
451 Point loc = r.getLocation();
453 int prevIndex = locationToIndex(loc);
454 Rectangle prevR = getCellBounds(prevIndex, prevIndex);
456 if (prevR == null || prevR.y >= r.y) {
462 return super.getScrollableUnitIncrement(
463 visibleRect, orientation, direction);
467 list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
468 list.setLayoutOrientation(JList.HORIZONTAL_WRAP);
469 list.setVisibleRowCount(-1);
471 list.addMouseListener(new MouseAdapter() {
472 public void mouseClicked(MouseEvent e) {
473 if (e.getClickCount() == 2) {
474 monitor_button.doClick(); //emulate button click
478 JScrollPane listScroller = new JScrollPane(list);
479 listScroller.setPreferredSize(new Dimension(400, 80));
480 listScroller.setAlignmentX(LEFT_ALIGNMENT);
482 //Create a container so that we can add a title around
483 //the scroll pane. Can't add a title directly to the
484 //scroll pane because its background would be white.
485 //Lay out the label and scroll pane from top to bottom.
486 JPanel listPane = new JPanel();
487 listPane.setLayout(new BoxLayout(listPane, BoxLayout.PAGE_AXIS));
489 JLabel label = new JLabel("Select Device");
490 label.setLabelFor(list);
492 listPane.add(Box.createRigidArea(new Dimension(0,5)));
493 listPane.add(listScroller);
494 listPane.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));
496 c.fill = GridBagConstraints.BOTH;
497 c.anchor = GridBagConstraints.CENTER;
505 c.anchor = GridBagConstraints.CENTER;
507 pane.add(listPane, c);
509 cancel_button = new JButton("Cancel");
510 cancel_button.addActionListener(this);
511 cancel_button.setActionCommand("cancel");
513 c.fill = GridBagConstraints.NONE;
514 c.anchor = GridBagConstraints.CENTER;
520 c.gridy = y_offset + 1;
522 c.anchor = GridBagConstraints.CENTER;
524 pane.add(cancel_button, c);
526 monitor_button = new JButton("Monitor");
527 monitor_button.addActionListener(this);
528 monitor_button.setActionCommand("monitor");
530 c.fill = GridBagConstraints.NONE;
531 c.anchor = GridBagConstraints.CENTER;
537 c.gridy = y_offset + 1;
539 c.anchor = GridBagConstraints.CENTER;
541 pane.add(monitor_button, c);
544 setLocationRelativeTo(owner);
546 addWindowListener(new ConfigListener(this));