1 package fr.ifremer.quadrige2.ui.swing.common.table.renderer;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27 import javax.swing.table.DefaultTableCellRenderer;
28 import java.util.Collection;
29
30
31
32
33
34
35
36 public class CollectionCellRenderer<T> extends DefaultTableCellRenderer {
37
38 private String separator = ", ";
39
40
41
42
43 public CollectionCellRenderer() {
44 }
45
46
47
48
49
50
51 public CollectionCellRenderer(String separator) {
52 this.separator = separator;
53 }
54
55
56
57
58
59
60
61 protected String getObjectString(T object) {
62 return object.toString();
63 }
64
65
66
67
68
69
70
71 protected String getFinalDisplayString(String text) {
72 return text;
73 }
74
75
76
77
78 protected void onParsingCollectionError() {
79 throw new IllegalArgumentException("the cell content is not a Collection");
80 }
81
82
83 @Override
84 @SuppressWarnings("unchecked")
85 protected void setValue(Object value) {
86 if (value == null) {
87 setText(null);
88 setToolTipText(null);
89 return;
90 }
91 if (value instanceof Collection) {
92 String text = "";
93 for (T object : (Collection<T>) value) {
94 text += getObjectString(object) + separator;
95 }
96 text = getFinalDisplayString(text.substring(0, text.lastIndexOf(separator)));
97 setText(text);
98 setToolTipText(text);
99 } else {
100 onParsingCollectionError();
101 }
102 }
103
104 }