From: Keith Packard Date: Thu, 29 Jul 2010 03:18:36 +0000 (-0700) Subject: altosui: Replace device dialog. Center eeprom monitor. X-Git-Tag: debian/0.6+268+gd0fd53b~3 X-Git-Url: https://git.gag.com/?p=fw%2Faltos;a=commitdiff_plain;h=d2d772164af95a35ea0f5d2413a5be67de9a210f altosui: Replace device dialog. Center eeprom monitor. This adds a custom dialog for selecting device, which makes it look much nicer on the screen and allows the user to double-click on an entry to select it. Signed-off-by: Keith Packard --- diff --git a/ao-tools/altosui/AltosDeviceDialog.java b/ao-tools/altosui/AltosDeviceDialog.java index 08921c3d..536a8057 100644 --- a/ao-tools/altosui/AltosDeviceDialog.java +++ b/ao-tools/altosui/AltosDeviceDialog.java @@ -20,26 +20,34 @@ package altosui; import java.lang.*; 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 altosui.AltosDevice; -public class AltosDeviceDialog { +public class AltosDeviceDialog extends JDialog implements ActionListener { - static altos_device show (JFrame frame, String product) { + private static AltosDeviceDialog dialog; + private static altos_device value = null; + private JList list; + + public static altos_device show (Component frameComp, String product) { + + Frame frame = JOptionPane.getFrameForComponent(frameComp); AltosDevice[] devices; devices = AltosDevice.list(product); + if (devices != null & devices.length > 0) { - Object o = JOptionPane.showInputDialog(frame, - "Select a device", - "Device Selection", - JOptionPane.PLAIN_MESSAGE, - null, - devices, - devices[0]); - return (altos_device) o; + value = null; + dialog = new AltosDeviceDialog(frame, frameComp, + devices, + devices[0]); + + dialog.setVisible(true); + return value; } else { JOptionPane.showMessageDialog(frame, "No AltOS devices available", @@ -48,4 +56,107 @@ public class AltosDeviceDialog { return null; } } + + private AltosDeviceDialog (Frame frame, Component location, + AltosDevice[] devices, + AltosDevice initial) { + super(frame, "Device Selection", true); + + value = null; + + JButton cancelButton = new JButton("Cancel"); + cancelButton.addActionListener(this); + + final JButton selectButton = new JButton("Select"); + selectButton.setActionCommand("select"); + selectButton.addActionListener(this); + getRootPane().setDefaultButton(selectButton); + + 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) { + selectButton.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(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(location); + } + + //Handle clicks on the Set and Cancel buttons. + public void actionPerformed(ActionEvent e) { + if ("select".equals(e.getActionCommand())) { + System.out.printf("got select action\n"); + AltosDeviceDialog.value = (altos_device)(list.getSelectedValue()); + } + AltosDeviceDialog.dialog.setVisible(false); + } + } diff --git a/ao-tools/altosui/AltosEepromMonitor.java b/ao-tools/altosui/AltosEepromMonitor.java index 9eb3f0d0..e110a354 100644 --- a/ao-tools/altosui/AltosEepromMonitor.java +++ b/ao-tools/altosui/AltosEepromMonitor.java @@ -55,8 +55,8 @@ public class AltosEepromMonitor extends JDialog { super (owner, "Download Flight Data", false); GridBagConstraints c; - Insets il = new Insets(4,0,4,4); - Insets ir = new Insets(4,4,4,0); + Insets il = new Insets(4,4,4,4); + Insets ir = new Insets(4,4,4,4); pane = getContentPane(); pane.setLayout(new GridBagLayout()); @@ -126,7 +126,7 @@ public class AltosEepromMonitor extends JDialog { c.anchor = GridBagConstraints.CENTER; c.gridx = 0; c.gridy = 3; c.gridwidth = GridBagConstraints.REMAINDER; - Insets ib = new Insets(4,0,4,0); + Insets ib = new Insets(4,4,4,4); c.insets = ib; pane.add(pbar, c); @@ -142,6 +142,7 @@ public class AltosEepromMonitor extends JDialog { pane.add(cancel, c); pack(); + setLocationRelativeTo(owner); setVisible(true); }