1
|
/**
|
2
|
* Copyright (C) 2007 EDIT
|
3
|
* European Distributed Institute of Taxonomy
|
4
|
* http://www.e-taxonomy.eu
|
5
|
*
|
6
|
* The contents of this file are subject to the Mozilla Public License Version 1.1
|
7
|
* See LICENSE.TXT at the top of this package for the full license terms.
|
8
|
*/
|
9
|
|
10
|
package eu.etaxonomy.taxeditor.editor.view.checklist;
|
11
|
|
12
|
import java.util.ArrayList;
|
13
|
import java.util.Arrays;
|
14
|
import java.util.HashMap;
|
15
|
import java.util.HashSet;
|
16
|
import java.util.List;
|
17
|
import java.util.Set;
|
18
|
import java.util.SortedSet;
|
19
|
import java.util.TreeSet;
|
20
|
import java.util.UUID;
|
21
|
|
22
|
import org.eclipse.jface.preference.IPreferenceStore;
|
23
|
import org.eclipse.jface.viewers.ITableLabelProvider;
|
24
|
import org.eclipse.jface.viewers.LabelProvider;
|
25
|
import org.eclipse.jface.viewers.TableViewer;
|
26
|
import org.eclipse.swt.graphics.Image;
|
27
|
import org.eclipse.swt.widgets.TableColumn;
|
28
|
|
29
|
import eu.etaxonomy.cdm.api.service.IDescriptionService;
|
30
|
import eu.etaxonomy.cdm.api.service.IVocabularyService;
|
31
|
import eu.etaxonomy.cdm.common.CdmUtils;
|
32
|
import eu.etaxonomy.cdm.hibernate.HibernateProxyHelper;
|
33
|
import eu.etaxonomy.cdm.model.common.DefinedTermBase;
|
34
|
import eu.etaxonomy.cdm.model.common.Language;
|
35
|
import eu.etaxonomy.cdm.model.common.OrderedTermBase;
|
36
|
import eu.etaxonomy.cdm.model.common.OrderedTermComparator;
|
37
|
import eu.etaxonomy.cdm.model.common.TermIdInVocabularyComparator;
|
38
|
import eu.etaxonomy.cdm.model.common.TermLanguageComparator;
|
39
|
import eu.etaxonomy.cdm.model.common.TermVocabulary;
|
40
|
import eu.etaxonomy.cdm.model.description.DescriptionElementBase;
|
41
|
import eu.etaxonomy.cdm.model.description.Distribution;
|
42
|
import eu.etaxonomy.cdm.model.description.TaxonDescription;
|
43
|
import eu.etaxonomy.cdm.model.name.TaxonName;
|
44
|
import eu.etaxonomy.cdm.model.taxon.Taxon;
|
45
|
import eu.etaxonomy.taxeditor.preference.PreferencesUtil;
|
46
|
import eu.etaxonomy.taxeditor.store.CdmStore;
|
47
|
|
48
|
/**
|
49
|
*
|
50
|
* @author a.oppermann
|
51
|
* @created 30.04.2014
|
52
|
*/
|
53
|
public class ChecklistLabelProvider extends LabelProvider implements ITableLabelProvider {
|
54
|
|
55
|
public static final String DEFAULT_ENTRY = ""; //$NON-NLS-1$
|
56
|
private IDescriptionService descriptionService;
|
57
|
private SortedSet<DefinedTermBase> namedAreas;
|
58
|
TableViewer viewer = null;
|
59
|
|
60
|
/**
|
61
|
*
|
62
|
*/
|
63
|
public ChecklistLabelProvider() {
|
64
|
namedAreas = loadNamedAreas();
|
65
|
}
|
66
|
|
67
|
public ChecklistLabelProvider(TableViewer viewer) {
|
68
|
namedAreas = loadNamedAreas();
|
69
|
this.viewer = viewer;
|
70
|
}
|
71
|
|
72
|
/*
|
73
|
* (non-Javadoc)
|
74
|
*
|
75
|
* @see
|
76
|
* org.eclipse.jface.viewers.ITableLabelProvider#getColumnImage(java.lang
|
77
|
* .Object, int)
|
78
|
*/
|
79
|
/** {@inheritDoc} */
|
80
|
@Override
|
81
|
public Image getColumnImage(Object element, int columnIndex) {
|
82
|
// TODO
|
83
|
return null;
|
84
|
}
|
85
|
|
86
|
/** {@inheritDoc} */
|
87
|
@Override
|
88
|
public String getColumnText(Object element, int columnIndex) {
|
89
|
// descriptionService = CdmStore.getService(IDescriptionService.class);
|
90
|
Taxon taxon = (Taxon) element;
|
91
|
//TODO load areas by this list
|
92
|
|
93
|
// List<TaxonDescription> listTaxonDescriptions = descriptionService.listTaxonDescriptions(taxon, null, null, null, null, null, DESC_INIT_STRATEGY);;
|
94
|
Set<TaxonDescription> listTaxonDescriptions = taxon.getDescriptions();
|
95
|
TaxonName nonViralName = HibernateProxyHelper.deproxy(taxon.getName());
|
96
|
|
97
|
switch (columnIndex) {
|
98
|
case 0:
|
99
|
String taxonName = null;
|
100
|
taxonName = (nonViralName != null) ? nonViralName.getNameCache() : null;
|
101
|
return (taxonName != null) ? taxonName : DEFAULT_ENTRY;
|
102
|
|
103
|
// case 1:
|
104
|
// String authorship = null;
|
105
|
// authorship = (nonViralName != null) ? nonViralName.getAuthorshipCache() : null;
|
106
|
// return (authorship != null) ? authorship : DEFAULT_ENTRY;
|
107
|
// case 2:
|
108
|
// String ref = null;
|
109
|
// if (taxon.getName() != null) {
|
110
|
// INomenclaturalReference nomenclaturalReference = HibernateProxyHelper.deproxy(
|
111
|
// nonVirlaName.getNomenclaturalReference(), Reference.class);
|
112
|
// ref = (nomenclaturalReference != null) ? nomenclaturalReference.getAbbrevTitleCache() : null;
|
113
|
// }
|
114
|
// return (ref != null) ? ref : DEFAULT_ENTRY;
|
115
|
case 1:
|
116
|
if (PreferencesUtil.isShowRankInChecklistEditor()){
|
117
|
String rank = null;
|
118
|
|
119
|
if (taxon.getName().getRank() != null) {
|
120
|
rank = taxon.getName().getRank().toString();
|
121
|
}
|
122
|
return (rank != null) ? rank : DEFAULT_ENTRY;
|
123
|
}
|
124
|
}
|
125
|
|
126
|
if(columnIndex >2 || (!PreferencesUtil.isShowRankInChecklistEditor() && columnIndex > 0)){
|
127
|
|
128
|
for (TaxonDescription td : listTaxonDescriptions) {
|
129
|
for (DescriptionElementBase deb : td.getElements()) {
|
130
|
if (deb instanceof Distribution) {
|
131
|
Distribution distribution = HibernateProxyHelper.deproxy(deb, Distribution.class);
|
132
|
String area = null;
|
133
|
if (distribution.getArea() != null ){
|
134
|
if (PreferencesUtil.isShowIdInVocabularyInChecklistEditor()){
|
135
|
if (distribution.getArea().getIdInVocabulary() != null){
|
136
|
area = distribution.getArea().getIdInVocabulary();
|
137
|
} else{
|
138
|
area = distribution.getArea().getTitleCache();
|
139
|
}
|
140
|
}else{
|
141
|
area = distribution.getArea().getTitleCache();
|
142
|
}
|
143
|
|
144
|
}else{
|
145
|
continue;
|
146
|
}
|
147
|
|
148
|
if(viewer != null){
|
149
|
TableColumn column = viewer.getTable().getColumn(columnIndex);
|
150
|
if (area.equalsIgnoreCase(column.getText())) {
|
151
|
if (PreferencesUtil.isShowIdInVocabularyInChecklistEditor()){
|
152
|
return (distribution.getStatus().getIdInVocabulary() != null)?distribution.getStatus().getIdInVocabulary():CdmUtils.Nz(distribution.getStatus().getTitleCache());
|
153
|
}else{
|
154
|
return (distribution.getStatus().getTitleCache() != null)?distribution.getStatus().getTitleCache():DEFAULT_ENTRY;
|
155
|
}
|
156
|
}
|
157
|
|
158
|
}
|
159
|
}
|
160
|
}
|
161
|
}
|
162
|
}
|
163
|
return DEFAULT_ENTRY;
|
164
|
|
165
|
}
|
166
|
|
167
|
private static final List<String> DESC_INIT_STRATEGY = Arrays.asList(new String[] { "descriptions", //$NON-NLS-1$
|
168
|
"descriptions.*", "description.state" }); //$NON-NLS-1$ //$NON-NLS-2$
|
169
|
|
170
|
|
171
|
private SortedSet<DefinedTermBase> loadNamedAreas() {
|
172
|
IPreferenceStore preferenceStore = PreferencesUtil.getPreferenceStore();
|
173
|
|
174
|
String valuesAreas = preferenceStore.getString(PreferencesUtil.DISTRIBUTION_AREA_OCCURENCE_STATUS);
|
175
|
String values = preferenceStore.getString(PreferencesUtil.DISTRIBUTION_VOCABULARIES);
|
176
|
if (values != null && values != "") { //$NON-NLS-1$
|
177
|
String[] split = values.split(";"); //$NON-NLS-1$
|
178
|
List<String> listValue = Arrays.asList(split);
|
179
|
List<DefinedTermBase> termlist = new ArrayList<DefinedTermBase>();
|
180
|
Set<UUID> uuidList = new HashSet<UUID>();
|
181
|
UUID uuid;
|
182
|
for(String s : listValue){
|
183
|
uuid = UUID.fromString(s);
|
184
|
uuidList.add(uuid);
|
185
|
|
186
|
}
|
187
|
IVocabularyService service = CdmStore.getService(IVocabularyService.class);
|
188
|
List<TermVocabulary> vocs = service.find(uuidList);
|
189
|
split = valuesAreas.split(";");
|
190
|
listValue = Arrays.asList(split);
|
191
|
for (TermVocabulary voc: vocs){
|
192
|
termlist.addAll(service.getTerms(voc, null, null, null, null).getRecords());
|
193
|
}
|
194
|
List<DefinedTermBase> filteredList = new ArrayList();
|
195
|
for (DefinedTermBase area: termlist){
|
196
|
if (listValue.contains(area.getUuid().toString())) {
|
197
|
filteredList.add(area);
|
198
|
}
|
199
|
|
200
|
}
|
201
|
|
202
|
if (PreferencesUtil.isSortNamedAreaByOrderInVocabulary()){
|
203
|
return getTermsOrderedByVocabularyOrder(filteredList);
|
204
|
} else if (PreferencesUtil.isShowIdInVocabularyInChecklistEditor()){
|
205
|
return getTermsOrderedByIdInVocabulary(filteredList);
|
206
|
}else{
|
207
|
return getTermsOrderedByLabels(filteredList, CdmStore.getDefaultLanguage());
|
208
|
}
|
209
|
}
|
210
|
return null;
|
211
|
}
|
212
|
|
213
|
public SortedSet<DefinedTermBase> getTermsOrderedByLabels(List<DefinedTermBase> listTerm,Language language){
|
214
|
TermLanguageComparator<?> comp = new TermLanguageComparator<>();
|
215
|
comp.setCompareLanguage(language);
|
216
|
SortedSet<DefinedTermBase> result = new TreeSet(comp);
|
217
|
if(listTerm != null){
|
218
|
result.addAll(listTerm);
|
219
|
}
|
220
|
return result;
|
221
|
}
|
222
|
|
223
|
public SortedSet<DefinedTermBase> getTermsOrderedByVocabularyOrder(List<DefinedTermBase> listAreas){
|
224
|
HashMap<TermVocabulary<DefinedTermBase>, List<DefinedTermBase>> vocs = new HashMap<>();
|
225
|
OrderedTermComparator<?> comp = new OrderedTermComparator<>();
|
226
|
boolean allOrderedTerms = true;
|
227
|
List<TermVocabulary> alreadyOrderIndexNull = new ArrayList<>();
|
228
|
for (DefinedTermBase term: listAreas){
|
229
|
if (!(term instanceof OrderedTermBase)){
|
230
|
allOrderedTerms = false;
|
231
|
break;
|
232
|
}else if (((OrderedTermBase)term).getOrderIndex() == 0){
|
233
|
if(alreadyOrderIndexNull.contains(term.getVocabulary())) {
|
234
|
allOrderedTerms = false;
|
235
|
break;
|
236
|
}else{
|
237
|
alreadyOrderIndexNull.add(term.getVocabulary());
|
238
|
}
|
239
|
|
240
|
|
241
|
}
|
242
|
}
|
243
|
if (allOrderedTerms){
|
244
|
SortedSet<DefinedTermBase> result = new TreeSet(comp.reversed());
|
245
|
result.addAll(listAreas);
|
246
|
return result;
|
247
|
}else{
|
248
|
return getTermsOrderedByLabels(listAreas, PreferencesUtil.getGlobalLanguage());
|
249
|
}
|
250
|
|
251
|
|
252
|
}
|
253
|
|
254
|
/**
|
255
|
* @return the namedAreas
|
256
|
*/
|
257
|
public SortedSet<DefinedTermBase> getNamedAreas(boolean load) {
|
258
|
if (load){
|
259
|
namedAreas = loadNamedAreas();
|
260
|
}
|
261
|
return namedAreas;
|
262
|
}
|
263
|
|
264
|
/**
|
265
|
* @param namedAreas
|
266
|
* @param defaultLanguage
|
267
|
* @return
|
268
|
*/
|
269
|
public SortedSet<DefinedTermBase> getTermsOrderedByIdInVocabulary(List<DefinedTermBase> namedAreas) {
|
270
|
TermIdInVocabularyComparator<?> comp = new TermIdInVocabularyComparator<>();
|
271
|
|
272
|
SortedSet<DefinedTermBase> result = new TreeSet(comp);
|
273
|
if(namedAreas != null){
|
274
|
result.addAll(namedAreas);
|
275
|
}
|
276
|
return result;
|
277
|
}
|
278
|
}
|