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