X-Git-Url: https://git.gag.com/?p=fw%2Faltos;a=blobdiff_plain;f=altosuilib%2FAltosDeviceDialog.java;h=d8a24b1841d0e0c58c3606907055c8622e96863e;hp=82620b8b387a58580120a5a95c876c3d9414fae3;hb=bb3f961022032390bfac6104ea4136354df67689;hpb=d83587c3c66b730cc54ca153714eee520ee40b2c diff --git a/altosuilib/AltosDeviceDialog.java b/altosuilib/AltosDeviceDialog.java index 82620b8b..d8a24b18 100644 --- a/altosuilib/AltosDeviceDialog.java +++ b/altosuilib/AltosDeviceDialog.java @@ -3,7 +3,8 @@ * * 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. + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. * * This program is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of @@ -15,7 +16,7 @@ * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. */ -package org.altusmetrum.altosuilib; +package org.altusmetrum.altosuilib_13; import javax.swing.*; import java.awt.*; @@ -23,13 +24,15 @@ 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; + private AltosDevice value; + private JList list; + private JButton cancel_button; + private JButton select_button; + public Frame frame; + public int product; + public JPanel buttonPane; + private Timer timer; + AltosDevice[] devices; public AltosDevice getValue() { return value; @@ -37,12 +40,22 @@ public abstract class AltosDeviceDialog extends AltosUIDialog implements ActionL public abstract AltosDevice[] devices(); - private void update_devices() { - AltosDevice[] devices = devices(); + public void update_devices() { + AltosDevice selected = list.getSelectedValue(); + + devices = devices(); list.setListData(devices); + for (AltosDevice d : devices) { + if (d.equals(selected)) { + list.setSelectedValue(d, true); + break; + } + } select_button.setEnabled(devices.length > 0); } + public void add_bluetooth() { } + public AltosDeviceDialog (Frame in_frame, Component location, int in_product) { super(in_frame, "Device Selection", true); @@ -50,24 +63,17 @@ public abstract class AltosDeviceDialog extends AltosUIDialog implements ActionL 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); + select_button.setEnabled(false); getRootPane().setDefaultButton(select_button); - list = new JList(devices) { + 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 @@ -126,14 +132,16 @@ public abstract class AltosDeviceDialog extends AltosUIDialog implements ActionL listPane.setBorder(BorderFactory.createEmptyBorder(10,10,10,10)); //Lay out the buttons from left to right. - JPanel buttonPane = new 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))); + + if (AltosUILib.has_bluetooth) + add_bluetooth(); + buttonPane.add(select_button); //Put everything together, using the content pane's BorderLayout. @@ -142,22 +150,41 @@ public abstract class AltosDeviceDialog extends AltosUIDialog implements ActionL contentPane.add(buttonPane, BorderLayout.PAGE_END); //Initialize values. + update_devices(); if (devices != null && devices.length != 0) list.setSelectedValue(devices[0], true); pack(); setLocationRelativeTo(location); + + timer = new Timer(1000, new ActionListener () { + public void actionPerformed(ActionEvent evt) { + update_devices(); + } + }); + + addComponentListener(new ComponentListener() { + public void componentShown(ComponentEvent e) { + timer.start(); + } + public void componentMoved(ComponentEvent e) { + } + public void componentResized(ComponentEvent e) { + } + public void componentHidden(ComponentEvent e) { + timer.stop(); + } + }); + } //Handle clicks on the Set and Cancel buttons. public void actionPerformed(ActionEvent e) { - if ("select".equals(e.getActionCommand())) + 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); + setVisible(false); + } + if ("cancel".equals(e.getActionCommand())) + setVisible(false); } }