source: src/main/java/genius/gui/panels/ComboboxSelectionPanel.java@ 3

Last change on this file since 3 was 1, checked in by Wouter Pasman, 7 years ago

Initial import : Genius 9.0.0

File size: 2.4 KB
Line 
1package genius.gui.panels;
2
3import java.awt.BorderLayout;
4import java.awt.Component;
5
6import javax.swing.DefaultListCellRenderer;
7import javax.swing.JComboBox;
8import javax.swing.JLabel;
9import javax.swing.JPanel;
10import javax.swing.ListCellRenderer;
11import javax.swing.event.ListDataEvent;
12import 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")
23public 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}&lt;object&gt; and ignores the proper
83 * typing
84 */
85 public void setCellRenderer(DefaultListCellRenderer renderer) {
86 combo.setRenderer(renderer);
87 }
88
89}
Note: See TracBrowser for help on using the repository browser.