135 lines
3.3 KiB
Java
135 lines
3.3 KiB
Java
package org.hwo.ui;
|
|
|
|
import java.awt.BorderLayout;
|
|
import java.awt.FlowLayout;
|
|
|
|
import javax.swing.DefaultListModel;
|
|
import javax.swing.JButton;
|
|
import javax.swing.JDialog;
|
|
import javax.swing.JPanel;
|
|
import javax.swing.border.EmptyBorder;
|
|
|
|
import java.awt.GridBagLayout;
|
|
|
|
import javax.swing.JLabel;
|
|
|
|
import java.awt.GridBagConstraints;
|
|
|
|
import javax.swing.JList;
|
|
|
|
import java.awt.Insets;
|
|
import java.awt.event.ActionListener;
|
|
import java.awt.event.ActionEvent;
|
|
import java.awt.Dialog.ModalExclusionType;
|
|
import java.awt.Dialog.ModalityType;
|
|
|
|
public class JObjectSelector extends JDialog {
|
|
|
|
public static Object execute(Object[] items){
|
|
|
|
JObjectSelector os = new JObjectSelector();
|
|
os.setItems(items);
|
|
os.setVisible(true);
|
|
return os.selectedItem;
|
|
}
|
|
|
|
|
|
private final JPanel contentPanel = new JPanel();
|
|
private JList lItems;
|
|
|
|
private Object[] items;
|
|
private Object selectedItem;
|
|
private JLabel lText;
|
|
|
|
/**
|
|
* Create the dialog.
|
|
*/
|
|
public JObjectSelector() {
|
|
setModalityType(ModalityType.APPLICATION_MODAL);
|
|
setModalExclusionType(ModalExclusionType.APPLICATION_EXCLUDE);
|
|
setTitle("Auswahl\u2026");
|
|
setBounds(100, 100, 450, 300);
|
|
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};
|
|
gbl_contentPanel.rowHeights = new int[]{0, 0, 0};
|
|
gbl_contentPanel.columnWeights = new double[]{1.0, Double.MIN_VALUE};
|
|
gbl_contentPanel.rowWeights = new double[]{0.0, 1.0, Double.MIN_VALUE};
|
|
contentPanel.setLayout(gbl_contentPanel);
|
|
{
|
|
lText = new JLabel("-");
|
|
GridBagConstraints gbc_lText = new GridBagConstraints();
|
|
gbc_lText.insets = new Insets(0, 0, 5, 0);
|
|
gbc_lText.gridx = 0;
|
|
gbc_lText.gridy = 0;
|
|
contentPanel.add(lText, gbc_lText);
|
|
}
|
|
{
|
|
lItems = new JList();
|
|
GridBagConstraints gbc_lItems = new GridBagConstraints();
|
|
gbc_lItems.fill = GridBagConstraints.BOTH;
|
|
gbc_lItems.gridx = 0;
|
|
gbc_lItems.gridy = 1;
|
|
contentPanel.add(lItems, gbc_lItems);
|
|
}
|
|
{
|
|
JPanel buttonPane = new JPanel();
|
|
buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
|
|
getContentPane().add(buttonPane, BorderLayout.SOUTH);
|
|
{
|
|
JButton okButton = new JButton("OK");
|
|
okButton.addActionListener(new ActionListener() {
|
|
public void actionPerformed(ActionEvent arg0) {
|
|
accept();
|
|
}
|
|
});
|
|
okButton.setActionCommand("OK");
|
|
buttonPane.add(okButton);
|
|
getRootPane().setDefaultButton(okButton);
|
|
}
|
|
{
|
|
JButton cancelButton = new JButton("Cancel");
|
|
cancelButton.addActionListener(new ActionListener() {
|
|
public void actionPerformed(ActionEvent e) {
|
|
cancel();
|
|
}
|
|
});
|
|
cancelButton.setActionCommand("Cancel");
|
|
buttonPane.add(cancelButton);
|
|
}
|
|
}
|
|
}
|
|
|
|
public void accept()
|
|
{
|
|
selectedItem = lItems.getSelectedValue();
|
|
setVisible(false);
|
|
}
|
|
|
|
public void cancel(){
|
|
setVisible(false);
|
|
}
|
|
|
|
public void setItems(Object[] items) {
|
|
this.items = items;
|
|
DefaultListModel dlm = new DefaultListModel();
|
|
for (Object o:items){
|
|
dlm.addElement(o);
|
|
}
|
|
lItems.setModel(dlm);
|
|
}
|
|
public Object[] getItems() {
|
|
return items;
|
|
}
|
|
|
|
public void setText(String text){
|
|
this.lText.setText(text);
|
|
}
|
|
public String getText(){
|
|
return this.lText.getText();
|
|
}
|
|
|
|
}
|