Project

General

Profile

Download (3.5 KB) Statistics
| Branch: | Tag: | Revision:
1
/**
2
 * Copyright (C) 2009 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
package eu.etaxonomy.taxeditor.editor.definedterm;
10

    
11
import org.eclipse.jface.viewers.StyledCellLabelProvider;
12
import org.eclipse.jface.viewers.StyledString;
13
import org.eclipse.jface.viewers.StyledString.Styler;
14
import org.eclipse.jface.viewers.ViewerCell;
15
import org.eclipse.swt.SWT;
16
import org.eclipse.swt.custom.StyleRange;
17
import org.eclipse.swt.graphics.Color;
18
import org.eclipse.swt.graphics.TextStyle;
19
import org.eclipse.swt.widgets.Display;
20

    
21
import eu.etaxonomy.cdm.common.CdmUtils;
22
import eu.etaxonomy.cdm.persistence.dto.AbstractTermDto;
23
import eu.etaxonomy.cdm.persistence.dto.TermDto;
24
import eu.etaxonomy.cdm.persistence.dto.TermVocabularyDto;
25
import eu.etaxonomy.cdm.remote.l10n.TermRepresentation_L10n;
26

    
27
/**
28
 *
29
 * @author pplitzner
30
 * @since Oct 29, 2018
31
 *
32
 */
33
public class TermDtoLabelProvider extends StyledCellLabelProvider {
34

    
35
    private static Color vocColor = Display.getCurrent().getSystemColor(SWT.COLOR_BLUE);
36
    private static Color kindOfColor = Display.getCurrent().getSystemColor(SWT.COLOR_DARK_GRAY);
37
    private Styler vocStyler;
38
    private Styler kindOfStyler;
39

    
40
    public TermDtoLabelProvider() {
41
    }
42

    
43
    public TermDtoLabelProvider(Styler vocStyler){
44
        this.vocStyler = vocStyler;
45
    }
46

    
47
    @Override
48
    public void update(ViewerCell cell) {
49
        Object element = cell.getElement();
50

    
51
        String text = getText(element);
52
        cell.setText(text);
53

    
54
        Styler styler = null;
55
        if (element instanceof TermVocabularyDto && text != null) {
56
            styler = getVocabularyStyler();
57
        }
58
        else if(element instanceof TermDto && ((TermDto) element).getKindOfUuid()!=null){
59
            styler = getKindOfStyler();
60
        }
61
        if(styler!=null){
62
            StyledString styledString = new StyledString(text, styler);
63
            StyleRange[] styleRanges;
64
            styleRanges = styledString.getStyleRanges();
65
            cell.setStyleRanges(styleRanges);
66
        }
67
        super.update(cell);
68
    }
69

    
70
    public String getText(Object element) {
71
        String label = null;
72
        if(element instanceof AbstractTermDto){
73
            AbstractTermDto termDto = (AbstractTermDto)element;
74
            termDto.localize(new TermRepresentation_L10n());
75
            label = termDto.getRepresentation_L10n();
76

    
77
            if(element instanceof TermDto && ((TermDto) termDto).getIdInVocabulary()!=null){
78
                label = CdmUtils.concat(" : ", ((TermDto) termDto).getIdInVocabulary(), label);
79
            }
80
        }
81
        // TODO add fallback for label
82
        if(label==null){
83
            label = element.toString();
84
        }
85
        return label;
86
    }
87

    
88
    protected Styler getVocabularyStyler() {
89
        if (vocStyler == null) {
90
            vocStyler = new Styler() {
91
                @Override
92
                public void applyStyles(TextStyle textStyle) {
93
                    textStyle.foreground = vocColor;
94
                }
95
            };
96
        }
97
        return vocStyler;
98
    }
99

    
100
    protected Styler getKindOfStyler() {
101
        if (kindOfStyler == null) {
102
            kindOfStyler = new Styler() {
103
                @Override
104
                public void applyStyles(TextStyle textStyle) {
105
                    textStyle.foreground = kindOfColor;
106
                }
107
            };
108
        }
109
        return kindOfStyler;
110
    }
111

    
112
}
(5-5/6)