ref #7095 Make matrix editable vie DetailsView
[taxeditor.git] / eu.etaxonomy.taxeditor.editor / src / main / java / eu / etaxonomy / taxeditor / editor / workingSet / matrix / SpecimenColumnPropertyAccessor.java
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.List;
13 import java.util.Map;
14 import java.util.Set;
15
16 import org.apache.commons.collections4.map.LinkedMap;
17 import org.eclipse.nebula.widgets.nattable.data.IColumnPropertyAccessor;
18
19 import eu.etaxonomy.cdm.model.description.DescriptionElementBase;
20 import eu.etaxonomy.cdm.model.description.Feature;
21 import eu.etaxonomy.cdm.model.description.FeatureTree;
22 import eu.etaxonomy.cdm.model.description.SpecimenDescription;
23 import eu.etaxonomy.cdm.model.description.WorkingSet;
24
25 /**
26 * @author pplitzner
27 * @since Nov 26, 2017
28 *
29 */
30 public class SpecimenColumnPropertyAccessor implements IColumnPropertyAccessor<SpecimenDescription>{
31
32 private List<Feature> features = new ArrayList<>();
33 private LinkedMap<String, String> propertyToLabelMap = new LinkedMap<>();
34
35
36 public SpecimenColumnPropertyAccessor(WorkingSet workingSet) {
37 FeatureTree tree = workingSet.getDescriptiveSystem();
38 Set<Feature> distinctFeatures = tree.getDistinctFeatures();
39 for (Feature feature : distinctFeatures) {
40 if(feature!=null){
41 features.add(feature);
42 propertyToLabelMap.put(feature.getLabel(), feature.getLabel());
43 }
44 }
45 }
46
47 public Map<String, String> getPropertyToLabelMap() {
48 return propertyToLabelMap;
49 }
50
51 /**
52 * {@inheritDoc}
53 */
54 @Override
55 public DescriptionElementBase getDataValue(SpecimenDescription rowObject, int columnIndex) {
56 Feature feature = features.get(columnIndex);
57 Set<DescriptionElementBase> elements = rowObject.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(SpecimenDescription rowObject, int columnIndex, Object newValue) {
71 features.remove(columnIndex);
72 features.add(columnIndex, (Feature) newValue);
73 }
74
75 /**
76 * {@inheritDoc}
77 */
78 @Override
79 public int getColumnCount() {
80 return features.size();
81 }
82
83 /**
84 * {@inheritDoc}
85 */
86 @Override
87 public String getColumnProperty(int columnIndex) {
88 return features.get(columnIndex).getLabel();
89 }
90
91 /**
92 * {@inheritDoc}
93 */
94 @Override
95 public int getColumnIndex(String propertyName){
96 return propertyToLabelMap.indexOf(propertyName);
97 }
98
99 }