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.swt.graphics.Image;
|
26
|
|
27
|
import eu.etaxonomy.cdm.api.service.IDescriptionService;
|
28
|
import eu.etaxonomy.cdm.api.service.IVocabularyService;
|
29
|
import eu.etaxonomy.cdm.hibernate.HibernateProxyHelper;
|
30
|
import eu.etaxonomy.cdm.model.common.DefinedTermBase;
|
31
|
import eu.etaxonomy.cdm.model.common.Language;
|
32
|
import eu.etaxonomy.cdm.model.common.OrderedTermBase;
|
33
|
import eu.etaxonomy.cdm.model.common.OrderedTermComparator;
|
34
|
import eu.etaxonomy.cdm.model.common.Representation;
|
35
|
import eu.etaxonomy.cdm.model.common.TermIdInVocabularyComparator;
|
36
|
import eu.etaxonomy.cdm.model.common.TermLanguageComparator;
|
37
|
import eu.etaxonomy.cdm.model.common.TermVocabulary;
|
38
|
import eu.etaxonomy.cdm.model.description.DescriptionElementBase;
|
39
|
import eu.etaxonomy.cdm.model.description.Distribution;
|
40
|
import eu.etaxonomy.cdm.model.description.PresenceAbsenceTerm;
|
41
|
import eu.etaxonomy.cdm.model.description.TaxonDescription;
|
42
|
import eu.etaxonomy.cdm.model.name.TaxonName;
|
43
|
import eu.etaxonomy.cdm.model.taxon.Taxon;
|
44
|
import eu.etaxonomy.taxeditor.editor.view.checklist.e4.ChecklistEditorE4;
|
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
|
private ChecklistEditorE4 editor= null;
|
59
|
|
60
|
/**
|
61
|
*
|
62
|
*/
|
63
|
public ChecklistLabelProvider() {
|
64
|
namedAreas = loadNamedAreas();
|
65
|
}
|
66
|
|
67
|
public ChecklistLabelProvider(ChecklistEditorE4 editor) {
|
68
|
namedAreas = loadNamedAreas();
|
69
|
this.editor = editor;
|
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
|
|
92
|
Set<TaxonDescription> listTaxonDescriptions = taxon.getDescriptions();
|
93
|
TaxonName nonViralName = HibernateProxyHelper.deproxy(taxon.getName());
|
94
|
|
95
|
switch (columnIndex) {
|
96
|
case 0:
|
97
|
String taxonName = null;
|
98
|
taxonName = (nonViralName != null) ? nonViralName.getNameCache() : null;
|
99
|
return (taxonName != null) ? taxonName : DEFAULT_ENTRY;
|
100
|
|
101
|
case 1:
|
102
|
if (PreferencesUtil.isShowRankInChecklistEditor()){
|
103
|
String rank = null;
|
104
|
|
105
|
if (taxon.getName().getRank() != null) {
|
106
|
rank = taxon.getName().getRank().toString();
|
107
|
}
|
108
|
return (rank != null) ? rank : DEFAULT_ENTRY;
|
109
|
}
|
110
|
}
|
111
|
|
112
|
if(columnIndex >1 || (!PreferencesUtil.isShowRankInChecklistEditor() && columnIndex > 0)){
|
113
|
|
114
|
for (TaxonDescription td : listTaxonDescriptions) {
|
115
|
for (DescriptionElementBase deb : td.getElements()) {
|
116
|
if (deb instanceof Distribution) {
|
117
|
Distribution distribution = HibernateProxyHelper.deproxy(deb, Distribution.class);
|
118
|
if (distribution.getArea() != null){
|
119
|
if ( editor.getAreaPosition().get(distribution.getArea().getUuid()) != null && columnIndex == editor.getAreaPosition().get(distribution.getArea().getUuid())){
|
120
|
if (distribution.getStatus() == null){
|
121
|
return DEFAULT_ENTRY;
|
122
|
}
|
123
|
PresenceAbsenceTerm term = distribution.getStatus();
|
124
|
Representation rep = distribution.getStatus().getPreferredRepresentation(CdmStore.getDefaultLanguage());
|
125
|
String label = rep.getAbbreviatedLabel();
|
126
|
if (label == null){
|
127
|
label = rep.getLabel();
|
128
|
}
|
129
|
if (label == null){
|
130
|
distribution.getStatus().getTitleCache();
|
131
|
}
|
132
|
if (PreferencesUtil.isShowSymbolInChecklistEditor()){
|
133
|
return (distribution.getStatus().getSymbol() != null)?distribution.getStatus().getSymbol():label;
|
134
|
}else{
|
135
|
return (label != null)?label:DEFAULT_ENTRY;
|
136
|
}
|
137
|
}
|
138
|
}
|
139
|
}
|
140
|
}
|
141
|
}
|
142
|
}
|
143
|
return DEFAULT_ENTRY;
|
144
|
|
145
|
}
|
146
|
|
147
|
private static final List<String> DESC_INIT_STRATEGY = Arrays.asList(new String[] { "descriptions", //$NON-NLS-1$
|
148
|
"descriptions.*", "description.state" }); //$NON-NLS-1$ //$NON-NLS-2$
|
149
|
|
150
|
|
151
|
private SortedSet<DefinedTermBase> loadNamedAreas() {
|
152
|
IPreferenceStore preferenceStore = PreferencesUtil.getPreferenceStore();
|
153
|
|
154
|
String valuesAreas = preferenceStore.getString(PreferencesUtil.DISTRIBUTION_AREA_OCCURENCE_STATUS);
|
155
|
String values = preferenceStore.getString(PreferencesUtil.DISTRIBUTION_VOCABULARIES);
|
156
|
if (values != null && values != "") { //$NON-NLS-1$
|
157
|
String[] split = values.split(";"); //$NON-NLS-1$
|
158
|
List<String> listValue = Arrays.asList(split);
|
159
|
List<DefinedTermBase> termlist = new ArrayList<DefinedTermBase>();
|
160
|
Set<UUID> uuidList = new HashSet<UUID>();
|
161
|
UUID uuid;
|
162
|
for(String s : listValue){
|
163
|
uuid = UUID.fromString(s);
|
164
|
uuidList.add(uuid);
|
165
|
|
166
|
}
|
167
|
IVocabularyService service = CdmStore.getService(IVocabularyService.class);
|
168
|
List<TermVocabulary> vocs = service.find(uuidList);
|
169
|
split = valuesAreas.split(";");
|
170
|
listValue = Arrays.asList(split);
|
171
|
for (TermVocabulary voc: vocs){
|
172
|
termlist.addAll(service.getTerms(voc, null, null, null, null).getRecords());
|
173
|
}
|
174
|
List<DefinedTermBase> filteredList = new ArrayList();
|
175
|
for (DefinedTermBase area: termlist){
|
176
|
if (listValue.contains(area.getUuid().toString())) {
|
177
|
filteredList.add(area);
|
178
|
}
|
179
|
|
180
|
}
|
181
|
|
182
|
if (PreferencesUtil.isSortNamedAreaByOrderInVocabulary()){
|
183
|
return getTermsOrderedByVocabularyOrder(filteredList);
|
184
|
} else if (PreferencesUtil.isShowIdInVocabularyInChecklistEditor()){
|
185
|
return getTermsOrderedByIdInVocabulary(filteredList);
|
186
|
}else{
|
187
|
return getTermsOrderedByLabels(filteredList, CdmStore.getDefaultLanguage());
|
188
|
}
|
189
|
}
|
190
|
return null;
|
191
|
}
|
192
|
|
193
|
public SortedSet<DefinedTermBase> getTermsOrderedByLabels(List<DefinedTermBase> listTerm,Language language){
|
194
|
TermLanguageComparator<?> comp = new TermLanguageComparator<>();
|
195
|
comp.setCompareLanguage(language);
|
196
|
SortedSet<DefinedTermBase> result = new TreeSet(comp);
|
197
|
if(listTerm != null){
|
198
|
result.addAll(listTerm);
|
199
|
}
|
200
|
return result;
|
201
|
}
|
202
|
|
203
|
public SortedSet<DefinedTermBase> getTermsOrderedByVocabularyOrder(List<DefinedTermBase> listAreas){
|
204
|
HashMap<TermVocabulary<DefinedTermBase>, List<DefinedTermBase>> vocs = new HashMap<>();
|
205
|
OrderedTermComparator<?> comp = new OrderedTermComparator<>();
|
206
|
boolean allOrderedTerms = true;
|
207
|
List<TermVocabulary> alreadyOrderIndexNull = new ArrayList<>();
|
208
|
for (DefinedTermBase term: listAreas){
|
209
|
if (!(term instanceof OrderedTermBase)){
|
210
|
allOrderedTerms = false;
|
211
|
break;
|
212
|
}else if (((OrderedTermBase)term).getOrderIndex() == 0){
|
213
|
if(alreadyOrderIndexNull.contains(term.getVocabulary())) {
|
214
|
allOrderedTerms = false;
|
215
|
break;
|
216
|
}else{
|
217
|
alreadyOrderIndexNull.add(term.getVocabulary());
|
218
|
}
|
219
|
|
220
|
|
221
|
}
|
222
|
}
|
223
|
if (allOrderedTerms){
|
224
|
SortedSet<DefinedTermBase> result = new TreeSet(comp.reversed());
|
225
|
result.addAll(listAreas);
|
226
|
return result;
|
227
|
}else{
|
228
|
return getTermsOrderedByLabels(listAreas, PreferencesUtil.getGlobalLanguage());
|
229
|
}
|
230
|
|
231
|
|
232
|
}
|
233
|
|
234
|
/**
|
235
|
* @return the namedAreas
|
236
|
*/
|
237
|
public SortedSet<DefinedTermBase> getNamedAreas(boolean load) {
|
238
|
if (load){
|
239
|
namedAreas = loadNamedAreas();
|
240
|
}
|
241
|
return namedAreas;
|
242
|
}
|
243
|
|
244
|
/**
|
245
|
* @param namedAreas
|
246
|
* @param defaultLanguage
|
247
|
* @return
|
248
|
*/
|
249
|
public SortedSet<DefinedTermBase> getTermsOrderedByIdInVocabulary(List<DefinedTermBase> namedAreas) {
|
250
|
TermIdInVocabularyComparator<?> comp = new TermIdInVocabularyComparator<>();
|
251
|
|
252
|
SortedSet<DefinedTermBase> result = new TreeSet(comp);
|
253
|
if(namedAreas != null){
|
254
|
result.addAll(namedAreas);
|
255
|
}
|
256
|
return result;
|
257
|
}
|
258
|
}
|