Project

General

Profile

Download (24.8 KB) Statistics
| Branch: | Tag: | Revision:
1 6702c7e1 Andreas Kohlbecker
/**
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 8d7c5c55 Andreas Kohlbecker
package eu.etaxonomy.cdm.vaadin.view.name;
10 6702c7e1 Andreas Kohlbecker
11
import java.util.Collection;
12 ff20525a Andreas Kohlbecker
import java.util.EnumSet;
13 810e8605 Andreas Kohlbecker
import java.util.HashMap;
14
import java.util.List;
15
import java.util.Map;
16 0844524a Andreas Kohlbecker
import java.util.UUID;
17 6702c7e1 Andreas Kohlbecker
18 d13ec7ca Andreas Kohlbecker
import org.apache.commons.lang3.BooleanUtils;
19 810e8605 Andreas Kohlbecker
import org.apache.log4j.Level;
20 be4a9789 Andreas Kohlbecker
import org.springframework.context.annotation.Scope;
21 6702c7e1 Andreas Kohlbecker
import org.springframework.security.core.GrantedAuthority;
22
23 810e8605 Andreas Kohlbecker
import com.vaadin.data.Property;
24
import com.vaadin.data.Property.ValueChangeListener;
25 8d14e2c1 Andreas Kohlbecker
import com.vaadin.shared.ui.label.ContentMode;
26 be4a9789 Andreas Kohlbecker
import com.vaadin.spring.annotation.SpringComponent;
27 e90e4ec3 Andreas Kohlbecker
import com.vaadin.ui.AbstractField;
28 6702c7e1 Andreas Kohlbecker
import com.vaadin.ui.Alignment;
29 b6ea271c Andreas Kohlbecker
import com.vaadin.ui.CheckBox;
30 6702c7e1 Andreas Kohlbecker
import com.vaadin.ui.GridLayout;
31 8d14e2c1 Andreas Kohlbecker
import com.vaadin.ui.Label;
32 6702c7e1 Andreas Kohlbecker
import com.vaadin.ui.ListSelect;
33
import com.vaadin.ui.TextField;
34
35 e90e4ec3 Andreas Kohlbecker
import eu.etaxonomy.cdm.model.agent.TeamOrPersonBase;
36 bd93502c Andreas Kohlbecker
import eu.etaxonomy.cdm.model.common.CdmBase;
37 6702c7e1 Andreas Kohlbecker
import eu.etaxonomy.cdm.model.name.Rank;
38 da93b6de Andreas Kohlbecker
import eu.etaxonomy.cdm.model.name.TaxonName;
39 7349cf26 Andreas Kohlbecker
import eu.etaxonomy.cdm.model.reference.Reference;
40 6702c7e1 Andreas Kohlbecker
import eu.etaxonomy.cdm.vaadin.component.common.TeamOrPersonField;
41 7349cf26 Andreas Kohlbecker
import eu.etaxonomy.cdm.vaadin.event.ReferenceEditorAction;
42 333b3e62 Andreas Kohlbecker
import eu.etaxonomy.cdm.vaadin.event.TaxonNameEditorAction;
43 c256784f Andreas Kohlbecker
import eu.etaxonomy.cdm.vaadin.model.name.TaxonNameDTO;
44 877a8283 Andreas Kohlbecker
import eu.etaxonomy.cdm.vaadin.permission.AccessRestrictedView;
45
import eu.etaxonomy.cdm.vaadin.permission.CdmEditDeletePermissionTester;
46 85ddc538 Andreas Kohlbecker
import eu.etaxonomy.cdm.vaadin.util.TeamOrPersonBaseCaptionGenerator;
47 b6ea271c Andreas Kohlbecker
import eu.etaxonomy.cdm.vaadin.util.converter.SetToListConverter;
48 2ffe661a Andreas Kohlbecker
import eu.etaxonomy.vaadin.component.ReloadableLazyComboBox;
49 fcba7787 Andreas Kohlbecker
import eu.etaxonomy.vaadin.component.SwitchableTextField;
50 b6ea271c Andreas Kohlbecker
import eu.etaxonomy.vaadin.component.ToManyRelatedEntitiesComboboxSelect;
51 7349cf26 Andreas Kohlbecker
import eu.etaxonomy.vaadin.component.ToOneRelatedEntityCombobox;
52 333b3e62 Andreas Kohlbecker
import eu.etaxonomy.vaadin.event.EditorActionType;
53 c256784f Andreas Kohlbecker
import eu.etaxonomy.vaadin.mvp.AbstractCdmDTOPopupEditor;
54 6702c7e1 Andreas Kohlbecker
55
/**
56
 * @author a.kohlbecker
57
 * @since May 22, 2017
58
 *
59
 */
60 be4a9789 Andreas Kohlbecker
@SpringComponent
61
@Scope("prototype")
62 c256784f Andreas Kohlbecker
public class TaxonNamePopupEditor extends AbstractCdmDTOPopupEditor<TaxonNameDTO, TaxonName, TaxonNameEditorPresenter> implements TaxonNamePopupEditorView, AccessRestrictedView {
63 6702c7e1 Andreas Kohlbecker
64
    private static final long serialVersionUID = -7037436241474466359L;
65
66
    private final static int GRID_COLS = 4;
67
68 2dd23a3c Andreas Kohlbecker
    private final static int GRID_ROWS = 13;
69 b6ea271c Andreas Kohlbecker
70
    private static final boolean HAS_BASIONYM_DEFAULT = false;
71 6702c7e1 Andreas Kohlbecker
72
    private TextField genusOrUninomialField;
73
74
    private TextField infraGenericEpithetField;
75
76
    private TextField specificEpithetField;
77
78
    private TextField infraSpecificEpithetField;
79
80 fcba7787 Andreas Kohlbecker
    private SwitchableTextField fullTitleCacheFiled;
81
82
    private SwitchableTextField protectedNameCacheField;
83
84 7349cf26 Andreas Kohlbecker
    private ToOneRelatedEntityCombobox<Reference> nomReferenceCombobox;
85
86
    private TextField nomenclaturalReferenceDetail;
87
88 b6ea271c Andreas Kohlbecker
    private TeamOrPersonField exBasionymAuthorshipField;
89
90
    private TeamOrPersonField basionymAuthorshipField;
91
92 333b3e62 Andreas Kohlbecker
    private ToManyRelatedEntitiesComboboxSelect<TaxonName> basionymsComboboxSelect;
93 b6ea271c Andreas Kohlbecker
94
    private CheckBox basionymToggle;
95
96 2dd23a3c Andreas Kohlbecker
    private CheckBox validationToggle;
97
98 b6ea271c Andreas Kohlbecker
    private ListSelect rankSelect;
99
100 1103009f Andreas Kohlbecker
    private TeamOrPersonField combinationAuthorshipField;
101
102
    private TeamOrPersonField exCombinationAuthorshipField;
103
104 ff20525a Andreas Kohlbecker
    private EnumSet<TaxonNamePopupEditorMode> modesActive = EnumSet.noneOf(TaxonNamePopupEditorMode.class);
105 6702c7e1 Andreas Kohlbecker
106 e90e4ec3 Andreas Kohlbecker
    private Boolean isInferredCombinationAuthorship = null;
107
108
    private Boolean isInferredBasionymAuthorship = null;
109
110
    private Boolean isInferredExBasionymAuthorship = null;
111
112 810e8605 Andreas Kohlbecker
    private Map<AbstractField, Property.ValueChangeListener> authorshipUpdateListeners = new HashMap<>();
113
114 6702c7e1 Andreas Kohlbecker
    /**
115
     * @param layout
116
     * @param dtoType
117
     */
118
    public TaxonNamePopupEditor() {
119 c256784f Andreas Kohlbecker
        super(new GridLayout(GRID_COLS, GRID_ROWS), TaxonNameDTO.class);
120 6702c7e1 Andreas Kohlbecker
    }
121
122
    /**
123
     * {@inheritDoc}
124
     */
125
    @Override
126
    public String getWindowCaption() {
127
        return "Name editor";
128
    }
129
130 19ba9005 Andreas Kohlbecker
131
132
    /**
133
     * {@inheritDoc}
134
     */
135
    @Override
136 14fd703d Andreas Kohlbecker
    public int getWindowWidth() {
137 d13ec7ca Andreas Kohlbecker
        return 800;
138 19ba9005 Andreas Kohlbecker
    }
139
140 6702c7e1 Andreas Kohlbecker
    /**
141
     * {@inheritDoc}
142
     */
143
    @Override
144
    public void focusFirst() {
145
        // titleField.focus();
146
147
    }
148
149
    /**
150
     * {@inheritDoc}
151
     */
152
    @Override
153
    protected String getDefaultComponentStyles() {
154
        return "tiny";
155
    }
156
157
    /**
158
     * {@inheritDoc}
159
     */
160
    @Override
161
    protected void initContent() {
162
163
        GridLayout grid = (GridLayout)getFieldLayout();
164 c864dc38 Andreas Kohlbecker
        grid.setSizeFull();
165 0b673f50 Andreas Kohlbecker
        grid.setHideEmptyRowsAndColumns(true);
166 6702c7e1 Andreas Kohlbecker
        grid.setSpacing(true);
167 b6ea271c Andreas Kohlbecker
        grid.setColumnExpandRatio(0, 0.3f);
168
        grid.setColumnExpandRatio(1, 0.3f);
169
        grid.setColumnExpandRatio(2, 0.3f);
170
        grid.setColumnExpandRatio(3, 0.0f);
171 6702c7e1 Andreas Kohlbecker
172
        /*
173
         - nameType: preset, needs to be set in the presenter for new names
174
         - appendedPhrase: -> TODO field
175
         - nomenclaturalMicroReference:  -> TODO field
176
         - nomenclaturalReference ->  field but disabled for REGISTRY
177
         - rank -> SelectField which determines the visiblity of the other fields
178
179
         - fullTitleCache + protectedFullTitleCache -> SwitchableTextField : ADVANCED_MODE
180
         - nameCache + protectedNameCache -> SwitchableTextField : ADVANCED_MODE
181
182
         - homotypicalGroup -> hidden
183
         - typeDesignations -> hidden
184
         - descriptions -> hidden
185
         - taxonBases -> hidden
186
         - registrations -> hidden
187
188
         - relationsFromThisName-> TODO implement later
189
         - relationsToThisName -> TODO implement later
190
191
         - genusOrUninomial -> textField
192
         - infraGenericEpithet  -> textField
193
         - specificEpithet  -> textField
194
         - infraSpecificEpithet  -> textField
195
196
         - authorshipCache + protectedAuthorshipCache -> SwitchableTextField : only ADVANCED_MODE and disabled for REGISTRY
197
         - basionymAuthorship -> field but disabled for REGISTRY, basionym is set as nameRelationShip
198
         - combinationAuthorship -> field but disabled for REGISTRY author team of the reference
199
         - exCombinationAuthorship -> textField
200
         - exBasionymAuthorship -> textField
201
202
         - status -> TODO field
203
         - monomHybrid -> TODO implement hybrids later
204
         - binomHybrid -> TODO implement hybrids later
205
         - trinomHybrid -> TODO implement hybrids later
206
207
         - hybridParentRelations -> TODO implement hybrids later
208
         - hybridChildRelations -> TODO implement hybrids later
209
         - hybridFormula -> TODO implement hybrids later
210
211
         ** ViralName attributes **
212
         - acronym
213
214
         ** BacterialName attributes **
215
         - subGenusAuthorship
216
         - nameApprobation
217
         - breed
218
         - publicationYear
219
         - originalPublicationYear
220
         - cultivarName
221
        */
222
223
        int row = 0;
224 b6ea271c Andreas Kohlbecker
225 fe785c1e Andreas Kohlbecker
        rankSelect = new ListSelect("Rank");
226 6702c7e1 Andreas Kohlbecker
        rankSelect.setNullSelectionAllowed(false);
227
        rankSelect.setRows(1);
228 b6ea271c Andreas Kohlbecker
        rankSelect.setWidth(100, Unit.PERCENTAGE);
229 d13ec7ca Andreas Kohlbecker
        rankSelect.addValueChangeListener(e -> updateFieldVisibility());
230 1103009f Andreas Kohlbecker
        addField(rankSelect, "rank", 0, row, 1, row);
231 6702c7e1 Andreas Kohlbecker
        grid.setComponentAlignment(rankSelect, Alignment.TOP_RIGHT);
232 1103009f Andreas Kohlbecker
233
        basionymToggle = new CheckBox("With basionym");
234
        basionymToggle.setValue(HAS_BASIONYM_DEFAULT);
235 810e8605 Andreas Kohlbecker
236 1103009f Andreas Kohlbecker
        basionymToggle.setStyleName(getDefaultComponentStyles());
237
        grid.addComponent(basionymToggle, 2, row, 3, row);
238
        grid.setComponentAlignment(basionymToggle, Alignment.BOTTOM_LEFT);
239 2dd23a3c Andreas Kohlbecker
240
        row++;
241 b4322671 Andreas Kohlbecker
        validationToggle = new CheckBox("Validation");
242 2dd23a3c Andreas Kohlbecker
        validationToggle.addValueChangeListener(e -> {
243
                boolean enable = e.getProperty().getValue() != null && (Boolean)e.getProperty().getValue();
244
                exCombinationAuthorshipField.setVisible(enable);
245
            });
246
        grid.addComponent(validationToggle, 2, row, 3, row);
247
        grid.setComponentAlignment(validationToggle, Alignment.BOTTOM_LEFT);
248
249 6702c7e1 Andreas Kohlbecker
        row++;
250
        // fullTitleCache
251 fcba7787 Andreas Kohlbecker
        fullTitleCacheFiled = addSwitchableTextField("Full title cache", "fullTitleCache", "protectedFullTitleCache", 0, row, GRID_COLS-1, row);
252
        fullTitleCacheFiled.setWidth(100, Unit.PERCENTAGE);
253 6702c7e1 Andreas Kohlbecker
        row++;
254 fcba7787 Andreas Kohlbecker
        protectedNameCacheField = addSwitchableTextField("Name cache", "nameCache", "protectedNameCache", 0, row, GRID_COLS-1, row);
255
        protectedNameCacheField.setWidth(100, Unit.PERCENTAGE);
256 6702c7e1 Andreas Kohlbecker
        row++;
257
        genusOrUninomialField = addTextField("Genus or uninomial", "genusOrUninomial", 0, row, 1, row);
258
        genusOrUninomialField.setWidth(200, Unit.PIXELS);
259
        infraGenericEpithetField = addTextField("Infrageneric epithet", "infraGenericEpithet", 2, row, 3, row);
260
        infraGenericEpithetField.setWidth(200, Unit.PIXELS);
261
        row++;
262
        specificEpithetField = addTextField("Specific epithet", "specificEpithet", 0, row, 1, row);
263
        specificEpithetField.setWidth(200, Unit.PIXELS);
264
        infraSpecificEpithetField = addTextField("Infraspecific epithet", "infraSpecificEpithet", 2, row, 3, row);
265
        infraSpecificEpithetField.setWidth(200, Unit.PIXELS);
266 222f9662 Andreas Kohlbecker
267
        row++;
268
        grid.addComponent(new Label("Hint: <i>Edit nomenclatural authors in the nomenclatural reference.</i>", ContentMode.HTML), 0, row, 3, row);
269
270 6702c7e1 Andreas Kohlbecker
        row++;
271 85ddc538 Andreas Kohlbecker
        combinationAuthorshipField = new TeamOrPersonField("combination author(s)", TeamOrPersonBaseCaptionGenerator.CacheType.NOMENCLATURAL_TITLE);
272 f65345c7 Andreas Kohlbecker
        combinationAuthorshipField.setWidth(100,  Unit.PERCENTAGE);
273
        addField(combinationAuthorshipField, "combinationAuthorship", 0, row, GRID_COLS-1, row);
274 6702c7e1 Andreas Kohlbecker
275 b6ea271c Andreas Kohlbecker
        row++;
276 7349cf26 Andreas Kohlbecker
        nomReferenceCombobox = new ToOneRelatedEntityCombobox<Reference>("Nomenclatural reference", Reference.class);
277 3b66edef Andreas Kohlbecker
        nomReferenceCombobox.addClickListenerAddEntity(e -> getViewEventBus().publish(
278
                this,
279 333b3e62 Andreas Kohlbecker
                new ReferenceEditorAction(EditorActionType.ADD, null, nomReferenceCombobox, this)
280 7349cf26 Andreas Kohlbecker
                ));
281
        nomReferenceCombobox.addClickListenerEditEntity(e -> {
282
            if(nomReferenceCombobox.getValue() != null){
283 be4a9789 Andreas Kohlbecker
                getViewEventBus().publish(this,
284 7349cf26 Andreas Kohlbecker
                    new ReferenceEditorAction(
285 333b3e62 Andreas Kohlbecker
                            EditorActionType.EDIT,
286 0844524a Andreas Kohlbecker
                            nomReferenceCombobox.getValue().getUuid(),
287 7349cf26 Andreas Kohlbecker
                            nomReferenceCombobox,
288
                            this)
289
                );
290
            }
291
            });
292 810e8605 Andreas Kohlbecker
        logger.setLevel(Level.DEBUG);
293
        nomReferenceCombobox.getSelect().addValueChangeListener(e -> logger.debug("nomReferenceCombobox value changed #1"));
294 a373eee1 Andreas Kohlbecker
        // nomReferenceCombobox.setWidth(300, Unit.PIXELS);
295
        nomReferenceCombobox.setWidth("100%");
296 b6ea271c Andreas Kohlbecker
        addField(nomReferenceCombobox, "nomenclaturalReference", 0, row, 2, row);
297
        nomenclaturalReferenceDetail = addTextField("Reference detail", "nomenclaturalMicroReference", 3, row, 3, row);
298
        nomenclaturalReferenceDetail.setWidth(100, Unit.PIXELS);
299
300 8d14e2c1 Andreas Kohlbecker
        row++;
301 85ddc538 Andreas Kohlbecker
        exCombinationAuthorshipField = new TeamOrPersonField("Ex-combination author(s)", TeamOrPersonBaseCaptionGenerator.CacheType.NOMENCLATURAL_TITLE);
302 8d14e2c1 Andreas Kohlbecker
        exCombinationAuthorshipField.setWidth(100,  Unit.PERCENTAGE);
303
        addField(exCombinationAuthorshipField, "exCombinationAuthorship", 0, row, GRID_COLS-1, row);
304 b393aff8 Andreas Kohlbecker
305 b6ea271c Andreas Kohlbecker
        // Basionym
306
        row++;
307 333b3e62 Andreas Kohlbecker
        basionymsComboboxSelect = new ToManyRelatedEntitiesComboboxSelect<TaxonName>(TaxonName.class, "Basionym");
308
        basionymsComboboxSelect.setConverter(new SetToListConverter<TaxonName>());
309
        addField(basionymsComboboxSelect, "basionyms", 0, row, 3, row);
310
        basionymsComboboxSelect.setWidth(100, Unit.PERCENTAGE);
311
        basionymsComboboxSelect.withEditButton(true);
312 f951fe45 Andreas Kohlbecker
        basionymsComboboxSelect.setEditPermissionTester(new CdmEditDeletePermissionTester());
313 333b3e62 Andreas Kohlbecker
        basionymsComboboxSelect.setEditActionListener(e -> {
314
315
            Object fieldValue = e.getSource().getValue();
316 0844524a Andreas Kohlbecker
            UUID beanUuid = null;
317 333b3e62 Andreas Kohlbecker
            if(fieldValue != null){
318 0844524a Andreas Kohlbecker
                beanUuid = ((CdmBase)fieldValue).getUuid();
319 333b3e62 Andreas Kohlbecker
320
            }
321 2ffe661a Andreas Kohlbecker
            ReloadableLazyComboBox<TaxonName>  lazyCombobox = (ReloadableLazyComboBox<TaxonName>) e.getSource();
322
            getViewEventBus().publish(this, new TaxonNameEditorAction(e.getAction(), beanUuid, lazyCombobox, this));
323 333b3e62 Andreas Kohlbecker
        });
324
        grid.setComponentAlignment(basionymsComboboxSelect, Alignment.TOP_RIGHT);
325 b6ea271c Andreas Kohlbecker
        row++;
326 85ddc538 Andreas Kohlbecker
        basionymAuthorshipField = new TeamOrPersonField("Basionym author(s)", TeamOrPersonBaseCaptionGenerator.CacheType.NOMENCLATURAL_TITLE);
327 b6ea271c Andreas Kohlbecker
        basionymAuthorshipField.setWidth(100,  Unit.PERCENTAGE);
328
        addField(basionymAuthorshipField, "basionymAuthorship", 0, row, GRID_COLS-1, row);
329 7349cf26 Andreas Kohlbecker
        row++;
330 85ddc538 Andreas Kohlbecker
        exBasionymAuthorshipField = new TeamOrPersonField("Ex-basionym author(s)", TeamOrPersonBaseCaptionGenerator.CacheType.NOMENCLATURAL_TITLE);
331 b6ea271c Andreas Kohlbecker
        exBasionymAuthorshipField.setWidth(100,  Unit.PERCENTAGE);
332
        addField(exBasionymAuthorshipField, "exBasionymAuthorship", 0, row, GRID_COLS-1, row);
333
334
335 7349cf26 Andreas Kohlbecker
336 fcba7787 Andreas Kohlbecker
        setAdvancedModeEnabled(true);
337
        registerAdvancedModeComponents(fullTitleCacheFiled, protectedNameCacheField);
338 e90e4ec3 Andreas Kohlbecker
339
        registerAdvancedModeComponents(combinationAuthorshipField);
340
        registerAdvancedModeComponents(basionymAuthorshipField);
341
        registerAdvancedModeComponents(exBasionymAuthorshipField);
342
343 b6ea271c Andreas Kohlbecker
        registerAdvancedModeComponents(combinationAuthorshipField.getCachFields());
344
        registerAdvancedModeComponents(exCombinationAuthorshipField.getCachFields());
345 e90e4ec3 Andreas Kohlbecker
        registerAdvancedModeComponents(basionymAuthorshipField.getCachFields());
346
        registerAdvancedModeComponents(exBasionymAuthorshipField.getCachFields());
347
348 fcba7787 Andreas Kohlbecker
        setAdvancedMode(false);
349 6702c7e1 Andreas Kohlbecker
350
    }
351
352 810e8605 Andreas Kohlbecker
    protected TeamOrPersonBase inferBasiomynAuthors() {
353
        List<TaxonName> basionyms = basionymsComboboxSelect.getValue();
354
        if(!basionyms.isEmpty()){
355
            TaxonName basionym = basionyms.get(0);
356 e90e4ec3 Andreas Kohlbecker
            if(basionym.getCombinationAuthorship() != null){
357
                return basionym.getCombinationAuthorship();
358
            } else if(basionym.getNomenclaturalReference() != null){
359
                return basionym.getNomenclaturalReference().getAuthorship();
360 8d14e2c1 Andreas Kohlbecker
            }
361 ff20525a Andreas Kohlbecker
        }
362 e90e4ec3 Andreas Kohlbecker
        return null;
363
    }
364 b6ea271c Andreas Kohlbecker
365 810e8605 Andreas Kohlbecker
    protected TeamOrPersonBase inferExBasiomynAuthors() {
366
        List<TaxonName> basionyms = basionymsComboboxSelect.getValue();
367
        if(!basionyms.isEmpty()){
368
            TaxonName basionym = basionyms.get(0);
369 e90e4ec3 Andreas Kohlbecker
                return basionym.getExCombinationAuthorship();
370
        }
371
        return null;
372
    }
373
374 810e8605 Andreas Kohlbecker
    protected TeamOrPersonBase inferCombinationAuthors() {
375
        Reference nomRef = nomReferenceCombobox.getValue();
376
        if(nomRef != null) {
377
            return nomRef.getAuthorship();
378 e90e4ec3 Andreas Kohlbecker
        }
379
        return null;
380 6702c7e1 Andreas Kohlbecker
    }
381
382 1103009f Andreas Kohlbecker
    @Override
383
    protected void afterItemDataSourceSet() {
384 c256784f Andreas Kohlbecker
        TaxonNameDTO taxonName = getBean();
385 1103009f Andreas Kohlbecker
        boolean showBasionymSection = taxonName.getBasionyms().size() > 0
386
                || taxonName.getBasionymAuthorship() != null
387
                || taxonName.getExBasionymAuthorship() != null;
388
        basionymToggle.setValue(showBasionymSection);
389 2dd23a3c Andreas Kohlbecker
        basionymToggle.setReadOnly(showBasionymSection);
390 810e8605 Andreas Kohlbecker
        basionymToggle.addValueChangeListener(e -> {
391
            updateAuthorshipFields();
392
            updateFieldVisibility();
393
        });
394 2dd23a3c Andreas Kohlbecker
395
        boolean showExAuthors = taxonName.getExCombinationAuthorship() != null;
396
        validationToggle.setValue(showExAuthors);
397
        validationToggle.setReadOnly(showExAuthors);
398 d13ec7ca Andreas Kohlbecker
        exCombinationAuthorshipField.setVisible(showExAuthors);
399 1103009f Andreas Kohlbecker
400 e90e4ec3 Andreas Kohlbecker
        if(isModeEnabled(TaxonNamePopupEditorMode.AUTOFILL_AUTHORSHIP_DATA)){
401
            updateAuthorshipFields();
402 ff20525a Andreas Kohlbecker
        }
403 e90e4ec3 Andreas Kohlbecker
        if(isModeEnabled(TaxonNamePopupEditorMode.NOMENCLATURALREFERENCE_SECTION_EDITING_ONLY) && getBean().getNomenclaturalReference() != null) {
404 222f9662 Andreas Kohlbecker
            nomReferenceCombobox.setCaption("Selection limited to nomenclatural reference and sections");
405 937c2efc Andreas Kohlbecker
        }
406 e90e4ec3 Andreas Kohlbecker
        if(isModeEnabled(TaxonNamePopupEditorMode.REQUIRE_NOMENCLATURALREFERENCE)) {
407 0b673f50 Andreas Kohlbecker
            if(combinationAuthorshipField.getValue() == null){
408
                nomReferenceCombobox.setRequired(true);
409
            } else {
410
                combinationAuthorshipField.addValueChangeListener(e -> {
411
                    if(e.getProperty().getValue() == null){
412
                        nomReferenceCombobox.setRequired(true);
413
                    }
414
                });
415
            }
416
        }
417 ff20525a Andreas Kohlbecker
418 1103009f Andreas Kohlbecker
    }
419
420 e90e4ec3 Andreas Kohlbecker
    /**
421
     * @param taxonName
422
     */
423
    @Override
424
    public void updateAuthorshipFields() {
425
426 c256784f Andreas Kohlbecker
        TaxonNameDTO taxonName = getBean();
427 e90e4ec3 Andreas Kohlbecker
428
        // ------------- CombinationAuthors
429 810e8605 Andreas Kohlbecker
        isInferredCombinationAuthorship = updateAuthorshipFieldData(
430 e90e4ec3 Andreas Kohlbecker
                taxonName.getCombinationAuthorship(),
431 810e8605 Andreas Kohlbecker
                inferCombinationAuthors(),
432 e90e4ec3 Andreas Kohlbecker
                combinationAuthorshipField,
433
                nomReferenceCombobox.getSelect(),
434
                isInferredCombinationAuthorship);
435
436
437
        // ------------- Basionym and ExBasionymAuthors
438
        if(BooleanUtils.isTrue(basionymToggle.getValue())){
439
440 810e8605 Andreas Kohlbecker
            isInferredBasionymAuthorship = updateAuthorshipFieldData(
441 e90e4ec3 Andreas Kohlbecker
                    taxonName.getBasionymAuthorship(),
442 810e8605 Andreas Kohlbecker
                    inferBasiomynAuthors(),
443 e90e4ec3 Andreas Kohlbecker
                    basionymAuthorshipField,
444
                    basionymsComboboxSelect,
445
                    isInferredBasionymAuthorship
446
                    );
447
448 810e8605 Andreas Kohlbecker
            isInferredExBasionymAuthorship = updateAuthorshipFieldData(
449 e90e4ec3 Andreas Kohlbecker
                    taxonName.getExBasionymAuthorship(),
450 810e8605 Andreas Kohlbecker
                    inferExBasiomynAuthors(),
451 e90e4ec3 Andreas Kohlbecker
                    exBasionymAuthorshipField,
452 810e8605 Andreas Kohlbecker
                    basionymsComboboxSelect,
453 e90e4ec3 Andreas Kohlbecker
                    isInferredExBasionymAuthorship
454
                    );
455 810e8605 Andreas Kohlbecker
456 e90e4ec3 Andreas Kohlbecker
        }
457 810e8605 Andreas Kohlbecker
458
        updateFieldVisibility();
459
460 e90e4ec3 Andreas Kohlbecker
    }
461
462 810e8605 Andreas Kohlbecker
//    /**
463
//     *
464
//     */
465
//    protected void updateAuthorshipFieldsVisibility() {
466
//        combinationAuthorshipField.setVisible(!isInferredCombinationAuthorship);
467
//        if(BooleanUtils.isTrue(basionymToggle.getValue())){
468
//            basionymAuthorshipField.setVisible(!isInferredBasionymAuthorship);
469
//            exBasionymAuthorshipField.setVisible(!isInferredExBasionymAuthorship);
470
//        }
471
//    }
472
473 e90e4ec3 Andreas Kohlbecker
    /**
474
     *
475
     * @param authorship
476
     *    the value of the taxonName authorship field
477
     * @param inferredAuthors
478
     *    the value inferred from other fields which may be set as authorship to the taxon name
479
     * @param authorshipField
480
     *    the ui element to edit the taxonName authorship field
481
     * @param updateTriggerField
482
     * @param lastInferredAuthorshipState
483
     * @return
484
     */
485 810e8605 Andreas Kohlbecker
    protected Boolean updateAuthorshipFieldData(TeamOrPersonBase<?> authorship, TeamOrPersonBase inferredAuthors,
486 e90e4ec3 Andreas Kohlbecker
            TeamOrPersonField authorshipField, AbstractField updateTriggerField,
487
            Boolean lastInferredAuthorshipState) {
488
489
        if(authorship == null){
490
            authorshipField.setValue(inferredAuthors);
491
            lastInferredAuthorshipState = true;
492
        } else {
493
            boolean authorshipMatch = authorship == inferredAuthors;
494
            if(lastInferredAuthorshipState == null){
495 810e8605 Andreas Kohlbecker
                // initialization of authorshipState, this comes only into account when the editor is just being initialized
496 e90e4ec3 Andreas Kohlbecker
                lastInferredAuthorshipState = authorshipMatch;
497
            }
498
            if(!authorshipMatch && lastInferredAuthorshipState){
499
                // update the combinationAuthorshipField to follow changes of the nomenclatural reference in case it was autofilled before
500
                authorshipField.setValue(inferredAuthors);
501
                lastInferredAuthorshipState = true;
502
            }
503
        }
504 810e8605 Andreas Kohlbecker
505
        if(updateTriggerField != null){
506
            // IMPORTANT!
507
            // this ChangeListener must be added at this very late point in the editor lifecycle so that it is called after
508
            // the ToOneRelatedEntityReloader which may have been added to the updateTriggerField in the presenters handleViewEntered() method.
509
            // Otherwise we risk multiple representation problems in the hibernate session
510
            if(!authorshipUpdateListeners.containsKey(updateTriggerField)){
511
                ValueChangeListener listener = e ->  {
512
                    logger.debug(" value changed #2");
513
                    updateAuthorshipFields();
514
                };
515
                updateTriggerField.addValueChangeListener(listener);
516
                authorshipUpdateListeners.put(updateTriggerField, listener);
517
            }
518
        }
519
520 e90e4ec3 Andreas Kohlbecker
        return lastInferredAuthorshipState;
521
    }
522
523
    /**
524
     * @param rank
525
     * @return
526
     */
527
    private void updateFieldVisibility() {
528
529
        // TODO use getField() instead and remove field references
530
531 c256784f Andreas Kohlbecker
        TaxonNameDTO taxonName = getBean();
532 e90e4ec3 Andreas Kohlbecker
        Rank rank = taxonName.getRank();
533
534
        boolean isSpeciesOrBelow = !rank.isHigher(Rank.SPECIES());
535
        Boolean withBasionym = BooleanUtils.isTrue(basionymToggle.getValue());
536
        Boolean withValidation = BooleanUtils.isTrue(validationToggle.getValue());
537
538
        basionymAuthorshipField.setVisible(withBasionym != null && withBasionym);
539
        exBasionymAuthorshipField.setVisible(withBasionym);
540
        basionymsComboboxSelect.setVisible(withBasionym);
541
542 810e8605 Andreas Kohlbecker
        combinationAuthorshipField.setVisible(isInferredCombinationAuthorship != null && !isInferredCombinationAuthorship);
543
        if(BooleanUtils.isTrue(basionymToggle.getValue())){
544
            basionymAuthorshipField.setVisible(isInferredBasionymAuthorship != null && !isInferredBasionymAuthorship);
545
            exBasionymAuthorshipField.setVisible(isInferredExBasionymAuthorship != null && !isInferredExBasionymAuthorship);
546 e90e4ec3 Andreas Kohlbecker
        }
547
548 810e8605 Andreas Kohlbecker
//        if(taxonName != null){
549
//            if(modesActive.contains(TaxonNamePopupEditorMode.AUTOFILL_AUTHORSHIP_DATA)){
550
//            }
551
//        }
552
553 e90e4ec3 Andreas Kohlbecker
        infraSpecificEpithetField.setVisible(rank.isInfraSpecific());
554
        specificEpithetField.setVisible(isSpeciesOrBelow);
555
        infraGenericEpithetField.setVisible(rank.isInfraGenericButNotSpeciesGroup());
556
        genusOrUninomialField.setCaption(isSpeciesOrBelow ? "Genus" : "Uninomial");
557
        exCombinationAuthorshipField.setVisible(isSpeciesOrBelow && withValidation);
558
    }
559
560 810e8605 Andreas Kohlbecker
    @Override
561
    public void cancel() {
562
        authorshipUpdateListeners.keySet().forEach(field -> field.removeValueChangeListener(authorshipUpdateListeners.get(field)));
563
        super.cancel();
564
    }
565
566 6702c7e1 Andreas Kohlbecker
    /**
567
     * {@inheritDoc}
568
     */
569
    @Override
570
    public boolean allowAnonymousAccess() {
571
        return false;
572
    }
573
574
    /**
575
     * {@inheritDoc}
576
     */
577
    @Override
578
    public Collection<Collection<GrantedAuthority>> allowedGrantedAuthorities() {
579
        return null;
580
    }
581
582 7349cf26 Andreas Kohlbecker
    /**
583
     * {@inheritDoc}
584
     */
585
    @Override
586
    public ToOneRelatedEntityCombobox<Reference> getNomReferenceCombobox() {
587
        return nomReferenceCombobox;
588
    }
589
590 b6ea271c Andreas Kohlbecker
    /**
591
     * {@inheritDoc}
592
     */
593
    @Override
594 333b3e62 Andreas Kohlbecker
    public ToManyRelatedEntitiesComboboxSelect<TaxonName> getBasionymComboboxSelect() {
595
        return basionymsComboboxSelect;
596 b6ea271c Andreas Kohlbecker
    }
597
598 fe785c1e Andreas Kohlbecker
    /**
599
     * {@inheritDoc}
600
     */
601
    @Override
602
    public ListSelect getRankSelect() {
603
        return rankSelect;
604
    }
605
606 19ba9005 Andreas Kohlbecker
    /**
607
     * @return the exBasionymAuthorshipField
608
     */
609
    @Override
610
    public TeamOrPersonField getExBasionymAuthorshipField() {
611
        return exBasionymAuthorshipField;
612
    }
613
614
    /**
615
     * @return the basionymAuthorshipField
616
     */
617
    @Override
618
    public TeamOrPersonField getBasionymAuthorshipField() {
619
        return basionymAuthorshipField;
620
    }
621
622
    /**
623
     * @return the combinationAuthorshipField
624
     */
625
    @Override
626
    public TeamOrPersonField getCombinationAuthorshipField() {
627
        return combinationAuthorshipField;
628
    }
629
630
    /**
631
     * @return the exCombinationAuthorshipField
632
     */
633
    @Override
634
    public TeamOrPersonField getExCombinationAuthorshipField() {
635
        return exCombinationAuthorshipField;
636
    }
637
638 ff20525a Andreas Kohlbecker
    @Override
639
    public void enableMode(TaxonNamePopupEditorMode mode){
640 222f9662 Andreas Kohlbecker
            modesActive.add(mode);
641 ff20525a Andreas Kohlbecker
    }
642 19ba9005 Andreas Kohlbecker
643 21c1abac Andreas Kohlbecker
    @Override
644
    public boolean isModeEnabled(TaxonNamePopupEditorMode mode){
645
        return modesActive.contains(mode);
646
    }
647
648 ff20525a Andreas Kohlbecker
    @Override
649
    public void disableMode(TaxonNamePopupEditorMode mode){
650
        modesActive.remove(mode);
651
    }
652 19ba9005 Andreas Kohlbecker
653 222f9662 Andreas Kohlbecker
    @Override
654
    public EnumSet<TaxonNamePopupEditorMode> getModesActive(){
655
        return modesActive;
656
    }
657
658 333b3e62 Andreas Kohlbecker
    @Override
659
    public CheckBox getBasionymToggle() {
660
        return basionymToggle;
661
    }
662
663 2dd23a3c Andreas Kohlbecker
    /**
664
     * {@inheritDoc}
665
     */
666
    @Override
667
    public void setReadOnly(boolean readOnly) {
668
        boolean basionymToggleReadonly = basionymToggle.isReadOnly();
669
        boolean validationToggleReadonly = validationToggle.isReadOnly();
670
        super.setReadOnly(readOnly);
671
        // preserve old readonly states if they were true
672
        if(basionymToggleReadonly){
673
            basionymToggle.setReadOnly(true);
674
        }
675
        if(validationToggleReadonly){
676
            validationToggle.setReadOnly(true);
677
        }
678
    }
679
680
681
682 f65345c7 Andreas Kohlbecker
683 6702c7e1 Andreas Kohlbecker
}