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