1
|
/**
|
2
|
* Copyright (C) 2018 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.view.checklist.e4;
|
10
|
|
11
|
import java.util.HashMap;
|
12
|
import java.util.HashSet;
|
13
|
import java.util.Iterator;
|
14
|
import java.util.Map;
|
15
|
import java.util.Set;
|
16
|
|
17
|
import org.eclipse.nebula.widgets.nattable.edit.editor.ComboBoxCellEditor;
|
18
|
import org.eclipse.nebula.widgets.nattable.widget.NatCombo;
|
19
|
import org.eclipse.swt.events.SelectionEvent;
|
20
|
import org.eclipse.swt.events.SelectionListener;
|
21
|
import org.eclipse.swt.widgets.Composite;
|
22
|
import org.eclipse.swt.widgets.Control;
|
23
|
|
24
|
import eu.etaxonomy.cdm.api.service.dto.TaxonDistributionDTO;
|
25
|
import eu.etaxonomy.cdm.model.description.DescriptionElementBase;
|
26
|
import eu.etaxonomy.cdm.model.description.DescriptionElementSource;
|
27
|
import eu.etaxonomy.cdm.model.description.Distribution;
|
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;
|
32
|
|
33
|
/**
|
34
|
* @author k.luther
|
35
|
* @since 05.12.2018
|
36
|
*
|
37
|
*/
|
38
|
public class StatusDataCellEditor extends ComboBoxCellEditor {
|
39
|
private DistributionEditor editor;
|
40
|
|
41
|
|
42
|
|
43
|
public StatusDataCellEditor(DistributionEditor editor, StatusComboBoxDataProvider statusComboBoxDataProvider) {
|
44
|
super(statusComboBoxDataProvider, statusComboBoxDataProvider.getMaxVisibleItems());
|
45
|
this.editor = editor;
|
46
|
setUseCheckbox(false);
|
47
|
setMultiselect(false);
|
48
|
|
49
|
}
|
50
|
|
51
|
@Override
|
52
|
public void setCanonicalValue(Object canonicalValue) {
|
53
|
if(canonicalValue instanceof Distribution){
|
54
|
Object rowWrapper = editor.getBodyDataProvider().getRowObject(this.getRowIndex());
|
55
|
if(rowWrapper instanceof TaxonDistributionDTO){
|
56
|
NamedArea area = editor.getAreaToColumnIndexMap().get(getColumnIndex());
|
57
|
Map<NamedArea, Set<DescriptionElementBase>> distributionMap = editor.taxonDistributionMap.get(((TaxonDistributionDTO) rowWrapper).getTaxonUuid());
|
58
|
if (distributionMap == null){
|
59
|
distributionMap = new HashMap();
|
60
|
editor.taxonDistributionMap.put(((TaxonDistributionDTO) rowWrapper).getTaxonUuid(),distributionMap);
|
61
|
}
|
62
|
Set<DescriptionElementBase> elements = distributionMap.get(area);
|
63
|
if (elements == null){
|
64
|
|
65
|
}else if (elements.size() == 1){
|
66
|
DescriptionElementBase base = elements.iterator().next();
|
67
|
Distribution distr = (Distribution) base;
|
68
|
editor.descriptionsToSave.add( (TaxonDistributionDTO) rowWrapper);
|
69
|
//TODO set value corresponding to preference
|
70
|
if (distr.getStatus() != null){
|
71
|
if (distr.getStatus().getLabel() == ""){
|
72
|
distr.setStatus(null);
|
73
|
super.setCanonicalValue("");
|
74
|
|
75
|
}else{
|
76
|
super.setCanonicalValue(distr.getStatus().getLabel());
|
77
|
}
|
78
|
}
|
79
|
}
|
80
|
}
|
81
|
}else if (canonicalValue != null){
|
82
|
super.setCanonicalValue(canonicalValue);
|
83
|
}else{
|
84
|
Object rowWrapper = editor.getBodyDataProvider().getRowObject(this.getRowIndex());
|
85
|
Distribution newDistribution = Distribution.NewInstance();
|
86
|
NamedArea area = editor.getAreaToColumnIndexMap().get(getColumnIndex());
|
87
|
newDistribution.setArea(area);
|
88
|
if (editor.getDefaultSource() != null){
|
89
|
newDistribution.addSource(DescriptionElementSource.NewPrimarySourceInstance(editor.getDefaultSource(), null));
|
90
|
}
|
91
|
Map<NamedArea, Set<DescriptionElementBase>> distributionMap = editor.taxonDistributionMap.get(((TaxonDistributionDTO) rowWrapper).getTaxonUuid());
|
92
|
if (distributionMap == null){
|
93
|
distributionMap = new HashMap();
|
94
|
editor.taxonDistributionMap.put(((TaxonDistributionDTO) rowWrapper).getTaxonUuid(),distributionMap);
|
95
|
}
|
96
|
Set<DescriptionElementBase> elements = distributionMap.get(area);
|
97
|
if (elements == null){
|
98
|
elements = new HashSet<DescriptionElementBase>();
|
99
|
distributionMap.put(area, elements);
|
100
|
}
|
101
|
elements.add(newDistribution);
|
102
|
Set<TaxonDescription> descriptionSet = ((TaxonDistributionDTO) rowWrapper).getDescriptionsWrapper().getDescriptions();
|
103
|
Iterator<TaxonDescription> iterator = descriptionSet.iterator();
|
104
|
if (iterator.hasNext()){
|
105
|
TaxonDescription desc = iterator.next();
|
106
|
desc.addElement(newDistribution);
|
107
|
editor.descriptionsToSave.add((TaxonDistributionDTO) rowWrapper);
|
108
|
}else{
|
109
|
|
110
|
TaxonDescription desc = TaxonDescription.NewInstance();
|
111
|
desc.addElement(newDistribution);
|
112
|
((TaxonDistributionDTO) rowWrapper).getDescriptionsWrapper().getDescriptions().add(desc);
|
113
|
editor.descriptionsToSave.add((TaxonDistributionDTO) rowWrapper);
|
114
|
}
|
115
|
}
|
116
|
}
|
117
|
|
118
|
@Override
|
119
|
protected Control activateCell(Composite parent, Object originalCanonicalValue) {
|
120
|
if (originalCanonicalValue instanceof Distribution || originalCanonicalValue == null){
|
121
|
NatCombo natCombo = (NatCombo) super.activateCell(parent, originalCanonicalValue);
|
122
|
natCombo.addSelectionListener(new SelectionListener() {
|
123
|
@Override
|
124
|
public void widgetSelected(SelectionEvent e) {
|
125
|
editor.setDirty();
|
126
|
EventUtility.postEvent(WorkbenchEventConstants.REFRESH_DETAILS, true);
|
127
|
}
|
128
|
@Override
|
129
|
public void widgetDefaultSelected(SelectionEvent e) {
|
130
|
}
|
131
|
});
|
132
|
return natCombo;
|
133
|
}else{
|
134
|
return null;
|
135
|
}
|
136
|
|
137
|
|
138
|
}
|
139
|
}
|