altosui/altosuilib: Cleanup -Xlint:unchecked warnings
[fw/altos] / altosui / AltosScanUI.java
index b47405c8bf5f4fb61eed5207013d2cb74d259513..5cc74a7796b6a91cad6068b1d6ff3f0f3938b05d 100644 (file)
@@ -20,14 +20,13 @@ package altosui;
 import java.awt.*;
 import java.awt.event.*;
 import javax.swing.*;
-import javax.swing.filechooser.FileNameExtensionFilter;
-import javax.swing.table.*;
 import javax.swing.event.*;
 import java.io.*;
 import java.util.*;
 import java.text.*;
-import java.util.prefs.*;
 import java.util.concurrent.*;
+import org.altusmetrum.altoslib_3.*;
+import org.altusmetrum.altosuilib_1.*;
 
 class AltosScanResult {
        String          callsign;
@@ -35,9 +34,9 @@ class AltosScanResult {
        int             flight;
        AltosFrequency  frequency;
        int             telemetry;
-       
+
        boolean interrupted = false;
-       
+
        public String toString() {
                return String.format("%-9.9s serial %-4d flight %-4d (%s %s)",
                                     callsign, serial, flight, frequency.toShortString(), Altos.telemetry_name(telemetry));
@@ -58,36 +57,57 @@ class AltosScanResult {
        }
 
        public boolean equals(AltosScanResult other) {
-               return (callsign.equals(other.callsign) &&
-                       serial == other.serial &&
-                       flight == other.flight &&
+               return (serial == other.serial &&
                        frequency.frequency == other.frequency.frequency &&
                        telemetry == other.telemetry);
        }
+
+       public boolean up_to_date(AltosScanResult other) {
+               if (flight == 0 && other.flight != 0) {
+                       flight = other.flight;
+                       return false;
+               }
+               if (callsign.equals("N0CALL") && !other.callsign.equals("N0CALL")) {
+                       callsign = other.callsign;
+                       return false;
+               }
+               return true;
+       }
 }
 
-class AltosScanResults extends LinkedList<AltosScanResult> implements ListModel {
-       
+class AltosScanResults extends LinkedList<AltosScanResult> implements ListModel<AltosScanResult> {
+
        LinkedList<ListDataListener>    listeners = new LinkedList<ListDataListener>();
 
+       void changed(ListDataEvent de) {
+               for (ListDataListener l : listeners)
+                       l.contentsChanged(de);
+       }
+
        public boolean add(AltosScanResult r) {
-               for (AltosScanResult old : this)
-                       if (old.equals(r))
+               int i = 0;
+               for (AltosScanResult old : this) {
+                       if (old.equals(r)) {
+                               if (!old.up_to_date(r))
+                                       changed (new ListDataEvent(this,
+                                                                  ListDataEvent.CONTENTS_CHANGED,
+                                                                  i, i));
                                return true;
+                       }
+                       i++;
+               }
 
                super.add(r);
-               ListDataEvent   de = new ListDataEvent(this,
-                                                      ListDataEvent.INTERVAL_ADDED,
-                                                      this.size() - 2, this.size() - 1);
-               for (ListDataListener l : listeners)
-                       l.contentsChanged(de);
+               changed(new ListDataEvent(this,
+                                         ListDataEvent.INTERVAL_ADDED,
+                                         this.size() - 2, this.size() - 1));
                return true;
        }
 
        public void addListDataListener(ListDataListener l) {
                listeners.add(l);
        }
-       
+
        public void removeListDataListener(ListDataListener l) {
                listeners.remove(l);
        }
@@ -102,19 +122,20 @@ class AltosScanResults extends LinkedList<AltosScanResult> implements ListModel
 }
 
 public class AltosScanUI
-       extends JDialog
+       extends AltosUIDialog
        implements ActionListener
 {
        AltosUI                         owner;
        AltosDevice                     device;
        AltosConfigData                 config_data;
        AltosTelemetryReader            reader;
-       private JList                   list;
+       private JList<AltosScanResult>  list;
        private JLabel                  scanning_label;
        private JLabel                  frequency_label;
        private JLabel                  telemetry_label;
        private JButton                 cancel_button;
        private JButton                 monitor_button;
+       private JCheckBox[]             telemetry_boxes;
        javax.swing.Timer               timer;
        AltosScanResults                results = new AltosScanResults();
 
@@ -129,8 +150,7 @@ public class AltosScanUI
        void scan_exception(Exception e) {
                if (e instanceof FileNotFoundException) {
                        JOptionPane.showMessageDialog(owner,
-                                                     String.format("Cannot open device \"%s\"",
-                                                                   device.toShortString()),
+                                                     ((FileNotFoundException) e).getMessage(),
                                                      "Cannot open target device",
                                                      JOptionPane.ERROR_MESSAGE);
                } else if (e instanceof AltosSerialInUseException) {
@@ -164,13 +184,13 @@ public class AltosScanUI
                        try {
                                for (;;) {
                                        try {
-                                               AltosRecord     record = reader.read();
-                                               if (record == null)
+                                               AltosState      state = reader.read();
+                                               if (state == null)
                                                        continue;
-                                               if ((record.seen & AltosRecord.seen_flight) != 0) {
-                                                       final AltosScanResult   result = new AltosScanResult(record.callsign,
-                                                                                                    record.serial,
-                                                                                                    record.flight,
+                                               if (state.flight != AltosLib.MISSING) {
+                                                       final AltosScanResult   result = new AltosScanResult(state.callsign,
+                                                                                                    state.serial,
+                                                                                                    state.flight,
                                                                                                     frequencies[frequency_index],
                                                                                                     telemetry);
                                                        Runnable r = new Runnable() {
@@ -201,25 +221,30 @@ public class AltosScanUI
        void set_telemetry() {
                reader.set_telemetry(telemetry);
        }
-       
+
        void set_frequency() throws InterruptedException, TimeoutException {
                reader.set_frequency(frequencies[frequency_index].frequency);
+               reader.reset();
        }
-       
+
        void next() throws InterruptedException, TimeoutException {
-               reader.serial.set_monitor(false);
+               reader.set_monitor(false);
                Thread.sleep(100);
                ++frequency_index;
-               if (frequency_index >= frequencies.length) {
+               if (frequency_index >= frequencies.length ||
+                   !telemetry_boxes[telemetry - Altos.ao_telemetry_min].isSelected())
+               {
                        frequency_index = 0;
-                       ++telemetry;
-                       if (telemetry > Altos.ao_telemetry_max)
-                               telemetry = Altos.ao_telemetry_min;
+                       do {
+                               ++telemetry;
+                               if (telemetry > Altos.ao_telemetry_max)
+                                       telemetry = Altos.ao_telemetry_min;
+                       } while (!telemetry_boxes[telemetry - Altos.ao_telemetry_min].isSelected());
                        set_telemetry();
                }
                set_frequency();
                set_label();
-               reader.serial.set_monitor(true);
+               reader.set_monitor(true);
        }
 
 
@@ -251,6 +276,21 @@ public class AltosScanUI
                        if (cmd.equals("tick"))
                                tick_timer();
 
+                       if (cmd.equals("telemetry")) {
+                               int k;
+                               int scanning_telemetry = 0;
+                               for (k = Altos.ao_telemetry_min; k <= Altos.ao_telemetry_max; k++) {
+                                       int j = k - Altos.ao_telemetry_min;
+                                       if (telemetry_boxes[j].isSelected())
+                                               scanning_telemetry |= (1 << k);
+                               }
+                               if (scanning_telemetry == 0) {
+                                       scanning_telemetry |= (1 << Altos.ao_telemetry_standard);
+                                       telemetry_boxes[Altos.ao_telemetry_standard - Altos.ao_telemetry_min].setSelected(true);
+                               }
+                               AltosUIPreferences.set_scanning_telemetry(scanning_telemetry);
+                       }
+
                        if (cmd.equals("monitor")) {
                                close();
                                AltosScanResult r = (AltosScanResult) (list.getSelectedValue());
@@ -288,11 +328,11 @@ public class AltosScanUI
        }
 
        private boolean open() {
-               device = AltosDeviceDialog.show(owner, Altos.product_basestation);
+               device = AltosDeviceUIDialog.show(owner, Altos.product_basestation);
                if (device == null)
                        return false;
                try {
-                       reader = new AltosTelemetryReader(device);
+                       reader = new AltosTelemetryReader(new AltosSerial(device));
                        set_frequency();
                        set_telemetry();
                        try {
@@ -306,8 +346,7 @@ public class AltosScanUI
                        return true;
                } catch (FileNotFoundException ee) {
                        JOptionPane.showMessageDialog(owner,
-                                                     String.format("Cannot open device \"%s\"",
-                                                                   device.toShortString()),
+                                                     ee.getMessage(),
                                                      "Cannot open target device",
                                                      JOptionPane.ERROR_MESSAGE);
                } catch (AltosSerialInUseException si) {
@@ -341,7 +380,7 @@ public class AltosScanUI
 
                owner = in_owner;
 
-               frequencies = AltosPreferences.common_frequencies();
+               frequencies = AltosUIPreferences.common_frequencies();
                frequency_index = 0;
                telemetry = Altos.ao_telemetry_min;
 
@@ -363,10 +402,10 @@ public class AltosScanUI
                scanning_label = new JLabel("Scanning:");
                frequency_label = new JLabel("");
                telemetry_label = new JLabel("");
-               
+
                set_label();
 
-               c.fill = GridBagConstraints.NONE;
+               c.fill = GridBagConstraints.HORIZONTAL;
                c.anchor = GridBagConstraints.WEST;
                c.insets = i;
                c.weightx = 1;
@@ -382,7 +421,21 @@ public class AltosScanUI
                c.gridy = 2;
                pane.add(telemetry_label, c);
 
-               list = new JList(results) {
+               int     scanning_telemetry = AltosUIPreferences.scanning_telemetry();
+               telemetry_boxes = new JCheckBox[Altos.ao_telemetry_max - Altos.ao_telemetry_min + 1];
+               for (int k = Altos.ao_telemetry_min; k <= Altos.ao_telemetry_max; k++) {
+                       int j = k - Altos.ao_telemetry_min;
+                       telemetry_boxes[j] = new JCheckBox(AltosLib.telemetry_name(k));
+                       c.gridy = 3 + j;
+                       pane.add(telemetry_boxes[j], c);
+                       telemetry_boxes[j].setActionCommand("telemetry");
+                       telemetry_boxes[j].addActionListener(this);
+                       telemetry_boxes[j].setSelected((scanning_telemetry & (1 << k)) != 0);
+               }
+
+               int     y_offset = 3 + (Altos.ao_telemetry_max - Altos.ao_telemetry_min + 1);
+
+               list = new JList<AltosScanResult>(results) {
                                //Subclass JList to workaround bug 4832765, which can cause the
                                //scroll pane to not let the user easily scroll up to the beginning
                                //of the list.  An alternative would be to set the unitIncrement
@@ -448,7 +501,7 @@ public class AltosScanUI
                c.weighty = 1;
 
                c.gridx = 0;
-               c.gridy = 3;
+               c.gridy = y_offset;
                c.gridwidth = 2;
                c.anchor = GridBagConstraints.CENTER;
 
@@ -465,7 +518,7 @@ public class AltosScanUI
                c.weighty = 1;
 
                c.gridx = 0;
-               c.gridy = 4;
+               c.gridy = y_offset + 1;
                c.gridwidth = 1;
                c.anchor = GridBagConstraints.CENTER;
 
@@ -482,7 +535,7 @@ public class AltosScanUI
                c.weighty = 1;
 
                c.gridx = 1;
-               c.gridy = 4;
+               c.gridy = y_offset + 1;
                c.gridwidth = 1;
                c.anchor = GridBagConstraints.CENTER;
 
@@ -495,4 +548,4 @@ public class AltosScanUI
 
                setVisible(true);
        }
-}
\ No newline at end of file
+}