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 %4d %4d %2d %2d",
43 callsign, serial, flight, channel, 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 channel_label;
113 private JLabel monitor_label;
114 private JButton fake_button;
115 private JButton cancel_button;
116 private JButton ok_button;
117 javax.swing.Timer timer;
118 AltosScanResults results = new AltosScanResults();
120 static final int[] monitors = { Altos.ao_telemetry_split_len,
121 Altos.ao_telemetry_legacy_len };
125 final static int timeout = 5 * 1000;
126 TelemetryHandler handler;
129 void scan_exception(Exception e) {
130 if (e instanceof FileNotFoundException) {
131 JOptionPane.showMessageDialog(owner,
132 String.format("Cannot open device \"%s\"",
133 device.toShortString()),
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 AltosScanResult result = new AltosScanResult(record.callsign,
178 } catch (ParseException pp) {
179 System.out.printf("Parse error: %d \"%s\"\n", pp.getErrorOffset(), pp.getMessage());
180 } catch (AltosCRCException ce) {
183 } catch (InterruptedException ee) {
185 } catch (IOException ie) {
187 reader.close(interrupted);
193 reader.serial.set_channel(channel);
197 reader.serial.set_telemetry(monitors[monitor]);
205 if (monitor == monitors.length)
214 if (thread != null && thread.isAlive()) {
218 } catch (InterruptedException ie) {}
231 public void actionPerformed(ActionEvent e) {
232 String cmd = e.getActionCommand();
234 if (cmd.equals("fake")) {
235 results.add(new AltosScanResult("N0CALL", 300, 1, 0, 1));
238 if (cmd.equals("cancel")) {
242 if (cmd.equals("ok")) {
244 AltosScanResult r = (AltosScanResult) (list.getSelectedValue());
245 System.out.printf("Selected channel %d telemetry %d\n",
246 r.channel, r.telemetry);
247 if (device != null) {
248 if (reader != null) {
249 reader.set_telemetry(r.telemetry);
250 reader.set_channel(r.channel);
251 owner.telemetry_window(device);
257 /* A window listener to catch closing events and tell the config code */
258 class ConfigListener extends WindowAdapter {
261 public ConfigListener(AltosScanUI this_ui) {
265 public void windowClosing(WindowEvent e) {
266 ui.actionPerformed(new ActionEvent(e.getSource(),
267 ActionEvent.ACTION_PERFORMED,
272 private boolean open() {
273 device = AltosDeviceDialog.show(owner, Altos.product_any);
274 if (device != null) {
276 reader = new AltosTelemetryReader(device);
279 handler = new TelemetryHandler();
280 thread = new Thread(handler);
283 } catch (Exception e) {
290 public AltosScanUI(AltosUI in_owner) {
297 Container pane = getContentPane();
298 GridBagConstraints c = new GridBagConstraints();
299 Insets i = new Insets(4,4,4,4);
301 timer = new javax.swing.Timer(timeout, this);
302 timer.setActionCommand("tick");
307 pane.setLayout(new GridBagLayout());
309 list = new JList(results) {
310 //Subclass JList to workaround bug 4832765, which can cause the
311 //scroll pane to not let the user easily scroll up to the beginning
312 //of the list. An alternative would be to set the unitIncrement
313 //of the JScrollBar to a fixed value. You wouldn't get the nice
314 //aligned scrolling, but it should work.
315 public int getScrollableUnitIncrement(Rectangle visibleRect,
319 if (orientation == SwingConstants.VERTICAL &&
320 direction < 0 && (row = getFirstVisibleIndex()) != -1) {
321 Rectangle r = getCellBounds(row, row);
322 if ((r.y == visibleRect.y) && (row != 0)) {
323 Point loc = r.getLocation();
325 int prevIndex = locationToIndex(loc);
326 Rectangle prevR = getCellBounds(prevIndex, prevIndex);
328 if (prevR == null || prevR.y >= r.y) {
334 return super.getScrollableUnitIncrement(
335 visibleRect, orientation, direction);
339 list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
340 list.setLayoutOrientation(JList.HORIZONTAL_WRAP);
341 list.setVisibleRowCount(-1);
343 list.addMouseListener(new MouseAdapter() {
344 public void mouseClicked(MouseEvent e) {
345 if (e.getClickCount() == 2) {
346 ok_button.doClick(); //emulate button click
350 JScrollPane listScroller = new JScrollPane(list);
351 listScroller.setPreferredSize(new Dimension(400, 80));
352 listScroller.setAlignmentX(LEFT_ALIGNMENT);
354 //Create a container so that we can add a title around
355 //the scroll pane. Can't add a title directly to the
356 //scroll pane because its background would be white.
357 //Lay out the label and scroll pane from top to bottom.
358 JPanel listPane = new JPanel();
359 listPane.setLayout(new BoxLayout(listPane, BoxLayout.PAGE_AXIS));
361 JLabel label = new JLabel("Select Device");
362 label.setLabelFor(list);
364 listPane.add(Box.createRigidArea(new Dimension(0,5)));
365 listPane.add(listScroller);
366 listPane.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));
368 c.fill = GridBagConstraints.NONE;
369 c.anchor = GridBagConstraints.CENTER;
377 c.anchor = GridBagConstraints.CENTER;
379 pane.add(listPane, c);
381 fake_button = new JButton("fake");
382 fake_button.addActionListener(this);
383 fake_button.setActionCommand("fake");
385 c.fill = GridBagConstraints.NONE;
386 c.anchor = GridBagConstraints.CENTER;
394 c.anchor = GridBagConstraints.CENTER;
396 pane.add(fake_button, c);
398 cancel_button = new JButton("Cancel");
399 cancel_button.addActionListener(this);
400 cancel_button.setActionCommand("cancel");
402 c.fill = GridBagConstraints.NONE;
403 c.anchor = GridBagConstraints.CENTER;
411 c.anchor = GridBagConstraints.CENTER;
413 pane.add(cancel_button, c);
415 ok_button = new JButton("OK");
416 ok_button.addActionListener(this);
417 ok_button.setActionCommand("ok");
419 c.fill = GridBagConstraints.NONE;
420 c.anchor = GridBagConstraints.CENTER;
428 c.anchor = GridBagConstraints.CENTER;
430 pane.add(ok_button, c);
433 setLocationRelativeTo(owner);
435 addWindowListener(new ConfigListener(this));