Project

General

Profile

Download (3.84 KB) Statistics
| Branch: | Tag: | Revision:
1
/**
2
* Copyright (C) 2020 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
package eu.etaxonomy.taxeditor.editor.descriptiveDataSet.matrix.handler;
10

    
11
import java.util.ArrayList;
12
import java.util.Collection;
13
import java.util.Iterator;
14
import java.util.List;
15

    
16
import javax.inject.Named;
17

    
18
import org.eclipse.e4.core.di.annotations.CanExecute;
19
import org.eclipse.e4.core.di.annotations.Execute;
20
import org.eclipse.e4.ui.di.UISynchronize;
21
import org.eclipse.e4.ui.model.application.ui.basic.MPart;
22
import org.eclipse.e4.ui.model.application.ui.menu.MHandledMenuItem;
23
import org.eclipse.e4.ui.services.IServiceConstants;
24
import org.eclipse.jface.viewers.IStructuredSelection;
25
import org.eclipse.jface.window.Window;
26

    
27
import eu.etaxonomy.cdm.api.service.dto.SpecimenRowWrapperDTO;
28
import eu.etaxonomy.cdm.persistence.dto.SpecimenNodeWrapper;
29
import eu.etaxonomy.cdm.persistence.dto.TaxonNodeDto;
30
import eu.etaxonomy.taxeditor.editor.descriptiveDataSet.matrix.CharacterMatrix;
31
import eu.etaxonomy.taxeditor.editor.descriptiveDataSet.matrix.CharacterMatrixPart;
32
import eu.etaxonomy.taxeditor.editor.descriptiveDataSet.matrix.SpecimenSelectionDialog;
33
import eu.etaxonomy.taxeditor.model.MessagingUtils;
34

    
35
/**
36
 * @author k.luther
37
 * @since May 11, 2020
38
 */
39
public class AddDescriptionHandler {
40
    @Execute
41
    public void execute(@Named(IServiceConstants.ACTIVE_PART)MPart activePart, UISynchronize sync) {
42

    
43
        // dependent on the selection the specimens are filtered
44
        CharacterMatrixPart matrixPart = (CharacterMatrixPart) activePart.getObject();
45
//        if(StoreUtil.promptCheckIsDirty(matrixPart)){
46
//            return;
47
//        }
48
        CharacterMatrix matrix = matrixPart.getMatrix();
49
        IStructuredSelection selection = matrixPart.getSelection();
50
        List<String> treeIndexList = null;
51
        List<String> taxonTitleList = null;
52
        if(selection.size()>0){
53
            treeIndexList = new ArrayList<>();
54
            taxonTitleList = new ArrayList<>();
55
            Iterator iterator = selection.iterator();
56
            while (iterator.hasNext()){
57
                Object element = iterator.next();
58
                if (element instanceof TaxonNodeDto){
59
                    TaxonNodeDto taxonRow = (TaxonNodeDto) element;
60
                    String treeIndex = taxonRow.getTreeIndex();
61
                    treeIndexList.add(treeIndex);
62
                    taxonTitleList.add(taxonRow.getNameCache());
63
                }
64
            }
65

    
66
        }
67

    
68
        SpecimenSelectionDialog dialog = new SpecimenSelectionDialog(matrix.getShell(), matrix, treeIndexList, taxonTitleList);
69
        if(dialog.open()==Window.OK){
70
            Collection<SpecimenNodeWrapper> wrappers = dialog.getSpecimen();
71
            if(wrappers.stream().anyMatch(wrapper->wrapper.getTaxonDescriptionUuid()==null)
72
                    && !MessagingUtils.confirmDialog(
73
                            "Create Taxon Association",
74
                            "Some specimens are not linked with taxon via IndividualsAssociation yet.\n"
75
                            + "Do you want to create this association?")){
76
                return;
77
            }
78
            matrix.addRowsToMatrix(wrappers);
79
            wrappers.forEach(wrapper->matrix.getSpecimenCache().remove(wrapper));
80

    
81
        }
82
    }
83

    
84
    @CanExecute
85
    public boolean canExecute(@Named(IServiceConstants.ACTIVE_PART)MPart activePart,
86
            MHandledMenuItem menuItem){
87
        CharacterMatrixPart matrixPart = (CharacterMatrixPart) activePart.getObject();
88
        IStructuredSelection selection = matrixPart.getSelection();
89
        boolean canExecute = (selection.isEmpty() ||
90
                !(selection.getFirstElement() instanceof SpecimenRowWrapperDTO) );
91

    
92
        return canExecute;
93

    
94
    }
95
}
(1-1/7)