Project

General

Profile

Revision 1b02a92b

ID1b02a92b283420c565f781a91efe66c8df66899a
Parent f393241e
Child 333b3e62

Added by Andreas Kohlbecker almost 4 years ago

fix #7204 combined edit and create button sending EntityEditorActionEvents

View differences:

src/main/java/eu/etaxonomy/vaadin/component/ToManyRelatedEntitiesComboboxSelect.java
13 13
import org.vaadin.viritin.fields.LazyComboBox.FilterableCountProvider;
14 14
import org.vaadin.viritin.fields.LazyComboBox.FilterablePagingProvider;
15 15

  
16
import com.vaadin.ui.Button.ClickListener;
16
import eu.etaxonomy.cdm.vaadin.event.ToOneRelatedEntityReloader;
17
import eu.etaxonomy.cdm.vaadin.view.name.CachingPresenter;
17 18

  
18 19
/**
20
 * A ToManyRelatedEntitiesListSelect which uses a {@link LazyComboBox} as data field.
21
 *
19 22
 * @author a.kohlbecker
20 23
 * @since Jun 7, 2017
21 24
 *
......
30 33

  
31 34
    private CaptionGenerator<V> captionGenerator;
32 35

  
36
    private CachingPresenter cachingPresenter;
37

  
33 38
    /**
34 39
     * @param itemType
35 40
     * @param fieldType
......
49 54
     */
50 55
    @Override
51 56
    protected LazyComboBox<V> newFieldInstance(V val) throws InstantiationException, IllegalAccessException {
57

  
58
        // TODO use the setEntityFieldInstantiator(EntityFieldInstantiator) instead to inject as instantiator?
52 59
        LazyComboBox<V> field = new LazyComboBox<V>(itemType);
60
        // FIXME using the ToOneRelatedEntityReloader created a dependency to the cdm packages, this should be relaxed!!!
61
        field.addValueChangeListener(new ToOneRelatedEntityReloader(field, cachingPresenter));
53 62

  
54 63
        if(filterablePagingProvider == null || filterableCountProvider == null ||  pageLength == null) {
55 64
            throw new RuntimeException("The filterablePagingProvider, filterableCountProvider and pageLength must be set, use setPagingProviders().");
......
63 72
        return field;
64 73
    }
65 74

  
66
    public void setPagingProviders(FilterablePagingProvider<V> filterablePagingProvider, FilterableCountProvider filterableCountProvider, int pageLength){
75
    public void setPagingProviders(FilterablePagingProvider<V> filterablePagingProvider, FilterableCountProvider filterableCountProvider, int pageLength,
76
            CachingPresenter cachingPresenter){
67 77
        this.filterablePagingProvider = filterablePagingProvider;
68 78
        this.filterableCountProvider = filterableCountProvider;
69 79
        this.pageLength = pageLength;
80
        this.cachingPresenter = cachingPresenter;
70 81
        setInternalValue(null);
71 82
    }
72 83

  
......
77 88
        this.captionGenerator = captionGenerator;
78 89
    }
79 90

  
80

  
81
    /**
82
     * {@inheritDoc}
83
     */
84
    @Override
85
    protected ClickListener newEditButtonClicklistener(LazyComboBox<V> field) {
86
        return super.newEditButtonClicklistener(field);
87
    }
88

  
89
    /**
90
     * {@inheritDoc}
91
     */
92
    @Override
93
    protected ClickListener newAddButtonClicklistener(LazyComboBox<V> field) {
94
        return super.newAddButtonClicklistener(field);
95
    }
96

  
97
    /**
98
     * {@inheritDoc}
99
     */
100
    @Override
101
    protected ClickListener newRemoveButtonClicklistener(LazyComboBox<V> field) {
102
        return super.newRemoveButtonClicklistener(field);
103
    }
104

  
105

  
106

  
107 91
}
src/main/java/eu/etaxonomy/vaadin/component/ToManyRelatedEntitiesListSelect.java
20 20
import com.vaadin.server.FontAwesome;
21 21
import com.vaadin.ui.AbstractField;
22 22
import com.vaadin.ui.Button;
23
import com.vaadin.ui.Button.ClickListener;
24 23
import com.vaadin.ui.Component;
25 24
import com.vaadin.ui.CssLayout;
26 25
import com.vaadin.ui.Field;
27 26
import com.vaadin.ui.GridLayout;
28 27
import com.vaadin.ui.themes.ValoTheme;
29 28

  
29
import eu.etaxonomy.vaadin.event.EditorActionType;
30
import eu.etaxonomy.vaadin.event.EntityEditorActionEvent;
31
import eu.etaxonomy.vaadin.event.EntityEditorActionListener;
30 32
import eu.etaxonomy.vaadin.permission.EditPermissionTester;
31 33

  
32 34
/**
33 35
 * 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. Internally used fields are used in un-buffered mode.
36
 * set. Internally used fields are used in un-buffered mode. The actual instances of the field type <code>F</code> to be used to
37
 * edit or select the entities is created by a implementation of the <code>EntityFieldInstantiator</code>,
38
 * see {@link #setEntityFieldInstantiator(EntityFieldInstantiator).
35 39
 *
36 40
 * @author a.kohlbecker
37 41
 * @since May 11, 2017
......
49 53

  
50 54
    private FieldGroup parentFieldGroup = null;
51 55

  
52
    Boolean valueInitiallyWasNull = null;
56
    private Boolean valueInitiallyWasNull = null;
53 57

  
54 58
    protected boolean isOrderedCollection = false;
55 59

  
60
    /**
61
     * with a button to edit existing and to add new entities
62
     */
56 63
    private boolean withEditButton = false;
57 64

  
58
    private static final int GRID_X_FIELD = 0;
59

  
60 65
    protected boolean addEmptyRowOnInitContent = true;
61 66

  
62
    private int GRID_COLS = 2;
63

  
64
    private GridLayout grid = new GridLayout(GRID_COLS, 1);
65

  
66 67
    private EntityFieldInstantiator<F> entityFieldInstantiator;
67 68

  
68 69
    private EditPermissionTester editPermissionTester;
69 70

  
71
    private EntityEditorActionListener editActionListener;
72

  
73
    /**
74
     * X index of the data field in the grid
75
     */
76
    private static final int GRID_X_FIELD = 0;
77

  
78
    private int GRID_COLS = 2;
79

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

  
70 82
    public  ToManyRelatedEntitiesListSelect(Class<V> itemType, Class<F> fieldType, String caption){
71 83
        this.fieldType = fieldType;
72 84
        this.itemType = itemType;
......
173 185
    }
174 186

  
175 187
    /**
176
     * @param field
177
     * @return
178
     */
179
    protected ClickListener newEditButtonClicklistener(F field) {
180
        return null;
181
    }
182

  
183
    /**
184
     * @return
185
     */
186
    protected ClickListener newAddButtonClicklistener(F field) {
187
        return null;
188
    }
189

  
190
    /**
191
     * @return
192
     */
193
    protected ClickListener newRemoveButtonClicklistener(F field) {
194
        return null;
195
    }
196

  
197
    /**
198 188
     * {@inheritDoc}
199 189
     */
200 190
    @Override
......
334 324

  
335 325
        if(withEditButton){
336 326
            Button edit = new Button(FontAwesome.EDIT);
337
            ClickListener editClickListerner = newEditButtonClicklistener(field);
338
            if(editClickListerner != null){
339
                edit.addClickListener(editClickListerner);
340
            }
327
            edit.addClickListener(e -> editOrCreate(field));
341 328
            buttonGroup.addComponent(edit);
342 329
            addStyledComponents(edit);
343 330
        }
......
366 353
        return buttonGroup;
367 354
    }
368 355

  
356
    /**
357
     * @param e
358
     * @return
359
     */
360
    private void editOrCreate(F field) {
361

  
362
        if(editActionListener == null){
363
            throw new RuntimeException("editActionListener missing");
364
        }
365

  
366
        if(field.getValue() == null){
367
            // create
368
            editActionListener.onEntityEditorActionEvent(new EntityEditorActionEvent<V>(EditorActionType.ADD, null, field));
369
        } else {
370
            // edit
371
            V value = field.getValue();
372
            editActionListener.onEntityEditorActionEvent(new EntityEditorActionEvent<V>(EditorActionType.EDIT, (Class<V>) value.getClass(), value, field));
373
        }
374
    }
375

  
369 376
    private void updateButtonStates(){
370 377

  
371 378
        int fieldsCount = getNestedFields().size();
......
381 388
            if(withEditButton){
382 389
                addButtonIndex++;
383 390
                // edit
384
                buttonGroup.getComponent(0).setEnabled(field.getValue() != null && testEditButtonPermission(field.getValue()));
391
                ((Button)buttonGroup.getComponent(0)).setDescription(field.getValue() == null ? "New" : "Edit");
392
                buttonGroup.getComponent(0).setEnabled(field.getValue() == null
393
                        || field.getValue() != null && testEditButtonPermission(field.getValue()));
385 394
            }
386 395
            // add
387 396
            buttonGroup.getComponent(addButtonIndex).setEnabled(isLast || isOrderedCollection);
388 397
            // remove
389
            buttonGroup.getComponent(addButtonIndex + 1).setEnabled(!isFirst);
398
            buttonGroup.getComponent(addButtonIndex + 1).setEnabled(field.getValue() != null);
390 399
            // up
391 400
            if(isOrderedCollection){
392 401
                buttonGroup.getComponent(addButtonIndex + 2).setEnabled(!isFirst);
......
551 560
        // no default styles
552 561
    }
553 562

  
563

  
564
    /**
565
     * with a button edit existing and to add new entities
566
     */
554 567
    public void withEditButton(boolean withEditButton){
555 568
        this.withEditButton = withEditButton;
556 569
    }
......
609 622
        this.editPermissionTester = editPermissionTester;
610 623
    }
611 624

  
625
    /**
626
     * @return the editActionListener
627
     */
628
    public EntityEditorActionListener getEditActionListener() {
629
        return editActionListener;
630
    }
631

  
632
    /**
633
     * @param editActionListener the editActionListener to set
634
     */
635
    public void setEditActionListener(EntityEditorActionListener editActionListener) {
636
        this.editActionListener = editActionListener;
637
    }
638

  
639

  
612 640
}

Also available in: Unified diff

Add picture from clipboard (Maximum size: 40 MB)