Project

General

Profile

Download (3.71 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.Set;
15

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

    
18
import eu.etaxonomy.cdm.model.description.CategoricalData;
19
import eu.etaxonomy.cdm.model.description.DescriptionElementBase;
20
import eu.etaxonomy.cdm.model.description.Feature;
21
import eu.etaxonomy.cdm.model.description.State;
22
import eu.etaxonomy.cdm.model.description.StateData;
23

    
24
/**
25
 * @author pplitzner
26
 * @since Nov 26, 2017
27
 *
28
 */
29
public class SpecimenColumnPropertyAccessor implements IColumnPropertyAccessor<RowWrapper>{
30

    
31
    private CharacterMatrix matrix;
32

    
33
    public SpecimenColumnPropertyAccessor(CharacterMatrix matrix) {
34
        this.matrix = matrix;
35
    }
36

    
37

    
38
    /**
39
     * {@inheritDoc}
40
     */
41
    @Override
42
    public Object getDataValue(RowWrapper rowObject, int columnIndex) {
43
        switch (columnIndex) {
44
        case 0:
45
            return rowObject.getAssociatedTaxa();
46
        case 1:
47
            return rowObject.getFieldUnit();
48
        case 2:
49
            return rowObject.getIdentifier();
50
        case 3:
51
            return rowObject.getCountry();
52

    
53
        default:
54
            break;
55
        }
56
        Feature feature = matrix.getIndexToFeatureMap().get(columnIndex);
57
        Set<DescriptionElementBase> elements = rowObject.getSpecimenDescription().getElements();
58
        for (DescriptionElementBase descriptionElementBase : elements) {
59
            if(descriptionElementBase.getFeature().equals(feature)){
60
                return descriptionElementBase;
61
            }
62
        }
63
        return null;
64
    }
65

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

    
80

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

    
96
    /**
97
     * {@inheritDoc}
98
     */
99
    @Override
100
    public int getColumnCount() {
101
        return matrix.getPropertyToLabelMap().size();
102
    }
103

    
104
    /**
105
     * {@inheritDoc}
106
     */
107
    @Override
108
    public String getColumnProperty(int columnIndex) {
109
        return matrix.getPropertyToLabelMap().get(columnIndex);
110
    }
111

    
112
    /**
113
     * {@inheritDoc}
114
     */
115
    @Override
116
    public int getColumnIndex(String propertyName){
117
        return matrix.getPropertyToLabelMap().indexOf(propertyName);
118
    }
119

    
120
}
(4-4/4)