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 (channel %-2d %s)",
43 callsign, serial, flight, channel, Altos.telemetry_name(telemetry));
46 public String toShortString() {
47 return String.format("%s %d %d %d %d",
48 callsign, serial, flight, channel, telemetry);
51 public AltosScanResult(String in_callsign, int in_serial,
52 int in_flight, int in_channel, int in_telemetry) {
53 callsign = in_callsign;
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 channel == other.channel &&
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 AltosTelemetryReader reader;
112 private JLabel scanning_label;
113 private JButton cancel_button;
114 private JButton monitor_button;
115 javax.swing.Timer timer;
116 AltosScanResults results = new AltosScanResults();
121 final static int timeout = 1200;
122 TelemetryHandler handler;
125 void scan_exception(Exception e) {
126 if (e instanceof FileNotFoundException) {
127 JOptionPane.showMessageDialog(owner,
128 String.format("Cannot open device \"%s\"",
129 device.toShortString()),
130 "Cannot open target device",
131 JOptionPane.ERROR_MESSAGE);
132 } else if (e instanceof AltosSerialInUseException) {
133 JOptionPane.showMessageDialog(owner,
134 String.format("Device \"%s\" already in use",
135 device.toShortString()),
137 JOptionPane.ERROR_MESSAGE);
138 } else if (e instanceof IOException) {
139 IOException ee = (IOException) e;
140 JOptionPane.showMessageDialog(owner,
141 device.toShortString(),
142 ee.getLocalizedMessage(),
143 JOptionPane.ERROR_MESSAGE);
145 JOptionPane.showMessageDialog(owner,
146 String.format("Connection to \"%s\" failed",
147 device.toShortString()),
149 JOptionPane.ERROR_MESSAGE);
154 class TelemetryHandler implements Runnable {
158 boolean interrupted = false;
163 AltosRecord record = reader.read();
166 if ((record.seen & AltosRecord.seen_flight) != 0) {
167 final AltosScanResult result = new AltosScanResult(record.callsign,
172 Runnable r = new Runnable() {
177 SwingUtilities.invokeLater(r);
179 } catch (ParseException pp) {
180 } catch (AltosCRCException ce) {
183 } catch (InterruptedException ee) {
185 } catch (IOException ie) {
187 reader.close(interrupted);
193 scanning_label.setText(String.format("Scanning: channel %d %s",
195 Altos.telemetry_name(telemetry)));
199 reader.serial.set_monitor(false);
202 } catch (InterruptedException ie){
208 if (telemetry > Altos.ao_telemetry_max)
209 telemetry = Altos.ao_telemetry_min;
210 reader.serial.set_telemetry(telemetry);
212 reader.serial.set_channel(channel);
214 reader.serial.set_monitor(true);
219 if (thread != null && thread.isAlive()) {
223 } catch (InterruptedException ie) {}
236 public void actionPerformed(ActionEvent e) {
237 String cmd = e.getActionCommand();
239 if (cmd.equals("cancel"))
242 if (cmd.equals("tick"))
245 if (cmd.equals("monitor")) {
247 AltosScanResult r = (AltosScanResult) (list.getSelectedValue());
249 if (device != null) {
250 if (reader != null) {
251 reader.set_telemetry(r.telemetry);
252 reader.set_channel(r.channel);
253 owner.telemetry_window(device);
260 /* A window listener to catch closing events and tell the config code */
261 class ConfigListener extends WindowAdapter {
264 public ConfigListener(AltosScanUI this_ui) {
268 public void windowClosing(WindowEvent e) {
269 ui.actionPerformed(new ActionEvent(e.getSource(),
270 ActionEvent.ACTION_PERFORMED,
275 private boolean open() {
276 device = AltosDeviceDialog.show(owner, Altos.product_basestation);
280 reader = new AltosTelemetryReader(device);
281 reader.serial.set_channel(channel);
282 reader.serial.set_telemetry(telemetry);
285 } catch (InterruptedException ie) {
288 handler = new TelemetryHandler();
289 thread = new Thread(handler);
292 } catch (FileNotFoundException ee) {
293 JOptionPane.showMessageDialog(owner,
294 String.format("Cannot open device \"%s\"",
295 device.toShortString()),
296 "Cannot open target device",
297 JOptionPane.ERROR_MESSAGE);
298 } catch (AltosSerialInUseException si) {
299 JOptionPane.showMessageDialog(owner,
300 String.format("Device \"%s\" already in use",
301 device.toShortString()),
303 JOptionPane.ERROR_MESSAGE);
304 } catch (IOException ee) {
305 JOptionPane.showMessageDialog(owner,
306 device.toShortString(),
308 JOptionPane.ERROR_MESSAGE);
315 public AltosScanUI(AltosUI in_owner) {
320 telemetry = Altos.ao_telemetry_min;
325 Container pane = getContentPane();
326 GridBagConstraints c = new GridBagConstraints();
327 Insets i = new Insets(4,4,4,4);
329 timer = new javax.swing.Timer(timeout, this);
330 timer.setActionCommand("tick");
335 pane.setLayout(new GridBagLayout());
337 scanning_label = new JLabel("Scanning:");
341 c.fill = GridBagConstraints.NONE;
342 c.anchor = GridBagConstraints.CENTER;
350 c.anchor = GridBagConstraints.CENTER;
352 pane.add(scanning_label, c);
354 list = new JList(results) {
355 //Subclass JList to workaround bug 4832765, which can cause the
356 //scroll pane to not let the user easily scroll up to the beginning
357 //of the list. An alternative would be to set the unitIncrement
358 //of the JScrollBar to a fixed value. You wouldn't get the nice
359 //aligned scrolling, but it should work.
360 public int getScrollableUnitIncrement(Rectangle visibleRect,
364 if (orientation == SwingConstants.VERTICAL &&
365 direction < 0 && (row = getFirstVisibleIndex()) != -1) {
366 Rectangle r = getCellBounds(row, row);
367 if ((r.y == visibleRect.y) && (row != 0)) {
368 Point loc = r.getLocation();
370 int prevIndex = locationToIndex(loc);
371 Rectangle prevR = getCellBounds(prevIndex, prevIndex);
373 if (prevR == null || prevR.y >= r.y) {
379 return super.getScrollableUnitIncrement(
380 visibleRect, orientation, direction);
384 list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
385 list.setLayoutOrientation(JList.HORIZONTAL_WRAP);
386 list.setVisibleRowCount(-1);
388 list.addMouseListener(new MouseAdapter() {
389 public void mouseClicked(MouseEvent e) {
390 if (e.getClickCount() == 2) {
391 monitor_button.doClick(); //emulate button click
395 JScrollPane listScroller = new JScrollPane(list);
396 listScroller.setPreferredSize(new Dimension(400, 80));
397 listScroller.setAlignmentX(LEFT_ALIGNMENT);
399 //Create a container so that we can add a title around
400 //the scroll pane. Can't add a title directly to the
401 //scroll pane because its background would be white.
402 //Lay out the label and scroll pane from top to bottom.
403 JPanel listPane = new JPanel();
404 listPane.setLayout(new BoxLayout(listPane, BoxLayout.PAGE_AXIS));
406 JLabel label = new JLabel("Select Device");
407 label.setLabelFor(list);
409 listPane.add(Box.createRigidArea(new Dimension(0,5)));
410 listPane.add(listScroller);
411 listPane.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));
413 c.fill = GridBagConstraints.BOTH;
414 c.anchor = GridBagConstraints.CENTER;
422 c.anchor = GridBagConstraints.CENTER;
424 pane.add(listPane, c);
426 cancel_button = new JButton("Cancel");
427 cancel_button.addActionListener(this);
428 cancel_button.setActionCommand("cancel");
430 c.fill = GridBagConstraints.NONE;
431 c.anchor = GridBagConstraints.CENTER;
439 c.anchor = GridBagConstraints.CENTER;
441 pane.add(cancel_button, c);
443 monitor_button = new JButton("Monitor");
444 monitor_button.addActionListener(this);
445 monitor_button.setActionCommand("monitor");
447 c.fill = GridBagConstraints.NONE;
448 c.anchor = GridBagConstraints.CENTER;
456 c.anchor = GridBagConstraints.CENTER;
458 pane.add(monitor_button, c);
461 setLocationRelativeTo(owner);
463 addWindowListener(new ConfigListener(this));