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 ((FileNotFoundException) e).getMessage(),
134 "Cannot open target device",
135 JOptionPane.ERROR_MESSAGE);
136 } else if (e instanceof AltosSerialInUseException) {
137 JOptionPane.showMessageDialog(owner,
138 String.format("Device \"%s\" already in use",
139 device.toShortString()),
141 JOptionPane.ERROR_MESSAGE);
142 } else if (e instanceof IOException) {
143 IOException ee = (IOException) e;
144 JOptionPane.showMessageDialog(owner,
145 device.toShortString(),
146 ee.getLocalizedMessage(),
147 JOptionPane.ERROR_MESSAGE);
149 JOptionPane.showMessageDialog(owner,
150 String.format("Connection to \"%s\" failed",
151 device.toShortString()),
153 JOptionPane.ERROR_MESSAGE);
158 class TelemetryHandler implements Runnable {
162 boolean interrupted = false;
167 AltosRecord record = reader.read();
170 if ((record.seen & AltosRecord.seen_flight) != 0) {
171 final AltosScanResult result = new AltosScanResult(record.callsign,
174 frequencies[frequency_index],
176 Runnable r = new Runnable() {
181 SwingUtilities.invokeLater(r);
183 } catch (ParseException pp) {
184 } catch (AltosCRCException ce) {
187 } catch (InterruptedException ee) {
189 } catch (IOException ie) {
191 reader.close(interrupted);
197 frequency_label.setText(String.format("Frequency: %s", frequencies[frequency_index].toString()));
198 telemetry_label.setText(String.format("Telemetry: %s", Altos.telemetry_name(telemetry)));
201 void set_telemetry() {
202 reader.set_telemetry(telemetry);
205 void set_frequency() throws InterruptedException, TimeoutException {
206 reader.set_frequency(frequencies[frequency_index].frequency);
209 void next() throws InterruptedException, TimeoutException {
210 reader.serial.set_monitor(false);
213 if (frequency_index >= frequencies.length ||
214 !telemetry_boxes[telemetry - Altos.ao_telemetry_min].isSelected())
219 if (telemetry > Altos.ao_telemetry_max)
220 telemetry = Altos.ao_telemetry_min;
221 } while (!telemetry_boxes[telemetry - Altos.ao_telemetry_min].isSelected());
226 reader.serial.set_monitor(true);
231 if (thread != null && thread.isAlive()) {
235 } catch (InterruptedException ie) {}
244 void tick_timer() throws InterruptedException, TimeoutException {
248 public void actionPerformed(ActionEvent e) {
249 String cmd = e.getActionCommand();
252 if (cmd.equals("cancel"))
255 if (cmd.equals("tick"))
258 if (cmd.equals("telemetry")) {
260 int scanning_telemetry = 0;
261 for (k = Altos.ao_telemetry_min; k <= Altos.ao_telemetry_max; k++) {
262 int j = k - Altos.ao_telemetry_min;
263 if (telemetry_boxes[j].isSelected())
264 scanning_telemetry |= (1 << k);
266 if (scanning_telemetry == 0) {
267 scanning_telemetry |= (1 << Altos.ao_telemetry_standard);
268 telemetry_boxes[Altos.ao_telemetry_standard - Altos.ao_telemetry_min].setSelected(true);
270 AltosPreferences.set_scanning_telemetry(scanning_telemetry);
273 if (cmd.equals("monitor")) {
275 AltosScanResult r = (AltosScanResult) (list.getSelectedValue());
277 if (device != null) {
278 if (reader != null) {
279 reader.set_telemetry(r.telemetry);
280 reader.set_frequency(r.frequency.frequency);
281 reader.save_frequency();
282 owner.telemetry_window(device);
287 } catch (TimeoutException te) {
289 } catch (InterruptedException ie) {
294 /* A window listener to catch closing events and tell the config code */
295 class ConfigListener extends WindowAdapter {
298 public ConfigListener(AltosScanUI this_ui) {
302 public void windowClosing(WindowEvent e) {
303 ui.actionPerformed(new ActionEvent(e.getSource(),
304 ActionEvent.ACTION_PERFORMED,
309 private boolean open() {
310 device = AltosDeviceDialog.show(owner, Altos.product_basestation);
314 reader = new AltosTelemetryReader(device);
319 } catch (InterruptedException ie) {
322 handler = new TelemetryHandler();
323 thread = new Thread(handler);
326 } catch (FileNotFoundException ee) {
327 JOptionPane.showMessageDialog(owner,
329 "Cannot open target device",
330 JOptionPane.ERROR_MESSAGE);
331 } catch (AltosSerialInUseException si) {
332 JOptionPane.showMessageDialog(owner,
333 String.format("Device \"%s\" already in use",
334 device.toShortString()),
336 JOptionPane.ERROR_MESSAGE);
337 } catch (IOException ee) {
338 JOptionPane.showMessageDialog(owner,
339 device.toShortString(),
341 JOptionPane.ERROR_MESSAGE);
342 } catch (TimeoutException te) {
343 JOptionPane.showMessageDialog(owner,
344 device.toShortString(),
346 JOptionPane.ERROR_MESSAGE);
347 } catch (InterruptedException ie) {
348 JOptionPane.showMessageDialog(owner,
349 device.toShortString(),
350 "Interrupted exception",
351 JOptionPane.ERROR_MESSAGE);
358 public AltosScanUI(AltosUI in_owner) {
362 frequencies = AltosPreferences.common_frequencies();
364 telemetry = Altos.ao_telemetry_min;
369 Container pane = getContentPane();
370 GridBagConstraints c = new GridBagConstraints();
371 Insets i = new Insets(4,4,4,4);
373 timer = new javax.swing.Timer(timeout, this);
374 timer.setActionCommand("tick");
379 pane.setLayout(new GridBagLayout());
381 scanning_label = new JLabel("Scanning:");
382 frequency_label = new JLabel("");
383 telemetry_label = new JLabel("");
387 c.fill = GridBagConstraints.HORIZONTAL;
388 c.anchor = GridBagConstraints.WEST;
397 pane.add(scanning_label, c);
399 pane.add(frequency_label, c);
401 pane.add(telemetry_label, c);
403 int scanning_telemetry = AltosPreferences.scanning_telemetry();
404 telemetry_boxes = new JCheckBox[Altos.ao_telemetry_max - Altos.ao_telemetry_min + 1];
405 for (int k = Altos.ao_telemetry_min; k <= Altos.ao_telemetry_max; k++) {
406 int j = k - Altos.ao_telemetry_min;
407 telemetry_boxes[j] = new JCheckBox(Altos.ao_telemetry_name[k]);
409 pane.add(telemetry_boxes[j], c);
410 telemetry_boxes[j].setActionCommand("telemetry");
411 telemetry_boxes[j].addActionListener(this);
412 telemetry_boxes[j].setSelected((scanning_telemetry & (1 << k)) != 0);
415 int y_offset = 3 + (Altos.ao_telemetry_max - Altos.ao_telemetry_min + 1);
417 list = new JList(results) {
418 //Subclass JList to workaround bug 4832765, which can cause the
419 //scroll pane to not let the user easily scroll up to the beginning
420 //of the list. An alternative would be to set the unitIncrement
421 //of the JScrollBar to a fixed value. You wouldn't get the nice
422 //aligned scrolling, but it should work.
423 public int getScrollableUnitIncrement(Rectangle visibleRect,
427 if (orientation == SwingConstants.VERTICAL &&
428 direction < 0 && (row = getFirstVisibleIndex()) != -1) {
429 Rectangle r = getCellBounds(row, row);
430 if ((r.y == visibleRect.y) && (row != 0)) {
431 Point loc = r.getLocation();
433 int prevIndex = locationToIndex(loc);
434 Rectangle prevR = getCellBounds(prevIndex, prevIndex);
436 if (prevR == null || prevR.y >= r.y) {
442 return super.getScrollableUnitIncrement(
443 visibleRect, orientation, direction);
447 list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
448 list.setLayoutOrientation(JList.HORIZONTAL_WRAP);
449 list.setVisibleRowCount(-1);
451 list.addMouseListener(new MouseAdapter() {
452 public void mouseClicked(MouseEvent e) {
453 if (e.getClickCount() == 2) {
454 monitor_button.doClick(); //emulate button click
458 JScrollPane listScroller = new JScrollPane(list);
459 listScroller.setPreferredSize(new Dimension(400, 80));
460 listScroller.setAlignmentX(LEFT_ALIGNMENT);
462 //Create a container so that we can add a title around
463 //the scroll pane. Can't add a title directly to the
464 //scroll pane because its background would be white.
465 //Lay out the label and scroll pane from top to bottom.
466 JPanel listPane = new JPanel();
467 listPane.setLayout(new BoxLayout(listPane, BoxLayout.PAGE_AXIS));
469 JLabel label = new JLabel("Select Device");
470 label.setLabelFor(list);
472 listPane.add(Box.createRigidArea(new Dimension(0,5)));
473 listPane.add(listScroller);
474 listPane.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));
476 c.fill = GridBagConstraints.BOTH;
477 c.anchor = GridBagConstraints.CENTER;
485 c.anchor = GridBagConstraints.CENTER;
487 pane.add(listPane, c);
489 cancel_button = new JButton("Cancel");
490 cancel_button.addActionListener(this);
491 cancel_button.setActionCommand("cancel");
493 c.fill = GridBagConstraints.NONE;
494 c.anchor = GridBagConstraints.CENTER;
500 c.gridy = y_offset + 1;
502 c.anchor = GridBagConstraints.CENTER;
504 pane.add(cancel_button, c);
506 monitor_button = new JButton("Monitor");
507 monitor_button.addActionListener(this);
508 monitor_button.setActionCommand("monitor");
510 c.fill = GridBagConstraints.NONE;
511 c.anchor = GridBagConstraints.CENTER;
517 c.gridy = y_offset + 1;
519 c.anchor = GridBagConstraints.CENTER;
521 pane.add(monitor_button, c);
524 setLocationRelativeTo(owner);
526 addWindowListener(new ConfigListener(this));