1 package fr.ifremer.reefdb.ui.swing.util.table;
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 import fr.ifremer.quadrige3.ui.swing.table.ColumnIdentifier;
27 import fr.ifremer.quadrige3.ui.swing.table.PmfmColumnIdentifier;
28 import fr.ifremer.reefdb.dto.ErrorAware;
29 import fr.ifremer.reefdb.ui.swing.util.ReefDbUIs;
30 import org.apache.commons.collections4.CollectionUtils;
31 import org.jdesktop.swingx.JXTable;
32 import org.jdesktop.swingx.decorator.BorderHighlighter;
33 import org.jdesktop.swingx.decorator.ComponentAdapter;
34 import org.jdesktop.swingx.decorator.HighlightPredicate;
35 import org.nuiton.jaxx.application.swing.table.AbstractApplicationTableModel;
36
37 import javax.swing.JComponent;
38 import javax.swing.border.Border;
39 import java.awt.Component;
40 import java.util.List;
41
42
43
44
45
46
47 public abstract class ErrorHighlighter extends BorderHighlighter {
48
49 private final JXTable table;
50 private final String title;
51 private List<String> message;
52
53
54
55
56
57
58
59
60
61 public abstract List<String> getMessages(ErrorAware bean, String propertyName, Integer pmfmId);
62
63
64
65
66
67
68
69
70 public ErrorHighlighter(JXTable table, Border border, String title) {
71 super();
72
73 this.table = table;
74 this.title = title;
75 setHighlightPredicate(newPredicate());
76 setBorder(border);
77 setCompound(false);
78
79 }
80
81 private HighlightPredicate newPredicate() {
82 return (renderer, adapter) -> {
83 AbstractApplicationTableModel model = (AbstractApplicationTableModel) table.getModel();
84 int modelRow = adapter.convertRowIndexToModel(adapter.row);
85 AbstractReefDbRowUIModel row = (AbstractReefDbRowUIModel) model.getEntry(modelRow);
86 if (row instanceof ErrorAware) {
87
88
89 ColumnIdentifier identifier = (ColumnIdentifier) adapter.getColumnIdentifierAt(adapter.convertColumnIndexToModel(adapter.column));
90 String propertyName = identifier.getPropertyName();
91 Integer pmfmId = null;
92 if (identifier instanceof PmfmColumnIdentifier) {
93 pmfmId = ((PmfmColumnIdentifier) identifier).getPmfmId();
94 }
95
96 message = getMessages((ErrorAware) row, propertyName, pmfmId);
97 return CollectionUtils.isNotEmpty(message);
98 } else {
99 return false;
100 }
101 };
102 }
103
104
105 @Override
106 protected Component doHighlight(Component renderer, ComponentAdapter adapter) {
107 Component component = super.doHighlight(renderer, adapter);
108
109
110 ((JComponent) component).setToolTipText(ReefDbUIs.getHtmlString(title, message));
111
112 return component;
113 }
114
115 }