Project

General

Profile

Download (8.55 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
                           
140
                        }else{
141
                            continue;
142
                        }
143

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

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

    
161
    }
162

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

    
166

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

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

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

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

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

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

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