altosui: Add primitive bluetooth device manager UI.
authorKeith Packard <keithp@keithp.com>
Sat, 9 Apr 2011 02:46:15 +0000 (19:46 -0700)
committerKeith Packard <keithp@keithp.com>
Thu, 14 Apr 2011 16:47:09 +0000 (09:47 -0700)
This isn't useful, but does inquire for available bluetooth devices
and show them in a list.

Signed-off-by: Keith Packard <keithp@keithp.com>
altosui/AltosBTDevice.java
altosui/AltosBTDeviceIterator.java [new file with mode: 0644]
altosui/AltosBTManage.java [new file with mode: 0644]
altosui/AltosConfigureUI.java
altosui/AltosUI.java
altosui/Makefile.am

index 8eb18bb94270ca11d9b5d7499b0c7c133dd43e5e..233037de9893776e50571deaaab9088aa00d909b 100644 (file)
@@ -113,30 +113,4 @@ public class AltosBTDevice extends altos_bt_device {
 
                return false;
        }
 
                return false;
        }
-
-       static AltosBTDevice[] list(String product) {
-               if (!load_library())
-                       return null;
-
-               SWIGTYPE_p_altos_bt_list list = libaltos.altos_bt_list_start();
-
-               ArrayList<AltosBTDevice> device_list = new ArrayList<AltosBTDevice>();
-               if (list != null) {
-                       SWIGTYPE_p_altos_file file;
-
-                       for (;;) {
-                               AltosBTDevice device = new AltosBTDevice();
-                               if (libaltos.altos_bt_list_next(list, device) == 0)
-                                       break;
-                               if (device.matchProduct(product))
-                                       device_list.add(device);
-                       }
-                       libaltos.altos_bt_list_finish(list);
-               }
-
-               AltosBTDevice[] devices = new AltosBTDevice[device_list.size()];
-               for (int i = 0; i < device_list.size(); i++)
-                       devices[i] = device_list.get(i);
-               return devices;
-       }
 }
\ No newline at end of file
 }
\ No newline at end of file
diff --git a/altosui/AltosBTDeviceIterator.java b/altosui/AltosBTDeviceIterator.java
new file mode 100644 (file)
index 0000000..935bf82
--- /dev/null
@@ -0,0 +1,68 @@
+/*
+ * Copyright © 2011 Keith Packard <keithp@keithp.com>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; version 2 of the License.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
+ */
+
+package altosui;
+import java.lang.*;
+import java.util.*;
+import libaltosJNI.*;
+
+public class AltosBTDeviceIterator implements Iterator<AltosBTDevice> {
+       String          product;
+       AltosBTDevice   current;
+       boolean         done;
+       SWIGTYPE_p_altos_bt_list list;
+
+       public boolean hasNext() {
+               System.out.printf ("BT has next?\n");
+               if (list == null)
+                       return false;
+               if (current != null)
+                       return true;
+               if (done)
+                       return false;
+               current = new AltosBTDevice();
+               while (libaltos.altos_bt_list_next(list, current) != 0) {
+                       System.out.printf("Got BT device %s\n", current.toString());
+//                     if (current.matchProduct(product))
+                               return true;
+               }
+               current = null;
+               done = true;
+               return false;
+       }
+
+       public AltosBTDevice next() {
+               if (hasNext()) {
+                       AltosBTDevice   next = current;
+                       current = null;
+                       return next;
+               }
+               return null;
+       }
+
+       public void remove() {
+               throw new UnsupportedOperationException();
+       }
+
+       public AltosBTDeviceIterator(String in_product) {
+               product = in_product;
+               done = false;
+               current = null;
+               list = libaltos.altos_bt_list_start();
+               System.out.printf("Iteration of BT list started\n");
+       }
+}
diff --git a/altosui/AltosBTManage.java b/altosui/AltosBTManage.java
new file mode 100644 (file)
index 0000000..8e9e0f7
--- /dev/null
@@ -0,0 +1,188 @@
+/*
+ * Copyright © 2011 Keith Packard <keithp@keithp.com>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; version 2 of the License.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
+ */
+
+package altosui;
+
+import java.awt.*;
+import java.awt.event.*;
+import javax.swing.*;
+import javax.swing.filechooser.FileNameExtensionFilter;
+import javax.swing.table.*;
+import java.io.*;
+import java.util.*;
+import java.text.*;
+import java.util.prefs.*;
+import java.util.concurrent.*;
+
+import libaltosJNI.*;
+
+public class AltosBTManage extends JDialog implements ActionListener {
+       String  product;
+       LinkedBlockingQueue<AltosBTDevice> found_devices;
+       JFrame frame;
+
+       class DeviceList extends JList implements Iterable<AltosBTDevice> {
+               LinkedList<AltosBTDevice> devices;
+               DefaultListModel        list_model;
+
+               public void add (AltosBTDevice device) {
+                       devices.add(device);
+                       list_model.addElement(device);
+               }
+
+               //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
+               //of the JScrollBar to a fixed value. You wouldn't get the nice
+               //aligned scrolling, but it should work.
+               public int getScrollableUnitIncrement(Rectangle visibleRect,
+                                                     int orientation,
+                                                     int direction) {
+                       int row;
+                       if (orientation == SwingConstants.VERTICAL &&
+                           direction < 0 && (row = getFirstVisibleIndex()) != -1) {
+                               Rectangle r = getCellBounds(row, row);
+                               if ((r.y == visibleRect.y) && (row != 0))  {
+                                       Point loc = r.getLocation();
+                                       loc.y--;
+                                       int prevIndex = locationToIndex(loc);
+                                       Rectangle prevR = getCellBounds(prevIndex, prevIndex);
+
+                                       if (prevR == null || prevR.y >= r.y) {
+                                               return 0;
+                                       }
+                                       return prevR.height;
+                               }
+                       }
+                       return super.getScrollableUnitIncrement(
+                               visibleRect, orientation, direction);
+               }
+
+               public Iterator<AltosBTDevice> iterator() {
+                       return devices.iterator();
+               }
+
+               public DeviceList() {
+                       devices = new LinkedList<AltosBTDevice>();
+                       list_model = new DefaultListModel();
+                       setModel(list_model);
+                       setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
+                       setLayoutOrientation(JList.HORIZONTAL_WRAP);
+                       setVisibleRowCount(-1);
+               }
+       }
+
+       DeviceList      visible_devices;
+
+       DeviceList      selected_devices;
+
+       public void actionPerformed(ActionEvent e) {
+       }
+
+       public void got_visible_device() {
+               while (!found_devices.isEmpty()) {
+                       AltosBTDevice   device = found_devices.remove();
+                       visible_devices.add(device);
+               }
+       }
+
+       class BTGetVisibleDevices implements Runnable {
+               public void run () {
+
+                       try {
+                               AltosBTDeviceIterator   i = new AltosBTDeviceIterator(product);
+                               AltosBTDevice           device;
+
+                               while ((device = i.next()) != null) {
+                                       Runnable r;
+
+                                       found_devices.add(device);
+                                       r = new Runnable() {
+                                                       public void run() {
+                                                               got_visible_device();
+                                                       }
+                                               };
+                                       SwingUtilities.invokeLater(r);
+                               }
+                       } catch (Exception e) {
+                               System.out.printf("uh-oh, exception %s\n", e.toString());
+                       }
+               }
+       }
+
+       public AltosBTManage(String product, JFrame in_frame) {
+               frame = in_frame;
+               BTGetVisibleDevices     get_visible_devices = new BTGetVisibleDevices();
+               Thread t = new Thread(get_visible_devices);
+               t.start();
+
+               found_devices = new LinkedBlockingQueue<AltosBTDevice>();
+
+               JButton cancelButton = new JButton("Cancel");
+               cancelButton.addActionListener(this);
+
+               final JButton selectButton = new JButton("Select");
+               selectButton.setActionCommand("select");
+               selectButton.addActionListener(this);
+               getRootPane().setDefaultButton(selectButton);
+
+               selected_devices = new DeviceList();
+               JScrollPane selected_list_scroller = new JScrollPane(selected_devices);
+               selected_list_scroller.setPreferredSize(new Dimension(400, 80));
+               selected_list_scroller.setAlignmentX(LEFT_ALIGNMENT);
+
+               visible_devices = new DeviceList();
+               JScrollPane visible_list_scroller = new JScrollPane(visible_devices);
+               visible_list_scroller.setPreferredSize(new Dimension(400, 80));
+               visible_list_scroller.setAlignmentX(LEFT_ALIGNMENT);
+
+               //Create a container so that we can add a title around
+               //the scroll pane.  Can't add a title directly to the
+               //scroll pane because its background would be white.
+               //Lay out the label and scroll pane from top to bottom.
+               JPanel listPane = new JPanel();
+               listPane.setLayout(new BoxLayout(listPane, BoxLayout.PAGE_AXIS));
+
+               JLabel label = new JLabel("Select Device");
+               label.setLabelFor(selected_devices);
+               listPane.add(label);
+               listPane.add(Box.createRigidArea(new Dimension(0,5)));
+               listPane.add(selected_list_scroller);
+               listPane.add(visible_list_scroller);
+               listPane.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));
+
+               //Lay out the buttons from left to right.
+               JPanel buttonPane = new JPanel();
+               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(Box.createRigidArea(new Dimension(10, 0)));
+               buttonPane.add(selectButton);
+
+               //Put everything together, using the content pane's BorderLayout.
+               Container contentPane = getContentPane();
+               contentPane.add(listPane, BorderLayout.CENTER);
+               contentPane.add(buttonPane, BorderLayout.PAGE_END);
+
+               //Initialize values.
+//             list.setSelectedValue(initial, true);
+               pack();
+               setLocationRelativeTo(frame);
+               setVisible(true);
+       }
+}
index 9a292c915b760fd51fda9b2a392665492b0363ea..a2755a06d818d12afcf22ac1744463da5e57ef17 100644 (file)
@@ -49,6 +49,8 @@ public class AltosConfigureUI
 
        JRadioButton    serial_debug;
 
 
        JRadioButton    serial_debug;
 
+       JButton         manage_bluetooth;
+
        /* DocumentListener interface methods */
        public void changedUpdate(DocumentEvent e) {
                AltosPreferences.set_callsign(callsign_value.getText());
        /* DocumentListener interface methods */
        public void changedUpdate(DocumentEvent e) {
                AltosPreferences.set_callsign(callsign_value.getText());
@@ -199,6 +201,19 @@ public class AltosConfigureUI
                c.anchor = GridBagConstraints.WEST;
                pane.add(serial_debug, c);
 
                c.anchor = GridBagConstraints.WEST;
                pane.add(serial_debug, c);
 
+               manage_bluetooth = new JButton("Manage Bluetooth");
+               manage_bluetooth.addActionListener(new ActionListener() {
+                               public void actionPerformed(ActionEvent e) {
+                                       new AltosBTManage(AltosBTDevice.bt_product_any, owner);
+                               }
+                       });
+               c.gridx = 1;
+               c.gridy = 6;
+               c.gridwidth = 3;
+               c.fill = GridBagConstraints.NONE;
+               c.anchor = GridBagConstraints.WEST;
+               pane.add(manage_bluetooth, c);
+
                /* And a close button at the bottom */
                close = new JButton("Close");
                close.addActionListener(new ActionListener() {
                /* And a close button at the bottom */
                close = new JButton("Close");
                close.addActionListener(new ActionListener() {
@@ -207,7 +222,7 @@ public class AltosConfigureUI
                                }
                        });
                c.gridx = 0;
                                }
                        });
                c.gridx = 0;
-               c.gridy = 6;
+               c.gridy = 7;
                c.gridwidth = 3;
                c.fill = GridBagConstraints.NONE;
                c.anchor = GridBagConstraints.CENTER;
                c.gridwidth = 3;
                c.fill = GridBagConstraints.NONE;
                c.anchor = GridBagConstraints.CENTER;
index 4d17b0d24d68e961ffe963e922634849d9ee3056..73ddf979c254a8f2343c57bc8cebdf31687fc688 100644 (file)
@@ -198,6 +198,10 @@ 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,
                                                                AltosDevice.product_basestation);
 
                AltosDevice     device = AltosDeviceDialog.show(AltosUI.this,
                                                                AltosDevice.product_basestation);
 
index 5b11d1b0c2759d7e89ce0fba2769b9ea1259f4f0..37a40eaa894fcebfdaa4c1ee03d9a304c49d4f02 100644 (file)
@@ -27,6 +27,8 @@ altosui_JAVA = \
        AltosDeviceDialog.java \
        AltosDevice.java \
        AltosBTDevice.java \
        AltosDeviceDialog.java \
        AltosDevice.java \
        AltosBTDevice.java \
+       AltosBTDeviceIterator.java \
+       AltosBTManage.java \
        AltosDisplayThread.java \
        AltosEepromChunk.java \
        AltosEepromDelete.java \
        AltosDisplayThread.java \
        AltosEepromChunk.java \
        AltosEepromDelete.java \