1 package fr.ifremer.tutti.ui.swing.util.caracteristics;
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 com.google.common.base.Functions;
26 import com.google.common.base.Preconditions;
27 import com.google.common.collect.Lists;
28 import com.google.common.collect.Maps;
29 import fr.ifremer.tutti.persistence.entities.CaracteristicMap;
30 import fr.ifremer.tutti.persistence.entities.referential.Caracteristic;
31 import fr.ifremer.tutti.service.DecoratorService;
32 import fr.ifremer.tutti.ui.swing.util.TuttiBeanMonitor;
33 import fr.ifremer.tutti.ui.swing.util.table.AbstractTuttiTableUIHandler;
34 import jaxx.runtime.swing.editor.bean.BeanFilterableComboBox;
35 import jaxx.runtime.validator.swing.SwingValidator;
36 import org.apache.commons.collections4.MapUtils;
37 import org.apache.commons.logging.Log;
38 import org.apache.commons.logging.LogFactory;
39 import org.jdesktop.swingx.JXTable;
40 import org.jdesktop.swingx.table.DefaultTableColumnModelExt;
41
42 import javax.swing.JComponent;
43 import java.io.Serializable;
44 import java.util.ArrayList;
45 import java.util.List;
46 import java.util.Set;
47
48 import static fr.ifremer.tutti.ui.swing.util.caracteristics.CaracteristicMapCellComponent.CaracteristicMapCellEditor;
49
50
51
52
53
54 public class CaracteristicMapEditorUIHandler
55 extends AbstractTuttiTableUIHandler<CaracteristicMapEditorRowModel, CaracteristicMapEditorUIModel, CaracteristicMapEditorUI> {
56
57 private final static Log log =
58 LogFactory.getLog(CaracteristicMapEditorUIHandler.class);
59
60 protected CaracteristicMapCellEditor caracteristicMapCellEditor;
61
62 public CaracteristicMapEditorUIHandler() {
63 super(CaracteristicMapEditorRowModel.PROPERTY_VALUE);
64 }
65
66
67
68
69
70 @Override
71 public CaracteristicMapEditorTableModel getTableModel() {
72 return (CaracteristicMapEditorTableModel) getTable().getModel();
73 }
74
75 @Override
76 public JXTable getTable() {
77 return ui.getCaracteristicMapEditorTable();
78 }
79
80 @Override
81 protected boolean isRowValid(CaracteristicMapEditorRowModel row) {
82 return row.getKey() != null && row.getValue() != null;
83 }
84
85 @Override
86 protected void onRowModified(int rowIndex, CaracteristicMapEditorRowModel row,
87 String propertyName, Object oldValue, Object newValue) {
88 recomputeRowValidState(row);
89 super.onRowModified(rowIndex, row, propertyName, oldValue, newValue);
90 saveSelectedRowIfNeeded();
91 }
92
93 @Override
94 protected void saveSelectedRowIfRequired(TuttiBeanMonitor<CaracteristicMapEditorRowModel> rowMonitor,
95 CaracteristicMapEditorRowModel row) {
96 if (row.isValid()) {
97
98
99 if (rowMonitor.wasModified()) {
100
101
102 if (log.isInfoEnabled()) {
103 log.info("Row " + row + " was modified, will save it");
104 }
105
106 saveRow(row);
107
108 }
109 }
110 }
111
112 @Override
113 protected void beforeOpenPopup(int rowIndex, int columnIndex) {
114 super.beforeOpenPopup(rowIndex, columnIndex);
115
116 getModel().setRemoveCaracteristicEnabled(rowIndex >= 0);
117 }
118
119
120
121
122
123 @Override
124 public void beforeInit(CaracteristicMapEditorUI ui) {
125 super.beforeInit(ui);
126 CaracteristicMapEditorUIModel model = new CaracteristicMapEditorUIModel();
127 ui.setContextValue(model);
128 }
129
130 @Override
131 public void afterInit(CaracteristicMapEditorUI ui) {
132 initUI(ui);
133
134 initBeanFilterableComboBox(getKeyCombo(), new ArrayList<>(), null);
135 getModel().setAllAvailableCaracteristics(new ArrayList<>(getDataContext().getCaracteristicWithProtected()));
136
137 JXTable table = getTable();
138
139
140 DefaultTableColumnModelExt columnModel =
141 new DefaultTableColumnModelExt();
142
143 {
144
145 addColumnToModel(columnModel,
146 null,
147 newTableCellRender(Caracteristic.class, DecoratorService.CARACTERISTIC_WITH_UNIT),
148 CaracteristicMapEditorTableModel.KEY);
149 }
150
151 {
152
153 addColumnToModel(columnModel,
154 new CaracteristicValueEditor(getContext()),
155 new CaracteristicValueRenderer(getContext()),
156 CaracteristicMapEditorTableModel.VALUE);
157 }
158
159
160 CaracteristicMapEditorTableModel tableModel =
161 new CaracteristicMapEditorTableModel(columnModel);
162
163 table.setModel(tableModel);
164 table.setColumnModel(columnModel);
165
166 initTable(table);
167 }
168
169 @Override
170 protected JComponent getComponentToFocus() {
171 return getUI().getNewRowKey();
172 }
173
174 @Override
175 public void onCloseUI() {
176 if (log.isDebugEnabled()) {
177 log.debug("closing: " + ui);
178 }
179
180 caracteristicMapCellEditor.closeEditor();
181 }
182
183 @Override
184 public SwingValidator<CaracteristicMapEditorUIModel> getValidator() {
185 return null;
186 }
187
188 public CaracteristicMapCellEditor getCaracteristicMapCellEditor() {
189 return caracteristicMapCellEditor;
190 }
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239 public void editBatch(CaracteristicMapColumnRowModel caracteristicMapColumnRowModel,
240 CaracteristicMapCellEditor caracteristicMapCellEditor,
241 Set<Caracteristic> caracteristicsUsed) {
242
243 this.caracteristicMapCellEditor = caracteristicMapCellEditor;
244
245 CaracteristicMapEditorTableModel tableModel = getTableModel();
246 CaracteristicMapEditorUIModel model = getModel();
247
248 CaracteristicMap caracteristicMap = caracteristicMapColumnRowModel.getCaracteristics();
249 if (MapUtils.isEmpty(caracteristicMap)) {
250 caracteristicMap = new CaracteristicMap();
251 caracteristicMap.putAll(Maps.asMap(caracteristicsUsed, Functions.constant((Serializable) null)));
252 }
253 model.setCaracteristicMap(caracteristicMap);
254
255 List<CaracteristicMapEditorRowModel> rows = Lists.newArrayList();
256 List<Caracteristic> caracteristics = Lists.newArrayList(caracteristicMap.keySet());
257
258 List<Caracteristic> availableCaracteristics = model.getAvailableCaracteristics();
259
260 for (Caracteristic key : caracteristics) {
261 CaracteristicMapEditorRowModel newRow = tableModel.createNewRow();
262 newRow.setKey(key);
263 newRow.setValue(caracteristicMap.get(key));
264 rows.add(newRow);
265 }
266
267 model.setRows(rows);
268
269
270 caracteristics.add(caracteristicMapColumnRowModel.getLengthStepCaracteristic());
271
272 List<Caracteristic> caracteristicList = Lists.newArrayList();
273
274 for (Caracteristic caracteristic : availableCaracteristics) {
275 if (!caracteristics.contains(caracteristic)) {
276 caracteristicList.add(caracteristic);
277 }
278 }
279
280 BeanFilterableComboBox<Caracteristic> keyCombo = getKeyCombo();
281 keyCombo.setData(caracteristicList);
282 keyCombo.getHandler().reset();
283 model.setModify(false);
284 }
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329 protected BeanFilterableComboBox<Caracteristic> getKeyCombo() {
330 return ui.getNewRowKey();
331 }
332
333 protected void saveRow(CaracteristicMapEditorRowModel row) {
334
335 if (row.isValid()) {
336 CaracteristicMap caracteristics = getModel().getCaracteristicMap();
337 Preconditions.checkNotNull(caracteristics);
338
339 caracteristics.put(row.getKey(), row.getValue());
340 }
341 }
342 }