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.*;
31 import org.altusmetrum.AltosLib.*;
33 class AltosScanResult {
37 AltosFrequency frequency;
40 boolean interrupted = false;
42 public String toString() {
43 return String.format("%-9.9s serial %-4d flight %-4d (%s %s)",
44 callsign, serial, flight, frequency.toShortString(), Altos.telemetry_name(telemetry));
47 public String toShortString() {
48 return String.format("%s %d %d %7.3f %d",
49 callsign, serial, flight, frequency, telemetry);
52 public AltosScanResult(String in_callsign, int in_serial,
53 int in_flight, AltosFrequency in_frequency, int in_telemetry) {
54 callsign = in_callsign;
57 frequency = in_frequency;
58 telemetry = in_telemetry;
61 public boolean equals(AltosScanResult other) {
62 return (serial == other.serial &&
63 frequency.frequency == other.frequency.frequency &&
64 telemetry == other.telemetry);
67 public boolean up_to_date(AltosScanResult other) {
68 if (flight == 0 && other.flight != 0) {
69 flight = other.flight;
72 if (callsign.equals("N0CALL") && !other.callsign.equals("N0CALL")) {
73 callsign = other.callsign;
80 class AltosScanResults extends LinkedList<AltosScanResult> implements ListModel {
82 LinkedList<ListDataListener> listeners = new LinkedList<ListDataListener>();
84 void changed(ListDataEvent de) {
85 for (ListDataListener l : listeners)
86 l.contentsChanged(de);
89 public boolean add(AltosScanResult r) {
91 for (AltosScanResult old : this) {
93 if (!old.up_to_date(r))
94 changed (new ListDataEvent(this,
95 ListDataEvent.CONTENTS_CHANGED,
103 changed(new ListDataEvent(this,
104 ListDataEvent.INTERVAL_ADDED,
105 this.size() - 2, this.size() - 1));
109 public void addListDataListener(ListDataListener l) {
113 public void removeListDataListener(ListDataListener l) {
117 public AltosScanResult getElementAt(int i) {
121 public int getSize() {
126 public class AltosScanUI
128 implements ActionListener
132 AltosConfigData config_data;
133 AltosTelemetryReader reader;
135 private JLabel scanning_label;
136 private JLabel frequency_label;
137 private JLabel telemetry_label;
138 private JButton cancel_button;
139 private JButton monitor_button;
140 private JCheckBox[] telemetry_boxes;
141 javax.swing.Timer timer;
142 AltosScanResults results = new AltosScanResults();
146 final static int timeout = 1200;
147 TelemetryHandler handler;
149 AltosFrequency[] frequencies;
152 void scan_exception(Exception e) {
153 if (e instanceof FileNotFoundException) {
154 JOptionPane.showMessageDialog(owner,
155 ((FileNotFoundException) e).getMessage(),
156 "Cannot open target device",
157 JOptionPane.ERROR_MESSAGE);
158 } else if (e instanceof AltosSerialInUseException) {
159 JOptionPane.showMessageDialog(owner,
160 String.format("Device \"%s\" already in use",
161 device.toShortString()),
163 JOptionPane.ERROR_MESSAGE);
164 } else if (e instanceof IOException) {
165 IOException ee = (IOException) e;
166 JOptionPane.showMessageDialog(owner,
167 device.toShortString(),
168 ee.getLocalizedMessage(),
169 JOptionPane.ERROR_MESSAGE);
171 JOptionPane.showMessageDialog(owner,
172 String.format("Connection to \"%s\" failed",
173 device.toShortString()),
175 JOptionPane.ERROR_MESSAGE);
180 class TelemetryHandler implements Runnable {
184 boolean interrupted = false;
189 AltosRecord record = reader.read();
192 if ((record.seen & AltosRecord.seen_flight) != 0) {
193 final AltosScanResult result = new AltosScanResult(record.callsign,
196 frequencies[frequency_index],
198 Runnable r = new Runnable() {
203 SwingUtilities.invokeLater(r);
205 } catch (ParseException pp) {
206 } catch (AltosCRCException ce) {
209 } catch (InterruptedException ee) {
211 } catch (IOException ie) {
213 reader.close(interrupted);
219 frequency_label.setText(String.format("Frequency: %s", frequencies[frequency_index].toString()));
220 telemetry_label.setText(String.format("Telemetry: %s", Altos.telemetry_name(telemetry)));
223 void set_telemetry() {
224 reader.set_telemetry(telemetry);
227 void set_frequency() throws InterruptedException, TimeoutException {
228 reader.set_frequency(frequencies[frequency_index].frequency);
232 void next() throws InterruptedException, TimeoutException {
233 reader.set_monitor(false);
236 if (frequency_index >= frequencies.length ||
237 !telemetry_boxes[telemetry - Altos.ao_telemetry_min].isSelected())
242 if (telemetry > Altos.ao_telemetry_max)
243 telemetry = Altos.ao_telemetry_min;
244 } while (!telemetry_boxes[telemetry - Altos.ao_telemetry_min].isSelected());
249 reader.set_monitor(true);
254 if (thread != null && thread.isAlive()) {
258 } catch (InterruptedException ie) {}
267 void tick_timer() throws InterruptedException, TimeoutException {
271 public void actionPerformed(ActionEvent e) {
272 String cmd = e.getActionCommand();
275 if (cmd.equals("cancel"))
278 if (cmd.equals("tick"))
281 if (cmd.equals("telemetry")) {
283 int scanning_telemetry = 0;
284 for (k = Altos.ao_telemetry_min; k <= Altos.ao_telemetry_max; k++) {
285 int j = k - Altos.ao_telemetry_min;
286 if (telemetry_boxes[j].isSelected())
287 scanning_telemetry |= (1 << k);
289 if (scanning_telemetry == 0) {
290 scanning_telemetry |= (1 << Altos.ao_telemetry_standard);
291 telemetry_boxes[Altos.ao_telemetry_standard - Altos.ao_telemetry_min].setSelected(true);
293 AltosUIPreferences.set_scanning_telemetry(scanning_telemetry);
296 if (cmd.equals("monitor")) {
298 AltosScanResult r = (AltosScanResult) (list.getSelectedValue());
300 if (device != null) {
301 if (reader != null) {
302 reader.set_telemetry(r.telemetry);
303 reader.set_frequency(r.frequency.frequency);
304 reader.save_frequency();
305 owner.telemetry_window(device);
310 } catch (TimeoutException te) {
312 } catch (InterruptedException ie) {
317 /* A window listener to catch closing events and tell the config code */
318 class ConfigListener extends WindowAdapter {
321 public ConfigListener(AltosScanUI this_ui) {
325 public void windowClosing(WindowEvent e) {
326 ui.actionPerformed(new ActionEvent(e.getSource(),
327 ActionEvent.ACTION_PERFORMED,
332 private boolean open() {
333 device = AltosDeviceDialog.show(owner, Altos.product_basestation);
337 reader = new AltosTelemetryReader(new AltosSerial(device));
342 } catch (InterruptedException ie) {
345 handler = new TelemetryHandler();
346 thread = new Thread(handler);
349 } catch (FileNotFoundException ee) {
350 JOptionPane.showMessageDialog(owner,
352 "Cannot open target device",
353 JOptionPane.ERROR_MESSAGE);
354 } catch (AltosSerialInUseException si) {
355 JOptionPane.showMessageDialog(owner,
356 String.format("Device \"%s\" already in use",
357 device.toShortString()),
359 JOptionPane.ERROR_MESSAGE);
360 } catch (IOException ee) {
361 JOptionPane.showMessageDialog(owner,
362 device.toShortString(),
364 JOptionPane.ERROR_MESSAGE);
365 } catch (TimeoutException te) {
366 JOptionPane.showMessageDialog(owner,
367 device.toShortString(),
369 JOptionPane.ERROR_MESSAGE);
370 } catch (InterruptedException ie) {
371 JOptionPane.showMessageDialog(owner,
372 device.toShortString(),
373 "Interrupted exception",
374 JOptionPane.ERROR_MESSAGE);
381 public AltosScanUI(AltosUI in_owner) {
385 frequencies = AltosUIPreferences.common_frequencies();
387 telemetry = Altos.ao_telemetry_min;
392 Container pane = getContentPane();
393 GridBagConstraints c = new GridBagConstraints();
394 Insets i = new Insets(4,4,4,4);
396 timer = new javax.swing.Timer(timeout, this);
397 timer.setActionCommand("tick");
402 pane.setLayout(new GridBagLayout());
404 scanning_label = new JLabel("Scanning:");
405 frequency_label = new JLabel("");
406 telemetry_label = new JLabel("");
410 c.fill = GridBagConstraints.HORIZONTAL;
411 c.anchor = GridBagConstraints.WEST;
420 pane.add(scanning_label, c);
422 pane.add(frequency_label, c);
424 pane.add(telemetry_label, c);
426 int scanning_telemetry = AltosUIPreferences.scanning_telemetry();
427 telemetry_boxes = new JCheckBox[Altos.ao_telemetry_max - Altos.ao_telemetry_min + 1];
428 for (int k = Altos.ao_telemetry_min; k <= Altos.ao_telemetry_max; k++) {
429 int j = k - Altos.ao_telemetry_min;
430 telemetry_boxes[j] = new JCheckBox(Altos.ao_telemetry_name[k]);
432 pane.add(telemetry_boxes[j], c);
433 telemetry_boxes[j].setActionCommand("telemetry");
434 telemetry_boxes[j].addActionListener(this);
435 telemetry_boxes[j].setSelected((scanning_telemetry & (1 << k)) != 0);
438 int y_offset = 3 + (Altos.ao_telemetry_max - Altos.ao_telemetry_min + 1);
440 list = new JList(results) {
441 //Subclass JList to workaround bug 4832765, which can cause the
442 //scroll pane to not let the user easily scroll up to the beginning
443 //of the list. An alternative would be to set the unitIncrement
444 //of the JScrollBar to a fixed value. You wouldn't get the nice
445 //aligned scrolling, but it should work.
446 public int getScrollableUnitIncrement(Rectangle visibleRect,
450 if (orientation == SwingConstants.VERTICAL &&
451 direction < 0 && (row = getFirstVisibleIndex()) != -1) {
452 Rectangle r = getCellBounds(row, row);
453 if ((r.y == visibleRect.y) && (row != 0)) {
454 Point loc = r.getLocation();
456 int prevIndex = locationToIndex(loc);
457 Rectangle prevR = getCellBounds(prevIndex, prevIndex);
459 if (prevR == null || prevR.y >= r.y) {
465 return super.getScrollableUnitIncrement(
466 visibleRect, orientation, direction);
470 list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
471 list.setLayoutOrientation(JList.HORIZONTAL_WRAP);
472 list.setVisibleRowCount(-1);
474 list.addMouseListener(new MouseAdapter() {
475 public void mouseClicked(MouseEvent e) {
476 if (e.getClickCount() == 2) {
477 monitor_button.doClick(); //emulate button click
481 JScrollPane listScroller = new JScrollPane(list);
482 listScroller.setPreferredSize(new Dimension(400, 80));
483 listScroller.setAlignmentX(LEFT_ALIGNMENT);
485 //Create a container so that we can add a title around
486 //the scroll pane. Can't add a title directly to the
487 //scroll pane because its background would be white.
488 //Lay out the label and scroll pane from top to bottom.
489 JPanel listPane = new JPanel();
490 listPane.setLayout(new BoxLayout(listPane, BoxLayout.PAGE_AXIS));
492 JLabel label = new JLabel("Select Device");
493 label.setLabelFor(list);
495 listPane.add(Box.createRigidArea(new Dimension(0,5)));
496 listPane.add(listScroller);
497 listPane.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));
499 c.fill = GridBagConstraints.BOTH;
500 c.anchor = GridBagConstraints.CENTER;
508 c.anchor = GridBagConstraints.CENTER;
510 pane.add(listPane, c);
512 cancel_button = new JButton("Cancel");
513 cancel_button.addActionListener(this);
514 cancel_button.setActionCommand("cancel");
516 c.fill = GridBagConstraints.NONE;
517 c.anchor = GridBagConstraints.CENTER;
523 c.gridy = y_offset + 1;
525 c.anchor = GridBagConstraints.CENTER;
527 pane.add(cancel_button, c);
529 monitor_button = new JButton("Monitor");
530 monitor_button.addActionListener(this);
531 monitor_button.setActionCommand("monitor");
533 c.fill = GridBagConstraints.NONE;
534 c.anchor = GridBagConstraints.CENTER;
540 c.gridy = y_offset + 1;
542 c.anchor = GridBagConstraints.CENTER;
544 pane.add(monitor_button, c);
547 setLocationRelativeTo(owner);
549 addWindowListener(new ConfigListener(this));