package org.hwo.ui; import java.awt.BorderLayout; import java.awt.Component; import java.awt.FlowLayout; import javax.swing.DefaultListModel; import javax.swing.JButton; import javax.swing.JComponent; 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 T execute(T[] items){ return execute(items,null,null); } public static T execute(T[] items,Component parent){ return execute(items,parent,null,null); } public static T execute(T[] items,String title,String text){ return execute(items,null,title,text); } public static T execute(T[] items,Component parent,String title,String text){ JObjectSelector os = new JObjectSelector(); os.setItems(items); if (title != null) os.setTitle(title); if (text != null) os.setText(text); if (parent != null){ os.setLocationRelativeTo(parent); } os.setVisible(true); os.dispose(); return os.selectedItem; } private final JPanel contentPanel = new JPanel(); private JList lItems; private E[] items; private E 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 = (E)lItems.getSelectedValue(); setVisible(false); } public void cancel(){ setVisible(false); } public void setItems(E[] items) { this.items = items; DefaultListModel dlm = new DefaultListModel(); for (E o:items){ dlm.addElement(o); } lItems.setModel(dlm); } public E[] getItems() { return items; } public void setText(String text){ this.lText.setText(text); } public String getText(){ return this.lText.getText(); } }