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 {
39 boolean interrupted = false;
41 public String toString() {
42 return String.format("%-9.9s serial %-4d flight %-4d (frequency %7.3f %s)",
43 callsign, serial, flight, frequency, 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, double 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 == other.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 javax.swing.Timer timer;
119 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,
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) {
217 if (telemetry > Altos.ao_telemetry_max)
218 telemetry = Altos.ao_telemetry_min;
223 reader.serial.set_monitor(true);
228 if (thread != null && thread.isAlive()) {
232 } catch (InterruptedException ie) {}
241 void tick_timer() throws InterruptedException, TimeoutException {
245 public void actionPerformed(ActionEvent e) {
246 String cmd = e.getActionCommand();
249 if (cmd.equals("cancel"))
252 if (cmd.equals("tick"))
255 if (cmd.equals("monitor")) {
257 AltosScanResult r = (AltosScanResult) (list.getSelectedValue());
259 if (device != null) {
260 if (reader != null) {
261 reader.set_telemetry(r.telemetry);
262 reader.set_frequency(r.frequency);
263 owner.telemetry_window(device);
268 } catch (TimeoutException te) {
270 } catch (InterruptedException ie) {
275 /* A window listener to catch closing events and tell the config code */
276 class ConfigListener extends WindowAdapter {
279 public ConfigListener(AltosScanUI this_ui) {
283 public void windowClosing(WindowEvent e) {
284 ui.actionPerformed(new ActionEvent(e.getSource(),
285 ActionEvent.ACTION_PERFORMED,
290 private boolean open() {
291 device = AltosDeviceDialog.show(owner, Altos.product_basestation);
295 reader = new AltosTelemetryReader(device);
300 } catch (InterruptedException ie) {
303 handler = new TelemetryHandler();
304 thread = new Thread(handler);
307 } catch (FileNotFoundException ee) {
308 JOptionPane.showMessageDialog(owner,
309 String.format("Cannot open device \"%s\"",
310 device.toShortString()),
311 "Cannot open target device",
312 JOptionPane.ERROR_MESSAGE);
313 } catch (AltosSerialInUseException si) {
314 JOptionPane.showMessageDialog(owner,
315 String.format("Device \"%s\" already in use",
316 device.toShortString()),
318 JOptionPane.ERROR_MESSAGE);
319 } catch (IOException ee) {
320 JOptionPane.showMessageDialog(owner,
321 device.toShortString(),
323 JOptionPane.ERROR_MESSAGE);
324 } catch (TimeoutException te) {
325 JOptionPane.showMessageDialog(owner,
326 device.toShortString(),
328 JOptionPane.ERROR_MESSAGE);
329 } catch (InterruptedException ie) {
330 JOptionPane.showMessageDialog(owner,
331 device.toShortString(),
332 "Interrupted exception",
333 JOptionPane.ERROR_MESSAGE);
340 public AltosScanUI(AltosUI in_owner) {
344 frequencies = AltosPreferences.common_frequencies();
346 telemetry = Altos.ao_telemetry_min;
351 Container pane = getContentPane();
352 GridBagConstraints c = new GridBagConstraints();
353 Insets i = new Insets(4,4,4,4);
355 timer = new javax.swing.Timer(timeout, this);
356 timer.setActionCommand("tick");
361 pane.setLayout(new GridBagLayout());
363 scanning_label = new JLabel("Scanning:");
364 frequency_label = new JLabel("");
365 telemetry_label = new JLabel("");
369 c.fill = GridBagConstraints.NONE;
370 c.anchor = GridBagConstraints.WEST;
379 pane.add(scanning_label, c);
381 pane.add(frequency_label, c);
383 pane.add(telemetry_label, c);
385 list = new JList(results) {
386 //Subclass JList to workaround bug 4832765, which can cause the
387 //scroll pane to not let the user easily scroll up to the beginning
388 //of the list. An alternative would be to set the unitIncrement
389 //of the JScrollBar to a fixed value. You wouldn't get the nice
390 //aligned scrolling, but it should work.
391 public int getScrollableUnitIncrement(Rectangle visibleRect,
395 if (orientation == SwingConstants.VERTICAL &&
396 direction < 0 && (row = getFirstVisibleIndex()) != -1) {
397 Rectangle r = getCellBounds(row, row);
398 if ((r.y == visibleRect.y) && (row != 0)) {
399 Point loc = r.getLocation();
401 int prevIndex = locationToIndex(loc);
402 Rectangle prevR = getCellBounds(prevIndex, prevIndex);
404 if (prevR == null || prevR.y >= r.y) {
410 return super.getScrollableUnitIncrement(
411 visibleRect, orientation, direction);
415 list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
416 list.setLayoutOrientation(JList.HORIZONTAL_WRAP);
417 list.setVisibleRowCount(-1);
419 list.addMouseListener(new MouseAdapter() {
420 public void mouseClicked(MouseEvent e) {
421 if (e.getClickCount() == 2) {
422 monitor_button.doClick(); //emulate button click
426 JScrollPane listScroller = new JScrollPane(list);
427 listScroller.setPreferredSize(new Dimension(400, 80));
428 listScroller.setAlignmentX(LEFT_ALIGNMENT);
430 //Create a container so that we can add a title around
431 //the scroll pane. Can't add a title directly to the
432 //scroll pane because its background would be white.
433 //Lay out the label and scroll pane from top to bottom.
434 JPanel listPane = new JPanel();
435 listPane.setLayout(new BoxLayout(listPane, BoxLayout.PAGE_AXIS));
437 JLabel label = new JLabel("Select Device");
438 label.setLabelFor(list);
440 listPane.add(Box.createRigidArea(new Dimension(0,5)));
441 listPane.add(listScroller);
442 listPane.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));
444 c.fill = GridBagConstraints.BOTH;
445 c.anchor = GridBagConstraints.CENTER;
453 c.anchor = GridBagConstraints.CENTER;
455 pane.add(listPane, c);
457 cancel_button = new JButton("Cancel");
458 cancel_button.addActionListener(this);
459 cancel_button.setActionCommand("cancel");
461 c.fill = GridBagConstraints.NONE;
462 c.anchor = GridBagConstraints.CENTER;
470 c.anchor = GridBagConstraints.CENTER;
472 pane.add(cancel_button, c);
474 monitor_button = new JButton("Monitor");
475 monitor_button.addActionListener(this);
476 monitor_button.setActionCommand("monitor");
478 c.fill = GridBagConstraints.NONE;
479 c.anchor = GridBagConstraints.CENTER;
487 c.anchor = GridBagConstraints.CENTER;
489 pane.add(monitor_button, c);
492 setLocationRelativeTo(owner);
494 addWindowListener(new ConfigListener(this));