summaryrefslogtreecommitdiff
path: root/altosuilib
diff options
context:
space:
mode:
Diffstat (limited to 'altosuilib')
-rw-r--r--altosuilib/AltosDevice.java30
-rw-r--r--altosuilib/AltosDeviceDialog.java163
-rw-r--r--altosuilib/AltosUSBDevice.java112
-rw-r--r--altosuilib/Makefile.am3
4 files changed, 308 insertions, 0 deletions
diff --git a/altosuilib/AltosDevice.java b/altosuilib/AltosDevice.java
new file mode 100644
index 00000000..69b025ba
--- /dev/null
+++ b/altosuilib/AltosDevice.java
@@ -0,0 +1,30 @@
+/*
+ * 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 org.altusmetrum.altosuilib;
+
+import libaltosJNI.*;
+
+public interface AltosDevice {
+ public abstract String toString();
+ public abstract String toShortString();
+ public abstract int getSerial();
+ public abstract String getPath();
+ public abstract boolean matchProduct(int product);
+ public abstract String getErrorString();
+ public SWIGTYPE_p_altos_file open();
+}
diff --git a/altosuilib/AltosDeviceDialog.java b/altosuilib/AltosDeviceDialog.java
new file mode 100644
index 00000000..82620b8b
--- /dev/null
+++ b/altosuilib/AltosDeviceDialog.java
@@ -0,0 +1,163 @@
+/*
+ * Copyright © 2010 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 org.altusmetrum.altosuilib;
+
+import javax.swing.*;
+import java.awt.*;
+import java.awt.event.*;
+
+public abstract class AltosDeviceDialog extends AltosUIDialog implements ActionListener {
+
+ 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 AltosDevice getValue() {
+ return value;
+ }
+
+ public abstract AltosDevice[] devices();
+
+ private void update_devices() {
+ AltosDevice[] devices = devices();
+ list.setListData(devices);
+ select_button.setEnabled(devices.length > 0);
+ }
+
+ public AltosDeviceDialog (Frame in_frame, Component location, int in_product) {
+ super(in_frame, "Device Selection", true);
+
+ product = in_product;
+ frame = in_frame;
+ value = null;
+
+ 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);
+
+ 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
+ //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);
+ }
+ };
+
+ list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
+ list.setLayoutOrientation(JList.HORIZONTAL_WRAP);
+ list.setVisibleRowCount(-1);
+ list.addMouseListener(new MouseAdapter() {
+ public void mouseClicked(MouseEvent e) {
+ if (e.getClickCount() == 2) {
+ select_button.doClick(); //emulate button click
+ }
+ }
+ });
+ JScrollPane listScroller = new JScrollPane(list);
+ listScroller.setPreferredSize(new Dimension(400, 80));
+ listScroller.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(list);
+ listPane.add(label);
+ listPane.add(Box.createRigidArea(new Dimension(0,5)));
+ listPane.add(listScroller);
+ 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(cancel_button);
+ buttonPane.add(Box.createRigidArea(new Dimension(10, 0)));
+// 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();
+ contentPane.add(listPane, BorderLayout.CENTER);
+ contentPane.add(buttonPane, BorderLayout.PAGE_END);
+
+ //Initialize values.
+ if (devices != null && devices.length != 0)
+ list.setSelectedValue(devices[0], true);
+ pack();
+ setLocationRelativeTo(location);
+ }
+
+ //Handle clicks on the Set and Cancel buttons.
+ public void actionPerformed(ActionEvent e) {
+ if ("select".equals(e.getActionCommand()))
+ value = (AltosDevice)(list.getSelectedValue());
+// if ("manage".equals(e.getActionCommand())) {
+// AltosBTManage.show(frame, AltosBTKnown.bt_known());
+// update_devices();
+// return;
+// }
+ setVisible(false);
+ }
+
+}
diff --git a/altosuilib/AltosUSBDevice.java b/altosuilib/AltosUSBDevice.java
new file mode 100644
index 00000000..2f4e0dc6
--- /dev/null
+++ b/altosuilib/AltosUSBDevice.java
@@ -0,0 +1,112 @@
+/*
+ * Copyright © 2010 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 org.altusmetrum.altosuilib;
+
+import java.util.*;
+import libaltosJNI.*;
+
+public class AltosUSBDevice extends altos_device implements AltosDevice {
+
+ public String toString() {
+ String name = getName();
+ if (name == null)
+ name = "Altus Metrum";
+ return String.format("%-20.20s %4d %s",
+ name, getSerial(), getPath());
+ }
+
+ public String toShortString() {
+ String name = getName();
+ if (name == null)
+ name = "Altus Metrum";
+ return String.format("%s %d %s",
+ name, getSerial(), getPath());
+
+ }
+
+ public String getErrorString() {
+ altos_error error = new altos_error();
+
+ libaltos.altos_get_last_error(error);
+ return String.format("%s (%d)", error.getString(), error.getCode());
+ }
+
+ public SWIGTYPE_p_altos_file open() {
+ return libaltos.altos_open(this);
+ }
+
+ private boolean isAltusMetrum() {
+ if (getVendor() != AltosUILib.vendor_altusmetrum)
+ return false;
+ if (getProduct() < AltosUILib.product_altusmetrum_min)
+ return false;
+ if (getProduct() > AltosUILib.product_altusmetrum_max)
+ return false;
+ return true;
+ }
+
+ public boolean matchProduct(int want_product) {
+
+ if (!isAltusMetrum())
+ return false;
+
+ if (want_product == AltosUILib.product_any)
+ return true;
+
+ if (want_product == AltosUILib.product_basestation)
+ return matchProduct(AltosUILib.product_teledongle) ||
+ matchProduct(AltosUILib.product_teleterra) ||
+ matchProduct(AltosUILib.product_telebt) ||
+ matchProduct(AltosUILib.product_megadongle);
+
+ if (want_product == AltosUILib.product_altimeter)
+ return matchProduct(AltosUILib.product_telemetrum) ||
+ matchProduct(AltosUILib.product_megametrum);
+
+ int have_product = getProduct();
+
+ if (have_product == AltosUILib.product_altusmetrum) /* old devices match any request */
+ return true;
+
+ if (want_product == have_product)
+ return true;
+
+ return false;
+ }
+
+ static java.util.List<AltosDevice> list(int product) {
+ if (!AltosUILib.load_library())
+ return null;
+
+ SWIGTYPE_p_altos_list list = libaltos.altos_list_start();
+
+ ArrayList<AltosDevice> device_list = new ArrayList<AltosDevice>();
+ if (list != null) {
+ for (;;) {
+ AltosUSBDevice device = new AltosUSBDevice();
+ if (libaltos.altos_list_next(list, device) == 0)
+ break;
+ if (device.matchProduct(product))
+ device_list.add(device);
+ }
+ libaltos.altos_list_finish(list);
+ }
+
+ return device_list;
+ }
+} \ No newline at end of file
diff --git a/altosuilib/Makefile.am b/altosuilib/Makefile.am
index 26aee7c4..e2ff8cb5 100644
--- a/altosuilib/Makefile.am
+++ b/altosuilib/Makefile.am
@@ -11,6 +11,9 @@ AltosUILibdir = $(datadir)/java
AltosUILib_JAVA = \
AltosConfigureUI.java \
+ AltosDevice.java \
+ AltosDeviceDialog.java \
+ AltosUSBDevice.java \
AltosFontListener.java \
AltosUIDialog.java \
AltosUIFrame.java \