java-org.hwo.ui/src/org/hwo/ui/JObjectSelect.java

103 lines
2.5 KiB
Java

package org.hwo.ui;
import javax.swing.JComponent;
import java.awt.GridBagLayout;
import javax.swing.JTextField;
import java.awt.GridBagConstraints;
import java.awt.Insets;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import javax.swing.JButton;
import javax.swing.ImageIcon;
import javax.swing.JList;
import javax.swing.JPopupMenu;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class JObjectSelect extends JComponent {
private JTextField tfDisplay;
private List<Object> itemList;
private Object selectedItem;
public JObjectSelect() {
GridBagLayout gridBagLayout = new GridBagLayout();
gridBagLayout.columnWidths = new int[]{0, 0, 0};
gridBagLayout.rowHeights = new int[]{0, 0};
gridBagLayout.columnWeights = new double[]{1.0, 0.0, Double.MIN_VALUE};
gridBagLayout.rowWeights = new double[]{1.0, Double.MIN_VALUE};
setLayout(gridBagLayout);
tfDisplay = new JTextField();
tfDisplay.setEditable(false);
GridBagConstraints gbc_tfDisplay = new GridBagConstraints();
gbc_tfDisplay.insets = new Insets(0, 0, 0, 5);
gbc_tfDisplay.fill = GridBagConstraints.BOTH;
gbc_tfDisplay.gridx = 0;
gbc_tfDisplay.gridy = 0;
add(tfDisplay, gbc_tfDisplay);
tfDisplay.setColumns(10);
JButton btnNewButton = new JButton("...");
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
showListPopup();
}
});
btnNewButton.setIcon(null);
GridBagConstraints gbc_btnNewButton = new GridBagConstraints();
gbc_btnNewButton.fill = GridBagConstraints.BOTH;
gbc_btnNewButton.gridx = 1;
gbc_btnNewButton.gridy = 0;
add(btnNewButton, gbc_btnNewButton);
itemList = new ArrayList<Object>();
setSelectedItem(null);
}
public Object getSelectedItem(){
return this.selectedItem;
}
public void setSelectedItem(Object item){
this.selectedItem = item;
if (selectedItem != null){
tfDisplay.setText(selectedItem.toString());
} else {
tfDisplay.setText("");
}
}
public Object[] getItems(){
return itemList.toArray();
}
public void setItems(Object[] items){
this.itemList.clear();
this.itemList.addAll(Arrays.asList(items));
}
private void showListPopup(){
/* JPopupMenu popup = new JPopupMenu();
JList<Object> jlist = new JList<Object>(itemList.toArray());
popup.add(jlist);
popup.show(this, 0, getHeight());
*/
Object si = JObjectSelector.execute(getItems());
if (si != null){
setSelectedItem(si);
}
}
}