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_1.*;
29 import org.altusmetrum.altosuilib_1.*;
31 class AltosScanResult {
35 AltosFrequency frequency;
38 boolean interrupted = false;
40 public String toString() {
41 return String.format("%-9.9s serial %-4d flight %-4d (%s %s)",
42 callsign, serial, flight, frequency.toShortString(), Altos.telemetry_name(telemetry));
45 public String toShortString() {
46 return String.format("%s %d %d %7.3f %d",
47 callsign, serial, flight, frequency, telemetry);
50 public AltosScanResult(String in_callsign, int in_serial,
51 int in_flight, AltosFrequency in_frequency, int in_telemetry) {
52 callsign = in_callsign;
55 frequency = in_frequency;
56 telemetry = in_telemetry;
59 public boolean equals(AltosScanResult other) {
60 return (serial == other.serial &&
61 frequency.frequency == other.frequency.frequency &&
62 telemetry == other.telemetry);
65 public boolean up_to_date(AltosScanResult other) {
66 if (flight == 0 && other.flight != 0) {
67 flight = other.flight;
70 if (callsign.equals("N0CALL") && !other.callsign.equals("N0CALL")) {
71 callsign = other.callsign;
78 class AltosScanResults extends LinkedList<AltosScanResult> implements ListModel {
80 LinkedList<ListDataListener> listeners = new LinkedList<ListDataListener>();
82 void changed(ListDataEvent de) {
83 for (ListDataListener l : listeners)
84 l.contentsChanged(de);
87 public boolean add(AltosScanResult r) {
89 for (AltosScanResult old : this) {
91 if (!old.up_to_date(r))
92 changed (new ListDataEvent(this,
93 ListDataEvent.CONTENTS_CHANGED,
101 changed(new ListDataEvent(this,
102 ListDataEvent.INTERVAL_ADDED,
103 this.size() - 2, this.size() - 1));
107 public void addListDataListener(ListDataListener l) {
111 public void removeListDataListener(ListDataListener l) {
115 public AltosScanResult getElementAt(int i) {
119 public int getSize() {
124 public class AltosScanUI
125 extends AltosUIDialog
126 implements ActionListener
130 AltosConfigData config_data;
131 AltosTelemetryReader reader;
133 private JLabel scanning_label;
134 private JLabel frequency_label;
135 private JLabel telemetry_label;
136 private JButton cancel_button;
137 private JButton monitor_button;
138 private JCheckBox[] telemetry_boxes;
139 javax.swing.Timer timer;
140 AltosScanResults results = new AltosScanResults();
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 AltosRecord record = reader.read();
190 if ((record.seen & AltosRecord.seen_flight) != 0) {
191 final AltosScanResult result = new AltosScanResult(record.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 telemetry_label.setText(String.format("Telemetry: %s", Altos.telemetry_name(telemetry)));
221 void set_telemetry() {
222 reader.set_telemetry(telemetry);
225 void set_frequency() throws InterruptedException, TimeoutException {
226 reader.set_frequency(frequencies[frequency_index].frequency);
230 void next() throws InterruptedException, TimeoutException {
231 reader.set_monitor(false);
234 if (frequency_index >= frequencies.length ||
235 !telemetry_boxes[telemetry - Altos.ao_telemetry_min].isSelected())
240 if (telemetry > Altos.ao_telemetry_max)
241 telemetry = Altos.ao_telemetry_min;
242 } while (!telemetry_boxes[telemetry - Altos.ao_telemetry_min].isSelected());
247 reader.set_monitor(true);
252 if (thread != null && thread.isAlive()) {
256 } catch (InterruptedException ie) {}
265 void tick_timer() throws InterruptedException, TimeoutException {
269 public void actionPerformed(ActionEvent e) {
270 String cmd = e.getActionCommand();
273 if (cmd.equals("cancel"))
276 if (cmd.equals("tick"))
279 if (cmd.equals("telemetry")) {
281 int scanning_telemetry = 0;
282 for (k = Altos.ao_telemetry_min; k <= Altos.ao_telemetry_max; k++) {
283 int j = k - Altos.ao_telemetry_min;
284 if (telemetry_boxes[j].isSelected())
285 scanning_telemetry |= (1 << k);
287 if (scanning_telemetry == 0) {
288 scanning_telemetry |= (1 << Altos.ao_telemetry_standard);
289 telemetry_boxes[Altos.ao_telemetry_standard - Altos.ao_telemetry_min].setSelected(true);
291 AltosUIPreferences.set_scanning_telemetry(scanning_telemetry);
294 if (cmd.equals("monitor")) {
296 AltosScanResult r = (AltosScanResult) (list.getSelectedValue());
298 if (device != null) {
299 if (reader != null) {
300 reader.set_telemetry(r.telemetry);
301 reader.set_frequency(r.frequency.frequency);
302 reader.save_frequency();
303 owner.telemetry_window(device);
308 } catch (TimeoutException te) {
310 } catch (InterruptedException ie) {
315 /* A window listener to catch closing events and tell the config code */
316 class ConfigListener extends WindowAdapter {
319 public ConfigListener(AltosScanUI this_ui) {
323 public void windowClosing(WindowEvent e) {
324 ui.actionPerformed(new ActionEvent(e.getSource(),
325 ActionEvent.ACTION_PERFORMED,
330 private boolean open() {
331 device = AltosDeviceUIDialog.show(owner, Altos.product_basestation);
335 reader = new AltosTelemetryReader(new AltosSerial(device));
340 } catch (InterruptedException ie) {
343 handler = new TelemetryHandler();
344 thread = new Thread(handler);
347 } catch (FileNotFoundException ee) {
348 JOptionPane.showMessageDialog(owner,
350 "Cannot open target device",
351 JOptionPane.ERROR_MESSAGE);
352 } catch (AltosSerialInUseException si) {
353 JOptionPane.showMessageDialog(owner,
354 String.format("Device \"%s\" already in use",
355 device.toShortString()),
357 JOptionPane.ERROR_MESSAGE);
358 } catch (IOException ee) {
359 JOptionPane.showMessageDialog(owner,
360 device.toShortString(),
362 JOptionPane.ERROR_MESSAGE);
363 } catch (TimeoutException te) {
364 JOptionPane.showMessageDialog(owner,
365 device.toShortString(),
367 JOptionPane.ERROR_MESSAGE);
368 } catch (InterruptedException ie) {
369 JOptionPane.showMessageDialog(owner,
370 device.toShortString(),
371 "Interrupted exception",
372 JOptionPane.ERROR_MESSAGE);
379 public AltosScanUI(AltosUI in_owner) {
383 frequencies = AltosUIPreferences.common_frequencies();
385 telemetry = Altos.ao_telemetry_min;
390 Container pane = getContentPane();
391 GridBagConstraints c = new GridBagConstraints();
392 Insets i = new Insets(4,4,4,4);
394 timer = new javax.swing.Timer(timeout, this);
395 timer.setActionCommand("tick");
400 pane.setLayout(new GridBagLayout());
402 scanning_label = new JLabel("Scanning:");
403 frequency_label = new JLabel("");
404 telemetry_label = new JLabel("");
408 c.fill = GridBagConstraints.HORIZONTAL;
409 c.anchor = GridBagConstraints.WEST;
418 pane.add(scanning_label, c);
420 pane.add(frequency_label, c);
422 pane.add(telemetry_label, c);
424 int scanning_telemetry = AltosUIPreferences.scanning_telemetry();
425 telemetry_boxes = new JCheckBox[Altos.ao_telemetry_max - Altos.ao_telemetry_min + 1];
426 for (int k = Altos.ao_telemetry_min; k <= Altos.ao_telemetry_max; k++) {
427 int j = k - Altos.ao_telemetry_min;
428 telemetry_boxes[j] = new JCheckBox(AltosLib.telemetry_name(k));
430 pane.add(telemetry_boxes[j], c);
431 telemetry_boxes[j].setActionCommand("telemetry");
432 telemetry_boxes[j].addActionListener(this);
433 telemetry_boxes[j].setSelected((scanning_telemetry & (1 << k)) != 0);
436 int y_offset = 3 + (Altos.ao_telemetry_max - Altos.ao_telemetry_min + 1);
438 list = new JList(results) {
439 //Subclass JList to workaround bug 4832765, which can cause the
440 //scroll pane to not let the user easily scroll up to the beginning
441 //of the list. An alternative would be to set the unitIncrement
442 //of the JScrollBar to a fixed value. You wouldn't get the nice
443 //aligned scrolling, but it should work.
444 public int getScrollableUnitIncrement(Rectangle visibleRect,
448 if (orientation == SwingConstants.VERTICAL &&
449 direction < 0 && (row = getFirstVisibleIndex()) != -1) {
450 Rectangle r = getCellBounds(row, row);
451 if ((r.y == visibleRect.y) && (row != 0)) {
452 Point loc = r.getLocation();
454 int prevIndex = locationToIndex(loc);
455 Rectangle prevR = getCellBounds(prevIndex, prevIndex);
457 if (prevR == null || prevR.y >= r.y) {
463 return super.getScrollableUnitIncrement(
464 visibleRect, orientation, direction);
468 list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
469 list.setLayoutOrientation(JList.HORIZONTAL_WRAP);
470 list.setVisibleRowCount(-1);
472 list.addMouseListener(new MouseAdapter() {
473 public void mouseClicked(MouseEvent e) {
474 if (e.getClickCount() == 2) {
475 monitor_button.doClick(); //emulate button click
479 JScrollPane listScroller = new JScrollPane(list);
480 listScroller.setPreferredSize(new Dimension(400, 80));
481 listScroller.setAlignmentX(LEFT_ALIGNMENT);
483 //Create a container so that we can add a title around
484 //the scroll pane. Can't add a title directly to the
485 //scroll pane because its background would be white.
486 //Lay out the label and scroll pane from top to bottom.
487 JPanel listPane = new JPanel();
488 listPane.setLayout(new BoxLayout(listPane, BoxLayout.PAGE_AXIS));
490 JLabel label = new JLabel("Select Device");
491 label.setLabelFor(list);
493 listPane.add(Box.createRigidArea(new Dimension(0,5)));
494 listPane.add(listScroller);
495 listPane.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));
497 c.fill = GridBagConstraints.BOTH;
498 c.anchor = GridBagConstraints.CENTER;
506 c.anchor = GridBagConstraints.CENTER;
508 pane.add(listPane, c);
510 cancel_button = new JButton("Cancel");
511 cancel_button.addActionListener(this);
512 cancel_button.setActionCommand("cancel");
514 c.fill = GridBagConstraints.NONE;
515 c.anchor = GridBagConstraints.CENTER;
521 c.gridy = y_offset + 1;
523 c.anchor = GridBagConstraints.CENTER;
525 pane.add(cancel_button, c);
527 monitor_button = new JButton("Monitor");
528 monitor_button.addActionListener(this);
529 monitor_button.setActionCommand("monitor");
531 c.fill = GridBagConstraints.NONE;
532 c.anchor = GridBagConstraints.CENTER;
538 c.gridy = y_offset + 1;
540 c.anchor = GridBagConstraints.CENTER;
542 pane.add(monitor_button, c);
545 setLocationRelativeTo(owner);
547 addWindowListener(new ConfigListener(this));