Project

General

Profile

Download (5.12 KB) Statistics
| Branch: | Tag: | Revision:
1
/**
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
import java.util.HashMap;
12
import java.util.HashSet;
13
import java.util.Map;
14
import java.util.Set;
15

    
16
import org.eclipse.nebula.widgets.nattable.data.IColumnPropertyAccessor;
17

    
18
import eu.etaxonomy.cdm.api.service.dto.TaxonDistributionDTO;
19
import eu.etaxonomy.cdm.model.description.DescriptionElementBase;
20
import eu.etaxonomy.cdm.model.description.Distribution;
21
import eu.etaxonomy.cdm.model.description.PresenceAbsenceTerm;
22
import eu.etaxonomy.cdm.model.description.TaxonDescription;
23
import eu.etaxonomy.cdm.model.location.NamedArea;
24

    
25

    
26
/**
27
 * @author k.luther
28
 * @since 28.11.2018
29
 *
30
 */
31
public class DistributionColumnAccessor implements IColumnPropertyAccessor<TaxonDistributionDTO> {
32
    private DistributionEditor editor;
33
    public static final String DEFAULT_ENTRY = "";
34

    
35
    public DistributionColumnAccessor(DistributionEditor editor) {
36
        this.editor = editor;
37
    }
38

    
39

    
40
    /**
41
     * {@inheritDoc}
42
     */
43
    @Override
44
    public Object getDataValue(TaxonDistributionDTO rowObject, int columnIndex) {
45
     switch (columnIndex) {
46
            case 0:
47
                return rowObject.getNameCache();
48
            case 1:
49
                if (editor.isShowRank()){
50
                    return rowObject.getRankString();
51
                }else{
52
                    break;
53
                }
54
            default:
55
                break;
56
            }
57
        NamedArea area = editor.getAreaToColumnIndexMap().get(columnIndex);
58

    
59
        //TODO: do not get the distribution objcts, but the label of the status.
60
        Map<NamedArea, Set<DescriptionElementBase>> distributionMap = editor.taxonDistributionMap.get(rowObject.getTaxonUuid());
61
        if (distributionMap != null){
62
            Set<DescriptionElementBase> distributionsForArea = editor.taxonDistributionMap.get(rowObject.getTaxonUuid()).get(area);
63
            if (distributionsForArea == null){
64
                return null;
65
            }
66
            if (distributionsForArea.size() == 1){
67
                return distributionsForArea.iterator().next();
68
            }
69
            if (distributionsForArea.size() > 1){
70
                return distributionsForArea;
71
            }
72
        }
73
        return null;
74

    
75
    }
76

    
77

    
78
    /**
79
     * {@inheritDoc}
80
     */
81
    @Override
82
    public int getColumnCount() {
83
        return editor.getPropertyToLabelMap().size();
84
    }
85

    
86
    /**
87
     * {@inheritDoc}
88
     */
89
    @Override
90
    public String getColumnProperty(int columnIndex) {
91
        return editor.getPropertyToLabelMap().get(columnIndex);
92
    }
93

    
94
    /**
95
     * {@inheritDoc}
96
     */
97
    @Override
98
    public int getColumnIndex(String propertyName){
99
        return editor.getPropertyToLabelMap().indexOf(propertyName);
100
    }
101

    
102

    
103
    /**
104
     * {@inheritDoc}
105
     */
106
    @Override
107
    public void setDataValue(TaxonDistributionDTO taxonWrapper, int columnIndex, Object newValue) {
108
            if (newValue instanceof PresenceAbsenceTerm){
109
                NamedArea area =editor.getAreaToColumnIndexMap().get(columnIndex);
110
                Map<NamedArea, Set<DescriptionElementBase>> distributionMap = editor.taxonDistributionMap.get(taxonWrapper.getTaxonUuid());
111
                if (distributionMap == null){
112
                    distributionMap = new HashMap();
113
                    editor.taxonDistributionMap.put(taxonWrapper.getTaxonUuid(),distributionMap);
114
                }
115
                Set<DescriptionElementBase> distributions = distributionMap.get(area);
116
                if (distributions != null && !distributions.isEmpty()){
117
                    DescriptionElementBase desc = distributions.iterator().next();
118
                    if (desc instanceof Distribution){
119
                        if (((PresenceAbsenceTerm)newValue).getId() == 0){
120
                            desc.getInDescription().removeElement(desc);
121
                            distributions.remove(desc);
122
                        }else {
123
                            ((Distribution)desc).setStatus((PresenceAbsenceTerm)newValue);
124
                        }
125
                    }
126
                }else{
127
                    if (distributions == null){
128
                        distributions = new HashSet();
129
                        distributionMap.put(area, distributions);
130
                    }
131
                    Distribution dist = Distribution.NewInstance(area, (PresenceAbsenceTerm)newValue);
132
                    Set<TaxonDescription> descs = taxonWrapper.getDescriptionsWrapper().getDescriptions();
133
                    TaxonDescription desc;
134
                    if (descs.size() >= 1){
135
                        desc = descs.iterator().next();
136
                    }else {
137
                        desc = TaxonDescription.NewInstance(taxonWrapper.getTaxon());
138
                        taxonWrapper.getDescriptionsWrapper().getDescriptions().add(desc);
139
                    }
140
                    desc.addElement(dist);
141
                    distributions.add(dist);
142

    
143
                }
144

    
145
            }
146

    
147
    }
148

    
149
}
(6-6/19)