java-org.hwo/src/org/hwo/io/SerialPortChooser.java

154 lines
4.0 KiB
Java

package org.hwo.io;
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import java.awt.Dialog.ModalExclusionType;
import java.awt.Dialog.ModalityType;
import java.awt.GridBagLayout;
import org.hwo.i18n.Messages;
import org.hwo.ui.JComboBoxEx;
import java.awt.GridBagConstraints;
import javax.swing.JLabel;
import java.awt.Insets;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class SerialPortChooser extends JDialog {
public static SerialPort execute()
{
return execute(null);
}
public static SerialPort execute(String selectedPortName)
{
SerialPortChooser spc = new SerialPortChooser();
if (selectedPortName != null)
spc.setSelectedSerialPort(selectedPortName);
spc.setVisible(true);
return spc.getSelectedSerialPort();
}
private final JPanel contentPanel = new JPanel();
private SerialPort selectedSerialPort;
private JComboBoxEx cbPortList;
/**
* Create the dialog.
*/
public SerialPortChooser() {
setTitle(Messages.getString("SerialPortChooser.0"));
setModalityType(ModalityType.APPLICATION_MODAL);
setModalExclusionType(ModalExclusionType.APPLICATION_EXCLUDE);
setModal(true);
setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
setBounds(100, 100, 491, 169);
getContentPane().setLayout(new BorderLayout());
contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
getContentPane().add(contentPanel, BorderLayout.CENTER);
GridBagLayout gbl_contentPanel = new GridBagLayout();
gbl_contentPanel.columnWidths = new int[]{0, 0, 0};
gbl_contentPanel.rowHeights = new int[]{0, 0};
gbl_contentPanel.columnWeights = new double[]{0.0, 1.0, Double.MIN_VALUE};
gbl_contentPanel.rowWeights = new double[]{0.0, Double.MIN_VALUE};
contentPanel.setLayout(gbl_contentPanel);
{
JLabel lblSchnittstelle = new JLabel(Messages.getString("interface"));
GridBagConstraints gbc_lblSchnittstelle = new GridBagConstraints();
gbc_lblSchnittstelle.insets = new Insets(0, 0, 0, 5);
gbc_lblSchnittstelle.anchor = GridBagConstraints.EAST;
gbc_lblSchnittstelle.gridx = 0;
gbc_lblSchnittstelle.gridy = 0;
contentPanel.add(lblSchnittstelle, gbc_lblSchnittstelle);
}
{
cbPortList = new JComboBoxEx();
GridBagConstraints gbc_cbPortList = new GridBagConstraints();
gbc_cbPortList.fill = GridBagConstraints.HORIZONTAL;
gbc_cbPortList.gridx = 1;
gbc_cbPortList.gridy = 0;
contentPanel.add(cbPortList, gbc_cbPortList);
}
{
JPanel buttonPane = new JPanel();
buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
getContentPane().add(buttonPane, BorderLayout.SOUTH);
{
JButton okButton = new JButton(Messages.getString("ok"));
okButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
accept();
}
});
okButton.setActionCommand("OK");
buttonPane.add(okButton);
getRootPane().setDefaultButton(okButton);
}
{
JButton cancelButton = new JButton(Messages.getString("cancel"));
cancelButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
cancel();
}
});
cancelButton.setActionCommand("Cancel");
buttonPane.add(cancelButton);
}
}
initialize();
}
private void initialize()
{
selectedSerialPort = null;
cbPortList.removeAllItems();
for (String portName: SerialPort.getPortNames())
cbPortList.addItem(portName);
}
private void accept()
{
selectedSerialPort = SerialPort.newInstance();
selectedSerialPort.setPortName(cbPortList.getSelectedItem().toString());
setVisible(false);
}
private void cancel()
{
selectedSerialPort = null;
setVisible(false);
}
public SerialPort getSelectedSerialPort() {
return selectedSerialPort;
}
public void setSelectedSerialPort(SerialPort selectedSerialPort) {
cbPortList.selectEqualItem(selectedSerialPort.getPortName());
}
public void setSelectedSerialPort(String portName) {
cbPortList.selectEqualItem(portName);
}
}