fix #7237: description editor: when status is empty and no description exist, do...
[taxeditor.git] / eu.etaxonomy.taxeditor.editor / src / main / java / eu / etaxonomy / taxeditor / editor / view / checklist / ChecklistLabelProvider.java
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.ITermService;
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.OrderedTermComparator;
36 import eu.etaxonomy.cdm.model.common.TermIdInVocabularyComparator;
37 import eu.etaxonomy.cdm.model.common.TermLanguageComparator;
38 import eu.etaxonomy.cdm.model.common.TermVocabulary;
39 import eu.etaxonomy.cdm.model.description.DescriptionElementBase;
40 import eu.etaxonomy.cdm.model.description.Distribution;
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.preference.PreferencesUtil;
45 import eu.etaxonomy.taxeditor.store.CdmStore;
46
47 /**
48 *
49 * @author a.oppermann
50 * @created 30.04.2014
51 */
52 public class ChecklistLabelProvider extends LabelProvider implements ITableLabelProvider {
53
54 public static final String DEFAULT_ENTRY = ""; //$NON-NLS-1$
55 private IDescriptionService descriptionService;
56 private final SortedSet<DefinedTermBase> namedAreas;
57 TableViewer viewer = null;
58
59 /**
60 *
61 */
62 public ChecklistLabelProvider() {
63 namedAreas = loadNamedAreas();
64 }
65
66 public ChecklistLabelProvider(TableViewer viewer) {
67 namedAreas = loadNamedAreas();
68 this.viewer = viewer;
69 }
70
71 /*
72 * (non-Javadoc)
73 *
74 * @see
75 * org.eclipse.jface.viewers.ITableLabelProvider#getColumnImage(java.lang
76 * .Object, int)
77 */
78 /** {@inheritDoc} */
79 @Override
80 public Image getColumnImage(Object element, int columnIndex) {
81 // TODO
82 return null;
83 }
84
85 /** {@inheritDoc} */
86 @Override
87 public String getColumnText(Object element, int columnIndex) {
88 // descriptionService = CdmStore.getService(IDescriptionService.class);
89 Taxon taxon = (Taxon) element;
90 //TODO load areas by this list
91
92 // List<TaxonDescription> listTaxonDescriptions = descriptionService.listTaxonDescriptions(taxon, null, null, null, null, null, DESC_INIT_STRATEGY);;
93 Set<TaxonDescription> listTaxonDescriptions = taxon.getDescriptions();
94 TaxonName nonViralName = HibernateProxyHelper.deproxy(taxon.getName());
95
96 switch (columnIndex) {
97 case 0:
98 String taxonName = null;
99 taxonName = (nonViralName != null) ? nonViralName.getNameCache() : null;
100 return (taxonName != null) ? taxonName : DEFAULT_ENTRY;
101
102 // case 1:
103 // String authorship = null;
104 // authorship = (nonViralName != null) ? nonViralName.getAuthorshipCache() : null;
105 // return (authorship != null) ? authorship : DEFAULT_ENTRY;
106 // case 2:
107 // String ref = null;
108 // if (taxon.getName() != null) {
109 // INomenclaturalReference nomenclaturalReference = HibernateProxyHelper.deproxy(
110 // nonVirlaName.getNomenclaturalReference(), Reference.class);
111 // ref = (nomenclaturalReference != null) ? nomenclaturalReference.getAbbrevTitleCache() : null;
112 // }
113 // return (ref != null) ? ref : DEFAULT_ENTRY;
114 case 1:
115 if (PreferencesUtil.isShowRankInChecklistEditor()){
116 String rank = null;
117
118 if (taxon.getName().getRank() != null) {
119 rank = taxon.getName().getRank().toString();
120 }
121 return (rank != null) ? rank : DEFAULT_ENTRY;
122 }
123 }
124
125 if(columnIndex >2 || (!PreferencesUtil.isShowRankInChecklistEditor() && columnIndex > 0)){
126
127 for (TaxonDescription td : listTaxonDescriptions) {
128 for (DescriptionElementBase deb : td.getElements()) {
129 if (deb instanceof Distribution) {
130 Distribution distribution = HibernateProxyHelper.deproxy(deb, Distribution.class);
131 String area = null;
132 if (distribution.getArea() != null ){
133 if (PreferencesUtil.isShowIdInVocabularyInChecklistEditor()){
134 if (distribution.getArea().getIdInVocabulary() != null){
135 area = distribution.getArea().getIdInVocabulary();
136 } else{
137 area = distribution.getArea().getTitleCache();
138 }
139 }else{
140 area = distribution.getArea().getTitleCache();
141 }
142
143 }else{
144 continue;
145 }
146
147 if(viewer != null){
148 TableColumn column = viewer.getTable().getColumn(columnIndex);
149 if (area.equalsIgnoreCase(column.getText())) {
150 if (PreferencesUtil.isShowIdInVocabularyInChecklistEditor()){
151 return (distribution.getStatus().getIdInVocabulary() != null)?distribution.getStatus().getIdInVocabulary():CdmUtils.Nz(distribution.getStatus().getTitleCache());
152 }else{
153 return (distribution.getStatus().getTitleCache() != null)?distribution.getStatus().getTitleCache():DEFAULT_ENTRY;
154 }
155 }
156
157 }
158 }
159 }
160 }
161 }
162 return DEFAULT_ENTRY;
163
164 }
165
166 private static final List<String> DESC_INIT_STRATEGY = Arrays.asList(new String[] { "descriptions", //$NON-NLS-1$
167 "descriptions.*", "description.state" }); //$NON-NLS-1$ //$NON-NLS-2$
168
169
170 private SortedSet<DefinedTermBase> loadNamedAreas() {
171 IPreferenceStore preferenceStore = PreferencesUtil.getPreferenceStore();
172
173 String values = preferenceStore.getString(PreferencesUtil.DISTRIBUTION_AREA_OCCURENCE_STATUS);
174
175 if (values != null && values != "") { //$NON-NLS-1$
176 String[] split = values.split(","); //$NON-NLS-1$
177 List<String> listValue = Arrays.asList(split);
178 List<DefinedTermBase> termlist = new ArrayList<DefinedTermBase>();
179 Set<UUID> uuidList = new HashSet<UUID>();
180 UUID uuid;
181 for(String s : listValue){
182 uuid = UUID.fromString(s);
183 uuidList.add(uuid);
184
185 }
186 ITermService service = CdmStore.getService(ITermService.class);
187 termlist = service.find(uuidList);
188 if (PreferencesUtil.isSortNamedAreaByOrderInVocabulary()){
189 return getTermsOrderedByVocabularyOrder(termlist);
190 } else if (PreferencesUtil.isShowIdInVocabularyInChecklistEditor()){
191 return getTermsOrderedByIdInVocabulary(termlist);
192 }else{
193 return getTermsOrderedByLabels(termlist, CdmStore.getDefaultLanguage());
194 }
195 }
196 return null;
197 }
198
199 public SortedSet<DefinedTermBase> getTermsOrderedByLabels(List<DefinedTermBase> listTerm,Language language){
200 TermLanguageComparator<?> comp = new TermLanguageComparator<>();
201 comp.setCompareLanguage(language);
202 SortedSet<DefinedTermBase> result = new TreeSet(comp);
203 if(listTerm != null){
204 result.addAll(listTerm);
205 }
206 return result;
207 }
208
209 public SortedSet<DefinedTermBase> getTermsOrderedByVocabularyOrder(List<DefinedTermBase> listAreas){
210 HashMap<TermVocabulary<DefinedTermBase>, List<DefinedTermBase>> vocs = new HashMap<>();
211 OrderedTermComparator<?> comp = new OrderedTermComparator<>();
212
213 SortedSet<DefinedTermBase> result = new TreeSet(comp.reversed());
214 result.addAll(listAreas);
215
216 return result;
217 }
218
219 /**
220 * @return the namedAreas
221 */
222 public SortedSet<DefinedTermBase> getNamedAreas() {
223 return namedAreas;
224 }
225
226 /**
227 * @param namedAreas
228 * @param defaultLanguage
229 * @return
230 */
231 public SortedSet<DefinedTermBase> getTermsOrderedByIdInVocabulary(List<DefinedTermBase> namedAreas) {
232 TermIdInVocabularyComparator<?> comp = new TermIdInVocabularyComparator<>();
233
234 SortedSet<DefinedTermBase> result = new TreeSet(comp);
235 if(namedAreas != null){
236 result.addAll(namedAreas);
237 }
238 return result;
239 }
240 }