Project

General

Profile

Download (3.87 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
                    return;
79
                }
80
            }
81
        }
82
    }
83

    
84

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

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

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

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

    
124
}
(9-9/11)