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.apache.commons.lang.StringUtils;
|
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.metadata.PreferencePredicate;
|
43
|
import eu.etaxonomy.cdm.model.metadata.TermDisplayEnum;
|
44
|
import eu.etaxonomy.cdm.model.name.TaxonName;
|
45
|
import eu.etaxonomy.cdm.model.taxon.Taxon;
|
46
|
import eu.etaxonomy.taxeditor.editor.view.checklist.e4.ChecklistEditorE4;
|
47
|
import eu.etaxonomy.taxeditor.preference.EditorPreferencePredicate;
|
48
|
import eu.etaxonomy.taxeditor.preference.PreferencesUtil;
|
49
|
import eu.etaxonomy.taxeditor.store.CdmStore;
|
50
|
|
51
|
/**
|
52
|
*
|
53
|
* @author a.oppermann
|
54
|
* @created 30.04.2014
|
55
|
*/
|
56
|
public class ChecklistLabelProvider extends LabelProvider implements ITableLabelProvider {
|
57
|
|
58
|
public static final String DEFAULT_ENTRY = ""; //$NON-NLS-1$
|
59
|
private IDescriptionService descriptionService;
|
60
|
private SortedSet<DefinedTermBase> namedAreas;
|
61
|
private ChecklistEditorE4 editor= null;
|
62
|
|
63
|
/**
|
64
|
*
|
65
|
*/
|
66
|
public ChecklistLabelProvider() {
|
67
|
namedAreas = loadNamedAreas();
|
68
|
}
|
69
|
|
70
|
public ChecklistLabelProvider(ChecklistEditorE4 editor) {
|
71
|
namedAreas = loadNamedAreas();
|
72
|
this.editor = editor;
|
73
|
}
|
74
|
|
75
|
/*
|
76
|
* (non-Javadoc)
|
77
|
*
|
78
|
* @see
|
79
|
* org.eclipse.jface.viewers.ITableLabelProvider#getColumnImage(java.lang
|
80
|
* .Object, int)
|
81
|
*/
|
82
|
/** {@inheritDoc} */
|
83
|
@Override
|
84
|
public Image getColumnImage(Object element, int columnIndex) {
|
85
|
// TODO
|
86
|
return null;
|
87
|
}
|
88
|
|
89
|
/** {@inheritDoc} */
|
90
|
@Override
|
91
|
public String getColumnText(Object element, int columnIndex) {
|
92
|
// descriptionService = CdmStore.getService(IDescriptionService.class);
|
93
|
Taxon taxon = (Taxon) element;
|
94
|
|
95
|
Set<TaxonDescription> listTaxonDescriptions = taxon.getDescriptions();
|
96
|
TaxonName nonViralName = HibernateProxyHelper.deproxy(taxon.getName());
|
97
|
|
98
|
switch (columnIndex) {
|
99
|
case 0:
|
100
|
String taxonName = null;
|
101
|
taxonName = (nonViralName != null) ? nonViralName.getNameCache() : null;
|
102
|
return (taxonName != null) ? taxonName : DEFAULT_ENTRY;
|
103
|
|
104
|
case 1:
|
105
|
if (PreferencesUtil.isShowRankInChecklistEditor()){
|
106
|
String rank = null;
|
107
|
|
108
|
if (taxon.getName().getRank() != null) {
|
109
|
rank = taxon.getName().getRank().toString();
|
110
|
}
|
111
|
return (rank != null) ? rank : DEFAULT_ENTRY;
|
112
|
}
|
113
|
}
|
114
|
|
115
|
if(columnIndex >1 || (!PreferencesUtil.isShowRankInChecklistEditor() && columnIndex > 0)){
|
116
|
|
117
|
for (TaxonDescription td : listTaxonDescriptions) {
|
118
|
for (DescriptionElementBase deb : td.getElements()) {
|
119
|
if (deb instanceof Distribution) {
|
120
|
Distribution distribution = HibernateProxyHelper.deproxy(deb, Distribution.class);
|
121
|
if (distribution.getArea() != null){
|
122
|
if ( editor.getAreaPosition().get(distribution.getArea().getUuid()) != null && columnIndex == editor.getAreaPosition().get(distribution.getArea().getUuid())){
|
123
|
if (distribution.getStatus() == null){
|
124
|
return DEFAULT_ENTRY;
|
125
|
}
|
126
|
PresenceAbsenceTerm term = distribution.getStatus();
|
127
|
Representation rep = distribution.getStatus().getPreferredRepresentation(CdmStore.getDefaultLanguage());
|
128
|
String label = rep.getLabel();
|
129
|
|
130
|
if (label == null){
|
131
|
label = distribution.getStatus().getTitleCache();
|
132
|
}
|
133
|
if (PreferencesUtil.displayStatusInChecklistEditor() != null){
|
134
|
if(PreferencesUtil.displayStatusInChecklistEditor().equals(TermDisplayEnum.IdInVocabulary.getKey())){
|
135
|
return (StringUtils.isNotBlank(distribution.getStatus().getIdInVocabulary()))?distribution.getStatus().getIdInVocabulary():label;
|
136
|
}else if(PreferencesUtil.displayStatusInChecklistEditor().equals(TermDisplayEnum.Symbol1.getKey())){
|
137
|
System.err.println(columnIndex + " " +label);
|
138
|
return (StringUtils.isNotBlank(distribution.getStatus().getSymbol() ))?distribution.getStatus().getSymbol():label;
|
139
|
}else if(PreferencesUtil.displayStatusInChecklistEditor().equals(TermDisplayEnum.Symbol2.getKey())){
|
140
|
return (StringUtils.isNotBlank(distribution.getStatus().getSymbol2() ))?distribution.getStatus().getSymbol2():label;
|
141
|
}else{
|
142
|
return (label != null)?label:DEFAULT_ENTRY;
|
143
|
}
|
144
|
|
145
|
}else{
|
146
|
return (label != null)?label:DEFAULT_ENTRY;
|
147
|
}
|
148
|
}
|
149
|
}
|
150
|
}
|
151
|
}
|
152
|
}
|
153
|
}
|
154
|
return DEFAULT_ENTRY;
|
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
|
private SortedSet<DefinedTermBase> loadNamedAreas() {
|
163
|
//IPreferenceStore preferenceStore = PreferencesUtil.getPreferenceStore();
|
164
|
|
165
|
String valuesAreas = PreferencesUtil.getStringValue(EditorPreferencePredicate.AvailableDistributionAreaTerms.getKey());
|
166
|
String values = PreferencesUtil.getStringValue(PreferencePredicate.AvailableDistributionAreaVocabularies.getKey());
|
167
|
if (values != null && values != "") { //$NON-NLS-1$
|
168
|
String[] split = values.split(";"); //$NON-NLS-1$
|
169
|
List<String> listValue = Arrays.asList(split);
|
170
|
List<DefinedTermBase> termlist = new ArrayList<DefinedTermBase>();
|
171
|
Set<UUID> uuidList = new HashSet<UUID>();
|
172
|
UUID uuid;
|
173
|
for(String s : listValue){
|
174
|
uuid = UUID.fromString(s);
|
175
|
uuidList.add(uuid);
|
176
|
|
177
|
}
|
178
|
IVocabularyService service = CdmStore.getService(IVocabularyService.class);
|
179
|
List<TermVocabulary> vocs = service.find(uuidList);
|
180
|
split = valuesAreas.split(";");
|
181
|
listValue = Arrays.asList(split);
|
182
|
for (TermVocabulary voc: vocs){
|
183
|
termlist.addAll(service.getTerms(voc, null, null, null, null).getRecords());
|
184
|
}
|
185
|
List<DefinedTermBase> filteredList = new ArrayList();
|
186
|
for (DefinedTermBase area: termlist){
|
187
|
if (listValue.contains(area.getUuid().toString())) {
|
188
|
filteredList.add(area);
|
189
|
}
|
190
|
|
191
|
}
|
192
|
|
193
|
if (PreferencesUtil.isSortNamedAreaByOrderInVocabulary()){
|
194
|
return getTermsOrderedByVocabularyOrder(filteredList);
|
195
|
} else if (PreferencesUtil.isShowIdInVocabularyInChecklistEditor()){
|
196
|
return getTermsOrderedByIdInVocabulary(filteredList);
|
197
|
}else{
|
198
|
return getTermsOrderedByLabels(filteredList, CdmStore.getDefaultLanguage());
|
199
|
}
|
200
|
}
|
201
|
return null;
|
202
|
}
|
203
|
|
204
|
public SortedSet<DefinedTermBase> getTermsOrderedByLabels(List<DefinedTermBase> listTerm,Language language){
|
205
|
TermLanguageComparator<?> comp = new TermLanguageComparator<>();
|
206
|
comp.setCompareLanguage(language);
|
207
|
SortedSet<DefinedTermBase> result = new TreeSet(comp);
|
208
|
if(listTerm != null){
|
209
|
result.addAll(listTerm);
|
210
|
}
|
211
|
return result;
|
212
|
}
|
213
|
|
214
|
public SortedSet<DefinedTermBase> getTermsOrderedByVocabularyOrder(List<DefinedTermBase> listAreas){
|
215
|
HashMap<TermVocabulary<DefinedTermBase>, List<DefinedTermBase>> vocs = new HashMap<>();
|
216
|
OrderedTermComparator<?> comp = new OrderedTermComparator<>();
|
217
|
boolean allOrderedTerms = true;
|
218
|
List<TermVocabulary> alreadyOrderIndexNull = new ArrayList<>();
|
219
|
for (DefinedTermBase term: listAreas){
|
220
|
if (!(term instanceof OrderedTermBase)){
|
221
|
allOrderedTerms = false;
|
222
|
break;
|
223
|
}else if (((OrderedTermBase)term).getOrderIndex() == 0){
|
224
|
if(alreadyOrderIndexNull.contains(term.getVocabulary())) {
|
225
|
allOrderedTerms = false;
|
226
|
break;
|
227
|
}else{
|
228
|
alreadyOrderIndexNull.add(term.getVocabulary());
|
229
|
}
|
230
|
|
231
|
|
232
|
}
|
233
|
}
|
234
|
if (allOrderedTerms){
|
235
|
SortedSet<DefinedTermBase> result = new TreeSet(comp.reversed());
|
236
|
result.addAll(listAreas);
|
237
|
return result;
|
238
|
}else{
|
239
|
return getTermsOrderedByLabels(listAreas, PreferencesUtil.getGlobalLanguage());
|
240
|
}
|
241
|
|
242
|
|
243
|
}
|
244
|
|
245
|
/**
|
246
|
* @return the namedAreas
|
247
|
*/
|
248
|
public SortedSet<DefinedTermBase> getNamedAreas(boolean load) {
|
249
|
if (load){
|
250
|
namedAreas = loadNamedAreas();
|
251
|
}
|
252
|
return namedAreas;
|
253
|
}
|
254
|
|
255
|
/**
|
256
|
* @param namedAreas
|
257
|
* @param defaultLanguage
|
258
|
* @return
|
259
|
*/
|
260
|
public SortedSet<DefinedTermBase> getTermsOrderedByIdInVocabulary(List<DefinedTermBase> namedAreas) {
|
261
|
TermIdInVocabularyComparator<?> comp = new TermIdInVocabularyComparator<>();
|
262
|
|
263
|
SortedSet<DefinedTermBase> result = new TreeSet(comp);
|
264
|
if(namedAreas != null){
|
265
|
result.addAll(namedAreas);
|
266
|
}
|
267
|
return result;
|
268
|
}
|
269
|
}
|