1 package fr.ifremer.tutti.ui.swing.content.operation.catches;
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.FishingOperation;
26 import fr.ifremer.tutti.service.DecoratorService;
27 import fr.ifremer.tutti.ui.swing.util.AbstractTuttiBeanUIModel;
28 import fr.ifremer.tutti.ui.swing.util.TuttiUI;
29 import fr.ifremer.tutti.ui.swing.util.TuttiUIUtil;
30 import fr.ifremer.tutti.ui.swing.util.table.AbstractTuttiTableUIHandler;
31 import org.apache.commons.collections4.CollectionUtils;
32 import org.apache.commons.lang3.StringUtils;
33 import org.jdesktop.swingx.JXTable;
34 import org.jdesktop.swingx.decorator.HighlightPredicate;
35 import org.jdesktop.swingx.decorator.Highlighter;
36 import org.jdesktop.swingx.sort.TableSortController;
37 import org.jdesktop.swingx.table.TableColumnModelExt;
38 import org.nuiton.decorator.Decorator;
39 import org.nuiton.jaxx.application.swing.table.AbstractApplicationTableModel;
40 import org.nuiton.jaxx.application.swing.table.ColumnIdentifier;
41
42 import javax.swing.JLabel;
43 import javax.swing.JTable;
44 import javax.swing.SwingConstants;
45 import javax.swing.table.TableCellRenderer;
46 import javax.swing.table.TableColumnModel;
47 import javax.swing.table.TableModel;
48 import java.awt.Color;
49 import java.awt.Component;
50 import java.util.ArrayList;
51 import java.util.Collection;
52 import java.util.List;
53
54
55
56
57
58
59
60 public abstract class AbstractTuttiBatchTableUIHandler<R extends AbstractTuttiBeanUIModel, M extends AbstractTuttiBatchUIModel<R, M>, T extends AbstractApplicationTableModel<R>, UI extends TuttiUI<M, ?>> extends AbstractTuttiTableUIHandler<R, M, UI> {
61
62 protected abstract ColumnIdentifier<R> getCommentIdentifier();
63
64 protected abstract ColumnIdentifier<R> getAttachementIdentifier();
65
66 public abstract void selectFishingOperation(FishingOperation bean);
67
68 protected final List<BatchSavedListener> batchSavedListeners = new ArrayList<>();
69
70 protected AbstractTuttiBatchTableUIHandler(String... properties) {
71 super(properties);
72 }
73
74
75
76
77
78
79
80
81 public final void clearTableSelection() {
82
83 JXTable table = getTable();
84
85 if (table.isEditing()) {
86
87
88 table.editingCanceled(null);
89 }
90
91
92 table.clearSelection();
93 }
94
95 public void addBatchSavedListener(BatchSavedListener listener) {
96 batchSavedListeners.add(listener);
97 }
98
99 public void removeBatchSavedListener(BatchSavedListener listener) {
100 batchSavedListeners.remove(listener);
101 }
102
103 public void fireBatchSaved(R row) {
104 for (BatchSavedListener listener : batchSavedListeners) {
105 listener.onBatchSaved(new BatchSavedEvent(getModel(), row));
106 }
107 }
108
109 @Override
110 protected void onRowModified(int rowIndex,
111 R row,
112 String propertyName,
113 Object oldValue,
114 Object newValue) {
115 recomputeRowValidState(row);
116
117 saveSelectedRowIfNeeded();
118 }
119
120 protected void initBatchTable(JXTable table,
121 TableColumnModelExt columnModel,
122 T tableModel) {
123
124 installTableKeyListener(columnModel, table);
125
126 TableSortController<TableModel> sorter = new TableSortController<>(tableModel);
127 sorter.setSortable(false);
128 table.setRowSorter(sorter);
129
130 initTable(table);
131 }
132
133 @Override
134 protected void addHighlighters(final JXTable table) {
135 super.addHighlighters(table);
136 addCommentHighlighter(table, getCommentIdentifier());
137 addAttachementHighlighter(table, getAttachementIdentifier());
138 }
139
140 protected void addCommentHighlighter(JXTable table, ColumnIdentifier identifier) {
141 Color cellWithValueColor = getConfig().getColorCellWithValue();
142
143 Highlighter commentHighlighter = TuttiUIUtil.newBackgroundColorHighlighter(
144 new HighlightPredicate.AndHighlightPredicate(
145 new HighlightPredicate.IdentifierHighlightPredicate(identifier),
146
147 (renderer, adapter) -> {
148 String value = (String) adapter.getValue();
149 return StringUtils.isNotBlank(value);
150 }), cellWithValueColor);
151 table.addHighlighter(commentHighlighter);
152 }
153
154 protected void addAttachementHighlighter(JXTable table, ColumnIdentifier identifier) {
155 Color cellWithValueColor = getConfig().getColorCellWithValue();
156
157 Highlighter attachmentHighlighter = TuttiUIUtil.newBackgroundColorHighlighter(
158 new HighlightPredicate.AndHighlightPredicate(
159 new HighlightPredicate.IdentifierHighlightPredicate(identifier),
160
161 (renderer, adapter) -> {
162 Collection attachments = (Collection) adapter.getValue();
163 return CollectionUtils.isNotEmpty(attachments);
164 }
165 ), cellWithValueColor);
166 table.addHighlighter(attachmentHighlighter);
167 }
168
169 protected void addIdColumnToModel(TableColumnModel model,
170 ColumnIdentifier<R> identifier,
171 JTable table) {
172
173 final TableCellRenderer defaultRenderer = table.getDefaultRenderer(Number.class);
174 final Decorator<String> idDecorator = getDecorator(String.class, DecoratorService.SPACE_EVERY_3_DIGIT);
175
176 TableCellRenderer idTableCellRenderer = (table1, value, isSelected, hasFocus, row, column) -> {
177
178 String text = null;
179 if (value != null) {
180 text = idDecorator.toString(value);
181 }
182
183 Component result = defaultRenderer.getTableCellRendererComponent(table1, text, isSelected, hasFocus, row, column);
184 if (result instanceof JLabel) {
185 JLabel jLabel = (JLabel) result;
186 jLabel.setHorizontalTextPosition(SwingConstants.RIGHT);
187
188 }
189 return result;
190 };
191
192 addColumnToModel(model, null, idTableCellRenderer, identifier);
193
194 }
195 }