Project

General

Profile

Download (4.1 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
            Set<DescriptionElementBase> elements = rowWrapper.getSpecimenDescription().getElements();
60
            for (DescriptionElementBase descriptionElementBase : elements) {
61
                if(descriptionElementBase.getFeature().equals(feature)){
62
                    return descriptionElementBase;
63
                }
64
            }
65
        } else if (columnIndex == 0) {
66
            return rowObject;
67
        }
68
        return null;
69
    }
70

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

    
88

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

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

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

    
120
    /**
121
     * {@inheritDoc}
122
     */
123
    @Override
124
    public int getColumnIndex(String propertyName){
125
        return matrix.getPropertyToLabelMap().indexOf(propertyName);
126
    }
127

    
128
}
(9-9/11)