2 * Copyright (C) 2018 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
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.
9 package eu
.etaxonomy
.taxeditor
.editor
.view
.checklist
.e4
;
11 import java
.util
.ArrayList
;
12 import java
.util
.HashMap
;
13 import java
.util
.HashSet
;
14 import java
.util
.List
;
18 import javax
.inject
.Inject
;
20 import org
.eclipse
.e4
.core
.services
.events
.IEventBroker
;
21 import org
.eclipse
.nebula
.widgets
.nattable
.data
.IColumnPropertyAccessor
;
23 import eu
.etaxonomy
.cdm
.api
.service
.dto
.TaxonDistributionDTO
;
24 import eu
.etaxonomy
.cdm
.model
.description
.DescriptionElementBase
;
25 import eu
.etaxonomy
.cdm
.model
.description
.DescriptionElementSource
;
26 import eu
.etaxonomy
.cdm
.model
.description
.Distribution
;
27 import eu
.etaxonomy
.cdm
.model
.description
.PresenceAbsenceTerm
;
28 import eu
.etaxonomy
.cdm
.model
.description
.TaxonDescription
;
29 import eu
.etaxonomy
.cdm
.model
.location
.NamedArea
;
30 import eu
.etaxonomy
.taxeditor
.event
.EventUtility
;
31 import eu
.etaxonomy
.taxeditor
.event
.WorkbenchEventConstants
;
39 public class DistributionColumnAccessor
implements IColumnPropertyAccessor
<TaxonDistributionDTO
> {
40 private DistributionEditor editor
;
41 public static final String DEFAULT_ENTRY
= "";
43 private IEventBroker eventBroker
;
45 public DistributionColumnAccessor(DistributionEditor editor
) {
54 public Object
getDataValue(TaxonDistributionDTO rowObject
, int columnIndex
) {
55 switch (columnIndex
) {
57 return rowObject
.getNameCache();
59 if (editor
.isShowRank()){
60 return rowObject
.getRankString();
67 NamedArea area
= editor
.getAreaToColumnIndexMap().get(columnIndex
);
69 //TODO: do not get the distribution objcts, but the label of the status.
70 Map
<NamedArea
, Set
<DescriptionElementBase
>> distributionMap
= editor
.taxonDistributionMap
.get(rowObject
.getTaxonUuid());
71 if (distributionMap
!= null){
72 Set
<DescriptionElementBase
> distributionsForArea
= editor
.taxonDistributionMap
.get(rowObject
.getTaxonUuid()).get(area
);
73 if (distributionsForArea
== null){
76 if (distributionsForArea
.size() == 1){
77 return distributionsForArea
.iterator().next();
79 if (distributionsForArea
.size() > 1){
80 List
<String
> labels
= new ArrayList();
81 distributionsForArea
.forEach(desc
-> labels
.add(((Distribution
)desc
).getStatus().getLabel()));
94 public int getColumnCount() {
95 return editor
.getPropertyToLabelMap().size();
102 public String
getColumnProperty(int columnIndex
) {
103 return editor
.getPropertyToLabelMap().get(columnIndex
);
110 public int getColumnIndex(String propertyName
){
111 return editor
.getPropertyToLabelMap().indexOf(propertyName
);
119 public void setDataValue(TaxonDistributionDTO taxonWrapper
, int columnIndex
, Object newValue
) {
120 if (newValue
instanceof PresenceAbsenceTerm
){
121 NamedArea area
=editor
.getAreaToColumnIndexMap().get(columnIndex
);
122 Map
<NamedArea
, Set
<DescriptionElementBase
>> distributionMap
= editor
.taxonDistributionMap
.get(taxonWrapper
.getTaxonUuid());
123 if (distributionMap
== null){
124 distributionMap
= new HashMap();
125 editor
.taxonDistributionMap
.put(taxonWrapper
.getTaxonUuid(),distributionMap
);
127 Distribution dist
= null;
128 Set
<DescriptionElementBase
> distributions
= distributionMap
.get(area
);
129 if (distributions
!= null && !distributions
.isEmpty()){
130 DescriptionElementBase desc
= distributions
.iterator().next();
131 if (desc
instanceof Distribution
){
132 if (((PresenceAbsenceTerm
)newValue
).getId() == 0){
133 desc
.getInDescription().removeElement(desc
);
134 distributions
.remove(desc
);
136 ((Distribution
)desc
).setStatus((PresenceAbsenceTerm
)newValue
);
140 if (distributions
== null){
141 distributions
= new HashSet();
142 distributionMap
.put(area
, distributions
);
144 dist
= Distribution
.NewInstance(area
, (PresenceAbsenceTerm
)newValue
);
145 Set
<TaxonDescription
> descs
= taxonWrapper
.getDescriptionsWrapper().getDescriptions();
146 TaxonDescription desc
;
147 if (descs
.size() >= 1){
148 desc
= descs
.iterator().next();
150 desc
= TaxonDescription
.NewInstance();
151 taxonWrapper
.getDescriptionsWrapper().getDescriptions().add(desc
);
153 if (editor
.getDefaultSource() != null){
154 dist
.addSource(DescriptionElementSource
.NewPrimarySourceInstance(editor
.getDefaultSource(), null));
156 desc
.addElement(dist
);
157 editor
.part
.getCdmEntitySession().load(desc
, true);
158 distributions
.add(dist
);
165 EventUtility
.postEvent(WorkbenchEventConstants
.REFRESH_DETAILS
, true);