1 package fr.ifremer.tutti.ui.swing.content.protocol.rtp;
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 fr.ifremer.tutti.ui.swing.content.protocol.EditProtocolSpeciesRowModel;
28 import fr.ifremer.tutti.ui.swing.content.protocol.EditProtocolSpeciesTableModel;
29 import jaxx.runtime.SwingUtil;
30
31 import javax.swing.JComponent;
32 import javax.swing.JTable;
33 import javax.swing.UIManager;
34 import javax.swing.table.DefaultTableCellRenderer;
35 import java.awt.Color;
36 import java.awt.Font;
37
38 import static org.nuiton.i18n.I18n.n;
39 import static org.nuiton.i18n.I18n.t;
40
41
42
43
44
45
46 public class RtpCellRenderer extends DefaultTableCellRenderer {
47
48 public static final String TEXT_PATTERN = "<html><body>%s</body></html>";
49
50 private static final long serialVersionUID = 1L;
51
52 private final String withoutRtp;
53
54 private final String withRtp;
55
56 private Font defaultFont;
57
58 private Font selectedFont;
59
60 public RtpCellRenderer() {
61 setHorizontalAlignment(CENTER);
62 setIcon(SwingUtil.createActionIcon("edit-rtp"));
63 this.withoutRtp = n("tutti.editRtp.withoutRtp.tip");
64 this.withRtp = n("tutti.editRtp.withRtp.tip");
65 }
66
67 @Override
68 protected void setValue(Object value) {
69
70 }
71
72 @Override
73 public JComponent getTableCellRendererComponent(JTable table,
74 Object value,
75 boolean isSelected,
76 boolean hasFocus,
77 int row,
78 int column) {
79
80 if (defaultFont == null) {
81 defaultFont = UIManager.getFont("Table.font");
82 selectedFont = defaultFont.deriveFont(Font.BOLD);
83 }
84
85 int rowIndex = table.convertRowIndexToModel(row);
86 EditProtocolSpeciesRowModel rowModel = ((EditProtocolSpeciesTableModel) table.getModel()).getEntry(rowIndex);
87
88 boolean useRtp = rowModel.isUseRtp();
89
90 String toolTipTextValue;
91 String textValue;
92 if (!useRtp) {
93
94
95 toolTipTextValue = "<i>" + t(withoutRtp) + "</i>";
96 textValue = t(withoutRtp);
97
98
99 } else {
100 toolTipTextValue = t("tutti.editRtp.tooltip",
101 rowModel.getRtpMale().getA(), rowModel.getRtpMale().getB(),
102 rowModel.getRtpFemale().getA(), rowModel.getRtpFemale().getB(),
103 rowModel.getRtpUndefined().getA(), rowModel.getRtpUndefined().getB());
104 textValue = t(withRtp);
105
106 }
107 boolean editable = table.isCellEditable(row, column);
108 toolTipTextValue = String.format(TEXT_PATTERN, toolTipTextValue);
109 setEnabled(editable);
110 setText(textValue);
111 setToolTipText(toolTipTextValue);
112 setBackground(null);
113 setForeground(Color.BLACK);
114
115 if (isSelected) {
116 setFont(selectedFont);
117 } else {
118 setFont(defaultFont);
119 }
120
121 return this;
122 }
123 }