Project

General

Profile

Download (3.84 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<Object>{
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(Object rowObject, int columnIndex) {
43
        if(rowObject instanceof RowWrapper){
44
            RowWrapper rowWrapper = (RowWrapper)rowObject;
45
            switch (columnIndex) {
46
            case 0:
47
                return rowWrapper.getAssociatedTaxon();
48
            case 1:
49
                return rowWrapper.getFieldUnit();
50
            case 2:
51
                return rowWrapper.getIdentifier();
52
            case 3:
53
                return rowWrapper.getCountry();
54

    
55
            default:
56
                break;
57
            }
58
            Feature feature = matrix.getIndexToFeatureMap().get(columnIndex);
59
            return rowWrapper.getDescriptionElementForFeature(feature);
60
        } else if (columnIndex == 0) {
61
            return rowObject;
62
        }
63
        return null;
64
    }
65

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

    
83

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

    
99
    /**
100
     * {@inheritDoc}
101
     */
102
    @Override
103
    public int getColumnCount() {
104
        return matrix.getPropertyToLabelMap().size();
105
    }
106

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

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

    
123
}
(9-9/11)