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