1
|
package eu.etaxonomy.taxeditor.editor.view.checklist.e4;
|
2
|
|
3
|
import java.util.ArrayList;
|
4
|
import java.util.Arrays;
|
5
|
import java.util.Collection;
|
6
|
import java.util.Comparator;
|
7
|
import java.util.List;
|
8
|
import java.util.Map;
|
9
|
import java.util.Set;
|
10
|
import java.util.SortedSet;
|
11
|
import java.util.UUID;
|
12
|
|
13
|
import javax.inject.Inject;
|
14
|
|
15
|
import org.apache.commons.lang.StringUtils;
|
16
|
import org.apache.log4j.Logger;
|
17
|
import org.eclipse.e4.ui.workbench.modeling.ESelectionService;
|
18
|
import org.eclipse.jface.viewers.ArrayContentProvider;
|
19
|
import org.eclipse.jface.viewers.CellEditor;
|
20
|
import org.eclipse.jface.viewers.ComboBoxViewerCellEditor;
|
21
|
import org.eclipse.jface.viewers.EditingSupport;
|
22
|
import org.eclipse.jface.viewers.ISelection;
|
23
|
import org.eclipse.jface.viewers.ISelectionChangedListener;
|
24
|
import org.eclipse.jface.viewers.LabelProvider;
|
25
|
import org.eclipse.jface.viewers.StructuredSelection;
|
26
|
import org.eclipse.jface.viewers.TableViewer;
|
27
|
import org.eclipse.swt.SWT;
|
28
|
import org.eclipse.swt.widgets.Composite;
|
29
|
import org.eclipse.swt.widgets.TableColumn;
|
30
|
|
31
|
import eu.etaxonomy.cdm.model.common.DefinedTermBase;
|
32
|
import eu.etaxonomy.cdm.model.common.OrderedTermComparator;
|
33
|
import eu.etaxonomy.cdm.model.common.Representation;
|
34
|
import eu.etaxonomy.cdm.model.common.TermType;
|
35
|
import eu.etaxonomy.cdm.model.description.DescriptionElementBase;
|
36
|
import eu.etaxonomy.cdm.model.description.DescriptionElementSource;
|
37
|
import eu.etaxonomy.cdm.model.description.Distribution;
|
38
|
import eu.etaxonomy.cdm.model.description.PresenceAbsenceTerm;
|
39
|
import eu.etaxonomy.cdm.model.description.TaxonDescription;
|
40
|
import eu.etaxonomy.cdm.model.location.NamedArea;
|
41
|
import eu.etaxonomy.cdm.model.taxon.Taxon;
|
42
|
import eu.etaxonomy.taxeditor.event.EventUtility;
|
43
|
import eu.etaxonomy.taxeditor.event.WorkbenchEventConstants;
|
44
|
import eu.etaxonomy.taxeditor.preference.PreferencesUtil;
|
45
|
import eu.etaxonomy.taxeditor.store.CdmStore;
|
46
|
|
47
|
public final class DistributionEditingSupportE4 extends EditingSupport {
|
48
|
|
49
|
private ComboBoxViewerCellEditor cellEditor = null;
|
50
|
//private final TableViewer viewer;
|
51
|
private final ChecklistEditorE4 editor;
|
52
|
// private final IDescriptionService descriptionService;
|
53
|
private final int columnIndex;
|
54
|
|
55
|
private Taxon taxon;
|
56
|
|
57
|
@Inject
|
58
|
private ESelectionService selService;
|
59
|
private ISelectionChangedListener selectionChangedListener;
|
60
|
|
61
|
private static final Logger logger = Logger.getLogger(DistributionEditingSupportE4.class);
|
62
|
|
63
|
public DistributionEditingSupportE4(TableViewer viewer, ChecklistEditorE4 checklistEditor, int columnIndex) {
|
64
|
super(viewer);
|
65
|
|
66
|
this.columnIndex = columnIndex;
|
67
|
|
68
|
editor = checklistEditor;
|
69
|
cellEditor = new ComboBoxViewerCellEditor((Composite) this.getViewer().getControl(), SWT.READ_ONLY);
|
70
|
cellEditor.setLabelProvider(new LabelProvider(){
|
71
|
@Override
|
72
|
public String getText(Object element) {
|
73
|
if (element instanceof PresenceAbsenceTerm) {
|
74
|
PresenceAbsenceTerm status = (PresenceAbsenceTerm)element;
|
75
|
Representation rep = status.getPreferredRepresentation(CdmStore.getDefaultLanguage());
|
76
|
String label = null;
|
77
|
if (rep != null){
|
78
|
label = rep.getAbbreviatedLabel();
|
79
|
if (label == null){
|
80
|
label = rep.getLabel();
|
81
|
}
|
82
|
}else{
|
83
|
label = status.getTitleCache();
|
84
|
}
|
85
|
String result = null;
|
86
|
if (PreferencesUtil.isShowSymbolInChecklistEditor() && !StringUtils.isBlank(status.getSymbol())){
|
87
|
result = label + "("+ status.getSymbol()+")";
|
88
|
}else{
|
89
|
result = label;
|
90
|
}
|
91
|
return result;
|
92
|
}
|
93
|
return null;
|
94
|
}
|
95
|
});
|
96
|
|
97
|
cellEditor.setContentProvider(new ArrayContentProvider());
|
98
|
|
99
|
|
100
|
List<DefinedTermBase> inputAll = new ArrayList<>();
|
101
|
PresenceAbsenceTerm noneTerm = PresenceAbsenceTerm.NewInstance();
|
102
|
noneTerm.setTitleCache(" ", true);
|
103
|
inputAll.add(noneTerm);
|
104
|
Collection<DefinedTermBase> input = CdmStore.getTermManager().getPreferredTerms(TermType.PresenceAbsenceTerm);
|
105
|
List<DefinedTermBase> inputList = new ArrayList(input);
|
106
|
Comparator<DefinedTermBase> comp = new OrderedTermComparator<>();
|
107
|
// Collections.sort(inputList, comp);
|
108
|
inputAll.addAll(1,inputList);
|
109
|
|
110
|
|
111
|
cellEditor.setInput(inputAll);
|
112
|
|
113
|
|
114
|
|
115
|
}
|
116
|
|
117
|
@Override
|
118
|
protected CellEditor getCellEditor(Object element) {
|
119
|
return cellEditor;
|
120
|
}
|
121
|
|
122
|
@Override
|
123
|
protected boolean canEdit(Object element) {
|
124
|
return true;
|
125
|
}
|
126
|
|
127
|
@Override
|
128
|
protected PresenceAbsenceTerm getValue(Object element) {
|
129
|
|
130
|
if (element instanceof Taxon) {
|
131
|
taxon = (Taxon) element;
|
132
|
String result = null;
|
133
|
Distribution distributionForColumn = getDistributionForColumn(taxon);
|
134
|
|
135
|
if (distributionForColumn != null) {
|
136
|
PresenceAbsenceTerm status = distributionForColumn.getStatus();
|
137
|
return status;
|
138
|
|
139
|
}
|
140
|
|
141
|
//fireStateChanged(distributionForColumn);
|
142
|
return null;
|
143
|
}
|
144
|
return null;
|
145
|
}
|
146
|
|
147
|
|
148
|
|
149
|
protected void fireStateChanged() {
|
150
|
ISelection selection = new StructuredSelection(getDistributionForColumn(taxon));
|
151
|
getViewer().setSelection(selection);
|
152
|
|
153
|
}
|
154
|
|
155
|
@Override
|
156
|
protected void setValue(Object element, Object value) {
|
157
|
if (element instanceof Taxon && value instanceof PresenceAbsenceTerm) {
|
158
|
|
159
|
Taxon taxon = (Taxon) element;
|
160
|
PresenceAbsenceTerm occurenceStatus = (PresenceAbsenceTerm) value;
|
161
|
Distribution distribution = getDistributionForColumn(taxon);
|
162
|
|
163
|
if (distribution != null) {
|
164
|
// change status for already exsiting distribution
|
165
|
if (occurenceStatus.getTitleCache().equals(" ")){
|
166
|
TaxonDescription descr = (TaxonDescription)distribution.getInDescription();
|
167
|
descr.removeElement(distribution);
|
168
|
}else{
|
169
|
distribution.setStatus(occurenceStatus);
|
170
|
}
|
171
|
} else {
|
172
|
if (!occurenceStatus.getTitleCache().equals(" ")){
|
173
|
createDistributionForColumn(taxon, occurenceStatus);
|
174
|
}
|
175
|
}
|
176
|
|
177
|
|
178
|
editor.changed(element);
|
179
|
getViewer().refresh();
|
180
|
|
181
|
EventUtility.postEvent(WorkbenchEventConstants.REFRESH_TAXON_DETAILS, true);
|
182
|
}
|
183
|
}
|
184
|
|
185
|
private static final List<String> DESC_INIT_STRATEGY = Arrays.asList(new String[] { "descriptions", //$NON-NLS-1$
|
186
|
"descriptions.*", "description.state" }); //$NON-NLS-1$ //$NON-NLS-2$
|
187
|
|
188
|
/**
|
189
|
*
|
190
|
* @param taxon
|
191
|
* @return
|
192
|
*/
|
193
|
private Distribution getDistributionForColumn(Taxon taxon) {
|
194
|
|
195
|
Set<TaxonDescription> listTaxonDescriptions = taxon.getDescriptions();
|
196
|
TableColumn column = ((TableViewer)getViewer()).getTable().getColumn(columnIndex);
|
197
|
for (TaxonDescription td : listTaxonDescriptions) {
|
198
|
for (DescriptionElementBase deb : td.getElements()) {
|
199
|
if (deb instanceof Distribution) {
|
200
|
Distribution distribution = (Distribution) deb;
|
201
|
UUID areaUuid = null;
|
202
|
for (Map.Entry<UUID,Integer> entry: editor.getAreaPosition().entrySet()){
|
203
|
if (entry.getValue().intValue()== columnIndex){
|
204
|
areaUuid = entry.getKey();
|
205
|
break;
|
206
|
}
|
207
|
}
|
208
|
if (distribution.getArea() != null){
|
209
|
if (distribution.getArea().getUuid().equals(areaUuid)){
|
210
|
return distribution;
|
211
|
|
212
|
}
|
213
|
}
|
214
|
}
|
215
|
}
|
216
|
}
|
217
|
return null;
|
218
|
}
|
219
|
|
220
|
private void createDistributionForColumn(Taxon taxon, PresenceAbsenceTerm occurenceStatus) {
|
221
|
TableColumn column = ((TableViewer)this.getViewer()).getTable().getColumn(columnIndex);
|
222
|
|
223
|
SortedSet<DefinedTermBase> namedAreas = this.editor.getLabelProvider().getNamedAreas(false);
|
224
|
|
225
|
|
226
|
if (namedAreas != null) {
|
227
|
for (DefinedTermBase term : namedAreas) {
|
228
|
Integer areaIndex = editor.getAreaPosition().get(term.getUuid());
|
229
|
if (areaIndex == columnIndex){
|
230
|
NamedArea area = (NamedArea) term;
|
231
|
Distribution distribution = Distribution.NewInstance(area, occurenceStatus);
|
232
|
if (editor.getDefaultSource() != null){
|
233
|
DescriptionElementSource source = DescriptionElementSource.NewPrimarySourceInstance(editor.getDefaultSource(), "");
|
234
|
distribution.addSource(source);
|
235
|
}
|
236
|
Set<TaxonDescription> descriptions = taxon.getDescriptions();
|
237
|
if (!descriptions.isEmpty()) {
|
238
|
for (TaxonDescription desc : descriptions) {
|
239
|
// add to first taxon description
|
240
|
desc.addElement(distribution);
|
241
|
break;
|
242
|
}
|
243
|
} else {// there are no TaxonDescription yet.
|
244
|
TaxonDescription td = TaxonDescription.NewInstance(taxon);
|
245
|
td.addElement(distribution);
|
246
|
break;
|
247
|
}
|
248
|
|
249
|
}
|
250
|
|
251
|
}
|
252
|
}
|
253
|
|
254
|
return;
|
255
|
}
|
256
|
|
257
|
|
258
|
}
|