1
|
package eu.etaxonomy.taxeditor.editor.view.checklist.edit;
|
2
|
|
3
|
import java.util.Arrays;
|
4
|
import java.util.List;
|
5
|
import java.util.Set;
|
6
|
import java.util.SortedSet;
|
7
|
|
8
|
import org.apache.log4j.Logger;
|
9
|
import org.eclipse.jface.viewers.ArrayContentProvider;
|
10
|
import org.eclipse.jface.viewers.CellEditor;
|
11
|
import org.eclipse.jface.viewers.ComboBoxViewerCellEditor;
|
12
|
import org.eclipse.jface.viewers.EditingSupport;
|
13
|
import org.eclipse.jface.viewers.LabelProvider;
|
14
|
import org.eclipse.jface.viewers.TableViewer;
|
15
|
import org.eclipse.swt.SWT;
|
16
|
import org.eclipse.swt.widgets.Composite;
|
17
|
import org.eclipse.swt.widgets.TableColumn;
|
18
|
|
19
|
import eu.etaxonomy.cdm.api.service.IDescriptionService;
|
20
|
import eu.etaxonomy.cdm.model.common.DefinedTermBase;
|
21
|
import eu.etaxonomy.cdm.model.common.TermType;
|
22
|
import eu.etaxonomy.cdm.model.description.DescriptionElementBase;
|
23
|
import eu.etaxonomy.cdm.model.description.Distribution;
|
24
|
import eu.etaxonomy.cdm.model.description.PresenceAbsenceTerm;
|
25
|
import eu.etaxonomy.cdm.model.description.TaxonDescription;
|
26
|
import eu.etaxonomy.cdm.model.location.NamedArea;
|
27
|
import eu.etaxonomy.cdm.model.taxon.Taxon;
|
28
|
import eu.etaxonomy.taxeditor.editor.view.checklist.ChecklistEditor;
|
29
|
import eu.etaxonomy.taxeditor.preference.PreferencesUtil;
|
30
|
import eu.etaxonomy.taxeditor.store.CdmStore;
|
31
|
|
32
|
public final class DistributionEditingSupport extends EditingSupport {
|
33
|
|
34
|
private ComboBoxViewerCellEditor cellEditor = null;
|
35
|
private final TableViewer viewer;
|
36
|
private final ChecklistEditor editor;
|
37
|
private final IDescriptionService descriptionService;
|
38
|
private final int columnIndex;
|
39
|
|
40
|
private static final Logger logger = Logger.getLogger(DistributionEditingSupport.class);
|
41
|
|
42
|
public DistributionEditingSupport(TableViewer viewer, ChecklistEditor checklistEditor, int columnIndex) {
|
43
|
super(viewer);
|
44
|
this.viewer = viewer;
|
45
|
this.columnIndex = columnIndex;
|
46
|
descriptionService = CdmStore.getService(IDescriptionService.class);
|
47
|
editor = checklistEditor;
|
48
|
cellEditor = new ComboBoxViewerCellEditor((Composite) viewer.getControl(), SWT.READ_ONLY);
|
49
|
cellEditor.setLabelProvider(new LabelProvider());
|
50
|
cellEditor.setContentProvider(new ArrayContentProvider());
|
51
|
// TODO set input
|
52
|
// List<PresenceAbsenceTermBase> input = CdmStore.getTermManager()
|
53
|
// .getPreferredTerms(PresenceAbsenceTermBase.class);
|
54
|
List<DefinedTermBase<?>> input = CdmStore.getTermManager().getPreferredTerms(TermType.PresenceAbsenceTerm);
|
55
|
cellEditor.setInput(input);
|
56
|
}
|
57
|
|
58
|
@Override
|
59
|
protected CellEditor getCellEditor(Object element) {
|
60
|
return cellEditor;
|
61
|
}
|
62
|
|
63
|
@Override
|
64
|
protected boolean canEdit(Object element) {
|
65
|
return true;
|
66
|
}
|
67
|
|
68
|
@Override
|
69
|
protected Object getValue(Object element) {
|
70
|
if (element instanceof Taxon) {
|
71
|
Taxon taxon = (Taxon) element;
|
72
|
Distribution distributionForColumn = getDistributionForColumn(taxon);
|
73
|
if (distributionForColumn != null) {
|
74
|
return distributionForColumn.getStatus().getTitleCache();
|
75
|
}
|
76
|
// return PresenceAbsenceTerms
|
77
|
}
|
78
|
return null;
|
79
|
}
|
80
|
|
81
|
@Override
|
82
|
protected void setValue(Object element, Object value) {
|
83
|
if (element instanceof Taxon && value instanceof PresenceAbsenceTerm) {
|
84
|
Taxon taxon = (Taxon) element;
|
85
|
PresenceAbsenceTerm occurenceStatus = (PresenceAbsenceTerm) value;
|
86
|
Distribution distribution = getDistributionForColumn(taxon);
|
87
|
if (distribution != null) {
|
88
|
// change status for already exsiting distribution
|
89
|
distribution.setStatus(occurenceStatus);
|
90
|
} else {
|
91
|
createDistributionForColumn(taxon, occurenceStatus);
|
92
|
}
|
93
|
// viewer.update(distribution, null);
|
94
|
// viewer.update(element, null);
|
95
|
// viewer.refresh();
|
96
|
// editor.setDirty(true);
|
97
|
editor.changed(element);
|
98
|
viewer.refresh();
|
99
|
}
|
100
|
}
|
101
|
|
102
|
private static final List<String> DESC_INIT_STRATEGY = Arrays.asList(new String[] { "descriptions",
|
103
|
"descriptions.*", "description.state" });
|
104
|
|
105
|
/**
|
106
|
*
|
107
|
* @param taxon
|
108
|
* @return
|
109
|
*/
|
110
|
private Distribution getDistributionForColumn(Taxon taxon) {
|
111
|
List<TaxonDescription> listTaxonDescriptions = descriptionService.listTaxonDescriptions(taxon, null, null,
|
112
|
null, null, null, DESC_INIT_STRATEGY);
|
113
|
TableColumn column = viewer.getTable().getColumn(columnIndex);
|
114
|
for (TaxonDescription td : listTaxonDescriptions) {
|
115
|
for (DescriptionElementBase deb : td.getElements()) {
|
116
|
if (deb instanceof Distribution) {
|
117
|
Distribution distribution = (Distribution) deb;
|
118
|
if (distribution.getArea() != null){
|
119
|
if (PreferencesUtil.isShowIdInVocabularyInChecklistEditor()){
|
120
|
if (column.getText().equalsIgnoreCase(distribution.getArea().getIdInVocabulary())) {
|
121
|
return distribution;
|
122
|
}
|
123
|
}else{
|
124
|
if (column.getText().equalsIgnoreCase(distribution.getArea().getTitleCache())) {
|
125
|
return distribution;
|
126
|
}
|
127
|
}
|
128
|
}
|
129
|
}
|
130
|
}
|
131
|
}
|
132
|
return null;
|
133
|
}
|
134
|
|
135
|
private void createDistributionForColumn(Taxon taxon, PresenceAbsenceTerm occurenceStatus) {
|
136
|
TableColumn column = viewer.getTable().getColumn(columnIndex);
|
137
|
|
138
|
SortedSet<DefinedTermBase> namedAreas = this.editor.getLabelProvider().getNamedAreas();
|
139
|
|
140
|
|
141
|
if (namedAreas != null) {
|
142
|
for (DefinedTermBase term : namedAreas) {
|
143
|
boolean sameArea = true;
|
144
|
if (!PreferencesUtil.isShowIdInVocabularyInChecklistEditor()){
|
145
|
sameArea = column.getText().equalsIgnoreCase(term.getTitleCache());
|
146
|
} else{
|
147
|
sameArea = column.getText().equalsIgnoreCase(term.getIdInVocabulary());
|
148
|
}
|
149
|
if (sameArea) {
|
150
|
NamedArea area = (NamedArea) term;
|
151
|
Distribution distribution = Distribution.NewInstance(area, occurenceStatus);
|
152
|
Set<TaxonDescription> descriptions = taxon.getDescriptions();
|
153
|
if (!descriptions.isEmpty()) {
|
154
|
for (TaxonDescription desc : descriptions) {
|
155
|
// add to first taxon description
|
156
|
desc.addElement(distribution);
|
157
|
break;
|
158
|
}
|
159
|
} else {// there are no TaxonDescription yet.
|
160
|
TaxonDescription td = TaxonDescription.NewInstance(taxon);
|
161
|
td.addElement(distribution);
|
162
|
break;
|
163
|
}
|
164
|
}
|
165
|
}
|
166
|
}
|
167
|
|
168
|
return;
|
169
|
}
|
170
|
}
|