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
.TaxonDescription
;
28 import eu
.etaxonomy
.cdm
.model
.location
.NamedArea
;
29 import eu
.etaxonomy
.taxeditor
.event
.EventUtility
;
30 import eu
.etaxonomy
.taxeditor
.event
.WorkbenchEventConstants
;
38 public class DistributionColumnAccessor
implements IColumnPropertyAccessor
<TaxonDistributionDTO
> {
39 private DistributionEditor editor
;
40 public static final String DEFAULT_ENTRY
= "";
42 private IEventBroker eventBroker
;
44 public DistributionColumnAccessor(DistributionEditor editor
) {
53 public Object
getDataValue(TaxonDistributionDTO rowObject
, int columnIndex
) {
54 switch (columnIndex
) {
56 return rowObject
.getNameCache();
58 if (editor
.isShowRank()){
59 return rowObject
.getRankString();
66 NamedArea area
= editor
.getAreaToColumnIndexMap().get(columnIndex
);
68 //TODO: do not get the distribution objcts, but the label of the status.
69 Map
<NamedArea
, Set
<DescriptionElementBase
>> distributionMap
= editor
.taxonDistributionMap
.get(rowObject
.getTaxonUuid());
70 if (distributionMap
!= null){
71 Set
<DescriptionElementBase
> distributionsForArea
= editor
.taxonDistributionMap
.get(rowObject
.getTaxonUuid()).get(area
);
72 if (distributionsForArea
== null){
75 if (distributionsForArea
.size() == 1){
76 return distributionsForArea
.iterator().next();
78 if (distributionsForArea
.size() > 1){
79 List
<String
> labels
= new ArrayList();
80 distributionsForArea
.forEach(desc
-> labels
.add(((Distribution
)desc
).getStatus().getLabel()));
93 public int getColumnCount() {
94 return editor
.getPropertyToLabelMap().size();
101 public String
getColumnProperty(int columnIndex
) {
102 return editor
.getPropertyToLabelMap().get(columnIndex
);
109 public int getColumnIndex(String propertyName
){
110 return editor
.getPropertyToLabelMap().indexOf(propertyName
);
118 public void setDataValue(TaxonDistributionDTO taxonWrapper
, int columnIndex
, Object newValue
) {
119 if (newValue
instanceof StatusHelper
){
120 NamedArea area
=editor
.getAreaToColumnIndexMap().get(columnIndex
);
121 Map
<NamedArea
, Set
<DescriptionElementBase
>> distributionMap
= editor
.taxonDistributionMap
.get(taxonWrapper
.getTaxonUuid());
122 if (distributionMap
== null){
123 distributionMap
= new HashMap();
124 editor
.taxonDistributionMap
.put(taxonWrapper
.getTaxonUuid(),distributionMap
);
126 Distribution dist
= null;
127 Set
<DescriptionElementBase
> distributions
= distributionMap
.get(area
);
128 if (distributions
!= null && !distributions
.isEmpty()){
129 DescriptionElementBase desc
= distributions
.iterator().next();
130 if (desc
instanceof Distribution
){
131 if (((StatusHelper
)newValue
).term
.getId() == 0){
132 desc
.getInDescription().removeElement(desc
);
133 distributions
.remove(desc
);
135 ((Distribution
)desc
).setStatus(((StatusHelper
)newValue
).term
);
139 if (((StatusHelper
)newValue
).term
.getId() == 0){
142 if (distributions
== null){
143 distributions
= new HashSet();
144 distributionMap
.put(area
, distributions
);
146 dist
= Distribution
.NewInstance(area
, ((StatusHelper
)newValue
).term
);
147 Set
<TaxonDescription
> descs
= taxonWrapper
.getDescriptionsWrapper().getDescriptions();
148 TaxonDescription desc
;
149 if (descs
.size() >= 1){
150 desc
= descs
.iterator().next();
152 desc
= TaxonDescription
.NewInstance();
153 taxonWrapper
.getDescriptionsWrapper().getDescriptions().add(desc
);
155 if (editor
.getDefaultSource() != null){
156 dist
.addSource(DescriptionElementSource
.NewPrimarySourceInstance(editor
.getDefaultSource(), null));
158 desc
.addElement(dist
);
159 if(desc
.isPersited()){
160 editor
.part
.getCdmEntitySession().load(desc
, true);
162 distributions
.add(dist
);
167 editor
.descriptionsToSave
.add(taxonWrapper
);
169 EventUtility
.postEvent(WorkbenchEventConstants
.REFRESH_DETAILS
, true);