1 | package genius.gui.panels;
|
---|
2 |
|
---|
3 | import java.awt.BorderLayout;
|
---|
4 | import java.awt.Dimension;
|
---|
5 | import java.awt.event.ActionEvent;
|
---|
6 | import java.awt.event.ActionListener;
|
---|
7 | import java.util.Arrays;
|
---|
8 | import java.util.List;
|
---|
9 |
|
---|
10 | import javax.swing.JButton;
|
---|
11 | import javax.swing.JFrame;
|
---|
12 | import javax.swing.JLabel;
|
---|
13 | import javax.swing.JPanel;
|
---|
14 | import javax.swing.JScrollPane;
|
---|
15 | import javax.swing.ListCellRenderer;
|
---|
16 |
|
---|
17 | import genius.core.listener.Listener;
|
---|
18 |
|
---|
19 | /**
|
---|
20 | * {@link SubsetSelectionPanel} with additional border, indicator of number of
|
---|
21 | * selected items, and clear button
|
---|
22 | *
|
---|
23 | */
|
---|
24 | @SuppressWarnings("serial")
|
---|
25 | public class SubsetSelectionPanelPlus<ItemType> extends VflowPanelWithBorder {
|
---|
26 |
|
---|
27 | private JLabel infolabel = new JLabel("0 selected");
|
---|
28 | private SubsetSelectionModel<ItemType> model;
|
---|
29 | private SubsetSelectionPanel<ItemType> listpanel;
|
---|
30 |
|
---|
31 | public SubsetSelectionPanelPlus(final String title, final SubsetSelectionModel<ItemType> model) {
|
---|
32 |
|
---|
33 | super(title);
|
---|
34 | this.model = model;
|
---|
35 | updateInfo();
|
---|
36 |
|
---|
37 | JPanel panel = new JPanel(new BorderLayout());
|
---|
38 | panel.setMaximumSize(new Dimension(999999999, 30));
|
---|
39 | panel.add(infolabel, BorderLayout.WEST);
|
---|
40 | JButton clearButton = new JButton("Clear");
|
---|
41 | panel.add(clearButton, BorderLayout.EAST);
|
---|
42 |
|
---|
43 | listpanel = new SubsetSelectionPanel<>(model);
|
---|
44 | add(new JScrollPane(listpanel));
|
---|
45 | add(panel);
|
---|
46 |
|
---|
47 | model.addListener(new Listener<ItemType>() {
|
---|
48 | @Override
|
---|
49 | public void notifyChange(ItemType data) {
|
---|
50 | updateInfo();
|
---|
51 | }
|
---|
52 | });
|
---|
53 |
|
---|
54 | clearButton.addActionListener(new ActionListener() {
|
---|
55 | @Override
|
---|
56 | public void actionPerformed(ActionEvent e) {
|
---|
57 | model.clear();
|
---|
58 | }
|
---|
59 | });
|
---|
60 |
|
---|
61 | }
|
---|
62 |
|
---|
63 | /**
|
---|
64 | * Set the cell renderer.
|
---|
65 | *
|
---|
66 | * @param renderer
|
---|
67 | * the renderer for list elements
|
---|
68 | *
|
---|
69 | */
|
---|
70 | public void setCellRenderer(ListCellRenderer<ItemType> renderer) {
|
---|
71 | listpanel.setCellRenderer(renderer);
|
---|
72 | }
|
---|
73 |
|
---|
74 | private void updateInfo() {
|
---|
75 | // FIXME invokelater
|
---|
76 | infolabel.setText("" + model.getSelectedItems().size() + " selected");
|
---|
77 | }
|
---|
78 |
|
---|
79 | /**
|
---|
80 | * simple stub to run this stand-alone (for testing).
|
---|
81 | *
|
---|
82 | * @param args
|
---|
83 | */
|
---|
84 | public static void main(String[] args) {
|
---|
85 | final JFrame gui = new JFrame();
|
---|
86 | gui.setLayout(new BorderLayout());
|
---|
87 | List<String> allItems = Arrays.asList("een", "twee", "drie", "vier");
|
---|
88 | SubsetSelectionModel<String> model = new SubsetSelectionModel<String>(allItems);
|
---|
89 | gui.getContentPane().add(new SubsetSelectionPanelPlus<String>("Paneel", model), BorderLayout.CENTER);
|
---|
90 | gui.pack();
|
---|
91 | gui.setVisible(true);
|
---|
92 |
|
---|
93 | }
|
---|
94 |
|
---|
95 | }
|
---|