Project

General

Profile

Download (9.91 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.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.IVocabularyService;
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 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 valuesAreas = preferenceStore.getString(PreferencesUtil.DISTRIBUTION_AREA_OCCURENCE_STATUS);
174
        String values = preferenceStore.getString(PreferencesUtil.DISTRIBUTION_VOCABULARIES);
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
            IVocabularyService service =  CdmStore.getService(IVocabularyService.class);
187
            List<TermVocabulary> vocs = service.find(uuidList);
188
            split = valuesAreas.split(",");
189
            listValue = Arrays.asList(split);
190
            for (TermVocabulary voc: vocs){
191
                termlist.addAll(service.getTerms(voc, null, null, null, null).getRecords());
192
            }
193
            List<DefinedTermBase> filteredList = new ArrayList();
194
            for (DefinedTermBase area: termlist){
195
                if (listValue.contains(area.getUuid().toString())) {
196
                    filteredList.add(area);
197
                }
198

    
199
            }
200

    
201
            if (PreferencesUtil.isSortNamedAreaByOrderInVocabulary()){
202
                return getTermsOrderedByVocabularyOrder(filteredList);
203
            } else if (PreferencesUtil.isShowIdInVocabularyInChecklistEditor()){
204
                return getTermsOrderedByIdInVocabulary(filteredList);
205
            }else{
206
                return getTermsOrderedByLabels(filteredList, CdmStore.getDefaultLanguage());
207
            }
208
        }
209
        return null;
210
    }
211

    
212
    public SortedSet<DefinedTermBase> getTermsOrderedByLabels(List<DefinedTermBase> listTerm,Language language){
213
        TermLanguageComparator<?> comp = new TermLanguageComparator<>();
214
        comp.setCompareLanguage(language);
215
        SortedSet<DefinedTermBase> result = new TreeSet(comp);
216
        if(listTerm != null){
217
            result.addAll(listTerm);
218
        }
219
        return result;
220
    }
221

    
222
    public SortedSet<DefinedTermBase> getTermsOrderedByVocabularyOrder(List<DefinedTermBase> listAreas){
223
        HashMap<TermVocabulary<DefinedTermBase>, List<DefinedTermBase>> vocs = new HashMap<>();
224
        OrderedTermComparator<?> comp = new OrderedTermComparator<>();
225

    
226
        SortedSet<DefinedTermBase> result = new TreeSet(comp.reversed());
227
        result.addAll(listAreas);
228

    
229
        return result;
230
    }
231

    
232
    /**
233
     * @return the namedAreas
234
     */
235
    public SortedSet<DefinedTermBase> getNamedAreas(boolean load) {
236
        if (load){
237
            namedAreas = loadNamedAreas();
238
        }
239
        return namedAreas;
240
    }
241

    
242
    /**
243
     * @param namedAreas
244
     * @param defaultLanguage
245
     * @return
246
     */
247
    public SortedSet<DefinedTermBase> getTermsOrderedByIdInVocabulary(List<DefinedTermBase> namedAreas) {
248
        TermIdInVocabularyComparator<?> comp = new TermIdInVocabularyComparator<>();
249

    
250
        SortedSet<DefinedTermBase> result = new TreeSet(comp);
251
        if(namedAreas != null){
252
            result.addAll(namedAreas);
253
        }
254
        return result;
255
    }
256
}
(4-4/4)