altosui: Make bluetooth dialog modal
authorKeith Packard <keithp@keithp.com>
Mon, 18 Apr 2011 23:16:38 +0000 (18:16 -0500)
committerKeith Packard <keithp@keithp.com>
Mon, 18 Apr 2011 23:16:38 +0000 (18:16 -0500)
This allows it to be displayed correctly while the device dialog box
(also modal) is up.

Signed-off-by: Keith Packard <keithp@keithp.com>
altosui/AltosBTDevice.java
altosui/AltosBTDeviceIterator.java
altosui/AltosBTManage.java
altosui/AltosDeviceDialog.java
altosui/AltosUI.java

index 5e946415235d7f24220ee637c24ed5762e3acb6d..ff2be49a66279009c6fc0f32ea69dce4d6ad89b7 100644 (file)
@@ -62,7 +62,7 @@ public class AltosBTDevice extends altos_bt_device implements AltosDevice {
                if (name == null)
                        name = "Altus Metrum";
                return String.format("%-20.20s %4d %s",
                if (name == null)
                        name = "Altus Metrum";
                return String.format("%-20.20s %4d %s",
-                                    getProduct(), getSerial(), getAddr());
+                                    getProductName(), getSerial(), getAddr());
        }
 
        public String toShortString() {
        }
 
        public String toShortString() {
index 935bf8226fbc50d8bdcf59722df449772c3800ec..63ce3674f8673f10a21136d9b5719dc4c693790e 100644 (file)
@@ -21,7 +21,7 @@ import java.util.*;
 import libaltosJNI.*;
 
 public class AltosBTDeviceIterator implements Iterator<AltosBTDevice> {
 import libaltosJNI.*;
 
 public class AltosBTDeviceIterator implements Iterator<AltosBTDevice> {
-       String          product;
+       int             product;
        AltosBTDevice   current;
        boolean         done;
        SWIGTYPE_p_altos_bt_list list;
        AltosBTDevice   current;
        boolean         done;
        SWIGTYPE_p_altos_bt_list list;
@@ -58,7 +58,7 @@ public class AltosBTDeviceIterator implements Iterator<AltosBTDevice> {
                throw new UnsupportedOperationException();
        }
 
                throw new UnsupportedOperationException();
        }
 
-       public AltosBTDeviceIterator(String in_product) {
+       public AltosBTDeviceIterator(int in_product) {
                product = in_product;
                done = false;
                current = null;
                product = in_product;
                done = false;
                current = null;
index 8e9e0f73f37d242a91f9575d3f1f1898527124b9..66e1210e9eb37aa627fa59bec85a6a68f4ecf1b1 100644 (file)
@@ -31,9 +31,8 @@ import java.util.concurrent.*;
 import libaltosJNI.*;
 
 public class AltosBTManage extends JDialog implements ActionListener {
 import libaltosJNI.*;
 
 public class AltosBTManage extends JDialog implements ActionListener {
-       String  product;
        LinkedBlockingQueue<AltosBTDevice> found_devices;
        LinkedBlockingQueue<AltosBTDevice> found_devices;
-       JFrame frame;
+       Frame frame;
 
        class DeviceList extends JList implements Iterable<AltosBTDevice> {
                LinkedList<AltosBTDevice> devices;
 
        class DeviceList extends JList implements Iterable<AltosBTDevice> {
                LinkedList<AltosBTDevice> devices;
@@ -104,7 +103,7 @@ public class AltosBTManage extends JDialog implements ActionListener {
                public void run () {
 
                        try {
                public void run () {
 
                        try {
-                               AltosBTDeviceIterator   i = new AltosBTDeviceIterator(product);
+                               AltosBTDeviceIterator   i = new AltosBTDeviceIterator(Altos.product_any);
                                AltosBTDevice           device;
 
                                while ((device = i.next()) != null) {
                                AltosBTDevice           device;
 
                                while ((device = i.next()) != null) {
@@ -124,9 +123,21 @@ public class AltosBTManage extends JDialog implements ActionListener {
                }
        }
 
                }
        }
 
-       public AltosBTManage(String product, JFrame in_frame) {
+       public static void show(Component frameComp) {
+               Frame   frame = JOptionPane.getFrameForComponent(frameComp);
+               AltosBTManage   dialog;
+
+               dialog = new AltosBTManage(frame);
+               dialog.setVisible(true);
+       }
+
+       public AltosBTManage(Frame in_frame) {
+               super(in_frame, "Manage Bluetooth Devices", true);
+
                frame = in_frame;
                frame = in_frame;
+
                BTGetVisibleDevices     get_visible_devices = new BTGetVisibleDevices();
                BTGetVisibleDevices     get_visible_devices = new BTGetVisibleDevices();
+
                Thread t = new Thread(get_visible_devices);
                t.start();
 
                Thread t = new Thread(get_visible_devices);
                t.start();
 
@@ -182,7 +193,5 @@ public class AltosBTManage extends JDialog implements ActionListener {
                //Initialize values.
 //             list.setSelectedValue(initial, true);
                pack();
                //Initialize values.
 //             list.setSelectedValue(initial, true);
                pack();
-               setLocationRelativeTo(frame);
-               setVisible(true);
        }
 }
        }
 }
index 154bf20b4743c30743cbc3b72a8e88501e398a6b..fa20f867524758cf51506067b536a4fb80dc2e54 100644 (file)
@@ -22,59 +22,55 @@ import java.util.*;
 import javax.swing.*;
 import java.awt.*;
 import java.awt.event.*;
 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 {
 
 
 public class AltosDeviceDialog extends JDialog implements ActionListener {
 
-       private static AltosDeviceDialog        dialog;
-       private static AltosDevice              value = null;
-       private JList                           list;
-
        public static AltosDevice show (Component frameComp, int product) {
 
                Frame frame = JOptionPane.getFrameForComponent(frameComp);
                AltosDevice[]   devices;
                devices = AltosUSBDevice.list(product);
        public static AltosDevice show (Component frameComp, int product) {
 
                Frame frame = JOptionPane.getFrameForComponent(frameComp);
                AltosDevice[]   devices;
                devices = AltosUSBDevice.list(product);
+               AltosDeviceDialog       dialog;
 
 
-               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;
-               }
+               dialog = new AltosDeviceDialog(frame, frameComp,
+                                              devices);
+               dialog.setVisible(true);
+               return dialog.getValue();
+       }
+
+       private AltosDevice     value;
+       private JList           list;
+       private JButton         cancel_button;
+       private JButton         select_button;
+       private JButton         manage_bluetooth_button;
+       private Frame           frame;
+
+       private AltosDevice getValue() {
+               return value;
        }
 
        }
 
-       private AltosDeviceDialog (Frame frame, Component location,
-                                  AltosDevice[] devices,
-                                  AltosDevice initial) {
-               super(frame, "Device Selection", true);
+       private AltosDeviceDialog (Frame in_frame, Component location,
+                                  AltosDevice[] devices) {
+               super(in_frame, "Device Selection", true);
 
 
+               frame = in_frame;
                value = null;
 
                value = null;
 
-               JButton cancelButton = new JButton("Cancel");
-               cancelButton.addActionListener(this);
+               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
 
                list = new JList(devices) {
                                //Subclass JList to workaround bug 4832765, which can cause the
@@ -112,7 +108,7 @@ public class AltosDeviceDialog extends JDialog implements ActionListener {
                list.addMouseListener(new MouseAdapter() {
                                 public void mouseClicked(MouseEvent e) {
                                         if (e.getClickCount() == 2) {
                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 +135,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.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(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();
 
                //Put everything together, using the content pane's BorderLayout.
                Container contentPane = getContentPane();
@@ -149,7 +147,8 @@ public class AltosDeviceDialog extends JDialog implements ActionListener {
                contentPane.add(buttonPane, BorderLayout.PAGE_END);
 
                //Initialize values.
                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);
        }
                pack();
                setLocationRelativeTo(location);
        }
@@ -157,8 +156,12 @@ 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()))
        //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);
+                       return;
+               }
+               setVisible(false);
        }
 
 }
        }
 
 }
index 0fc6583ca11c46771eaf32df6d18a41414fec4a5..4b808c410d35912cdf896997847209168310e1df 100644 (file)
@@ -198,10 +198,6 @@ public class AltosUI extends JFrame {
        }
 
        private void ConnectToDevice() {
        }
 
        private void ConnectToDevice() {
-               AltosBTManage   bt_manage;
-
-               bt_manage = new AltosBTManage(AltosBTDevice.bt_product_any, this);
-               bt_manage.list();
                AltosDevice     device = AltosDeviceDialog.show(AltosUI.this,
                                                                Altos.product_basestation);
 
                AltosDevice     device = AltosDeviceDialog.show(AltosUI.this,
                                                                Altos.product_basestation);