[1] | 1 | package genius.gui.panels;
|
---|
| 2 |
|
---|
| 3 | import java.awt.BorderLayout;
|
---|
| 4 | import java.awt.Component;
|
---|
| 5 |
|
---|
| 6 | import javax.swing.DefaultListCellRenderer;
|
---|
| 7 | import javax.swing.JComboBox;
|
---|
| 8 | import javax.swing.JLabel;
|
---|
| 9 | import javax.swing.JPanel;
|
---|
| 10 | import javax.swing.ListCellRenderer;
|
---|
| 11 | import javax.swing.event.ListDataEvent;
|
---|
| 12 | import javax.swing.event.ListDataListener;
|
---|
| 13 |
|
---|
| 14 | /**
|
---|
| 15 | * A GUI that shows panel with given title at the left and a combobox at the
|
---|
| 16 | * right. In the combo, the user can select an item from a list. This panel
|
---|
| 17 | * hides automatically if there are no selectable items.
|
---|
| 18 | *
|
---|
| 19 | * @param <ItemType>
|
---|
| 20 | * the type of the elements in the item list.
|
---|
| 21 | */
|
---|
| 22 | @SuppressWarnings("serial")
|
---|
| 23 | public class ComboboxSelectionPanel<ItemType> extends JPanel {
|
---|
| 24 |
|
---|
| 25 | private SingleSelectionModel<ItemType> model;
|
---|
| 26 | final JComboBox<ItemType> combo;
|
---|
| 27 |
|
---|
| 28 | /**
|
---|
| 29 | *
|
---|
| 30 | * @param title
|
---|
| 31 | * the text to be placed left of the combo box
|
---|
| 32 | * @param itemsModel
|
---|
| 33 | * the {@link SingleSelectionModel} that contains the possible
|
---|
| 34 | * choices and can be listened for changes.
|
---|
| 35 | */
|
---|
| 36 | public ComboboxSelectionPanel(final String title, final SingleSelectionModel<ItemType> itemsModel) {
|
---|
| 37 | this.model = itemsModel;
|
---|
| 38 | setLayout(new BorderLayout());
|
---|
| 39 | combo = new JComboBox<ItemType>(itemsModel);
|
---|
| 40 |
|
---|
| 41 | add(new JLabel(title), BorderLayout.WEST);
|
---|
| 42 | add(combo, BorderLayout.CENTER);
|
---|
| 43 | setAlignmentX(Component.RIGHT_ALIGNMENT);
|
---|
| 44 | updateVisibility();
|
---|
| 45 |
|
---|
| 46 | itemsModel.addListDataListener(new ListDataListener() {
|
---|
| 47 |
|
---|
| 48 | @Override
|
---|
| 49 | public void intervalRemoved(ListDataEvent e) {
|
---|
| 50 | updateVisibility();
|
---|
| 51 | }
|
---|
| 52 |
|
---|
| 53 | @Override
|
---|
| 54 | public void intervalAdded(ListDataEvent e) {
|
---|
| 55 | updateVisibility();
|
---|
| 56 | }
|
---|
| 57 |
|
---|
| 58 | @Override
|
---|
| 59 | public void contentsChanged(ListDataEvent e) {
|
---|
| 60 | updateVisibility();
|
---|
| 61 | }
|
---|
| 62 |
|
---|
| 63 | });
|
---|
| 64 | }
|
---|
| 65 |
|
---|
| 66 | /**
|
---|
| 67 | * We are visible only if there is at least one possible selection. This
|
---|
| 68 | * enables us to hide eg the Mediator combobox by just emptying the mediator
|
---|
| 69 | * list in the model.
|
---|
| 70 | */
|
---|
| 71 | private void updateVisibility() {
|
---|
| 72 | // FIXME invokelater
|
---|
| 73 | setVisible(model.getSize() > 0);
|
---|
| 74 | }
|
---|
| 75 |
|
---|
| 76 | /**
|
---|
| 77 | * Set the cell renderer.
|
---|
| 78 | *
|
---|
| 79 | * @param renderer
|
---|
| 80 | * a {@link DefaultListCellRenderer}. Note, for unknown reason,
|
---|
| 81 | * {@link DefaultListCellRenderer} implements
|
---|
| 82 | * {@link ListCellRenderer}<object> and ignores the proper
|
---|
| 83 | * typing
|
---|
| 84 | */
|
---|
| 85 | public void setCellRenderer(DefaultListCellRenderer renderer) {
|
---|
| 86 | combo.setRenderer(renderer);
|
---|
| 87 | }
|
---|
| 88 |
|
---|
| 89 | }
|
---|