1 package fr.ifremer.reefdb.ui.swing.content.manage.referential.samplingequipment.local;
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.core.dao.referential.StatusCode;
27 import fr.ifremer.quadrige3.ui.swing.table.SwingTable;
28 import fr.ifremer.reefdb.dao.technical.Daos;
29 import fr.ifremer.reefdb.dto.ReefDbBeans;
30 import fr.ifremer.reefdb.dto.referential.SamplingEquipmentDTO;
31 import fr.ifremer.reefdb.service.StatusFilter;
32 import fr.ifremer.reefdb.ui.swing.content.manage.referential.samplingequipment.menu.SamplingEquipmentsMenuUIModel;
33 import fr.ifremer.reefdb.ui.swing.content.manage.referential.samplingequipment.table.SamplingEquipmentsTableModel;
34 import fr.ifremer.reefdb.ui.swing.content.manage.referential.samplingequipment.table.SamplingEquipmentsTableRowModel;
35 import fr.ifremer.reefdb.ui.swing.util.ReefDbUI;
36 import fr.ifremer.reefdb.ui.swing.util.table.AbstractReefDbTableModel;
37 import fr.ifremer.reefdb.ui.swing.util.table.AbstractReefDbTableUIHandler;
38 import org.apache.commons.collections4.CollectionUtils;
39 import org.apache.commons.lang3.StringUtils;
40 import org.apache.commons.logging.Log;
41 import org.apache.commons.logging.LogFactory;
42 import org.jdesktop.swingx.table.TableColumnExt;
43
44 import java.util.List;
45
46 import static org.nuiton.i18n.I18n.t;
47
48
49
50
51 public class SamplingEquipmentsLocalUIHandler extends
52 AbstractReefDbTableUIHandler<SamplingEquipmentsTableRowModel, SamplingEquipmentsLocalUIModel, SamplingEquipmentsLocalUI> {
53
54
55
56
57 private static final Log LOG = LogFactory.getLog(SamplingEquipmentsLocalUIHandler.class);
58
59
60 @Override
61 public void beforeInit(final SamplingEquipmentsLocalUI ui) {
62 super.beforeInit(ui);
63
64
65 final SamplingEquipmentsLocalUIModel model = new SamplingEquipmentsLocalUIModel();
66 ui.setContextValue(model);
67 }
68
69
70 @Override
71 public void afterInit(final SamplingEquipmentsLocalUI ui) {
72 initUI(ui);
73
74
75 ui.getSamplingEquipmentsLocalMenuUI().getHandler().enableContextFilter(false);
76
77
78 ui.getSamplingEquipmentsLocalMenuUI().getHandler().forceLocal(true);
79
80
81 ui.getSamplingEquipmentsLocalMenuUI().getModel().addPropertyChangeListener(SamplingEquipmentsMenuUIModel.PROPERTY_RESULTS, evt -> loadTable((List<SamplingEquipmentDTO>) evt.getNewValue()));
82
83
84 initTable();
85
86 getUI().getReferentialSamplingEquipmentsLocalTableDeleteBouton().setEnabled(false);
87 getUI().getReferentialSamplingEquipmentsLocalTableReplaceBouton().setEnabled(false);
88
89 }
90
91
92
93
94 private void initTable() {
95
96
97 final TableColumnExt mnemonicCol = addColumn(
98 SamplingEquipmentsTableModel.NAME);
99 mnemonicCol.setSortable(true);
100
101
102 final TableColumnExt descriptionCol = addColumn(
103 SamplingEquipmentsTableModel.DESCRIPTION);
104 descriptionCol.setSortable(true);
105
106
107 final TableColumnExt sizeCol = addColumn(
108 newNumberCellEditor(Double.class, false, ReefDbUI.SIGNED_HIGH_DECIMAL_DIGITS_PATTERN),
109 newNumberCellRenderer(10),
110 SamplingEquipmentsTableModel.SIZE);
111 sizeCol.setSortable(true);
112
113
114 final TableColumnExt unitCol = addFilterableComboDataColumnToModel(
115 SamplingEquipmentsTableModel.UNIT,
116 getContext().getReferentialService().getUnits(StatusFilter.ACTIVE),
117 false);
118 unitCol.setSortable(true);
119
120
121
122 addCommentColumn(SamplingEquipmentsTableModel.COMMENT);
123 TableColumnExt creationDateCol = addDatePickerColumnToModel(SamplingEquipmentsTableModel.CREATION_DATE, getConfig().getDateTimeFormat(), false);
124 fixColumnWidth(creationDateCol, 120);
125 TableColumnExt updateDateCol = addDatePickerColumnToModel(SamplingEquipmentsTableModel.UPDATE_DATE, getConfig().getDateTimeFormat(), false);
126 fixColumnWidth(updateDateCol, 120);
127
128
129 final TableColumnExt statusCol = addFilterableComboDataColumnToModel(
130 SamplingEquipmentsTableModel.STATUS,
131 getContext().getReferentialService().getStatus(StatusFilter.LOCAL),
132 false);
133 statusCol.setSortable(true);
134
135 final SamplingEquipmentsTableModel tableModel = new SamplingEquipmentsTableModel(getTable().getColumnModel(), true);
136 getTable().setModel(tableModel);
137
138
139 addExportToCSVAction(t("reefdb.property.samplingEquipments.local"));
140
141
142 initTable(getTable());
143
144 creationDateCol.setVisible(false);
145 updateDateCol.setVisible(false);
146
147 getTable().setVisibleRowCount(5);
148 }
149
150
151
152
153 public void clearTable() {
154 getModel().setBeans(null);
155 }
156
157
158
159
160
161
162 public void loadTable(List<SamplingEquipmentDTO> samplingEquipments) {
163 getModel().setBeans(samplingEquipments);
164 }
165
166
167 @Override
168 public AbstractReefDbTableModel<SamplingEquipmentsTableRowModel> getTableModel() {
169 return (SamplingEquipmentsTableModel) getTable().getModel();
170 }
171
172
173 @Override
174 public SwingTable getTable() {
175 return getUI().getReferentialSamplingEquipmentsLocalTable();
176 }
177
178
179 @Override
180 protected void onRowsAdded(List<SamplingEquipmentsTableRowModel> addedRows) {
181 super.onRowsAdded(addedRows);
182
183 if (addedRows.size() == 1) {
184 SamplingEquipmentsTableRowModel rowModel = addedRows.get(0);
185
186
187 rowModel.setStatus(Daos.getStatus(StatusCode.LOCAL_ENABLE));
188
189 setFocusOnCell(rowModel);
190 }
191
192 }
193
194
195 @Override
196 protected void onRowModified(int rowIndex, SamplingEquipmentsTableRowModel row, String propertyName, Integer propertyIndex, Object oldValue, Object newValue) {
197 super.onRowModified(rowIndex, row, propertyName, propertyIndex, oldValue, newValue);
198 row.setDirty(true);
199 forceRevalidateModel();
200 }
201
202
203 @Override
204 protected boolean isRowValid(SamplingEquipmentsTableRowModel row) {
205 row.getErrors().clear();
206 return !row.isEditable() || super.isRowValid(row) && isSamplingEquipmentValid(row);
207 }
208
209 private boolean isSamplingEquipmentValid(SamplingEquipmentsTableRowModel row) {
210
211
212 if (row.getSize() != null && row.getUnit() == null) {
213 ReefDbBeans.addError(row, t("reefdb.validator.error.field.empty"), SamplingEquipmentsTableRowModel.PROPERTY_UNIT);
214 }
215
216 if (StringUtils.isNotBlank(row.getName())) {
217 boolean duplicateFound = false;
218
219
220 for (SamplingEquipmentsTableRowModel otherRow : getModel().getRows()) {
221 if (row == otherRow) continue;
222 if (row.getName().equalsIgnoreCase(otherRow.getName())) {
223
224 ReefDbBeans.addError(row,
225 t("reefdb.error.alreadyExists.referential", t("reefdb.property.samplingEquipment"), row.getName(), t("reefdb.property.referential.local")),
226 SamplingEquipmentsTableRowModel.PROPERTY_NAME);
227 duplicateFound = true;
228 break;
229 }
230 }
231
232 if (!duplicateFound) {
233
234 List<SamplingEquipmentDTO> existingSamplingEquipments = getContext().getReferentialService().searchSamplingEquipments(StatusFilter.ALL, row.getName(), null);
235 if (CollectionUtils.isNotEmpty(existingSamplingEquipments)) {
236 for (SamplingEquipmentDTO samplingEquipment : existingSamplingEquipments) {
237 if (!samplingEquipment.getId().equals(row.getId())) {
238
239 ReefDbBeans.addError(row,
240 t("reefdb.error.alreadyExists.referential",
241 t("reefdb.property.samplingEquipment"), row.getName(), ReefDbBeans.isLocalStatus(samplingEquipment.getStatus())
242 ? t("reefdb.property.referential.local") : t("reefdb.property.referential.national")),
243 SamplingEquipmentsTableRowModel.PROPERTY_NAME);
244 break;
245 }
246 }
247 }
248 }
249 }
250
251 return row.getErrors().isEmpty();
252 }
253
254 }