Project

General

Profile

Download (3.72 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
import eu.etaxonomy.taxeditor.preference.IPreferenceKeys;
27
import eu.etaxonomy.taxeditor.preference.PreferencesUtil;
28

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

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

    
42
    public TermDtoLabelProvider() {
43
    }
44

    
45
    public TermDtoLabelProvider(Styler vocStyler){
46
        this.vocStyler = vocStyler;
47
    }
48

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

    
53
        String text = getText(element);
54
        cell.setText(text);
55

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

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

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

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

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

    
115
}
(5-5/6)