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 {
36 AltosFrequency frequency;
39 boolean interrupted = false;
41 public String toString() {
42 return String.format("%-9.9s serial %-4d flight %-4d (%s %s)",
43 callsign, serial, flight, frequency.toShortString(), Altos.telemetry_name(telemetry));
46 public String toShortString() {
47 return String.format("%s %d %d %7.3f %d",
48 callsign, serial, flight, frequency, telemetry);
51 public AltosScanResult(String in_callsign, int in_serial,
52 int in_flight, AltosFrequency in_frequency, int in_telemetry) {
53 callsign = in_callsign;
56 frequency = in_frequency;
57 telemetry = in_telemetry;
60 public boolean equals(AltosScanResult other) {
61 return (callsign.equals(other.callsign) &&
62 serial == other.serial &&
63 flight == other.flight &&
64 frequency.frequency == other.frequency.frequency &&
65 telemetry == other.telemetry);
69 class AltosScanResults extends LinkedList<AltosScanResult> implements ListModel {
71 LinkedList<ListDataListener> listeners = new LinkedList<ListDataListener>();
73 public boolean add(AltosScanResult r) {
74 for (AltosScanResult old : this)
79 ListDataEvent de = new ListDataEvent(this,
80 ListDataEvent.INTERVAL_ADDED,
81 this.size() - 2, this.size() - 1);
82 for (ListDataListener l : listeners)
83 l.contentsChanged(de);
87 public void addListDataListener(ListDataListener l) {
91 public void removeListDataListener(ListDataListener l) {
95 public AltosScanResult getElementAt(int i) {
99 public int getSize() {
104 public class AltosScanUI
106 implements ActionListener
110 AltosConfigData config_data;
111 AltosTelemetryReader reader;
113 private JLabel scanning_label;
114 private JLabel frequency_label;
115 private JLabel telemetry_label;
116 private JButton cancel_button;
117 private JButton monitor_button;
118 private JCheckBox[] telemetry_boxes;
119 javax.swing.Timer timer;
120 AltosScanResults results = new AltosScanResults();
124 final static int timeout = 1200;
125 TelemetryHandler handler;
127 AltosFrequency[] frequencies;
130 void scan_exception(Exception e) {
131 if (e instanceof FileNotFoundException) {
132 JOptionPane.showMessageDialog(owner,
133 String.format("Cannot open device \"%s\"",
134 device.toShortString()),
135 "Cannot open target device",
136 JOptionPane.ERROR_MESSAGE);
137 } else if (e instanceof AltosSerialInUseException) {
138 JOptionPane.showMessageDialog(owner,
139 String.format("Device \"%s\" already in use",
140 device.toShortString()),
142 JOptionPane.ERROR_MESSAGE);
143 } else if (e instanceof IOException) {
144 IOException ee = (IOException) e;
145 JOptionPane.showMessageDialog(owner,
146 device.toShortString(),
147 ee.getLocalizedMessage(),
148 JOptionPane.ERROR_MESSAGE);
150 JOptionPane.showMessageDialog(owner,
151 String.format("Connection to \"%s\" failed",
152 device.toShortString()),
154 JOptionPane.ERROR_MESSAGE);
159 class TelemetryHandler implements Runnable {
163 boolean interrupted = false;
168 AltosRecord record = reader.read();
171 if ((record.seen & AltosRecord.seen_flight) != 0) {
172 final AltosScanResult result = new AltosScanResult(record.callsign,
175 frequencies[frequency_index],
177 Runnable r = new Runnable() {
182 SwingUtilities.invokeLater(r);
184 } catch (ParseException pp) {
185 } catch (AltosCRCException ce) {
188 } catch (InterruptedException ee) {
190 } catch (IOException ie) {
192 reader.close(interrupted);
198 frequency_label.setText(String.format("Frequency: %s", frequencies[frequency_index].toString()));
199 telemetry_label.setText(String.format("Telemetry: %s", Altos.telemetry_name(telemetry)));
202 void set_telemetry() {
203 reader.set_telemetry(telemetry);
206 void set_frequency() throws InterruptedException, TimeoutException {
207 reader.set_frequency(frequencies[frequency_index].frequency);
210 void next() throws InterruptedException, TimeoutException {
211 reader.serial.set_monitor(false);
214 if (frequency_index >= frequencies.length ||
215 !telemetry_boxes[telemetry - Altos.ao_telemetry_min].isSelected())
220 if (telemetry > Altos.ao_telemetry_max)
221 telemetry = Altos.ao_telemetry_min;
222 } while (!telemetry_boxes[telemetry - Altos.ao_telemetry_min].isSelected());
227 reader.serial.set_monitor(true);
232 if (thread != null && thread.isAlive()) {
236 } catch (InterruptedException ie) {}
245 void tick_timer() throws InterruptedException, TimeoutException {
249 public void actionPerformed(ActionEvent e) {
250 String cmd = e.getActionCommand();
253 if (cmd.equals("cancel"))
256 if (cmd.equals("tick"))
259 if (cmd.equals("telemetry")) {
261 int scanning_telemetry = 0;
262 for (k = Altos.ao_telemetry_min; k <= Altos.ao_telemetry_max; k++) {
263 int j = k - Altos.ao_telemetry_min;
264 if (telemetry_boxes[j].isSelected())
265 scanning_telemetry |= (1 << k);
267 if (scanning_telemetry == 0) {
268 scanning_telemetry |= (1 << Altos.ao_telemetry_standard);
269 telemetry_boxes[Altos.ao_telemetry_standard - Altos.ao_telemetry_min].setSelected(true);
271 AltosPreferences.set_scanning_telemetry(scanning_telemetry);
274 if (cmd.equals("monitor")) {
276 AltosScanResult r = (AltosScanResult) (list.getSelectedValue());
278 if (device != null) {
279 if (reader != null) {
280 reader.set_telemetry(r.telemetry);
281 reader.set_frequency(r.frequency.frequency);
282 reader.save_frequency();
283 owner.telemetry_window(device);
288 } catch (TimeoutException te) {
290 } catch (InterruptedException ie) {
295 /* A window listener to catch closing events and tell the config code */
296 class ConfigListener extends WindowAdapter {
299 public ConfigListener(AltosScanUI this_ui) {
303 public void windowClosing(WindowEvent e) {
304 ui.actionPerformed(new ActionEvent(e.getSource(),
305 ActionEvent.ACTION_PERFORMED,
310 private boolean open() {
311 device = AltosDeviceDialog.show(owner, Altos.product_basestation);
315 reader = new AltosTelemetryReader(device);
320 } catch (InterruptedException ie) {
323 handler = new TelemetryHandler();
324 thread = new Thread(handler);
327 } catch (FileNotFoundException ee) {
328 JOptionPane.showMessageDialog(owner,
329 String.format("Cannot open device \"%s\"",
330 device.toShortString()),
331 "Cannot open target device",
332 JOptionPane.ERROR_MESSAGE);
333 } catch (AltosSerialInUseException si) {
334 JOptionPane.showMessageDialog(owner,
335 String.format("Device \"%s\" already in use",
336 device.toShortString()),
338 JOptionPane.ERROR_MESSAGE);
339 } catch (IOException ee) {
340 JOptionPane.showMessageDialog(owner,
341 device.toShortString(),
343 JOptionPane.ERROR_MESSAGE);
344 } catch (TimeoutException te) {
345 JOptionPane.showMessageDialog(owner,
346 device.toShortString(),
348 JOptionPane.ERROR_MESSAGE);
349 } catch (InterruptedException ie) {
350 JOptionPane.showMessageDialog(owner,
351 device.toShortString(),
352 "Interrupted exception",
353 JOptionPane.ERROR_MESSAGE);
360 public AltosScanUI(AltosUI in_owner) {
364 frequencies = AltosPreferences.common_frequencies();
366 telemetry = Altos.ao_telemetry_min;
371 Container pane = getContentPane();
372 GridBagConstraints c = new GridBagConstraints();
373 Insets i = new Insets(4,4,4,4);
375 timer = new javax.swing.Timer(timeout, this);
376 timer.setActionCommand("tick");
381 pane.setLayout(new GridBagLayout());
383 scanning_label = new JLabel("Scanning:");
384 frequency_label = new JLabel("");
385 telemetry_label = new JLabel("");
389 c.fill = GridBagConstraints.NONE;
390 c.anchor = GridBagConstraints.WEST;
399 pane.add(scanning_label, c);
401 pane.add(frequency_label, c);
403 pane.add(telemetry_label, c);
405 int scanning_telemetry = AltosPreferences.scanning_telemetry();
406 telemetry_boxes = new JCheckBox[Altos.ao_telemetry_max - Altos.ao_telemetry_min + 1];
407 for (int k = Altos.ao_telemetry_min; k <= Altos.ao_telemetry_max; k++) {
408 int j = k - Altos.ao_telemetry_min;
409 telemetry_boxes[j] = new JCheckBox(Altos.ao_telemetry_name[k]);
411 pane.add(telemetry_boxes[j], c);
412 telemetry_boxes[j].setActionCommand("telemetry");
413 telemetry_boxes[j].addActionListener(this);
414 telemetry_boxes[j].setSelected((scanning_telemetry & (1 << k)) != 0);
417 int y_offset = 3 + (Altos.ao_telemetry_max - Altos.ao_telemetry_min + 1);
419 list = new JList(results) {
420 //Subclass JList to workaround bug 4832765, which can cause the
421 //scroll pane to not let the user easily scroll up to the beginning
422 //of the list. An alternative would be to set the unitIncrement
423 //of the JScrollBar to a fixed value. You wouldn't get the nice
424 //aligned scrolling, but it should work.
425 public int getScrollableUnitIncrement(Rectangle visibleRect,
429 if (orientation == SwingConstants.VERTICAL &&
430 direction < 0 && (row = getFirstVisibleIndex()) != -1) {
431 Rectangle r = getCellBounds(row, row);
432 if ((r.y == visibleRect.y) && (row != 0)) {
433 Point loc = r.getLocation();
435 int prevIndex = locationToIndex(loc);
436 Rectangle prevR = getCellBounds(prevIndex, prevIndex);
438 if (prevR == null || prevR.y >= r.y) {
444 return super.getScrollableUnitIncrement(
445 visibleRect, orientation, direction);
449 list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
450 list.setLayoutOrientation(JList.HORIZONTAL_WRAP);
451 list.setVisibleRowCount(-1);
453 list.addMouseListener(new MouseAdapter() {
454 public void mouseClicked(MouseEvent e) {
455 if (e.getClickCount() == 2) {
456 monitor_button.doClick(); //emulate button click
460 JScrollPane listScroller = new JScrollPane(list);
461 listScroller.setPreferredSize(new Dimension(400, 80));
462 listScroller.setAlignmentX(LEFT_ALIGNMENT);
464 //Create a container so that we can add a title around
465 //the scroll pane. Can't add a title directly to the
466 //scroll pane because its background would be white.
467 //Lay out the label and scroll pane from top to bottom.
468 JPanel listPane = new JPanel();
469 listPane.setLayout(new BoxLayout(listPane, BoxLayout.PAGE_AXIS));
471 JLabel label = new JLabel("Select Device");
472 label.setLabelFor(list);
474 listPane.add(Box.createRigidArea(new Dimension(0,5)));
475 listPane.add(listScroller);
476 listPane.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));
478 c.fill = GridBagConstraints.BOTH;
479 c.anchor = GridBagConstraints.CENTER;
487 c.anchor = GridBagConstraints.CENTER;
489 pane.add(listPane, c);
491 cancel_button = new JButton("Cancel");
492 cancel_button.addActionListener(this);
493 cancel_button.setActionCommand("cancel");
495 c.fill = GridBagConstraints.NONE;
496 c.anchor = GridBagConstraints.CENTER;
502 c.gridy = y_offset + 1;
504 c.anchor = GridBagConstraints.CENTER;
506 pane.add(cancel_button, c);
508 monitor_button = new JButton("Monitor");
509 monitor_button.addActionListener(this);
510 monitor_button.setActionCommand("monitor");
512 c.fill = GridBagConstraints.NONE;
513 c.anchor = GridBagConstraints.CENTER;
519 c.gridy = y_offset + 1;
521 c.anchor = GridBagConstraints.CENTER;
523 pane.add(monitor_button, c);
526 setLocationRelativeTo(owner);
528 addWindowListener(new ConfigListener(this));