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.commons.lang.StringUtils;
|
9
|
import org.apache.log4j.Logger;
|
10
|
import org.eclipse.jface.viewers.ArrayContentProvider;
|
11
|
import org.eclipse.jface.viewers.CellEditor;
|
12
|
import org.eclipse.jface.viewers.ComboBoxViewerCellEditor;
|
13
|
import org.eclipse.jface.viewers.EditingSupport;
|
14
|
import org.eclipse.jface.viewers.LabelProvider;
|
15
|
import org.eclipse.jface.viewers.TableViewer;
|
16
|
import org.eclipse.swt.SWT;
|
17
|
import org.eclipse.swt.widgets.Composite;
|
18
|
import org.eclipse.swt.widgets.TableColumn;
|
19
|
|
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 int columnIndex;
|
38
|
|
39
|
private static final Logger logger = Logger.getLogger(DistributionEditingSupport.class);
|
40
|
|
41
|
public DistributionEditingSupport(TableViewer viewer, ChecklistEditor checklistEditor, int columnIndex) {
|
42
|
super(viewer);
|
43
|
this.viewer = viewer;
|
44
|
this.columnIndex = columnIndex;
|
45
|
|
46
|
editor = checklistEditor;
|
47
|
cellEditor = new ComboBoxViewerCellEditor((Composite) viewer.getControl(), SWT.READ_ONLY);
|
48
|
cellEditor.setLabelProvider(new LabelProvider(){
|
49
|
@Override
|
50
|
public String getText(Object element) {
|
51
|
if (element instanceof PresenceAbsenceTerm) {
|
52
|
PresenceAbsenceTerm status = (PresenceAbsenceTerm)element;
|
53
|
String result = null;
|
54
|
if (PreferencesUtil.isShowSymbolInChecklistEditor() && !StringUtils.isBlank(status.getSymbol())){
|
55
|
result = status.getTitleCache() + "("+ status.getSymbol()+")";
|
56
|
}else{
|
57
|
result = status.getTitleCache();
|
58
|
}
|
59
|
return result;
|
60
|
}
|
61
|
return null;
|
62
|
}
|
63
|
});
|
64
|
cellEditor.setContentProvider(new ArrayContentProvider());
|
65
|
// TODO set input
|
66
|
// List<PresenceAbsenceTermBase> input = CdmStore.getTermManager()
|
67
|
// .getPreferredTerms(PresenceAbsenceTermBase.class);
|
68
|
List<DefinedTermBase<?>> input = CdmStore.getTermManager().getPreferredTerms(TermType.PresenceAbsenceTerm);
|
69
|
cellEditor.setInput(input);
|
70
|
}
|
71
|
|
72
|
@Override
|
73
|
protected CellEditor getCellEditor(Object element) {
|
74
|
return cellEditor;
|
75
|
}
|
76
|
|
77
|
@Override
|
78
|
protected boolean canEdit(Object element) {
|
79
|
return true;
|
80
|
}
|
81
|
|
82
|
@Override
|
83
|
protected Object getValue(Object element) {
|
84
|
if (element instanceof Taxon) {
|
85
|
Taxon taxon = (Taxon) element;
|
86
|
String result = null;
|
87
|
Distribution distributionForColumn = getDistributionForColumn(taxon);
|
88
|
|
89
|
if (distributionForColumn != null) {
|
90
|
PresenceAbsenceTerm status = distributionForColumn.getStatus();
|
91
|
if (PreferencesUtil.isShowIdInVocabularyInChecklistEditor()){
|
92
|
if (status.getIdInVocabulary() != null){
|
93
|
result = status.getIdInVocabulary();
|
94
|
} else{
|
95
|
result = status.getTitleCache();
|
96
|
}
|
97
|
} else{
|
98
|
result = status.getTitleCache();
|
99
|
}
|
100
|
if (PreferencesUtil.isShowSymbolInChecklistEditor() && status.getSymbol() != null){
|
101
|
result.concat(" ("+ status.getSymbol() + ")");
|
102
|
}
|
103
|
}
|
104
|
|
105
|
return result;
|
106
|
}
|
107
|
return null;
|
108
|
}
|
109
|
|
110
|
@Override
|
111
|
protected void setValue(Object element, Object value) {
|
112
|
if (element instanceof Taxon && value instanceof PresenceAbsenceTerm) {
|
113
|
Taxon taxon = (Taxon) element;
|
114
|
PresenceAbsenceTerm occurenceStatus = (PresenceAbsenceTerm) value;
|
115
|
Distribution distribution = getDistributionForColumn(taxon);
|
116
|
if (distribution != null) {
|
117
|
// change status for already exsiting distribution
|
118
|
distribution.setStatus(occurenceStatus);
|
119
|
} else {
|
120
|
createDistributionForColumn(taxon, occurenceStatus);
|
121
|
}
|
122
|
// viewer.update(distribution, null);
|
123
|
// viewer.update(element, null);
|
124
|
// viewer.refresh();
|
125
|
// editor.setDirty(true);
|
126
|
editor.changed(element);
|
127
|
viewer.refresh();
|
128
|
}
|
129
|
}
|
130
|
|
131
|
private static final List<String> DESC_INIT_STRATEGY = Arrays.asList(new String[] { "descriptions", //$NON-NLS-1$
|
132
|
"descriptions.*", "description.state" }); //$NON-NLS-1$ //$NON-NLS-2$
|
133
|
|
134
|
/**
|
135
|
*
|
136
|
* @param taxon
|
137
|
* @return
|
138
|
*/
|
139
|
private Distribution getDistributionForColumn(Taxon taxon) {
|
140
|
// List<TaxonDescription> listTaxonDescriptions = descriptionService.listTaxonDescriptions(taxon, null, null,
|
141
|
// null, null, null, DESC_INIT_STRATEGY);
|
142
|
Set<TaxonDescription> listTaxonDescriptions = taxon.getDescriptions();
|
143
|
TableColumn column = viewer.getTable().getColumn(columnIndex);
|
144
|
for (TaxonDescription td : listTaxonDescriptions) {
|
145
|
for (DescriptionElementBase deb : td.getElements()) {
|
146
|
if (deb instanceof Distribution) {
|
147
|
Distribution distribution = (Distribution) deb;
|
148
|
if (distribution.getArea() != null){
|
149
|
if (PreferencesUtil.isShowIdInVocabularyInChecklistEditor()){
|
150
|
if (column.getText().equalsIgnoreCase(distribution.getArea().getIdInVocabulary())) {
|
151
|
return distribution;
|
152
|
}
|
153
|
}else{
|
154
|
if (column.getText().equalsIgnoreCase(distribution.getArea().getTitleCache())) {
|
155
|
return distribution;
|
156
|
}
|
157
|
}
|
158
|
}
|
159
|
}
|
160
|
}
|
161
|
}
|
162
|
return null;
|
163
|
}
|
164
|
|
165
|
private void createDistributionForColumn(Taxon taxon, PresenceAbsenceTerm occurenceStatus) {
|
166
|
TableColumn column = viewer.getTable().getColumn(columnIndex);
|
167
|
|
168
|
SortedSet<DefinedTermBase> namedAreas = this.editor.getLabelProvider().getNamedAreas(false);
|
169
|
|
170
|
|
171
|
if (namedAreas != null) {
|
172
|
for (DefinedTermBase term : namedAreas) {
|
173
|
boolean sameArea = true;
|
174
|
if (!PreferencesUtil.isShowIdInVocabularyInChecklistEditor()){
|
175
|
sameArea = column.getText().equalsIgnoreCase(term.getTitleCache());
|
176
|
} else{
|
177
|
if (term.getIdInVocabulary() != null){
|
178
|
sameArea = column.getText().equalsIgnoreCase(term.getIdInVocabulary());
|
179
|
} else{
|
180
|
sameArea = column.getText().equalsIgnoreCase(term.getTitleCache());
|
181
|
}
|
182
|
}
|
183
|
if (sameArea) {
|
184
|
NamedArea area = (NamedArea) term;
|
185
|
Distribution distribution = Distribution.NewInstance(area, occurenceStatus);
|
186
|
Set<TaxonDescription> descriptions = taxon.getDescriptions();
|
187
|
if (!descriptions.isEmpty()) {
|
188
|
for (TaxonDescription desc : descriptions) {
|
189
|
// add to first taxon description
|
190
|
desc.addElement(distribution);
|
191
|
break;
|
192
|
}
|
193
|
} else {// there are no TaxonDescription yet.
|
194
|
TaxonDescription td = TaxonDescription.NewInstance(taxon);
|
195
|
td.addElement(distribution);
|
196
|
break;
|
197
|
}
|
198
|
}
|
199
|
}
|
200
|
}
|
201
|
|
202
|
return;
|
203
|
}
|
204
|
}
|