Project

General

Profile

Download (5.71 KB) Statistics
| Branch: | Tag: | Revision:
1 278a5e0a Katja Luther
/**
2
* Copyright (C) 2018 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.view.checklist.e4;
10
11 ad2e9ab4 Katja Luther
import java.util.ArrayList;
12 2e385b93 Katja Luther
import java.util.HashMap;
13 278a5e0a Katja Luther
import java.util.HashSet;
14 ad2e9ab4 Katja Luther
import java.util.List;
15 2e385b93 Katja Luther
import java.util.Map;
16 278a5e0a Katja Luther
import java.util.Set;
17
18 a893ba13 Katja Luther
import javax.inject.Inject;
19
20
import org.eclipse.e4.core.services.events.IEventBroker;
21 278a5e0a Katja Luther
import org.eclipse.nebula.widgets.nattable.data.IColumnPropertyAccessor;
22
23
import eu.etaxonomy.cdm.api.service.dto.TaxonDistributionDTO;
24
import eu.etaxonomy.cdm.model.description.DescriptionElementBase;
25
import eu.etaxonomy.cdm.model.description.Distribution;
26
import eu.etaxonomy.cdm.model.description.PresenceAbsenceTerm;
27
import eu.etaxonomy.cdm.model.description.TaxonDescription;
28
import eu.etaxonomy.cdm.model.location.NamedArea;
29 a893ba13 Katja Luther
import eu.etaxonomy.taxeditor.event.EventUtility;
30
import eu.etaxonomy.taxeditor.event.WorkbenchEventConstants;
31 278a5e0a Katja Luther
32
33
/**
34
 * @author k.luther
35
 * @since 28.11.2018
36
 *
37
 */
38
public class DistributionColumnAccessor implements IColumnPropertyAccessor<TaxonDistributionDTO> {
39
    private DistributionEditor editor;
40
    public static final String DEFAULT_ENTRY = "";
41 a893ba13 Katja Luther
    @Inject
42
    private IEventBroker eventBroker;
43 278a5e0a Katja Luther
44
    public DistributionColumnAccessor(DistributionEditor editor) {
45
        this.editor = editor;
46
    }
47
48
49
    /**
50
     * {@inheritDoc}
51
     */
52
    @Override
53
    public Object getDataValue(TaxonDistributionDTO rowObject, int columnIndex) {
54
     switch (columnIndex) {
55
            case 0:
56
                return rowObject.getNameCache();
57
            case 1:
58
                if (editor.isShowRank()){
59
                    return rowObject.getRankString();
60
                }else{
61
                    break;
62
                }
63
            default:
64
                break;
65
            }
66
        NamedArea area = editor.getAreaToColumnIndexMap().get(columnIndex);
67
68
        //TODO: do not get the distribution objcts, but the label of the status.
69 0747234f Katja Luther
        Map<NamedArea, Set<DescriptionElementBase>> distributionMap = editor.taxonDistributionMap.get(rowObject.getTaxonUuid());
70
        if (distributionMap != null){
71
            Set<DescriptionElementBase> distributionsForArea = editor.taxonDistributionMap.get(rowObject.getTaxonUuid()).get(area);
72
            if (distributionsForArea == null){
73
                return null;
74
            }
75
            if (distributionsForArea.size() == 1){
76
                return distributionsForArea.iterator().next();
77
            }
78
            if (distributionsForArea.size() > 1){
79 ad2e9ab4 Katja Luther
                List<String> labels = new ArrayList();
80
                distributionsForArea.forEach(desc -> labels.add(((Distribution)desc).getStatus().getLabel()));
81
                return labels;
82 0747234f Katja Luther
            }
83 278a5e0a Katja Luther
        }
84
        return null;
85
86
    }
87
88
89
    /**
90
     * {@inheritDoc}
91
     */
92
    @Override
93
    public int getColumnCount() {
94
        return editor.getPropertyToLabelMap().size();
95
    }
96
97
    /**
98
     * {@inheritDoc}
99
     */
100
    @Override
101
    public String getColumnProperty(int columnIndex) {
102 018b80b2 Katja Luther
        return editor.getPropertyToLabelMap().get(columnIndex);
103 278a5e0a Katja Luther
    }
104
105
    /**
106
     * {@inheritDoc}
107
     */
108
    @Override
109
    public int getColumnIndex(String propertyName){
110
        return editor.getPropertyToLabelMap().indexOf(propertyName);
111
    }
112
113
114
    /**
115
     * {@inheritDoc}
116
     */
117
    @Override
118
    public void setDataValue(TaxonDistributionDTO taxonWrapper, int columnIndex, Object newValue) {
119 018b80b2 Katja Luther
           if (newValue instanceof PresenceAbsenceTerm){
120 278a5e0a Katja Luther
                NamedArea area =editor.getAreaToColumnIndexMap().get(columnIndex);
121 2e385b93 Katja Luther
                Map<NamedArea, Set<DescriptionElementBase>> distributionMap = editor.taxonDistributionMap.get(taxonWrapper.getTaxonUuid());
122
                if (distributionMap == null){
123
                    distributionMap = new HashMap();
124
                    editor.taxonDistributionMap.put(taxonWrapper.getTaxonUuid(),distributionMap);
125
                }
126 a893ba13 Katja Luther
                Distribution dist = null;
127 2e385b93 Katja Luther
                Set<DescriptionElementBase> distributions = distributionMap.get(area);
128 278a5e0a Katja Luther
                if (distributions != null && !distributions.isEmpty()){
129
                    DescriptionElementBase desc = distributions.iterator().next();
130
                    if (desc instanceof Distribution){
131
                        if (((PresenceAbsenceTerm)newValue).getId() == 0){
132
                            desc.getInDescription().removeElement(desc);
133
                            distributions.remove(desc);
134
                        }else {
135
                            ((Distribution)desc).setStatus((PresenceAbsenceTerm)newValue);
136
                        }
137
                    }
138
                }else{
139
                    if (distributions == null){
140
                        distributions = new HashSet();
141 2e385b93 Katja Luther
                        distributionMap.put(area, distributions);
142 278a5e0a Katja Luther
                    }
143 a893ba13 Katja Luther
                    dist = Distribution.NewInstance(area, (PresenceAbsenceTerm)newValue);
144 278a5e0a Katja Luther
                    Set<TaxonDescription> descs = taxonWrapper.getDescriptionsWrapper().getDescriptions();
145
                    TaxonDescription desc;
146
                    if (descs.size() >= 1){
147
                        desc = descs.iterator().next();
148
                    }else {
149 ad2e9ab4 Katja Luther
                        desc = TaxonDescription.NewInstance();
150 2e385b93 Katja Luther
                        taxonWrapper.getDescriptionsWrapper().getDescriptions().add(desc);
151 278a5e0a Katja Luther
                    }
152
                    desc.addElement(dist);
153 bb082354 Katja Luther
                    editor.part.getCdmEntitySession().load(desc, true);
154 278a5e0a Katja Luther
                    distributions.add(dist);
155 2e385b93 Katja Luther
156 a893ba13 Katja Luther
157 278a5e0a Katja Luther
                }
158
159
160 018b80b2 Katja Luther
            }
161
            EventUtility.postEvent(WorkbenchEventConstants.REFRESH_DETAILS, true);
162 278a5e0a Katja Luther
    }
163
164
}