1 package fr.ifremer.tutti.ui.swing.util.comment;
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 jaxx.runtime.SwingUtil;
26 import org.apache.commons.lang3.StringUtils;
27
28 import javax.swing.JComponent;
29 import javax.swing.JTable;
30 import javax.swing.UIManager;
31 import javax.swing.table.DefaultTableCellRenderer;
32 import java.awt.Font;
33
34 import static org.nuiton.i18n.I18n.n;
35 import static org.nuiton.i18n.I18n.t;
36
37
38
39
40
41
42
43 public class CommentCellRenderer extends DefaultTableCellRenderer {
44
45 public static final String TEXT_PATTERN = "<html><body>%s</body></html>";
46
47 private static final long serialVersionUID = 1L;
48
49 private final String noneText;
50
51 protected Font defaulfFont;
52
53 protected Font selectedFont;
54
55
56 public static CommentCellRenderer newRender() {
57 return new CommentCellRenderer();
58 }
59
60 protected CommentCellRenderer() {
61 setHorizontalAlignment(CENTER);
62 setIcon(SwingUtil.createActionIcon("edit-comment"));
63 this.noneText = n("tutti.commentEditor.none.tip");
64 }
65
66 @Override
67 protected void setValue(Object value) {
68
69 }
70
71 @Override
72 public JComponent getTableCellRendererComponent(JTable table,
73 Object value,
74 boolean isSelected,
75 boolean hasFocus,
76 int row,
77 int column) {
78
79 String comment = (String) value;
80
81 String toolTipTextValue;
82
83 if (StringUtils.isEmpty(comment)) {
84
85
86 toolTipTextValue = "<i>" + t(noneText) + "</i>";
87
88
89 } else {
90
91
92 toolTipTextValue = String.valueOf(value).replace("\n", "<br/>");
93
94 }
95 boolean editable = table.isCellEditable(row, column);
96 toolTipTextValue = String.format(TEXT_PATTERN, toolTipTextValue);
97 setEnabled(editable);
98 setToolTipText(toolTipTextValue);
99 setBackground(null);
100
101 if (defaulfFont == null) {
102 defaulfFont = UIManager.getFont("Table.font");
103 selectedFont = defaulfFont.deriveFont(Font.BOLD);
104 }
105
106 if (isSelected) {
107 setFont(selectedFont);
108 } else {
109 setFont(defaulfFont);
110 }
111
112 return this;
113 }
114 }