forked from LupusNobilis/java-org.hwo.ui
172 lines
4.6 KiB
Java
172 lines
4.6 KiB
Java
package org.hwo.ui.dialog;
|
|
|
|
import java.awt.BorderLayout;
|
|
import java.awt.Component;
|
|
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.io.SerialPort;
|
|
import org.hwo.io.NewSerialPort.NewSerialPort;
|
|
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 NewSerialPort execute()
|
|
{
|
|
return execute(null);
|
|
}
|
|
public static NewSerialPort execute(Component parent,String selectedPortName)
|
|
{
|
|
return execute(null, selectedPortName);
|
|
}
|
|
public static NewSerialPort execute(Component parent,String selectedPortName)
|
|
{
|
|
SerialPortChooser spc = new SerialPortChooser();
|
|
|
|
if (parent != null)
|
|
spc.setLocationRelativeTo(parent);
|
|
|
|
if (selectedPortName != null)
|
|
spc.setSelectedSerialPort(selectedPortName);
|
|
|
|
if (parent != null)
|
|
spc.setLocationRelativeTo(parent);
|
|
|
|
spc.setVisible(true);
|
|
|
|
return spc.getSelectedSerialPort();
|
|
}
|
|
public static NewSerialPort execute(String selectedPortName)
|
|
{
|
|
return execute(null,selectedPortName);
|
|
}
|
|
|
|
|
|
private final JPanel contentPanel = new JPanel();
|
|
|
|
private NewSerialPort 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: NewSerialPort.getPortNames())
|
|
cbPortList.addItem(portName);
|
|
}
|
|
|
|
private void accept()
|
|
{
|
|
String portName = (String)cbPortList.getSelectedItem();
|
|
if (portName != null){
|
|
selectedSerialPort = new NewSerialPort(portName);
|
|
setVisible(false);
|
|
}
|
|
}
|
|
|
|
private void cancel()
|
|
{
|
|
selectedSerialPort = null;
|
|
setVisible(false);
|
|
}
|
|
|
|
public NewSerialPort getSelectedSerialPort() {
|
|
return selectedSerialPort;
|
|
}
|
|
|
|
public void setSelectedSerialPort(SerialPort selectedSerialPort) {
|
|
cbPortList.selectEqualItem(selectedSerialPort.getPortName());
|
|
}
|
|
|
|
public void setSelectedSerialPort(String portName) {
|
|
cbPortList.selectEqualItem(portName);
|
|
}
|
|
|
|
}
|