Project

General

Profile

Download (7.5 KB) Statistics
| Branch: | Tag: | Revision:
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.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.preference.PreferencesUtil;
42
import eu.etaxonomy.taxeditor.store.CdmStore;
43

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

    
52
    public static final String DEFAULT_ENTRY = "";
53
    private IDescriptionService descriptionService;
54
    private final SortedSet<DefinedTermBase> namedAreas;
55
    TableViewer viewer = null;
56

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

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

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

    
83
    /*
84
     * (non-Javadoc)
85
     *
86
     * @see
87
     * org.eclipse.jface.viewers.ITableLabelProvider#getColumnText(java.lang
88
     * .Object, int)
89
     */
90
    /** {@inheritDoc} */
91
	@Override
92
    public String getColumnText(Object element, int columnIndex) {
93
        descriptionService = CdmStore.getService(IDescriptionService.class);
94
        Taxon taxon = (Taxon) element;
95
        //TODO load areas by this list
96

    
97
        List<TaxonDescription> listTaxonDescriptions = descriptionService.listTaxonDescriptions(taxon, null, null, null, null, null, DESC_INIT_STRATEGY);;
98
        NonViralName<?> nonViralName = HibernateProxyHelper.deproxy(taxon.getName(), NonViralName.class);
99

    
100
        switch (columnIndex) {
101
        case 0:
102
            String taxonName = null;
103
            taxonName = (nonViralName != null) ? nonViralName.getNameCache() : null;
104
            return (taxonName != null) ? taxonName : DEFAULT_ENTRY;
105

    
106
        case 1:
107
            String authorship = null;
108
            authorship = (nonViralName != null) ? nonViralName.getAuthorshipCache() : null;
109
            return (authorship != null) ? authorship : DEFAULT_ENTRY;
110
//        case 2:
111
//            String ref = null;
112
//            if (taxon.getName() != null) {
113
//                INomenclaturalReference nomenclaturalReference = HibernateProxyHelper.deproxy(
114
//                        nonVirlaName.getNomenclaturalReference(), Reference.class);
115
//                ref = (nomenclaturalReference != null) ? nomenclaturalReference.getAbbrevTitleCache() : null;
116
//            }
117
//            return (ref != null) ? ref : DEFAULT_ENTRY;
118
        case 2:
119
            String rank = null;
120
            if (taxon.getName().getRank() != null) {
121
                rank = taxon.getName().getRank().toString();
122
            }
123
            return (rank != null) ? rank : DEFAULT_ENTRY;
124
        }
125

    
126
        if(columnIndex >2){
127

    
128
            for (TaxonDescription td : listTaxonDescriptions) {
129
                for (DescriptionElementBase deb : td.getElements()) {
130
                    if (deb instanceof Distribution) {
131
                        Distribution distribution = HibernateProxyHelper.deproxy(deb, Distribution.class);
132
                        String area = null;
133
                        if (distribution.getArea() != null ){
134
                            if (PreferencesUtil.isShowIdInVocabularyInChecklistEditor()){
135
                                area = distribution.getArea().getIdInVocabulary();
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
                        	    return (distribution.getStatus().getTitleCache() != null)?distribution.getStatus().getTitleCache():DEFAULT_ENTRY;
147
                            }
148

    
149
                        }
150
                    }
151
                }
152
            }
153
        }
154
        return DEFAULT_ENTRY;
155

    
156
    }
157

    
158
    private static final List<String> DESC_INIT_STRATEGY = Arrays.asList(new String[] { "descriptions",
159
            "descriptions.*", "description.state" });
160

    
161

    
162
    private SortedSet<DefinedTermBase> loadNamedAreas() {
163
        IPreferenceStore preferenceStore = PreferencesUtil.getPreferenceStore();
164
        String values = preferenceStore.getString(PreferencesUtil.DISTRIBUTION_AREA_OCCURENCE_STATUS);
165
        if (values != null && values != "") {
166
            String[] split = values.split(",");
167
            List<String> listValue = Arrays.asList(split);
168
            List<DefinedTermBase> termlist = new ArrayList<DefinedTermBase>();
169
           Set<UUID> uuidList = new HashSet<UUID>();
170
           UUID uuid;
171
            for(String s : listValue){
172
                uuid = UUID.fromString(s);
173
                uuidList.add(uuid);
174

    
175
            }
176
            ITermService service = CdmStore.getService(ITermService.class);
177
            termlist = service.find(uuidList);
178

    
179
            return getTermsOrderedByIdInVocabulary(termlist);
180
        }
181
        return null;
182
    }
183

    
184
    public SortedSet<?> getTermsOrderedByLabels(List<DefinedTermBase<?>> listTerm,Language language){
185
        TermLanguageComparator comp = new TermLanguageComparator();
186
        comp.setCompareLanguage(language);
187
        SortedSet result = new TreeSet(comp);
188
        if(listTerm != null){
189
            result.addAll(listTerm);
190
        }
191
        return result;
192
    }
193

    
194
    /**
195
     * @return the namedAreas
196
     */
197
    public SortedSet<DefinedTermBase> getNamedAreas() {
198
        return namedAreas;
199
    }
200

    
201
    /**
202
     * @param namedAreas
203
     * @param defaultLanguage
204
     * @return
205
     */
206
    public SortedSet<DefinedTermBase> getTermsOrderedByIdInVocabulary(List<DefinedTermBase> namedAreas) {
207
        TermIdInVocabularyComparator comp = new TermIdInVocabularyComparator();
208

    
209
        SortedSet result = new TreeSet(comp);
210
        if(namedAreas != null){
211
            result.addAll(namedAreas);
212
        }
213
        return result;
214
    }
215
}
(5-5/5)