Project

General

Profile

Download (4.33 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.taxeditor.model.MessagingUtils;
26
import eu.etaxonomy.taxeditor.preference.Resources;
27
import eu.etaxonomy.taxeditor.security.RequiredPermissions;
28
import eu.etaxonomy.taxeditor.store.CdmStore;
29
import eu.etaxonomy.taxeditor.store.StoreUtil;
30

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

    
39
    protected Styler notGrantedStyler = null;
40

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

    
84
    /**
85
     * {@inheritDoc}
86
     */
87
    @Override
88
    public StyledString getStyledText(Object element) {
89
        //classification
90
        if(element instanceof Classification){
91
            return new StyledString(getText(element), StyledString.DECORATIONS_STYLER);
92
        }
93
        //taxon node
94
        else if(element instanceof TaxonNode){
95
            // determine style base on  user grants
96
            Styler styler = null;
97
            if(!CdmStore.currentAuthentiationHasPermission((CdmBase) element, RequiredPermissions.TAXONNODE_EDIT)){
98
                styler = getNotGrantedStyler();
99
            }
100
            return new StyledString(getText(element), styler);
101
        }
102
        return new StyledString(getText(element));
103
    }
104

    
105
    private Styler getNotGrantedStyler() {
106
        if (notGrantedStyler  == null) {
107
            notGrantedStyler = new Styler() {
108
                @Override
109
                public void applyStyles(TextStyle textStyle) {
110
                    textStyle.underline = false;
111
                    textStyle.foreground = StoreUtil.getColor(Resources.COLOR_TEXT_DISABLED);
112
                }
113
            };
114
        }
115
        return notGrantedStyler;
116
    }
117
}
(5-5/5)