Project

General

Profile

Download (3.8 KB) Statistics
| Branch: | Tag: | Revision:
1
/**
2
* Copyright (C) 2017 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.workingSet.matrix;
10

    
11
import java.util.ArrayList;
12
import java.util.Collection;
13
import java.util.List;
14
import java.util.Map;
15
import java.util.Set;
16

    
17
import org.apache.commons.collections4.map.LinkedMap;
18
import org.eclipse.nebula.widgets.nattable.data.IColumnPropertyAccessor;
19

    
20
import eu.etaxonomy.cdm.model.description.CategoricalData;
21
import eu.etaxonomy.cdm.model.description.DescriptionElementBase;
22
import eu.etaxonomy.cdm.model.description.Feature;
23
import eu.etaxonomy.cdm.model.description.SpecimenDescription;
24
import eu.etaxonomy.cdm.model.description.State;
25
import eu.etaxonomy.cdm.model.description.StateData;
26

    
27
/**
28
 * @author pplitzner
29
 * @since Nov 26, 2017
30
 *
31
 */
32
public class SpecimenColumnPropertyAccessor implements IColumnPropertyAccessor<SpecimenDescription>{
33

    
34
    private final List<Feature> features;
35
    private LinkedMap<String, String> propertyToLabelMap = new LinkedMap<>();
36

    
37

    
38
    public SpecimenColumnPropertyAccessor(final List<Feature> features) {
39
        this.features = features;
40
        for (Feature feature : features) {
41
            propertyToLabelMap.put(feature.getLabel(), feature.getLabel());
42
        }
43
    }
44

    
45
    public Map<String, String> getPropertyToLabelMap() {
46
        return propertyToLabelMap;
47
    }
48

    
49
    /**
50
     * {@inheritDoc}
51
     */
52
    @Override
53
    public DescriptionElementBase getDataValue(SpecimenDescription rowObject, int columnIndex) {
54
        Feature feature = features.get(columnIndex);
55
        Set<DescriptionElementBase> elements = rowObject.getElements();
56
        for (DescriptionElementBase descriptionElementBase : elements) {
57
            if(descriptionElementBase.getFeature().equals(feature)){
58
                return descriptionElementBase;
59
            }
60
        }
61
        return null;
62
    }
63

    
64
    /**
65
     * {@inheritDoc}
66
     */
67
    @Override
68
    public void setDataValue(SpecimenDescription rowObject, int columnIndex, Object newValue) {
69
        Feature feature = features.get(columnIndex);
70
        Set<DescriptionElementBase> elements = rowObject.getElements();
71
        for (DescriptionElementBase descriptionElementBase : elements) {
72
            if(descriptionElementBase.getFeature().equals(feature)){
73
                setDescriptionElement(rowObject, descriptionElementBase, feature, newValue);
74
            }
75
        }
76
    }
77

    
78

    
79
    private void setDescriptionElement(SpecimenDescription description, DescriptionElementBase descriptionElementBase, Feature feature, Object newValue) {
80
        //FIXME move this to cdmlib service layer
81
        if(feature.isSupportsCategoricalData() && descriptionElementBase instanceof CategoricalData && newValue instanceof Collection){
82
            CategoricalData categoricalData = (CategoricalData)descriptionElementBase;
83
            List<StateData> stateData = new ArrayList<>(categoricalData.getStateData());
84
            for (StateData stateData2 : stateData) {
85
                categoricalData.removeStateData(stateData2);
86
            }
87
            Collection<State> states = (Collection<State>) newValue;
88
            for (State state : states) {
89
                categoricalData.addStateData(state);
90
            }
91
        }
92
    }
93

    
94
    /**
95
     * {@inheritDoc}
96
     */
97
    @Override
98
    public int getColumnCount() {
99
        return features.size();
100
    }
101

    
102
    /**
103
     * {@inheritDoc}
104
     */
105
    @Override
106
    public String getColumnProperty(int columnIndex) {
107
        return features.get(columnIndex).getLabel();
108
    }
109

    
110
    /**
111
     * {@inheritDoc}
112
     */
113
    @Override
114
    public int getColumnIndex(String propertyName){
115
        return propertyToLabelMap.indexOf(propertyName);
116
    }
117

    
118
}
(5-5/5)