Project

General

Profile

Download (4.33 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.HashSet;
12
import java.util.Set;
13

    
14
import org.eclipse.nebula.widgets.nattable.data.IColumnPropertyAccessor;
15

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

    
23

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

    
33
    public DistributionColumnAccessor(DistributionEditor editor) {
34
        this.editor = editor;
35
    }
36

    
37

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

    
57
        //TODO: do not get the distribution objcts, but the label of the status.
58
        Set<DescriptionElementBase> distributionsForArea = rowObject.getDistributionMap().get(area);
59
        if (distributionsForArea == null){
60
            return null;
61
        }
62
        if (distributionsForArea.size() == 1){
63
            return distributionsForArea.iterator().next();
64
        }
65
        if (distributionsForArea.size() > 1){
66
            return distributionsForArea;
67
        }
68
        return null;
69

    
70
    }
71

    
72

    
73
    /**
74
     * {@inheritDoc}
75
     */
76
    @Override
77
    public int getColumnCount() {
78
        return editor.getPropertyToLabelMap().size();
79
    }
80

    
81
    /**
82
     * {@inheritDoc}
83
     */
84
    @Override
85
    public String getColumnProperty(int columnIndex) {
86
        return editor.getPropertyToLabelMap().get(columnIndex);
87
    }
88

    
89
    /**
90
     * {@inheritDoc}
91
     */
92
    @Override
93
    public int getColumnIndex(String propertyName){
94
        return editor.getPropertyToLabelMap().indexOf(propertyName);
95
    }
96

    
97

    
98
    /**
99
     * {@inheritDoc}
100
     */
101
    @Override
102
    public void setDataValue(TaxonDistributionDTO taxonWrapper, int columnIndex, Object newValue) {
103
            if (newValue instanceof PresenceAbsenceTerm){
104
                NamedArea area =editor.getAreaToColumnIndexMap().get(columnIndex);
105
                Set<DescriptionElementBase> distributions = taxonWrapper.getDistributionMap().get(area);
106
                if (distributions != null && !distributions.isEmpty()){
107
                    DescriptionElementBase desc = distributions.iterator().next();
108
                    if (desc instanceof Distribution){
109
                        if (((PresenceAbsenceTerm)newValue).getId() == 0){
110
                            desc.getInDescription().removeElement(desc);
111
                            distributions.remove(desc);
112
                        }else {
113
                            ((Distribution)desc).setStatus((PresenceAbsenceTerm)newValue);
114
                        }
115
                    }
116
                }else{
117
                    if (distributions == null){
118
                        distributions = new HashSet();
119
                    }
120
                    Distribution dist = Distribution.NewInstance(area, (PresenceAbsenceTerm)newValue);
121
                    Set<TaxonDescription> descs = taxonWrapper.getDescriptionsWrapper().getDescriptions();
122
                    TaxonDescription desc;
123
                    if (descs.size() >= 1){
124
                        desc = descs.iterator().next();
125
                    }else {
126
                        desc = TaxonDescription.NewInstance();
127
                    }
128
                    desc.addElement(dist);
129
                    distributions.add(dist);
130
                }
131

    
132
            }
133

    
134
    }
135

    
136
}
(6-6/19)