Project

General

Profile

Download (8.52 KB) Statistics
| Branch: | Tag: | Revision:
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.TaxonName;
40
import eu.etaxonomy.cdm.model.taxon.Taxon;
41
import eu.etaxonomy.taxeditor.preference.PreferencesUtil;
42
import eu.etaxonomy.taxeditor.store.CdmStore;
43

    
44
/**
45
 *
46
 * @author a.oppermann
47
 * @created 30.04.2014
48
 */
49
public class ChecklistLabelProvider extends LabelProvider implements ITableLabelProvider {
50

    
51
    public static final String DEFAULT_ENTRY = ""; //$NON-NLS-1$
52
    private IDescriptionService descriptionService;
53
    private final SortedSet<DefinedTermBase> namedAreas;
54
    TableViewer viewer = null;
55

    
56
    /**
57
     *
58
     */
59
    public ChecklistLabelProvider() {
60
        namedAreas = loadNamedAreas();
61
    }
62

    
63
    public ChecklistLabelProvider(TableViewer viewer) {
64
        namedAreas = loadNamedAreas();
65
        this.viewer = viewer;
66
    }
67

    
68
    /*
69
     * (non-Javadoc)
70
     *
71
     * @see
72
     * org.eclipse.jface.viewers.ITableLabelProvider#getColumnImage(java.lang
73
     * .Object, int)
74
     */
75
    /** {@inheritDoc} */
76
    @Override
77
    public Image getColumnImage(Object element, int columnIndex) {
78
        // TODO
79
        return null;
80
    }
81

    
82
    /** {@inheritDoc} */
83
	@Override
84
    public String getColumnText(Object element, int columnIndex) {
85
       // descriptionService = CdmStore.getService(IDescriptionService.class);
86
        Taxon taxon = (Taxon) element;
87
        //TODO load areas by this list
88

    
89
       // List<TaxonDescription> listTaxonDescriptions = descriptionService.listTaxonDescriptions(taxon, null, null, null, null, null, DESC_INIT_STRATEGY);;
90
        Set<TaxonDescription> listTaxonDescriptions = taxon.getDescriptions();
91
        TaxonName nonViralName = HibernateProxyHelper.deproxy(taxon.getName());
92

    
93
        switch (columnIndex) {
94
        case 0:
95
            String taxonName = null;
96
            taxonName = (nonViralName != null) ? nonViralName.getNameCache() : null;
97
            return (taxonName != null) ? taxonName : DEFAULT_ENTRY;
98

    
99
 //       case 1:
100
//            String authorship = null;
101
//            authorship = (nonViralName != null) ? nonViralName.getAuthorshipCache() : null;
102
//            return (authorship != null) ? authorship : DEFAULT_ENTRY;
103
//        case 2:
104
//            String ref = null;
105
//            if (taxon.getName() != null) {
106
//                INomenclaturalReference nomenclaturalReference = HibernateProxyHelper.deproxy(
107
//                        nonVirlaName.getNomenclaturalReference(), Reference.class);
108
//                ref = (nomenclaturalReference != null) ? nomenclaturalReference.getAbbrevTitleCache() : null;
109
//            }
110
//            return (ref != null) ? ref : DEFAULT_ENTRY;
111
        case 1:
112
            if (PreferencesUtil.isShowRankInChecklistEditor()){
113
                String rank = null;
114

    
115
                if (taxon.getName().getRank() != null) {
116
                    rank = taxon.getName().getRank().toString();
117
                }
118
                return (rank != null) ? rank : DEFAULT_ENTRY;
119
            }
120
        }
121

    
122
        if(columnIndex >2 || (!PreferencesUtil.isShowRankInChecklistEditor() && columnIndex > 0)){
123

    
124
            for (TaxonDescription td : listTaxonDescriptions) {
125
                for (DescriptionElementBase deb : td.getElements()) {
126
                    if (deb instanceof Distribution) {
127
                        Distribution distribution = HibernateProxyHelper.deproxy(deb, Distribution.class);
128
                        String area = null;
129
                        if (distribution.getArea() != null ){
130
                            if (PreferencesUtil.isShowIdInVocabularyInChecklistEditor()){
131
                                if (distribution.getArea().getIdInVocabulary() != null){
132
                                    area = distribution.getArea().getIdInVocabulary();
133
                                } else{
134
                                    area = distribution.getArea().getTitleCache();
135
                                }
136
                            }else{
137
                                area = distribution.getArea().getTitleCache();
138
                            }
139
                        }else{
140
                            continue;
141
                        }
142

    
143
                        if(viewer != null){
144
                        	TableColumn column = viewer.getTable().getColumn(columnIndex);
145
                        	if (area.equalsIgnoreCase(column.getText())) {
146
                        	    if (PreferencesUtil.isShowIdInVocabularyInChecklistEditor()){
147
                        	        return (distribution.getStatus().getIdInVocabulary() != null)?distribution.getStatus().getIdInVocabulary():CdmUtils.Nz(distribution.getStatus().getTitleCache());
148
                        	    }else{
149
                        	        return (distribution.getStatus().getTitleCache() != null)?distribution.getStatus().getTitleCache():DEFAULT_ENTRY;
150
                        	    }
151
                            }
152

    
153
                        }
154
                    }
155
                }
156
            }
157
        }
158
        return DEFAULT_ENTRY;
159

    
160
    }
161

    
162
    private static final List<String> DESC_INIT_STRATEGY = Arrays.asList(new String[] { "descriptions", //$NON-NLS-1$
163
            "descriptions.*", "description.state" }); //$NON-NLS-1$ //$NON-NLS-2$
164

    
165

    
166
    private SortedSet<DefinedTermBase> loadNamedAreas() {
167
        IPreferenceStore preferenceStore = PreferencesUtil.getPreferenceStore();
168
        String values = preferenceStore.getString(PreferencesUtil.DISTRIBUTION_AREA_OCCURENCE_STATUS);
169

    
170
        if (values != null && values != "") { //$NON-NLS-1$
171
            String[] split = values.split(","); //$NON-NLS-1$
172
            List<String> listValue = Arrays.asList(split);
173
            List<DefinedTermBase> termlist = new ArrayList<DefinedTermBase>();
174
            Set<UUID> uuidList = new HashSet<UUID>();
175
            UUID uuid;
176
            for(String s : listValue){
177
                uuid = UUID.fromString(s);
178
                uuidList.add(uuid);
179

    
180
            }
181
            ITermService service = CdmStore.getService(ITermService.class);
182
            termlist = service.find(uuidList);
183
            if (PreferencesUtil.isShowIdInVocabularyInChecklistEditor()){
184
                return getTermsOrderedByIdInVocabulary(termlist);
185
            } else {
186
                return getTermsOrderedByLabels(termlist, CdmStore.getDefaultLanguage());
187
            }
188
        }
189
        return null;
190
    }
191

    
192
    public SortedSet<DefinedTermBase> getTermsOrderedByLabels(List<DefinedTermBase> listTerm,Language language){
193
        TermLanguageComparator<?> comp = new TermLanguageComparator<>();
194
        comp.setCompareLanguage(language);
195
        SortedSet<DefinedTermBase> result = new TreeSet(comp);
196
        if(listTerm != null){
197
            result.addAll(listTerm);
198
        }
199
        return result;
200
    }
201

    
202
    /**
203
     * @return the namedAreas
204
     */
205
    public SortedSet<DefinedTermBase> getNamedAreas() {
206
        return namedAreas;
207
    }
208

    
209
    /**
210
     * @param namedAreas
211
     * @param defaultLanguage
212
     * @return
213
     */
214
    public SortedSet<DefinedTermBase> getTermsOrderedByIdInVocabulary(List<DefinedTermBase> namedAreas) {
215
        TermIdInVocabularyComparator<?> comp = new TermIdInVocabularyComparator<>();
216

    
217
        SortedSet<DefinedTermBase> result = new TreeSet(comp);
218
        if(namedAreas != null){
219
            result.addAll(namedAreas);
220
        }
221
        return result;
222
    }
223
}
(5-5/5)