1
|
/**
|
2
|
* Copyright (C) 2017 EDIT
|
3
|
* European Distributed Institute of Taxonomy
|
4
|
* http://www.e-taxonomy.eu
|
5
|
*
|
6
|
* The contents of this file are subject to the Mozilla Public License Version 1.1
|
7
|
* See LICENSE.TXT at the top of this package for the full license terms.
|
8
|
*/
|
9
|
package eu.etaxonomy.vaadin.component;
|
10
|
|
11
|
import java.util.ArrayList;
|
12
|
import java.util.Arrays;
|
13
|
import java.util.Collections;
|
14
|
import java.util.HashSet;
|
15
|
import java.util.List;
|
16
|
import java.util.Optional;
|
17
|
import java.util.Set;
|
18
|
|
19
|
import org.apache.log4j.Logger;
|
20
|
|
21
|
import com.vaadin.data.Property;
|
22
|
import com.vaadin.data.Validator;
|
23
|
import com.vaadin.data.Validator.InvalidValueException;
|
24
|
import com.vaadin.data.fieldgroup.FieldGroup;
|
25
|
import com.vaadin.server.FontAwesome;
|
26
|
import com.vaadin.ui.AbstractField;
|
27
|
import com.vaadin.ui.Button;
|
28
|
import com.vaadin.ui.Component;
|
29
|
import com.vaadin.ui.CssLayout;
|
30
|
import com.vaadin.ui.Field;
|
31
|
import com.vaadin.ui.GridLayout;
|
32
|
import com.vaadin.ui.themes.ValoTheme;
|
33
|
|
34
|
import eu.etaxonomy.cdm.vaadin.component.ButtonFactory;
|
35
|
import eu.etaxonomy.vaadin.event.EditorActionType;
|
36
|
import eu.etaxonomy.vaadin.event.EntityEditorActionEvent;
|
37
|
import eu.etaxonomy.vaadin.event.EntityEditorActionListener;
|
38
|
import eu.etaxonomy.vaadin.permission.EditPermissionTester;
|
39
|
|
40
|
/**
|
41
|
* Manages the a collection of items internally as LinkedList<V>. If the Collection to operate on is a Set a Converter must be
|
42
|
* set. Internally used fields are used in un-buffered mode. The actual instances of the field type <code>F</code> to be used to
|
43
|
* edit or select the entities is created by a implementation of the <code>EntityFieldInstantiator</code>,
|
44
|
* see {@link #setEntityFieldInstantiator(EntityFieldInstantiator).
|
45
|
*
|
46
|
* @author a.kohlbecker
|
47
|
* @since May 11, 2017
|
48
|
*
|
49
|
*/
|
50
|
public class ToManyRelatedEntitiesListSelect<V extends Object, F extends AbstractField<V>> extends CompositeCustomField<List<V>> {
|
51
|
|
52
|
private static final long serialVersionUID = 4670707714503199599L;
|
53
|
|
54
|
private static final Logger logger = Logger.getLogger(ToManyRelatedEntitiesListSelect.class);
|
55
|
|
56
|
protected Class<F> fieldType;
|
57
|
|
58
|
protected Class<V> itemType;
|
59
|
|
60
|
private FieldGroup parentFieldGroup = null;
|
61
|
|
62
|
private Boolean valueInitiallyWasNull = null;
|
63
|
|
64
|
protected boolean isOrderedCollection = false;
|
65
|
|
66
|
/**
|
67
|
* with a button to edit existing and to add new entities
|
68
|
*/
|
69
|
private boolean withEditButton = false;
|
70
|
|
71
|
protected boolean addEmptyRowOnInitContent = true;
|
72
|
|
73
|
private EntityFieldInstantiator<F> entityFieldInstantiator;
|
74
|
|
75
|
private EditPermissionTester editPermissionTester;
|
76
|
|
77
|
private EntityEditorActionListener editActionListener;
|
78
|
|
79
|
/**
|
80
|
* X index of the data field in the grid
|
81
|
*/
|
82
|
private static final int GRID_X_FIELD = 0;
|
83
|
|
84
|
private static final int GRID_X_BUTTON_GROUP = 1;
|
85
|
|
86
|
private int GRID_COLS = 2;
|
87
|
|
88
|
private GridLayout grid = new GridLayout(GRID_COLS, 1);
|
89
|
|
90
|
private boolean creatingFields;
|
91
|
|
92
|
private List<Validator> fieldValidators = new ArrayList<>();
|
93
|
|
94
|
public ToManyRelatedEntitiesListSelect(Class<V> itemType, Class<F> fieldType, String caption){
|
95
|
this.fieldType = fieldType;
|
96
|
this.itemType = itemType;
|
97
|
setCaption(caption);
|
98
|
}
|
99
|
|
100
|
/**
|
101
|
* @param field
|
102
|
* @return
|
103
|
*/
|
104
|
protected Integer findRow(F field) {
|
105
|
Integer row = null;
|
106
|
for(int r = 0; r < grid.getRows(); r++){
|
107
|
if(grid.getComponent(GRID_X_FIELD, r).equals(field)){
|
108
|
row = r;
|
109
|
break;
|
110
|
}
|
111
|
}
|
112
|
return row;
|
113
|
}
|
114
|
|
115
|
/**
|
116
|
*
|
117
|
* @return an unmodifiable List of the data Fields
|
118
|
*/
|
119
|
protected List<F> fields() {
|
120
|
Integer row = null;
|
121
|
List<F> fields = new ArrayList<>();
|
122
|
for(int r = 0; r < grid.getRows(); r++){
|
123
|
fields.add((F) grid.getComponent(GRID_X_FIELD, r));
|
124
|
}
|
125
|
return fields;
|
126
|
}
|
127
|
|
128
|
/**
|
129
|
* @param field
|
130
|
* @return
|
131
|
*/
|
132
|
private void addRowAfter(F field) {
|
133
|
|
134
|
List<V> nestedValues = getValueFromNestedFields();
|
135
|
|
136
|
if(isOrderedCollection){
|
137
|
|
138
|
} else {
|
139
|
|
140
|
}
|
141
|
|
142
|
Integer row = findRow(field);
|
143
|
|
144
|
grid.insertRow(row + 1);
|
145
|
|
146
|
// setting null as value for new rows
|
147
|
// see newFieldInstance() !!!
|
148
|
addNewRow(row + 1, null);
|
149
|
updateValue();
|
150
|
|
151
|
}
|
152
|
|
153
|
/**
|
154
|
* @param field
|
155
|
* @return
|
156
|
*/
|
157
|
private void removeRow(F field) {
|
158
|
|
159
|
Integer row = findRow(field);
|
160
|
grid.removeRow(row);
|
161
|
// TODO remove from nested fields
|
162
|
updateValue();
|
163
|
updateButtonStates();
|
164
|
}
|
165
|
|
166
|
|
167
|
/**
|
168
|
* @param field
|
169
|
* @return
|
170
|
*/
|
171
|
private void moveRowDown(F field) {
|
172
|
|
173
|
Integer row = findRow(field);
|
174
|
swapRows(row);
|
175
|
}
|
176
|
|
177
|
/**
|
178
|
* @param field
|
179
|
* @return
|
180
|
*/
|
181
|
private void moveRowUp(F field) {
|
182
|
|
183
|
Integer row = findRow(field);
|
184
|
swapRows(row - 1);
|
185
|
}
|
186
|
|
187
|
/**
|
188
|
* @param i
|
189
|
*/
|
190
|
private void swapRows(int i) {
|
191
|
if(i >= 0 && i + 1 < grid.getRows()){
|
192
|
grid.replaceComponent(grid.getComponent(GRID_X_FIELD, i), grid.getComponent(GRID_X_FIELD, i + 1));
|
193
|
grid.replaceComponent(grid.getComponent(GRID_X_FIELD + 1 , i), grid.getComponent(GRID_X_FIELD + 1, i + 1));
|
194
|
updateButtonStates();
|
195
|
updateValue();
|
196
|
} else {
|
197
|
throw new RuntimeException("Cannot swap rows out of the grid bounds");
|
198
|
}
|
199
|
}
|
200
|
|
201
|
/**
|
202
|
* update Value is only called in turn of UI changes like adding, removing, swapping rows
|
203
|
*/
|
204
|
private void updateValue() {
|
205
|
List<V> nestedValues = getValueFromNestedFields();
|
206
|
List<V> beanList = getValue();
|
207
|
if(beanList != null){
|
208
|
beanList.clear();
|
209
|
beanList.addAll(nestedValues);
|
210
|
}
|
211
|
setInternalValue(beanList);
|
212
|
}
|
213
|
|
214
|
/**
|
215
|
* {@inheritDoc}
|
216
|
*/
|
217
|
@Override
|
218
|
protected Component initContent() {
|
219
|
grid.setColumnExpandRatio(0, 1.0f);
|
220
|
// set internal value to null to add an empty row
|
221
|
|
222
|
if(addEmptyRowOnInitContent){
|
223
|
// add an empty row
|
224
|
setInternalValue(null);
|
225
|
}
|
226
|
return grid;
|
227
|
}
|
228
|
|
229
|
/**
|
230
|
* {@inheritDoc}
|
231
|
*/
|
232
|
@Override
|
233
|
public Class getType() {
|
234
|
return List.class;
|
235
|
}
|
236
|
|
237
|
/**
|
238
|
* {@inheritDoc}
|
239
|
*/
|
240
|
@Override
|
241
|
protected void setInternalValue(List<V> newValue) {
|
242
|
|
243
|
super.setInternalValue(newValue);
|
244
|
|
245
|
if(valueInitiallyWasNull == null){
|
246
|
valueInitiallyWasNull = newValue == null;
|
247
|
}
|
248
|
|
249
|
if(newValue != null){
|
250
|
// newValue is already converted, need to use the original value from the data source
|
251
|
boolean isListType = List.class.isAssignableFrom(getPropertyDataSource().getValue().getClass());
|
252
|
// if(valueInitiallyWasNull && isOrderedCollection != isListType){
|
253
|
if(valueInitiallyWasNull && isOrderedCollection != isListType){
|
254
|
// need to reset the grid in this case, so that the button groups are created correctly
|
255
|
clearRows();
|
256
|
}
|
257
|
isOrderedCollection = isListType;
|
258
|
} else {
|
259
|
clearRows();
|
260
|
}
|
261
|
|
262
|
if(!creatingFields){
|
263
|
createFieldsForData();
|
264
|
}
|
265
|
}
|
266
|
|
267
|
private void clearRows() {
|
268
|
grid.removeAllComponents();
|
269
|
grid.setRows(1);
|
270
|
}
|
271
|
|
272
|
private void createFieldsForData(){
|
273
|
|
274
|
creatingFields = true;
|
275
|
List<V> data = getValue();
|
276
|
if(data == null || data.isEmpty()){
|
277
|
data = Arrays.asList((V)null);
|
278
|
}
|
279
|
for(int row = 0; row < data.size(); row++){
|
280
|
boolean newRowNeeded = true;
|
281
|
if(grid.getRows() > row){
|
282
|
Component fieldComponent = grid.getComponent(GRID_X_FIELD, row);
|
283
|
if(fieldComponent != null){
|
284
|
newRowNeeded = false;
|
285
|
F field = (F)fieldComponent;
|
286
|
if(data.get(row) != null && field.getValue() != data.get(row)){
|
287
|
field.setValue(data.get(row));
|
288
|
}
|
289
|
}
|
290
|
}
|
291
|
if(newRowNeeded){
|
292
|
addNewRow(row, data.get(row));
|
293
|
} else {
|
294
|
// update the editOrCreate buttons
|
295
|
ButtonGroup bg = (ToManyRelatedEntitiesListSelect<V, F>.ButtonGroup) grid.getComponent(GRID_X_BUTTON_GROUP, row);
|
296
|
updateEditOrCreateButton(bg, data.get(row));
|
297
|
}
|
298
|
}
|
299
|
creatingFields = false;
|
300
|
}
|
301
|
|
302
|
/**
|
303
|
* Obtains the List of values directly from the nested fields and ignores the
|
304
|
* value of the <code>propertyDataSource</code>. This is useful when the ToManyRelatedEntitiesListSelect
|
305
|
* is operating on a transient field, in which case the property is considered being read only by vaadin
|
306
|
* so that the commit is doing nothing.
|
307
|
*
|
308
|
* See also {@link AbstractCdmEditorPresenter#handleTransientProperties(DTO bean)}
|
309
|
*
|
310
|
* @return
|
311
|
*/
|
312
|
public List<V> getValueFromNestedFields() {
|
313
|
List<V> nestedValues = new ArrayList<>();
|
314
|
for(F f : getNestedFields()) {
|
315
|
logger.trace(
|
316
|
String.format("getValueFromNestedFields() - %s:%s",
|
317
|
f != null ? f.getClass().getSimpleName() : "null",
|
318
|
f != null && f.getValue() != null ? f.getValue() : "null"
|
319
|
));
|
320
|
V value = f.getValue();
|
321
|
if(f != null /*&& value != null*/){
|
322
|
nestedValues.add(f.getValue());
|
323
|
}
|
324
|
}
|
325
|
return nestedValues;
|
326
|
}
|
327
|
|
328
|
/**
|
329
|
* @param row the row index, starting from 0.
|
330
|
* @param val
|
331
|
* @return
|
332
|
*/
|
333
|
protected int addNewRow(int row, V val) {
|
334
|
try {
|
335
|
F field = newFieldInstance(val);
|
336
|
for(Validator validator : fieldValidators) {
|
337
|
field.addValidator(validator);
|
338
|
}
|
339
|
ButtonGroup buttonGroup = new ButtonGroup(field);
|
340
|
updateEditOrCreateButton(buttonGroup, val);
|
341
|
field.addValueChangeListener(e -> {
|
342
|
if(!creatingFields){
|
343
|
updateValue();
|
344
|
Object value = e.getProperty().getValue();
|
345
|
updateEditOrCreateButton(buttonGroup, value);
|
346
|
fireValueChange(true);
|
347
|
}
|
348
|
});
|
349
|
Property ds = getPropertyDataSource();
|
350
|
if(ds != null){
|
351
|
Object parentVal = ds.getValue();
|
352
|
}
|
353
|
addStyledComponent(field);
|
354
|
|
355
|
// important! all fields must be un-buffered
|
356
|
field.setBuffered(false);
|
357
|
|
358
|
if(getNestedFields().size() == grid.getRows()){
|
359
|
grid.setRows(grid.getRows() + 1);
|
360
|
}
|
361
|
grid.addComponent(field, GRID_X_FIELD, row);
|
362
|
grid.addComponent(buttonGroup, GRID_X_BUTTON_GROUP, row);
|
363
|
updateButtonStates();
|
364
|
nestFieldGroup(field);
|
365
|
row++;
|
366
|
} catch (InstantiationException e) {
|
367
|
// TODO Auto-generated catch block
|
368
|
e.printStackTrace();
|
369
|
} catch (IllegalAccessException e) {
|
370
|
// TODO Auto-generated catch block
|
371
|
e.printStackTrace();
|
372
|
}
|
373
|
return row;
|
374
|
}
|
375
|
|
376
|
/**
|
377
|
* @param buttonGroup
|
378
|
* @param value
|
379
|
*/
|
380
|
public void updateEditOrCreateButton(ButtonGroup buttonGroup, Object value) {
|
381
|
|
382
|
if(!withEditButton || buttonGroup == null || buttonGroup.getEditOrCreateButton() == null){
|
383
|
return;
|
384
|
}
|
385
|
|
386
|
ButtonFactory buttonStyle;
|
387
|
if(value == null){
|
388
|
buttonStyle = ButtonFactory.CREATE_NEW;
|
389
|
} else {
|
390
|
buttonStyle = ButtonFactory.EDIT_ITEM;
|
391
|
}
|
392
|
buttonGroup.getEditOrCreateButton().setIcon(buttonStyle.getIcon());
|
393
|
buttonGroup.getEditOrCreateButton().setDescription(buttonStyle.getDescription());
|
394
|
}
|
395
|
|
396
|
class ButtonGroup extends CssLayout{
|
397
|
|
398
|
private Button editOrCreate;
|
399
|
|
400
|
ButtonGroup (F field){
|
401
|
|
402
|
Button add = ButtonFactory.ADD_ITEM.createButton();
|
403
|
add.setDescription("Add item");
|
404
|
add.addClickListener(e -> addRowAfter(field));
|
405
|
|
406
|
if(withEditButton){
|
407
|
editOrCreate = ButtonFactory.EDIT_ITEM.createButton();
|
408
|
editOrCreate.addClickListener(e -> editOrCreate(field));
|
409
|
addComponent(editOrCreate);
|
410
|
addStyledComponents(editOrCreate);
|
411
|
}
|
412
|
|
413
|
Button remove = ButtonFactory.REMOVE_ITEM.createButton();
|
414
|
remove.setDescription("Remove item");
|
415
|
remove.addClickListener(e -> removeRow(field));
|
416
|
|
417
|
|
418
|
addComponent(add);
|
419
|
addComponent(remove);
|
420
|
addStyledComponents(add, remove);
|
421
|
if(isOrderedCollection){
|
422
|
Button moveUp = new Button(FontAwesome.ARROW_UP);
|
423
|
moveUp.setDescription("Move up");
|
424
|
moveUp.addClickListener(e -> moveRowUp(field));
|
425
|
Button moveDown = new Button(FontAwesome.ARROW_DOWN);
|
426
|
moveDown.addClickListener(e -> moveRowDown(field));
|
427
|
moveDown.setDescription("Move down");
|
428
|
|
429
|
addComponents(moveUp, moveDown);
|
430
|
addStyledComponents(moveUp, moveDown);
|
431
|
}
|
432
|
setStyleName(ValoTheme.LAYOUT_COMPONENT_GROUP);
|
433
|
}
|
434
|
|
435
|
Button getEditOrCreateButton(){
|
436
|
return editOrCreate;
|
437
|
}
|
438
|
|
439
|
}
|
440
|
|
441
|
/**
|
442
|
* @param e
|
443
|
* @return
|
444
|
*/
|
445
|
private void editOrCreate(F field) {
|
446
|
|
447
|
if(editActionListener == null){
|
448
|
throw new RuntimeException("editActionListener missing");
|
449
|
}
|
450
|
|
451
|
if(field.getValue() == null){
|
452
|
// create
|
453
|
editActionListener.onEntityEditorActionEvent(new EntityEditorActionEvent<V>(EditorActionType.ADD, null, field));
|
454
|
} else {
|
455
|
// edit
|
456
|
V value = field.getValue();
|
457
|
editActionListener.onEntityEditorActionEvent(new EntityEditorActionEvent<V>(EditorActionType.EDIT, (Class<V>) value.getClass(), value, field));
|
458
|
}
|
459
|
}
|
460
|
|
461
|
private void updateButtonStates(){
|
462
|
|
463
|
boolean isWritable = !getState().readOnly;
|
464
|
int fieldsCount = getNestedFields().size();
|
465
|
|
466
|
for(int row = 0; row < fieldsCount; row++){
|
467
|
|
468
|
boolean isFirst = row == 0;
|
469
|
boolean isLast = row == fieldsCount - 1;
|
470
|
|
471
|
F field = (F) grid.getComponent(GRID_X_FIELD, row);
|
472
|
CssLayout buttonGroup = (CssLayout) grid.getComponent(GRID_X_FIELD + 1, row);
|
473
|
|
474
|
int addButtonIndex = 0;
|
475
|
if(withEditButton){
|
476
|
addButtonIndex++;
|
477
|
// edit
|
478
|
Button editCreateButton = ((Button)buttonGroup.getComponent(0));
|
479
|
editCreateButton.setDescription(field.getValue() == null ? "New" : "Edit");
|
480
|
editCreateButton.setEnabled(isWritable && (field.getValue() == null
|
481
|
|| field.getValue() != null && testEditButtonPermission(field.getValue())));
|
482
|
}
|
483
|
// add
|
484
|
buttonGroup.getComponent(addButtonIndex).setEnabled(isWritable && (isLast || isOrderedCollection));
|
485
|
// remove
|
486
|
// can be always true, removing the last entry causes an new empty entry to be added.
|
487
|
buttonGroup.getComponent(addButtonIndex + 1).setEnabled(isWritable);
|
488
|
// up
|
489
|
if(isOrderedCollection && buttonGroup.getComponentCount() > addButtonIndex + 2){
|
490
|
buttonGroup.getComponent(addButtonIndex + 2).setEnabled(isWritable && !isFirst);
|
491
|
// down
|
492
|
buttonGroup.getComponent(addButtonIndex + 3).setEnabled(isWritable && !isLast);
|
493
|
}
|
494
|
}
|
495
|
}
|
496
|
|
497
|
/**
|
498
|
* @param field
|
499
|
* @return
|
500
|
*/
|
501
|
protected boolean testEditButtonPermission(Object rowValue) {
|
502
|
if(editPermissionTester != null) {
|
503
|
return editPermissionTester.userHasEditPermission(rowValue);
|
504
|
} else {
|
505
|
return true;
|
506
|
}
|
507
|
}
|
508
|
|
509
|
|
510
|
protected List<F> getNestedFields(){
|
511
|
|
512
|
List<F> nestedFields = new ArrayList<>(grid.getRows());
|
513
|
for(int r = 0; r < grid.getRows(); r++){
|
514
|
F f = (F) grid.getComponent(GRID_X_FIELD, r);
|
515
|
if(f == null){
|
516
|
logger.debug(String.format("NULL field at %d,%d", GRID_X_FIELD, r));
|
517
|
} else {
|
518
|
logger.trace(String.format("field " + f.hashCode() + " at %d,%d", GRID_X_FIELD, r) + ", value: " + f.getValue());
|
519
|
nestedFields.add(f);
|
520
|
}
|
521
|
}
|
522
|
return Collections.unmodifiableList(nestedFields);
|
523
|
}
|
524
|
|
525
|
/**
|
526
|
*
|
527
|
* @param val
|
528
|
* @return
|
529
|
* @throws InstantiationException
|
530
|
* @throws IllegalAccessException
|
531
|
*/
|
532
|
protected F newFieldInstance(V val) throws InstantiationException, IllegalAccessException {
|
533
|
|
534
|
F field;
|
535
|
if(entityFieldInstantiator != null){
|
536
|
field = entityFieldInstantiator.createNewInstance();
|
537
|
} else {
|
538
|
field = fieldType.newInstance();
|
539
|
}
|
540
|
|
541
|
field.setWidth(100, Unit.PERCENTAGE);
|
542
|
field.setValue(val);
|
543
|
|
544
|
// TODO
|
545
|
// when passing null as value the field must take care of creating a new
|
546
|
// instance by overriding setValue() in future we could improve this by passing a
|
547
|
// NewInstanceFactory to this class
|
548
|
return field;
|
549
|
}
|
550
|
|
551
|
/**
|
552
|
* Adds the validator to the list of validators which
|
553
|
* are applied to new fields and adds the validator to
|
554
|
* existing fields
|
555
|
*
|
556
|
* @param validator
|
557
|
*/
|
558
|
public void addFieldValidator(Validator validator){
|
559
|
fieldValidators.add(validator);
|
560
|
for(F field : fields()) {
|
561
|
field.addValidator(validator);
|
562
|
}
|
563
|
}
|
564
|
|
565
|
/**
|
566
|
* removes the validator from the list of validators which
|
567
|
* are applied to new fields and removes the validator from
|
568
|
* existing fields
|
569
|
*
|
570
|
* @param validator
|
571
|
*/
|
572
|
public void removeFieldValidator(Validator validator){
|
573
|
fieldValidators.remove(validator);
|
574
|
for(F field : fields()) {
|
575
|
field.removeValidator(validator);
|
576
|
}
|
577
|
}
|
578
|
|
579
|
/**
|
580
|
* @return a unmodifialble List of the fieldValidators
|
581
|
*/
|
582
|
public List<Validator> getFieldValidators() {
|
583
|
return Collections.unmodifiableList(fieldValidators);
|
584
|
}
|
585
|
|
586
|
/**
|
587
|
* Handle the data binding of the sub fields. Sub-fields can either be composite editor fields
|
588
|
* or 'simple' fields, usually select fields.
|
589
|
* <p>
|
590
|
* Composite editor fields allow editing the nested bean Items and must implement the
|
591
|
* {@link NestedFieldGroup} interface. Simple fields are only instantiated in
|
592
|
* {@link #newFieldInstance(Object)} where the value of the field is set. No further binding is needed
|
593
|
* for these 'simple' fields.
|
594
|
*
|
595
|
* @param field
|
596
|
*/
|
597
|
protected void nestFieldGroup(F field) {
|
598
|
if(NestedFieldGroup.class.isAssignableFrom(fieldType) && parentFieldGroup != null){
|
599
|
((NestedFieldGroup)field).registerParentFieldGroup(parentFieldGroup);
|
600
|
}
|
601
|
}
|
602
|
|
603
|
/**
|
604
|
* {@inheritDoc}
|
605
|
* <p>
|
606
|
* However, this class has no local fieldGroup but must delegate to the nested NestedFieldGroup
|
607
|
* if there are any. This happens in {@link #nestFieldGroup(AbstractField)}.
|
608
|
* <p>
|
609
|
*/
|
610
|
@Override
|
611
|
public Optional<FieldGroup> getFieldGroup() {
|
612
|
return Optional.empty();
|
613
|
}
|
614
|
|
615
|
/**
|
616
|
* This ToMany-CompositeCustomField has no own fields and this no local fieldGroup (see {@link #getFieldGroup()})
|
617
|
* which allow changing data. Editing of the list items is delegated to
|
618
|
* a list of sub-fields which are responsible for editing and committing the changes.
|
619
|
* Therefore the <code>parentFieldGroup</code> is only stored in a local field so that it can
|
620
|
* be passed to per item fields in {@link #nestFieldGroup}
|
621
|
*
|
622
|
* {@inheritDoc}
|
623
|
*/
|
624
|
@Override
|
625
|
public void registerParentFieldGroup(FieldGroup parent) {
|
626
|
parentFieldGroup = parent;
|
627
|
}
|
628
|
|
629
|
/**
|
630
|
* {@inheritDoc}
|
631
|
*/
|
632
|
@Override
|
633
|
public void commit() throws SourceException, InvalidValueException {
|
634
|
|
635
|
List<F> nestedFields = getNestedFields();
|
636
|
Set<F> emptyFields = new HashSet<>();
|
637
|
for(F f : nestedFields){
|
638
|
f.commit();
|
639
|
if(f.getValue() == null){
|
640
|
emptyFields.add(f);
|
641
|
}
|
642
|
}
|
643
|
for(F deleteF : emptyFields){
|
644
|
removeRow(deleteF);
|
645
|
}
|
646
|
/*
|
647
|
List<V> list = (List<V>) getPropertyDataSource().getValue();
|
648
|
|
649
|
Person p = Person.NewInstance();
|
650
|
p.setTitleCache("Hacky", true);
|
651
|
list.add((V) p);
|
652
|
|
653
|
List<V> clonedList = new ArrayList<>(list);
|
654
|
list.clear();
|
655
|
for(V value : clonedList){
|
656
|
if(value != null){
|
657
|
list.add(value);
|
658
|
}
|
659
|
}
|
660
|
//
|
661
|
*/
|
662
|
// calling super.commit() is useless if operating on a transient property!!
|
663
|
super.commit();
|
664
|
// if(getValue().isEmpty() && valueInitiallyWasNull){
|
665
|
// setPropertyDataSource(null);
|
666
|
// }
|
667
|
}
|
668
|
|
669
|
/**
|
670
|
* {@inheritDoc}
|
671
|
*/
|
672
|
@Override
|
673
|
public void setWidth(String width) {
|
674
|
super.setWidth(width);
|
675
|
grid.setWidth(width);
|
676
|
}
|
677
|
|
678
|
@Override
|
679
|
public void setWidth(float width, Unit unit){
|
680
|
super.setWidth(width, unit);
|
681
|
if(grid != null){
|
682
|
grid.setWidth(width, unit);
|
683
|
}
|
684
|
}
|
685
|
|
686
|
/**
|
687
|
* {@inheritDoc}
|
688
|
*/
|
689
|
@Override
|
690
|
protected void addDefaultStyles() {
|
691
|
// no default styles
|
692
|
}
|
693
|
|
694
|
|
695
|
/**
|
696
|
* with a button edit existing and to add new entities
|
697
|
*/
|
698
|
public void withEditButton(boolean withEditButton){
|
699
|
this.withEditButton = withEditButton;
|
700
|
if(getPropertyDataSource() != null) {
|
701
|
throw new RuntimeException("withEditButton must not be changed after the datasource is set.");
|
702
|
}
|
703
|
}
|
704
|
|
705
|
/**
|
706
|
* {@inheritDoc}
|
707
|
*/
|
708
|
@Override
|
709
|
public boolean hasNullContent() {
|
710
|
|
711
|
for(Field f : getNestedFields()){
|
712
|
if(f instanceof CompositeCustomField){
|
713
|
if(!((CompositeCustomField)f).hasNullContent()){
|
714
|
return false;
|
715
|
}
|
716
|
}
|
717
|
}
|
718
|
return true;
|
719
|
}
|
720
|
|
721
|
/**
|
722
|
* @return the enityFieldInstantiator
|
723
|
*/
|
724
|
public EntityFieldInstantiator<F> getEntityFieldInstantiator() {
|
725
|
return entityFieldInstantiator;
|
726
|
}
|
727
|
|
728
|
/**
|
729
|
* @param enityFieldInstantiator the enityFieldInstantiator to set
|
730
|
*/
|
731
|
public void setEntityFieldInstantiator(EntityFieldInstantiator<F> entityFieldInstantiator) {
|
732
|
this.entityFieldInstantiator = entityFieldInstantiator;
|
733
|
}
|
734
|
|
735
|
/**
|
736
|
* {@inheritDoc}
|
737
|
*/
|
738
|
@Override
|
739
|
public void setReadOnly(boolean readOnly) {
|
740
|
super.setReadOnly(readOnly);
|
741
|
updateButtonStates();
|
742
|
}
|
743
|
|
744
|
|
745
|
/**
|
746
|
* @return the editPermissionTester
|
747
|
*/
|
748
|
public EditPermissionTester getEditPermissionTester() {
|
749
|
return editPermissionTester;
|
750
|
}
|
751
|
|
752
|
/**
|
753
|
* @param editPermissionTester the editPermissionTester to set
|
754
|
*/
|
755
|
public void setEditPermissionTester(EditPermissionTester editPermissionTester) {
|
756
|
this.editPermissionTester = editPermissionTester;
|
757
|
}
|
758
|
|
759
|
/**
|
760
|
* @return the editActionListener
|
761
|
*/
|
762
|
public EntityEditorActionListener getEditActionListener() {
|
763
|
return editActionListener;
|
764
|
}
|
765
|
|
766
|
/**
|
767
|
* @param editActionListener the editActionListener to set
|
768
|
*/
|
769
|
public void setEditActionListener(EntityEditorActionListener editActionListener) {
|
770
|
this.editActionListener = editActionListener;
|
771
|
}
|
772
|
|
773
|
}
|