Project

General

Profile

Download (3.42 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.model.common.TermVocabulary;
22
import eu.etaxonomy.cdm.persistence.dto.TermDto;
23
import eu.etaxonomy.cdm.remote.l10n.TermRepresentation_L10n;
24

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

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

    
38
    public TermDtoLabelProvider() {
39
    }
40

    
41
    public TermDtoLabelProvider(Styler vocStyler){
42
        this.vocStyler = vocStyler;
43
    }
44

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

    
49
        String text = getText(element);
50
        cell.setText(text);
51

    
52
        Styler styler = null;
53
        if (element instanceof TermVocabulary && text != null) {
54
            styler = getVocabularyStyler();
55
        }
56
        //TODO: add kindOf UUID to TermDto
57
//        else if(element instanceof TermDto && ((TermDto) element).getKindOf()!=null){
58
//            styler = getKindOfStyler();
59
//        }
60
        if(styler!=null){
61
            StyledString styledString = new StyledString(text, styler);
62
            StyleRange[] styleRanges;
63
            styleRanges = styledString.getStyleRanges();
64
            cell.setStyleRanges(styleRanges);
65
        }
66
        super.update(cell);
67
    }
68

    
69
    public String getText(Object element) {
70
        String label = null;
71
        if(element instanceof TermVocabulary){
72

    
73
        }
74
        else if(element instanceof TermDto){
75
            TermDto termDto = (TermDto)element;
76
            termDto.localize(new TermRepresentation_L10n());
77
            label = termDto.getRepresentation_L10n();
78

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

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

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

    
113
}
(5-5/9)