Project

General

Profile

Revision 810e8605

ID810e8605d76502cf98ed49e5b87a97497098b9c1
Parent e90e4ec3
Child 13d24007

Added by Andreas Kohlbecker over 2 years ago

ref #7344 inferring authorship from the editor fields, reducing calls to updateAuthorshipFields(), fixing valueChange event handling

View differences:

src/main/java/eu/etaxonomy/cdm/vaadin/view/name/TaxonNamePopupEditor.java
10 10

  
11 11
import java.util.Collection;
12 12
import java.util.EnumSet;
13
import java.util.HashMap;
14
import java.util.List;
15
import java.util.Map;
13 16

  
14 17
import org.apache.commons.lang3.BooleanUtils;
18
import org.apache.log4j.Level;
15 19
import org.springframework.context.annotation.Scope;
16 20
import org.springframework.security.core.GrantedAuthority;
17 21

  
22
import com.vaadin.data.Property;
23
import com.vaadin.data.Property.ValueChangeListener;
18 24
import com.vaadin.shared.ui.label.ContentMode;
19 25
import com.vaadin.spring.annotation.SpringComponent;
20 26
import com.vaadin.ui.AbstractField;
......
102 108

  
103 109
    private Boolean isInferredExBasionymAuthorship = null;
104 110

  
111
    private Map<AbstractField, Property.ValueChangeListener> authorshipUpdateListeners = new HashMap<>();
112

  
105 113
    /**
106 114
     * @param layout
107 115
     * @param dtoType
......
223 231

  
224 232
        basionymToggle = new CheckBox("With basionym");
225 233
        basionymToggle.setValue(HAS_BASIONYM_DEFAULT);
226
        basionymToggle.addValueChangeListener(e -> {
227
                updateFieldVisibility();
228
                updateAuthorshipFields();
229
            });
234

  
230 235
        basionymToggle.setStyleName(getDefaultComponentStyles());
231 236
        grid.addComponent(basionymToggle, 2, row, 3, row);
232 237
        grid.setComponentAlignment(basionymToggle, Alignment.BOTTOM_LEFT);
......
283 288
                );
284 289
            }
285 290
            });
291
        logger.setLevel(Level.DEBUG);
292
        nomReferenceCombobox.getSelect().addValueChangeListener(e -> logger.debug("nomReferenceCombobox value changed #1"));
286 293
        // nomReferenceCombobox.setWidth(300, Unit.PIXELS);
287 294
        nomReferenceCombobox.setWidth("100%");
288 295
        addField(nomReferenceCombobox, "nomenclaturalReference", 0, row, 2, row);
......
346 353

  
347 354
    }
348 355

  
349
    protected TeamOrPersonBase inferBasiomynAuthors(TaxonName taxonName) {
350
        TaxonName basionym = taxonName.getBasionym();
351
        if(basionym != null){
356
    protected TeamOrPersonBase inferBasiomynAuthors() {
357
        List<TaxonName> basionyms = basionymsComboboxSelect.getValue();
358
        if(!basionyms.isEmpty()){
359
            TaxonName basionym = basionyms.get(0);
352 360
            if(basionym.getCombinationAuthorship() != null){
353 361
                return basionym.getCombinationAuthorship();
354 362
            } else if(basionym.getNomenclaturalReference() != null){
......
358 366
        return null;
359 367
    }
360 368

  
361
    protected TeamOrPersonBase inferExBasiomynAuthors(TaxonName taxonName) {
362
        TaxonName basionym = taxonName.getBasionym();
363
        if(basionym != null){
369
    protected TeamOrPersonBase inferExBasiomynAuthors() {
370
        List<TaxonName> basionyms = basionymsComboboxSelect.getValue();
371
        if(!basionyms.isEmpty()){
372
            TaxonName basionym = basionyms.get(0);
364 373
                return basionym.getExCombinationAuthorship();
365 374
        }
366 375
        return null;
367 376
    }
368 377

  
369
    protected TeamOrPersonBase inferCombinationAuthors(TaxonName taxonName) {
370
        if(taxonName.getNomenclaturalReference() != null) {
371
            return taxonName.getNomenclaturalReference().getAuthorship();
378
    protected TeamOrPersonBase inferCombinationAuthors() {
379
        Reference nomRef = nomReferenceCombobox.getValue();
380
        if(nomRef != null) {
381
            return nomRef.getAuthorship();
372 382
        }
373 383
        return null;
374 384
    }
......
381 391
                || taxonName.getExBasionymAuthorship() != null;
382 392
        basionymToggle.setValue(showBasionymSection);
383 393
        basionymToggle.setReadOnly(showBasionymSection);
394
        basionymToggle.addValueChangeListener(e -> {
395
            updateAuthorshipFields();
396
            updateFieldVisibility();
397
        });
384 398

  
385 399
        boolean showExAuthors = taxonName.getExCombinationAuthorship() != null;
386 400
        validationToggle.setValue(showExAuthors);
......
416 430
        TaxonName taxonName = getBean();
417 431

  
418 432
        // ------------- CombinationAuthors
419
        isInferredCombinationAuthorship = updateAuthorshipField(
433
        isInferredCombinationAuthorship = updateAuthorshipFieldData(
420 434
                taxonName.getCombinationAuthorship(),
421
                inferCombinationAuthors(taxonName),
435
                inferCombinationAuthors(),
422 436
                combinationAuthorshipField,
423 437
                nomReferenceCombobox.getSelect(),
424 438
                isInferredCombinationAuthorship);
......
427 441
        // ------------- Basionym and ExBasionymAuthors
428 442
        if(BooleanUtils.isTrue(basionymToggle.getValue())){
429 443

  
430
            isInferredBasionymAuthorship = updateAuthorshipField(
444
            isInferredBasionymAuthorship = updateAuthorshipFieldData(
431 445
                    taxonName.getBasionymAuthorship(),
432
                    inferBasiomynAuthors(taxonName),
446
                    inferBasiomynAuthors(),
433 447
                    basionymAuthorshipField,
434 448
                    basionymsComboboxSelect,
435 449
                    isInferredBasionymAuthorship
436 450
                    );
437 451

  
438
            isInferredExBasionymAuthorship = updateAuthorshipField(
452
            isInferredExBasionymAuthorship = updateAuthorshipFieldData(
439 453
                    taxonName.getExBasionymAuthorship(),
440
                    inferExBasiomynAuthors(taxonName),
454
                    inferExBasiomynAuthors(),
441 455
                    exBasionymAuthorshipField,
442
                    //no point adding the basionymsComboboxSelect again, it has bee registered already for the BasionymAuthorship above
443
                    null,
456
                    basionymsComboboxSelect,
444 457
                    isInferredExBasionymAuthorship
445 458
                    );
459

  
446 460
        }
461

  
462
        updateFieldVisibility();
463

  
447 464
    }
448 465

  
466
//    /**
467
//     *
468
//     */
469
//    protected void updateAuthorshipFieldsVisibility() {
470
//        combinationAuthorshipField.setVisible(!isInferredCombinationAuthorship);
471
//        if(BooleanUtils.isTrue(basionymToggle.getValue())){
472
//            basionymAuthorshipField.setVisible(!isInferredBasionymAuthorship);
473
//            exBasionymAuthorshipField.setVisible(!isInferredExBasionymAuthorship);
474
//        }
475
//    }
476

  
449 477
    /**
450 478
     *
451 479
     * @param authorship
......
458 486
     * @param lastInferredAuthorshipState
459 487
     * @return
460 488
     */
461
    protected Boolean updateAuthorshipField(TeamOrPersonBase<?> authorship, TeamOrPersonBase inferredAuthors,
489
    protected Boolean updateAuthorshipFieldData(TeamOrPersonBase<?> authorship, TeamOrPersonBase inferredAuthors,
462 490
            TeamOrPersonField authorshipField, AbstractField updateTriggerField,
463 491
            Boolean lastInferredAuthorshipState) {
464 492

  
......
468 496
        } else {
469 497
            boolean authorshipMatch = authorship == inferredAuthors;
470 498
            if(lastInferredAuthorshipState == null){
471
                // initialization of isInferredCombinationAuthorship in case the editor is just being initialized
499
                // initialization of authorshipState, this comes only into account when the editor is just being initialized
472 500
                lastInferredAuthorshipState = authorshipMatch;
473
                if(updateTriggerField != null){
474
                    // IMPORTANT!
475
                    // this ChangeListener must be added at this very late point in the editor lifecycle so that it is called after
476
                    // the ToOneRelatedEntityReloader which may have been added to the updateTriggerField in the presenters handleViewEntered() method.
477
                    // Otherwise we risk multiple representation problems in the hibernate session
478
                    updateTriggerField.addValueChangeListener(e -> updateAuthorshipFields());
479
                }
480 501
            }
481 502
            if(!authorshipMatch && lastInferredAuthorshipState){
482 503
                // update the combinationAuthorshipField to follow changes of the nomenclatural reference in case it was autofilled before
......
484 505
                lastInferredAuthorshipState = true;
485 506
            }
486 507
        }
487
        authorshipField.setVisible(!lastInferredAuthorshipState);
508

  
509
        if(updateTriggerField != null){
510
            // IMPORTANT!
511
            // this ChangeListener must be added at this very late point in the editor lifecycle so that it is called after
512
            // the ToOneRelatedEntityReloader which may have been added to the updateTriggerField in the presenters handleViewEntered() method.
513
            // Otherwise we risk multiple representation problems in the hibernate session
514
            if(!authorshipUpdateListeners.containsKey(updateTriggerField)){
515
                ValueChangeListener listener = e ->  {
516
                    logger.debug(" value changed #2");
517
                    updateAuthorshipFields();
518
                };
519
                updateTriggerField.addValueChangeListener(listener);
520
                authorshipUpdateListeners.put(updateTriggerField, listener);
521
            }
522
        }
523

  
488 524
        return lastInferredAuthorshipState;
489 525
    }
490 526

  
......
507 543
        exBasionymAuthorshipField.setVisible(withBasionym);
508 544
        basionymsComboboxSelect.setVisible(withBasionym);
509 545

  
510
        if(taxonName != null){
511
            if(modesActive.contains(TaxonNamePopupEditorMode.AUTOFILL_AUTHORSHIP_DATA)){
512
                updateAuthorshipFields();
513
            }
546
        combinationAuthorshipField.setVisible(isInferredCombinationAuthorship != null && !isInferredCombinationAuthorship);
547
        if(BooleanUtils.isTrue(basionymToggle.getValue())){
548
            basionymAuthorshipField.setVisible(isInferredBasionymAuthorship != null && !isInferredBasionymAuthorship);
549
            exBasionymAuthorshipField.setVisible(isInferredExBasionymAuthorship != null && !isInferredExBasionymAuthorship);
514 550
        }
515 551

  
552
//        if(taxonName != null){
553
//            if(modesActive.contains(TaxonNamePopupEditorMode.AUTOFILL_AUTHORSHIP_DATA)){
554
//            }
555
//        }
556

  
516 557
        infraSpecificEpithetField.setVisible(rank.isInfraSpecific());
517 558
        specificEpithetField.setVisible(isSpeciesOrBelow);
518 559
        infraGenericEpithetField.setVisible(rank.isInfraGenericButNotSpeciesGroup());
......
520 561
        exCombinationAuthorshipField.setVisible(isSpeciesOrBelow && withValidation);
521 562
    }
522 563

  
564
    @Override
565
    public void cancel() {
566
        authorshipUpdateListeners.keySet().forEach(field -> field.removeValueChangeListener(authorshipUpdateListeners.get(field)));
567
        super.cancel();
568
    }
569

  
523 570
    /**
524 571
     * {@inheritDoc}
525 572
     */
src/main/java/eu/etaxonomy/vaadin/component/ToManyRelatedEntitiesListSelect.java
306 306
            F field = newFieldInstance(val);
307 307
            field.addValueChangeListener(e -> {
308 308
                updateValue();
309
                fireValueChange(true);
309 310
            });
310 311
            Property ds = getPropertyDataSource();
311 312
            if(ds != null){
......
655 656
        this.editActionListener = editActionListener;
656 657
    }
657 658

  
658

  
659 659
}

Also available in: Unified diff

Add picture from clipboard (Maximum size: 40 MB)