1 | package genius.gui.renderer;
|
---|
2 |
|
---|
3 | import java.awt.Component;
|
---|
4 |
|
---|
5 | import javax.swing.DefaultListCellRenderer;
|
---|
6 | import javax.swing.JLabel;
|
---|
7 | import javax.swing.JList;
|
---|
8 |
|
---|
9 | import genius.core.repository.PartyRepItem;
|
---|
10 | import genius.core.repository.RepItem;
|
---|
11 | import genius.core.repository.boa.BoaPartyRepItem;
|
---|
12 | import genius.core.repository.boa.BoaRepItem;
|
---|
13 |
|
---|
14 | /**
|
---|
15 | * Renders RepItems, using {@link PartyRepItem#getName()} if possible, or
|
---|
16 | * toString otherwise.
|
---|
17 | */
|
---|
18 |
|
---|
19 | @SuppressWarnings("serial")
|
---|
20 | public class RepItemListCellRenderer extends DefaultListCellRenderer {
|
---|
21 |
|
---|
22 | @Override
|
---|
23 | public Component getListCellRendererComponent(JList<?> list, Object value,
|
---|
24 | int index, boolean isSelected, boolean cellHasFocus) {
|
---|
25 | JLabel comp = (JLabel) super.getListCellRendererComponent(list, value,
|
---|
26 | index, isSelected, cellHasFocus);
|
---|
27 | comp.setText(getText((RepItem) value));
|
---|
28 | return comp;
|
---|
29 | }
|
---|
30 |
|
---|
31 | /**
|
---|
32 | *
|
---|
33 | * HACK static to at least be able to re-use this code in
|
---|
34 | * {@link RepItemTableCellRenderer}.
|
---|
35 | *
|
---|
36 | * @return label to show for a given RepItem.
|
---|
37 | */
|
---|
38 | protected static String getText(RepItem value) {
|
---|
39 | if (value == null) {
|
---|
40 | return "NULL";
|
---|
41 | }
|
---|
42 | if (value instanceof PartyRepItem) {
|
---|
43 | PartyRepItem participant = ((PartyRepItem) value);
|
---|
44 | return participant.getName() + " (" + participant.getDescription()
|
---|
45 | + ")";
|
---|
46 | }
|
---|
47 | if (value instanceof BoaPartyRepItem) {
|
---|
48 | return ((BoaPartyRepItem) value).getName();
|
---|
49 | }
|
---|
50 |
|
---|
51 | if (value instanceof BoaRepItem) {
|
---|
52 | return ((BoaRepItem) value).getName();
|
---|
53 | }
|
---|
54 | return value.toString();
|
---|
55 | }
|
---|
56 | }
|
---|