014ff4fb7f231fcb20ed9b9d86b53527c2735a26
[taxeditor.git] / eu.etaxonomy.taxeditor.editor / src / main / java / eu / etaxonomy / taxeditor / editor / view / checklist / ChecklistLabelProvider.java
1 // $Id$
2 /**
3 * Copyright (C) 2007 EDIT
4 * European Distributed Institute of Taxonomy
5 * http://www.e-taxonomy.eu
6 *
7 * The contents of this file are subject to the Mozilla Public License Version 1.1
8 * See LICENSE.TXT at the top of this package for the full license terms.
9 */
10
11 package eu.etaxonomy.taxeditor.editor.view.checklist;
12
13 import java.util.ArrayList;
14 import java.util.Arrays;
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.TermIdInVocabularyComparator;
36 import eu.etaxonomy.cdm.model.common.TermLanguageComparator;
37 import eu.etaxonomy.cdm.model.description.DescriptionElementBase;
38 import eu.etaxonomy.cdm.model.description.Distribution;
39 import eu.etaxonomy.cdm.model.description.TaxonDescription;
40 import eu.etaxonomy.cdm.model.name.NonViralName;
41 import eu.etaxonomy.cdm.model.taxon.Taxon;
42 import eu.etaxonomy.taxeditor.model.MessagingUtils;
43 import eu.etaxonomy.taxeditor.preference.PreferencesUtil;
44 import eu.etaxonomy.taxeditor.store.CdmStore;
45
46 /**
47 *
48 * @author a.oppermann
49 * @created 30.04.2014
50 * @version 1.0
51 */
52 public class ChecklistLabelProvider extends LabelProvider implements ITableLabelProvider {
53
54 public static final String DEFAULT_ENTRY = "";
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 /*
86 * (non-Javadoc)
87 *
88 * @see
89 * org.eclipse.jface.viewers.ITableLabelProvider#getColumnText(java.lang
90 * .Object, int)
91 */
92 /** {@inheritDoc} */
93 @Override
94 public String getColumnText(Object element, int columnIndex) {
95 // descriptionService = CdmStore.getService(IDescriptionService.class);
96 Taxon taxon = (Taxon) element;
97 //TODO load areas by this list
98
99 // List<TaxonDescription> listTaxonDescriptions = descriptionService.listTaxonDescriptions(taxon, null, null, null, null, null, DESC_INIT_STRATEGY);;
100 Set<TaxonDescription> listTaxonDescriptions = taxon.getDescriptions();
101 NonViralName<?> nonViralName = HibernateProxyHelper.deproxy(taxon.getName(), NonViralName.class);
102
103 switch (columnIndex) {
104 case 0:
105 String taxonName = null;
106 taxonName = (nonViralName != null) ? nonViralName.getNameCache() : null;
107 return (taxonName != null) ? taxonName : DEFAULT_ENTRY;
108
109 // case 1:
110 // String authorship = null;
111 // authorship = (nonViralName != null) ? nonViralName.getAuthorshipCache() : null;
112 // return (authorship != null) ? authorship : DEFAULT_ENTRY;
113 // case 2:
114 // String ref = null;
115 // if (taxon.getName() != null) {
116 // INomenclaturalReference nomenclaturalReference = HibernateProxyHelper.deproxy(
117 // nonVirlaName.getNomenclaturalReference(), Reference.class);
118 // ref = (nomenclaturalReference != null) ? nomenclaturalReference.getAbbrevTitleCache() : null;
119 // }
120 // return (ref != null) ? ref : DEFAULT_ENTRY;
121 case 1:
122 if (PreferencesUtil.isShowRankInChecklistEditor()){
123 String rank = null;
124
125 if (taxon.getName().getRank() != null) {
126 rank = taxon.getName().getRank().toString();
127 }
128 return (rank != null) ? rank : DEFAULT_ENTRY;
129 }
130 }
131
132 if(columnIndex >2 || (!PreferencesUtil.isShowRankInChecklistEditor() && columnIndex > 1)){
133
134 for (TaxonDescription td : listTaxonDescriptions) {
135 for (DescriptionElementBase deb : td.getElements()) {
136 if (deb instanceof Distribution) {
137 Distribution distribution = HibernateProxyHelper.deproxy(deb, Distribution.class);
138 String area = null;
139 if (distribution.getArea() != null ){
140 if (PreferencesUtil.isShowIdInVocabularyInChecklistEditor()){
141 if (distribution.getArea().getIdInVocabulary() != null){
142 area = distribution.getArea().getIdInVocabulary();
143 } else{
144 area = distribution.getArea().getTitleCache();
145 }
146 }else{
147 area = distribution.getArea().getTitleCache();
148 }
149 }else{
150 continue;
151 }
152
153 if(viewer != null){
154 TableColumn column = viewer.getTable().getColumn(columnIndex);
155 if (area.equalsIgnoreCase(column.getText())) {
156 if (PreferencesUtil.isShowIdInVocabularyInChecklistEditor()){
157 return (distribution.getStatus().getIdInVocabulary() != null)?distribution.getStatus().getIdInVocabulary():CdmUtils.Nz(distribution.getStatus().getTitleCache());
158 }else{
159 return (distribution.getStatus().getTitleCache() != null)?distribution.getStatus().getTitleCache():DEFAULT_ENTRY;
160 }
161 }
162
163 }
164 }
165 }
166 }
167 }
168 return DEFAULT_ENTRY;
169
170 }
171
172 private static final List<String> DESC_INIT_STRATEGY = Arrays.asList(new String[] { "descriptions",
173 "descriptions.*", "description.state" });
174
175
176 private SortedSet<DefinedTermBase> loadNamedAreas() {
177 IPreferenceStore preferenceStore = PreferencesUtil.getPreferenceStore();
178 String values = preferenceStore.getString(PreferencesUtil.DISTRIBUTION_AREA_OCCURENCE_STATUS);
179
180 if (values != null && values != "") {
181 String[] split = values.split(",");
182 List<String> listValue = Arrays.asList(split);
183 List<DefinedTermBase> termlist = new ArrayList<DefinedTermBase>();
184 Set<UUID> uuidList = new HashSet<UUID>();
185 UUID uuid;
186 for(String s : listValue){
187 uuid = UUID.fromString(s);
188 uuidList.add(uuid);
189
190 }
191 ITermService service = CdmStore.getService(ITermService.class);
192 termlist = service.find(uuidList);
193 if (PreferencesUtil.isShowIdInVocabularyInChecklistEditor()){
194 return getTermsOrderedByIdInVocabulary(termlist);
195 } else {
196 return getTermsOrderedByLabels(termlist, CdmStore.getDefaultLanguage());
197 }
198 }
199 return null;
200 }
201
202 public SortedSet<DefinedTermBase> getTermsOrderedByLabels(List<DefinedTermBase> listTerm,Language language){
203 TermLanguageComparator comp = new TermLanguageComparator();
204 comp.setCompareLanguage(language);
205 SortedSet result = new TreeSet(comp);
206 if(listTerm != null){
207 result.addAll(listTerm);
208 }
209 return result;
210 }
211
212 /**
213 * @return the namedAreas
214 */
215 public SortedSet<DefinedTermBase> getNamedAreas() {
216 return namedAreas;
217 }
218
219 /**
220 * @param namedAreas
221 * @param defaultLanguage
222 * @return
223 */
224 public SortedSet<DefinedTermBase> getTermsOrderedByIdInVocabulary(List<DefinedTermBase> namedAreas) {
225 TermIdInVocabularyComparator comp = new TermIdInVocabularyComparator();
226
227 SortedSet result = new TreeSet(comp);
228 if(namedAreas != null){
229 result.addAll(namedAreas);
230 }
231 return result;
232 }
233 }