fix bashism that prevents building with /bin/sh->/bin/dash
[fw/altos] / altosui / AltosDeviceDialog.java
index 2966ad1e52bd44a6855d07e47fe3c68bad6bd9b1..610bb73e76dfbae75fc6ab59c5b9f85755e7c477 100644 (file)
@@ -22,59 +22,66 @@ import java.util.*;
 import javax.swing.*;
 import java.awt.*;
 import java.awt.event.*;
-import libaltosJNI.libaltos;
-import libaltosJNI.altos_device;
-import libaltosJNI.SWIGTYPE_p_altos_file;
-import libaltosJNI.SWIGTYPE_p_altos_list;
+import libaltosJNI.*;
 
 public class AltosDeviceDialog extends JDialog implements ActionListener {
 
-       private static AltosDeviceDialog        dialog;
-       private static AltosDevice              value = null;
-       private JList                           list;
+       private AltosDevice     value;
+       private JList           list;
+       private JButton         cancel_button;
+       private JButton         select_button;
+       private JButton         manage_bluetooth_button;
+       private Frame           frame;
+       private int             product;
 
-       public static AltosDevice show (Component frameComp, int product) {
+       private AltosDevice getValue() {
+               return value;
+       }
 
-               Frame frame = JOptionPane.getFrameForComponent(frameComp);
-               AltosDevice[]   devices;
-               devices = AltosDevice.list(product);
-
-               if (devices != null && devices.length > 0) {
-                       value = null;
-                       dialog = new AltosDeviceDialog(frame, frameComp,
-                                                      devices,
-                                                      devices[0]);
-
-                       dialog.setVisible(true);
-                       return value;
-               } else {
-                       /* check for missing altos JNI library, which
-                        * will put up its own error dialog
-                        */
-                       if (AltosUI.load_library(frame)) {
-                               JOptionPane.showMessageDialog(frame,
-                                                             "No AltOS devices available",
-                                                             "No AltOS devices",
-                                                             JOptionPane.ERROR_MESSAGE);
-                       }
-                       return null;
-               }
+       private AltosDevice[] devices() {
+               java.util.List<AltosDevice>     usb_devices = AltosUSBDevice.list(product);
+               int                             num_devices = usb_devices.size();
+               java.util.List<AltosDevice>     bt_devices = Altos.bt_known.list(product);
+               num_devices += bt_devices.size();
+               AltosDevice[]                   devices = new AltosDevice[num_devices];
+
+               for (int i = 0; i < usb_devices.size(); i++)
+                       devices[i] = usb_devices.get(i);
+               int off = usb_devices.size();
+               for (int j = 0; j < bt_devices.size(); j++)
+                       devices[off + j] = bt_devices.get(j);
+               return devices;
        }
 
-       private AltosDeviceDialog (Frame frame, Component location,
-                                  AltosDevice[] devices,
-                                  AltosDevice initial) {
-               super(frame, "Device Selection", true);
+       private void update_devices() {
+               AltosDevice[] devices = devices();
+               list.setListData(devices);
+               select_button.setEnabled(devices.length > 0);
+       }
 
+       private AltosDeviceDialog (Frame in_frame, Component location, int in_product) {
+               super(in_frame, "Device Selection", true);
+
+               product = in_product;
+               frame = in_frame;
                value = null;
 
-               JButton cancelButton = new JButton("Cancel");
-               cancelButton.addActionListener(this);
+               AltosDevice[]   devices = devices();
+
+               cancel_button = new JButton("Cancel");
+               cancel_button.setActionCommand("cancel");
+               cancel_button.addActionListener(this);
+
+               manage_bluetooth_button = new JButton("Manage Bluetooth");
+               manage_bluetooth_button.setActionCommand("manage");
+               manage_bluetooth_button.addActionListener(this);
 
-               final JButton selectButton = new JButton("Select");
-               selectButton.setActionCommand("select");
-               selectButton.addActionListener(this);
-               getRootPane().setDefaultButton(selectButton);
+               select_button = new JButton("Select");
+               select_button.setActionCommand("select");
+               select_button.addActionListener(this);
+               if (devices.length == 0)
+                       select_button.setEnabled(false);
+               getRootPane().setDefaultButton(select_button);
 
                list = new JList(devices) {
                                //Subclass JList to workaround bug 4832765, which can cause the
@@ -112,7 +119,7 @@ public class AltosDeviceDialog extends JDialog implements ActionListener {
                list.addMouseListener(new MouseAdapter() {
                                 public void mouseClicked(MouseEvent e) {
                                         if (e.getClickCount() == 2) {
-                                                selectButton.doClick(); //emulate button click
+                                                select_button.doClick(); //emulate button click
                                         }
                                 }
                        });
@@ -139,9 +146,11 @@ public class AltosDeviceDialog extends JDialog implements ActionListener {
                buttonPane.setLayout(new BoxLayout(buttonPane, BoxLayout.LINE_AXIS));
                buttonPane.setBorder(BorderFactory.createEmptyBorder(0, 10, 10, 10));
                buttonPane.add(Box.createHorizontalGlue());
-               buttonPane.add(cancelButton);
+               buttonPane.add(cancel_button);
                buttonPane.add(Box.createRigidArea(new Dimension(10, 0)));
-               buttonPane.add(selectButton);
+               buttonPane.add(manage_bluetooth_button);
+               buttonPane.add(Box.createRigidArea(new Dimension(10, 0)));
+               buttonPane.add(select_button);
 
                //Put everything together, using the content pane's BorderLayout.
                Container contentPane = getContentPane();
@@ -149,7 +158,8 @@ public class AltosDeviceDialog extends JDialog implements ActionListener {
                contentPane.add(buttonPane, BorderLayout.PAGE_END);
 
                //Initialize values.
-               list.setSelectedValue(initial, true);
+               if (devices != null && devices.length != 0)
+                       list.setSelectedValue(devices[0], true);
                pack();
                setLocationRelativeTo(location);
        }
@@ -157,8 +167,22 @@ public class AltosDeviceDialog extends JDialog implements ActionListener {
        //Handle clicks on the Set and Cancel buttons.
        public void actionPerformed(ActionEvent e) {
                if ("select".equals(e.getActionCommand()))
-                       AltosDeviceDialog.value = (AltosDevice)(list.getSelectedValue());
-               AltosDeviceDialog.dialog.setVisible(false);
+                       value = (AltosDevice)(list.getSelectedValue());
+               if ("manage".equals(e.getActionCommand())) {
+                       AltosBTManage.show(frame, Altos.bt_known);
+                       update_devices();
+                       return;
+               }
+               setVisible(false);
        }
 
+       public static AltosDevice show (Component frameComp, int product) {
+
+               Frame                           frame = JOptionPane.getFrameForComponent(frameComp);
+               AltosDeviceDialog       dialog;
+
+               dialog = new AltosDeviceDialog(frame, frameComp, product);
+               dialog.setVisible(true);
+               return dialog.getValue();
+       }
 }