Project

General

Profile

Download (5.16 KB) Statistics
| Branch: | Tag: | Revision:
1
// $Id$
2
/**
3
 * Copyright (C) 2017 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
package eu.etaxonomy.taxeditor.util;
11

    
12
import org.eclipse.jface.viewers.ColumnLabelProvider;
13
import org.eclipse.jface.viewers.DelegatingStyledCellLabelProvider.IStyledLabelProvider;
14
import org.eclipse.jface.viewers.StyledString;
15
import org.eclipse.jface.viewers.StyledString.Styler;
16
import org.eclipse.swt.graphics.TextStyle;
17
import org.hibernate.LazyInitializationException;
18

    
19
import eu.etaxonomy.cdm.hibernate.HibernateProxyHelper;
20
import eu.etaxonomy.cdm.model.common.CdmBase;
21
import eu.etaxonomy.cdm.model.common.IIdentifiableEntity;
22
import eu.etaxonomy.cdm.model.taxon.Classification;
23
import eu.etaxonomy.cdm.model.taxon.Taxon;
24
import eu.etaxonomy.cdm.model.taxon.TaxonNode;
25
import eu.etaxonomy.cdm.persistence.dto.TaxonNodeDto;
26
import eu.etaxonomy.taxeditor.model.MessagingUtils;
27
import eu.etaxonomy.taxeditor.preference.Resources;
28
import eu.etaxonomy.taxeditor.security.RequiredPermissions;
29
import eu.etaxonomy.taxeditor.store.CdmStore;
30
import eu.etaxonomy.taxeditor.store.StoreUtil;
31

    
32
/**
33
 * @author pplitzner
34
 * @date 05.09.2017
35
 *
36
 */
37
public class TaxonTreeNodeLabelProvider extends ColumnLabelProvider
38
implements IStyledLabelProvider {
39

    
40
    protected Styler notGrantedStyler = null;
41

    
42
    /**
43
     * {@inheritDoc}
44
     */
45
    @Override
46
    public String getText(Object element) {
47
        //classification
48
        if(element instanceof Classification){
49
            String text = ((Classification) element).getName().getText();
50
            return text != null ? text : "Unnamed Taxonomic Tree";
51
        }
52
        //taxon node
53
        else if (element instanceof TaxonNode){
54
            TaxonNode taxonNode = (TaxonNode) HibernateProxyHelper.deproxy(element);
55
          try{
56
                Taxon taxon = HibernateProxyHelper.deproxy(taxonNode.getTaxon());
57
                if(taxon == null){
58
                    String text = taxonNode.getClassification().getName() == null ? null : taxonNode.getClassification().getName().getText();
59
                    if(text==null){
60
                        text = taxonNode.getClassification().getTitleCache();
61
                    }
62
                    return text;
63
                }else{
64
                    try{
65
                        String text= "";
66
                            if (taxonNode.isUnplaced()){
67
                                text += Character.toString((char)63) + " ";
68
                            }
69
                            if (taxonNode.isExcluded()){
70
                                text +=Character.toString((char)248) + " ";
71
                            }
72
                            if (taxonNode.isDoubtful()){
73
                                text +=Character.toString((char)63) +Character.toString((char)63)+ " ";
74
                            }
75
                        text += taxon.getName() != null ? ((IIdentifiableEntity) HibernateProxyHelper.deproxy(taxon.getName())).getTitleCache() : new String();
76
                        return text;
77
                    }catch(Exception e){
78
                        MessagingUtils.error(getClass(), e);
79
                    }
80
                }
81
            }catch (LazyInitializationException e){
82
                MessagingUtils.error(getClass(), e);
83
            }
84
        }
85
        else if (element instanceof TaxonNodeDto){
86
            TaxonNodeDto taxonNode = (TaxonNodeDto) element;
87
            String text= "";
88
            if (taxonNode.isUnplaced()){
89
                text += Character.toString((char)63) + Character.toString((char)63) + " ";
90
            }
91
            if (taxonNode.isExcluded()) {
92
                text +=Character.toString((char)248) + " ";
93
            }
94
            if (taxonNode.isDoubtful()) {
95
                text +=Character.toString((char)63) + " ";
96
            }
97
            text += taxonNode.getTitleCache();
98
            return text;
99
        }
100
        return new String();
101
    }
102

    
103
    /**
104
     * {@inheritDoc}
105
     */
106
    @Override
107
    public StyledString getStyledText(Object element) {
108
        //classification
109
        if(element instanceof Classification){
110
            return new StyledString(getText(element), StyledString.DECORATIONS_STYLER);
111
        }
112
        //taxon node
113
        else if(element instanceof TaxonNode){
114
            // determine style base on  user grants
115
            Styler styler = null;
116
            if(!CdmStore.currentAuthentiationHasPermission((CdmBase) element, RequiredPermissions.TAXONNODE_EDIT)){
117
                styler = getNotGrantedStyler();
118
            }
119
            return new StyledString(getText(element), styler);
120
        }
121
        return new StyledString(getText(element));
122
    }
123

    
124
    private Styler getNotGrantedStyler() {
125
        if (notGrantedStyler  == null) {
126
            notGrantedStyler = new Styler() {
127
                @Override
128
                public void applyStyles(TextStyle textStyle) {
129
                    textStyle.underline = false;
130
                    textStyle.foreground = StoreUtil.getColor(Resources.COLOR_TEXT_DISABLED);
131
                }
132
            };
133
        }
134
        return notGrantedStyler;
135
    }
136
}
(5-5/5)