Project

General

Profile

Download (4.83 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
                        text += taxon.getName() != null ? ((IIdentifiableEntity) HibernateProxyHelper.deproxy(taxon.getName())).getTitleCache() : new String();
73
                        return text;
74
                    }catch(Exception e){
75
                        MessagingUtils.error(getClass(), e);
76
                    }
77
                }
78
            }catch (LazyInitializationException e){
79
                MessagingUtils.error(getClass(), e);
80
            }
81
        }
82
        else if (element instanceof TaxonNodeDto){
83
            TaxonNodeDto taxonNode = (TaxonNodeDto) element;
84
            String text= "";
85
            if (taxonNode.isUnplaced()){
86
                text += Character.toString((char)63) + " ";
87
            }
88
            if (taxonNode.isExcluded()) {
89
                text +=Character.toString((char)248) + " ";
90
            }
91
            text += taxonNode.getTitleCache();
92
            return text;
93
        }
94
        return new String();
95
    }
96

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

    
118
    private Styler getNotGrantedStyler() {
119
        if (notGrantedStyler  == null) {
120
            notGrantedStyler = new Styler() {
121
                @Override
122
                public void applyStyles(TextStyle textStyle) {
123
                    textStyle.underline = false;
124
                    textStyle.foreground = StoreUtil.getColor(Resources.COLOR_TEXT_DISABLED);
125
                }
126
            };
127
        }
128
        return notGrantedStyler;
129
    }
130
}
(5-5/5)