further refinment of Releasing document
[fw/altos] / altosui / AltosScanUI.java
1 /*
2  * Copyright © 2011 Keith Packard <keithp@keithp.com>
3  *
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.
7  *
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.
12  *
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.
16  */
17
18 package altosui;
19
20 import java.awt.*;
21 import java.awt.event.*;
22 import javax.swing.*;
23 import javax.swing.filechooser.FileNameExtensionFilter;
24 import javax.swing.table.*;
25 import javax.swing.event.*;
26 import java.io.*;
27 import java.util.*;
28 import java.text.*;
29 import java.util.prefs.*;
30 import java.util.concurrent.*;
31 import org.altusmetrum.AltosLib.*;
32
33 class AltosScanResult {
34         String          callsign;
35         int             serial;
36         int             flight;
37         AltosFrequency  frequency;
38         int             telemetry;
39         
40         boolean interrupted = false;
41         
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));
45         }
46
47         public String toShortString() {
48                 return String.format("%s %d %d %7.3f %d",
49                                      callsign, serial, flight, frequency, telemetry);
50         }
51
52         public AltosScanResult(String in_callsign, int in_serial,
53                                int in_flight, AltosFrequency in_frequency, int in_telemetry) {
54                 callsign = in_callsign;
55                 serial = in_serial;
56                 flight = in_flight;
57                 frequency = in_frequency;
58                 telemetry = in_telemetry;
59         }
60
61         public boolean equals(AltosScanResult other) {
62                 return (serial == other.serial &&
63                         frequency.frequency == other.frequency.frequency &&
64                         telemetry == other.telemetry);
65         }
66
67         public boolean up_to_date(AltosScanResult other) {
68                 if (flight == 0 && other.flight != 0) {
69                         flight = other.flight;
70                         return false;
71                 }
72                 if (callsign.equals("N0CALL") && !other.callsign.equals("N0CALL")) {
73                         callsign = other.callsign;
74                         return false;
75                 }
76                 return true;
77         }
78 }
79
80 class AltosScanResults extends LinkedList<AltosScanResult> implements ListModel {
81         
82         LinkedList<ListDataListener>    listeners = new LinkedList<ListDataListener>();
83
84         void changed(ListDataEvent de) {
85                 for (ListDataListener l : listeners)
86                         l.contentsChanged(de);
87         }
88
89         public boolean add(AltosScanResult r) {
90                 int i = 0;
91                 for (AltosScanResult old : this) {
92                         if (old.equals(r)) {
93                                 if (!old.up_to_date(r))
94                                         changed (new ListDataEvent(this,
95                                                                    ListDataEvent.CONTENTS_CHANGED,
96                                                                    i, i));
97                                 return true;
98                         }
99                         i++;
100                 }
101
102                 super.add(r);
103                 changed(new ListDataEvent(this,
104                                           ListDataEvent.INTERVAL_ADDED,
105                                           this.size() - 2, this.size() - 1));
106                 return true;
107         }
108
109         public void addListDataListener(ListDataListener l) {
110                 listeners.add(l);
111         }
112         
113         public void removeListDataListener(ListDataListener l) {
114                 listeners.remove(l);
115         }
116
117         public AltosScanResult getElementAt(int i) {
118                 return this.get(i);
119         }
120
121         public int getSize() {
122                 return this.size();
123         }
124 }
125
126 public class AltosScanUI
127         extends AltosDialog
128         implements ActionListener
129 {
130         AltosUI                         owner;
131         AltosDevice                     device;
132         AltosConfigData                 config_data;
133         AltosTelemetryReader            reader;
134         private JList                   list;
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();
143
144         int                             telemetry;
145
146         final static int                timeout = 1200;
147         TelemetryHandler                handler;
148         Thread                          thread;
149         AltosFrequency[]                frequencies;
150         int                             frequency_index;
151
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()),
162                                                       "Device in use",
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);
170                 } else {
171                         JOptionPane.showMessageDialog(owner,
172                                                       String.format("Connection to \"%s\" failed",
173                                                                     device.toShortString()),
174                                                       "Connection Failed",
175                                                       JOptionPane.ERROR_MESSAGE);
176                 }
177                 close();
178         }
179
180         class TelemetryHandler implements Runnable {
181
182                 public void run() {
183
184                         boolean interrupted = false;
185
186                         try {
187                                 for (;;) {
188                                         try {
189                                                 AltosRecord     record = reader.read();
190                                                 if (record == null)
191                                                         continue;
192                                                 if ((record.seen & AltosRecord.seen_flight) != 0) {
193                                                         final AltosScanResult   result = new AltosScanResult(record.callsign,
194                                                                                                      record.serial,
195                                                                                                      record.flight,
196                                                                                                      frequencies[frequency_index],
197                                                                                                      telemetry);
198                                                         Runnable r = new Runnable() {
199                                                                         public void run() {
200                                                                                 results.add(result);
201                                                                         }
202                                                                 };
203                                                         SwingUtilities.invokeLater(r);
204                                                 }
205                                         } catch (ParseException pp) {
206                                         } catch (AltosCRCException ce) {
207                                         }
208                                 }
209                         } catch (InterruptedException ee) {
210                                 interrupted = true;
211                         } catch (IOException ie) {
212                         } finally {
213                                 reader.close(interrupted);
214                         }
215                 }
216         }
217
218         void set_label() {
219                 frequency_label.setText(String.format("Frequency: %s", frequencies[frequency_index].toString()));
220                 telemetry_label.setText(String.format("Telemetry: %s", Altos.telemetry_name(telemetry)));
221         }
222
223         void set_telemetry() {
224                 reader.set_telemetry(telemetry);
225         }
226         
227         void set_frequency() throws InterruptedException, TimeoutException {
228                 reader.set_frequency(frequencies[frequency_index].frequency);
229                 reader.reset();
230         }
231         
232         void next() throws InterruptedException, TimeoutException {
233                 reader.set_monitor(false);
234                 Thread.sleep(100);
235                 ++frequency_index;
236                 if (frequency_index >= frequencies.length ||
237                     !telemetry_boxes[telemetry - Altos.ao_telemetry_min].isSelected())
238                 {
239                         frequency_index = 0;
240                         do {
241                                 ++telemetry;
242                                 if (telemetry > Altos.ao_telemetry_max)
243                                         telemetry = Altos.ao_telemetry_min;
244                         } while (!telemetry_boxes[telemetry - Altos.ao_telemetry_min].isSelected());
245                         set_telemetry();
246                 }
247                 set_frequency();
248                 set_label();
249                 reader.set_monitor(true);
250         }
251
252
253         void close() {
254                 if (thread != null && thread.isAlive()) {
255                         thread.interrupt();
256                         try {
257                                 thread.join();
258                         } catch (InterruptedException ie) {}
259                 }
260                 thread = null;
261                 if (timer != null)
262                         timer.stop();
263                 setVisible(false);
264                 dispose();
265         }
266
267         void tick_timer() throws InterruptedException, TimeoutException {
268                 next();
269         }
270
271         public void actionPerformed(ActionEvent e) {
272                 String cmd = e.getActionCommand();
273
274                 try {
275                         if (cmd.equals("cancel"))
276                                 close();
277
278                         if (cmd.equals("tick"))
279                                 tick_timer();
280
281                         if (cmd.equals("telemetry")) {
282                                 int k;
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);
288                                 }
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);
292                                 }
293                                 AltosUIPreferences.set_scanning_telemetry(scanning_telemetry);
294                         }
295
296                         if (cmd.equals("monitor")) {
297                                 close();
298                                 AltosScanResult r = (AltosScanResult) (list.getSelectedValue());
299                                 if (r != null) {
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);
306                                                 }
307                                         }
308                                 }
309                         }
310                 } catch (TimeoutException te) {
311                         close();
312                 } catch (InterruptedException ie) {
313                         close();
314                 }
315         }
316
317         /* A window listener to catch closing events and tell the config code */
318         class ConfigListener extends WindowAdapter {
319                 AltosScanUI     ui;
320
321                 public ConfigListener(AltosScanUI this_ui) {
322                         ui = this_ui;
323                 }
324
325                 public void windowClosing(WindowEvent e) {
326                         ui.actionPerformed(new ActionEvent(e.getSource(),
327                                                            ActionEvent.ACTION_PERFORMED,
328                                                            "close"));
329                 }
330         }
331
332         private boolean open() {
333                 device = AltosDeviceDialog.show(owner, Altos.product_basestation);
334                 if (device == null)
335                         return false;
336                 try {
337                         reader = new AltosTelemetryReader(new AltosSerial(device));
338                         set_frequency();
339                         set_telemetry();
340                         try {
341                                 Thread.sleep(100);
342                         } catch (InterruptedException ie) {
343                         }
344                         reader.flush();
345                         handler = new TelemetryHandler();
346                         thread = new Thread(handler);
347                         thread.start();
348                         return true;
349                 } catch (FileNotFoundException ee) {
350                         JOptionPane.showMessageDialog(owner,
351                                                       ee.getMessage(),
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()),
358                                                       "Device in use",
359                                                       JOptionPane.ERROR_MESSAGE);
360                 } catch (IOException ee) {
361                         JOptionPane.showMessageDialog(owner,
362                                                       device.toShortString(),
363                                                       "Unkonwn I/O error",
364                                                       JOptionPane.ERROR_MESSAGE);
365                 } catch (TimeoutException te) {
366                         JOptionPane.showMessageDialog(owner,
367                                                       device.toShortString(),
368                                                       "Timeout error",
369                                                       JOptionPane.ERROR_MESSAGE);
370                 } catch (InterruptedException ie) {
371                         JOptionPane.showMessageDialog(owner,
372                                                       device.toShortString(),
373                                                       "Interrupted exception",
374                                                       JOptionPane.ERROR_MESSAGE);
375                 }
376                 if (reader != null)
377                         reader.close(false);
378                 return false;
379         }
380
381         public AltosScanUI(AltosUI in_owner) {
382
383                 owner = in_owner;
384
385                 frequencies = AltosUIPreferences.common_frequencies();
386                 frequency_index = 0;
387                 telemetry = Altos.ao_telemetry_min;
388
389                 if (!open())
390                         return;
391
392                 Container               pane = getContentPane();
393                 GridBagConstraints      c = new GridBagConstraints();
394                 Insets                  i = new Insets(4,4,4,4);
395
396                 timer = new javax.swing.Timer(timeout, this);
397                 timer.setActionCommand("tick");
398                 timer.restart();
399
400                 owner = in_owner;
401
402                 pane.setLayout(new GridBagLayout());
403
404                 scanning_label = new JLabel("Scanning:");
405                 frequency_label = new JLabel("");
406                 telemetry_label = new JLabel("");
407                 
408                 set_label();
409
410                 c.fill = GridBagConstraints.HORIZONTAL;
411                 c.anchor = GridBagConstraints.WEST;
412                 c.insets = i;
413                 c.weightx = 1;
414                 c.weighty = 1;
415
416                 c.gridx = 0;
417                 c.gridy = 0;
418                 c.gridwidth = 2;
419
420                 pane.add(scanning_label, c);
421                 c.gridy = 1;
422                 pane.add(frequency_label, c);
423                 c.gridy = 2;
424                 pane.add(telemetry_label, c);
425
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(AltosLib.telemetry_name(k));
431                         c.gridy = 3 + j;
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);
436                 }
437
438                 int     y_offset = 3 + (Altos.ao_telemetry_max - Altos.ao_telemetry_min + 1);
439                                 
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,
447                                                                       int orientation,
448                                                                       int direction) {
449                                         int row;
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();
455                                                         loc.y--;
456                                                         int prevIndex = locationToIndex(loc);
457                                                         Rectangle prevR = getCellBounds(prevIndex, prevIndex);
458
459                                                         if (prevR == null || prevR.y >= r.y) {
460                                                                 return 0;
461                                                         }
462                                                         return prevR.height;
463                                                 }
464                                         }
465                                         return super.getScrollableUnitIncrement(
466                                                 visibleRect, orientation, direction);
467                                 }
468                         };
469
470                 list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
471                 list.setLayoutOrientation(JList.HORIZONTAL_WRAP);
472                 list.setVisibleRowCount(-1);
473
474                 list.addMouseListener(new MouseAdapter() {
475                                  public void mouseClicked(MouseEvent e) {
476                                          if (e.getClickCount() == 2) {
477                                                  monitor_button.doClick(); //emulate button click
478                                          }
479                                  }
480                         });
481                 JScrollPane listScroller = new JScrollPane(list);
482                 listScroller.setPreferredSize(new Dimension(400, 80));
483                 listScroller.setAlignmentX(LEFT_ALIGNMENT);
484
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));
491
492                 JLabel label = new JLabel("Select Device");
493                 label.setLabelFor(list);
494                 listPane.add(label);
495                 listPane.add(Box.createRigidArea(new Dimension(0,5)));
496                 listPane.add(listScroller);
497                 listPane.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));
498
499                 c.fill = GridBagConstraints.BOTH;
500                 c.anchor = GridBagConstraints.CENTER;
501                 c.insets = i;
502                 c.weightx = 1;
503                 c.weighty = 1;
504
505                 c.gridx = 0;
506                 c.gridy = y_offset;
507                 c.gridwidth = 2;
508                 c.anchor = GridBagConstraints.CENTER;
509
510                 pane.add(listPane, c);
511
512                 cancel_button = new JButton("Cancel");
513                 cancel_button.addActionListener(this);
514                 cancel_button.setActionCommand("cancel");
515
516                 c.fill = GridBagConstraints.NONE;
517                 c.anchor = GridBagConstraints.CENTER;
518                 c.insets = i;
519                 c.weightx = 1;
520                 c.weighty = 1;
521
522                 c.gridx = 0;
523                 c.gridy = y_offset + 1;
524                 c.gridwidth = 1;
525                 c.anchor = GridBagConstraints.CENTER;
526
527                 pane.add(cancel_button, c);
528
529                 monitor_button = new JButton("Monitor");
530                 monitor_button.addActionListener(this);
531                 monitor_button.setActionCommand("monitor");
532
533                 c.fill = GridBagConstraints.NONE;
534                 c.anchor = GridBagConstraints.CENTER;
535                 c.insets = i;
536                 c.weightx = 1;
537                 c.weighty = 1;
538
539                 c.gridx = 1;
540                 c.gridy = y_offset + 1;
541                 c.gridwidth = 1;
542                 c.anchor = GridBagConstraints.CENTER;
543
544                 pane.add(monitor_button, c);
545
546                 pack();
547                 setLocationRelativeTo(owner);
548
549                 addWindowListener(new ConfigListener(this));
550
551                 setVisible(true);
552         }
553 }