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