1 | package genius.gui.panels;
|
---|
2 |
|
---|
3 | import java.awt.BorderLayout;
|
---|
4 | import java.util.Arrays;
|
---|
5 | import java.util.List;
|
---|
6 |
|
---|
7 | import javax.swing.JFrame;
|
---|
8 | import javax.swing.JList;
|
---|
9 | import javax.swing.JPanel;
|
---|
10 | import javax.swing.JScrollPane;
|
---|
11 | import javax.swing.ListCellRenderer;
|
---|
12 |
|
---|
13 | /**
|
---|
14 | * A GUI to select a subset from a list of items. Just a bare list view. You
|
---|
15 | * probably want to wrap this into a {@link JScrollPane}.
|
---|
16 | */
|
---|
17 | @SuppressWarnings("serial")
|
---|
18 | public class SubsetSelectionPanel<ItemType> extends JPanel {
|
---|
19 | private SubsetSelectionModel<ItemType> model;
|
---|
20 | private JList<ItemType> list = new JList<ItemType>();
|
---|
21 |
|
---|
22 | public SubsetSelectionPanel(SubsetSelectionModel<ItemType> model) {
|
---|
23 | this.model = model;
|
---|
24 | initPanel();
|
---|
25 | }
|
---|
26 |
|
---|
27 | /**
|
---|
28 | * Set basic panel contents: buttons, list area
|
---|
29 | */
|
---|
30 | private void initPanel() {
|
---|
31 | setLayout(new BorderLayout());
|
---|
32 | list.setModel(new ListModelAdapter<>(model));
|
---|
33 | list.setSelectionModel(new SelectionModelAdapter<ItemType>(model));
|
---|
34 | add(list, BorderLayout.CENTER);
|
---|
35 |
|
---|
36 | }
|
---|
37 |
|
---|
38 | /**
|
---|
39 | * Set the cell renderer for the list items.
|
---|
40 | *
|
---|
41 | * @param renderer
|
---|
42 | * the cell renderer for the list items.
|
---|
43 | */
|
---|
44 | public void setCellRenderer(ListCellRenderer<ItemType> renderer) {
|
---|
45 | list.setCellRenderer(renderer);
|
---|
46 | }
|
---|
47 |
|
---|
48 | /**
|
---|
49 | * simple stub to run this stand-alone (for testing).
|
---|
50 | *
|
---|
51 | * @param args
|
---|
52 | */
|
---|
53 | public static void main(String[] args) {
|
---|
54 | final JFrame gui = new JFrame();
|
---|
55 | gui.setLayout(new BorderLayout());
|
---|
56 | List<String> allItems = Arrays.asList("een", "twee", "drie", "vier");
|
---|
57 | SubsetSelectionModel<String> model = new SubsetSelectionModel<String>(allItems);
|
---|
58 | gui.getContentPane().add(new SubsetSelectionPanel<String>(model), BorderLayout.CENTER);
|
---|
59 | gui.pack();
|
---|
60 | gui.setVisible(true);
|
---|
61 |
|
---|
62 | }
|
---|
63 | }
|
---|