1 package fr.ifremer.tutti.ui.swing.util.attachment;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25 import fr.ifremer.tutti.persistence.entities.data.Attachment;
26 import jaxx.runtime.SwingUtil;
27 import org.apache.commons.collections4.CollectionUtils;
28 import org.nuiton.decorator.Decorator;
29
30 import javax.swing.JComponent;
31 import javax.swing.JTable;
32 import javax.swing.UIManager;
33 import javax.swing.table.DefaultTableCellRenderer;
34 import java.awt.Color;
35 import java.awt.Font;
36 import java.util.List;
37
38 import static org.nuiton.i18n.I18n.n;
39 import static org.nuiton.i18n.I18n.t;
40
41
42
43
44
45
46
47
48 public class AttachmentCellRenderer extends DefaultTableCellRenderer {
49
50 public static final String TEXT_PATTERN = "<html><body>%s</body></html>";
51
52 private static final long serialVersionUID = 1L;
53
54 private final String noneText;
55
56 private final Decorator<Attachment> decorator;
57
58 private Font defaulfFont;
59
60 private Font selectedFont;
61
62 public static AttachmentCellRenderer newRender(Decorator<Attachment> decorator) {
63 return new AttachmentCellRenderer(decorator);
64 }
65
66 protected AttachmentCellRenderer(Decorator<Attachment> decorator) {
67 setHorizontalAlignment(CENTER);
68 setIcon(SwingUtil.createActionIcon("edit-attachment"));
69 this.noneText = n("tutti.attachmentEditor.none.tip");
70 this.decorator = decorator;
71 }
72
73 @Override
74 protected void setValue(Object value) {
75
76 }
77
78 @Override
79 public JComponent getTableCellRendererComponent(JTable table,
80 Object value,
81 boolean isSelected,
82 boolean hasFocus,
83 int row,
84 int column) {
85
86 if (defaulfFont == null) {
87 defaulfFont = UIManager.getFont("Table.font");
88 selectedFont = defaulfFont.deriveFont(Font.BOLD);
89 }
90
91 List<Attachment> attachments = (List<Attachment>) value;
92
93 String toolTipTextValue;
94
95 if (CollectionUtils.isEmpty(attachments)) {
96
97
98 toolTipTextValue = "<i>" + t(noneText) + "</i>";
99
100
101 } else {
102
103 StringBuilder sb = new StringBuilder();
104 for (Attachment attachment : attachments) {
105 sb.append("<br/>").append(decorator.toString(attachment));
106 }
107
108 toolTipTextValue = sb.substring(5);
109 }
110 String textValue = ButtonAttachment.getButtonText(attachments);
111 boolean editable = table.isCellEditable(row, column);
112 toolTipTextValue = String.format(TEXT_PATTERN, toolTipTextValue);
113 setEnabled(editable);
114 setText(textValue);
115 setToolTipText(toolTipTextValue);
116 setBackground(null);
117 setForeground(Color.BLACK);
118
119 if (isSelected) {
120 setFont(selectedFont);
121 } else {
122 setFont(defaulfFont);
123 }
124
125 return this;
126 }
127 }