Project

General

Profile

Revision 59634b15

ID59634b1594dbf7e715adc59257267ab32fa5884c
Parent d0242e14
Child 3197cc4e

Added by Andreas Kohlbecker almost 3 years ago

fixing loops in ToManyRelatedEntitiesListSelect

View differences:

src/main/java/eu/etaxonomy/vaadin/component/ToManyRelatedEntitiesListSelect.java
9 9
package eu.etaxonomy.vaadin.component;
10 10

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

  
......
79 80

  
80 81
    private GridLayout grid = new GridLayout(GRID_COLS, 1);
81 82

  
83
    private boolean creatingFields;
84

  
82 85
    public  ToManyRelatedEntitiesListSelect(Class<V> itemType, Class<F> fieldType, String caption){
83 86
        this.fieldType = fieldType;
84 87
        this.itemType = itemType;
......
226 229
        }
227 230

  
228 231
        if(newValue != null){
229

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

  
233
            boolean isListType = List.class.isAssignableFrom(getPropertyDataSource().getValue().getClass());
234
            if(valueInitiallyWasNull && isOrderedCollection != isListType){
235
                // need to reset the grid in this case, so that the button groups are created correctly
236
                grid.setRows(1);
237
                grid.removeAllComponents();
238
            }
239
            isOrderedCollection = isListType;
233 240
        }
234 241

  
235
        createFieldsForData();
242
        if(!creatingFields){
243
            createFieldsForData();
244
        }
236 245
    }
237 246

  
238 247
    private void createFieldsForData(){
239 248

  
240
        grid.removeAllComponents();
241
        grid.setRows(1);
242 249

  
250
//        for(int r = 0; r < grid.getRows(); r++){
251
//            grid.getComponent(GRID_X_FIELD, r);
252
//        }
253
        // grid.removeAllComponents();
254
        // grid.setRows(1);
255

  
256
        creatingFields = true;
243 257
        List<V> data = getValue();
244 258
        if(data == null || data.isEmpty()){
245
            addNewRow(0, null);
246
        } else {
247
            int row = 0;
248
            for(V val : data){
249
                row = addNewRow(row, val);
250
            }
259
//            addNewRow(0, null);
260
            data = Arrays.asList((V)null);
261
        }
262
        //else {
263
            for(int row = 0; row < data.size(); row++){
264
                boolean newRowNeeded = true;
265
                if(grid.getRows() > row){
266
                    Component fieldComponent = grid.getComponent(GRID_X_FIELD, row);
267
                    if(fieldComponent != null){
268
                        newRowNeeded = false;
269
                        Field field = (Field)fieldComponent;
270
                        if(data.get(row) != null && field.getValue() != data.get(row)){
271
                            field.setValue(data.get(row));
272
                        }
273
                    }
274

  
275
                }
276
                if(newRowNeeded){
277
                    row = addNewRow(row, data.get(row));
278
                }
279
//            }
251 280
        }
281
        creatingFields = false;
252 282
    }
253 283

  
254 284
    /**
......
278 308
    }
279 309

  
280 310
    /**
281
     * @param row
311
     * @param row the row index, starting from 0.
282 312
     * @param val
283 313
     * @return
284 314
     */
......
397 427
            // remove
398 428
            buttonGroup.getComponent(addButtonIndex + 1).setEnabled(field.getValue() != null);
399 429
            // up
400
            if(isOrderedCollection){
430
            if(isOrderedCollection && buttonGroup.getComponentCount() >  addButtonIndex + 2){
401 431
                buttonGroup.getComponent(addButtonIndex + 2).setEnabled(!isFirst);
402 432
                // down
403 433
                buttonGroup.getComponent(addButtonIndex + 3).setEnabled(!isLast);

Also available in: Unified diff

Add picture from clipboard (Maximum size: 40 MB)