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