Project

General

Profile

« Previous | Next » 

Revision 834db381

Added by Andreas Kohlbecker over 6 years ago

ref #6867 assigning UPDATE,DELETE for new Persons and Teams and fixing bugs related to TeamOrPersonField:

  • prevent from saving empty persons and teams

View differences:

src/main/java/eu/etaxonomy/vaadin/component/ToManyRelatedEntitiesListSelect.java
10 10

  
11 11
import java.util.ArrayList;
12 12
import java.util.Collections;
13
import java.util.LinkedList;
14 13
import java.util.List;
15 14

  
16 15
import org.apache.log4j.Logger;
17 16

  
17
import com.vaadin.data.Property;
18 18
import com.vaadin.data.Validator.InvalidValueException;
19 19
import com.vaadin.data.fieldgroup.FieldGroup;
20
import com.vaadin.data.util.BeanItemContainer;
21 20
import com.vaadin.server.FontAwesome;
22 21
import com.vaadin.ui.AbstractField;
23 22
import com.vaadin.ui.Button;
24 23
import com.vaadin.ui.Button.ClickListener;
25 24
import com.vaadin.ui.Component;
26 25
import com.vaadin.ui.CssLayout;
26
import com.vaadin.ui.Field;
27 27
import com.vaadin.ui.GridLayout;
28 28
import com.vaadin.ui.themes.ValoTheme;
29 29

  
30
import eu.etaxonomy.vaadin.mvp.AbstractCdmEditorPresenter;
31

  
32 30
/**
33 31
 * Manages the a collection of items internally as LinkedList<V>. If the Collection to operate on is a Set a Converter must be
34
 * set. THe internally used fields are used in un-buffered mode.
32
 * set. Internally used fields are used in un-buffered mode.
35 33
 *
36 34
 * @author a.kohlbecker
37 35
 * @since May 11, 2017
......
57 55

  
58 56
    protected boolean addEmptyRowOnInitContent = true;
59 57

  
60
    //NOTE: Managing the item
61
    //      IDs makes BeanContainer more complex to use, but it is necessary in some cases where the
62
    //      equals() or hashCode() methods have been re-implemented in the bean.
63
    //      TODO CdmBase has a re-implemented equals method, do we need to use the BeanContainer instead?
64
    private BeanItemContainer<V> beans;
65

  
66
   //private LinkedList<V> itemList = new LinkedList<>();
67

  
68 58
    private int GRID_COLS = 2;
69 59

  
70 60
    private GridLayout grid = new GridLayout(GRID_COLS, 1);
......
73 63
        this.fieldType = fieldType;
74 64
        this.itemType = itemType;
75 65
        setCaption(caption);
76
        beans = new BeanItemContainer<V>(itemType);
77

  
78 66
    }
79 67

  
80 68
    /**
......
102 90

  
103 91
        grid.insertRow(row + 1);
104 92

  
93
        // setting null as value for new rows
94
        // see newFieldInstance() !!!
105 95
        addNewRow(row + 1, null);
106 96
        updateValue();
107 97

  
......
115 105

  
116 106
        Integer row = findRow(field);
117 107
        grid.removeRow(row);
108
        // TODO remove from nested fields
118 109
        updateValue();
119 110
        updateButtonStates();
120 111

  
......
158 149
     *
159 150
     */
160 151
    protected void updateValue() {
161
        try {
162
            setValue(getValueFromNestedFields());
163
        } catch (ReadOnlyException e){
164
            logger.debug("datasource is readonly, only internal value was updated");
165
        }
152
        List<V> nestedValues = getValueFromNestedFields();
153
        List<V> beanList = getValue();
154
        beanList.clear();
155
        beanList.addAll(nestedValues);
156
        setInternalValue(beanList);
166 157
    }
167 158

  
168 159
    /**
......
220 211
         grid.setRows(1);
221 212

  
222 213
        if(newValue != null){
223
            // FIMXE is it really needed to backup as linked list?
224
            LinkedList<V> linkedList;
225
            if(newValue instanceof LinkedList){
226
                linkedList = (LinkedList<V>) newValue;
227
            } else {
228
                linkedList = new LinkedList<>(newValue);
229
            }
230
            super.setInternalValue(linkedList);
214
            super.setInternalValue(newValue);
231 215

  
232 216
            // newValue is already converted, need to use the original value from the data source
233 217
            isOrderedCollection = List.class.isAssignableFrom(getPropertyDataSource().getValue().getClass());
234 218

  
235
            //FIXME is beans really used?
236
            beans.addAll(linkedList);
237

  
238 219
            int row = 0;
239 220
            if(newValue.size() > 0){
240
                for(V val : linkedList){
221
                for(V val : newValue){
241 222
                    row = addNewRow(row, val);
242 223
                }
243 224
            }
......
257 238
    protected int addNewRow(int row, V val) {
258 239
        try {
259 240
            F field = newFieldInstance(val);
241
            Property ds = getPropertyDataSource();
242
            if(ds != null){
243
                Object parentVal = ds.getValue();
244
            }
260 245
            addStyledComponent(field);
261 246

  
262 247
            // important! all fields must be un-buffered
......
369 354
        F field = fieldType.newInstance();
370 355
        field.setWidth(100, Unit.PERCENTAGE);
371 356
        field.setValue(val);
357
        // TODO
358
        // when passing null as value the field must take care of creating a new
359
        // instance by overriding setValue() in future we could improve this by passing a
360
        // NewInstanceFactory to this class
372 361
        return field;
373 362
    }
374 363

  
......
420 409
     */
421 410
    @Override
422 411
    public void commit() throws SourceException, InvalidValueException {
423
        getNestedFields().forEach(f -> f.commit());
412

  
413
        List<F> nestedFields = getNestedFields();
414
        for(F f : nestedFields){
415
            f.commit();
416

  
417
        }
424 418
        // calling super.commit() is useless if operating on a transient property!!
425 419
        super.commit();
426 420
    }
......
437 431
     */
438 432
    public List<V> getValueFromNestedFields() {
439 433
        List<V> nestedValues = new ArrayList<>();
440
        getNestedFields().forEach(f -> {
441
                logger.trace(String.format("getValueFromNestedFields() - %s:%s",
434
        for(F f : getNestedFields()) {
435
            logger.trace(
436
                    String.format("getValueFromNestedFields() - %s:%s",
442 437
                       f != null ? f.getClass().getSimpleName() : "null",
443
                       f != null ? f.getValue() : "null"
444
                ));
445
                if(f != null){
446
                    nestedValues.add(f.getValue());
447
                }
448
            });
438
                       f != null && f.getValue() != null ? f.getValue() : "null"
439
            ));
440
            V value = f.getValue();
441
            if(f != null && value != null){
442
                nestedValues.add(f.getValue());
443
            }
444
         }
449 445
        return nestedValues;
450 446
    }
451 447

  
......
478 474
        this.withEditButton = withEditButton;
479 475
    }
480 476

  
477
    /**
478
     * {@inheritDoc}
479
     */
480
    @Override
481
    public boolean hasNullContent() {
482

  
483
        for(Field f : getNestedFields()){
484
            if(f instanceof CompositeCustomField){
485
                if(!((CompositeCustomField)f).hasNullContent()){
486
                    return false;
487
                }
488
            }
489
        }
490
        return true;
491
    }
492

  
493

  
494

  
481 495
}

Also available in: Unified diff