Project

General

Profile

Download (3.29 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 java.util.ArrayList;
12

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

    
23
import eu.etaxonomy.cdm.common.CdmUtils;
24
import eu.etaxonomy.cdm.model.common.DefinedTermBase;
25
import eu.etaxonomy.cdm.model.common.Language;
26
import eu.etaxonomy.cdm.model.common.Representation;
27
import eu.etaxonomy.cdm.model.common.TermBase;
28
import eu.etaxonomy.cdm.model.common.TermVocabulary;
29
import eu.etaxonomy.taxeditor.preference.PreferencesUtil;
30

    
31
/**
32
 * @author l.morris
33
 * @date 9 Dec 2011
34
 *
35
 */
36
public class TermLabelProvider extends StyledCellLabelProvider {
37

    
38
    private static Color vocColor = Display.getCurrent().getSystemColor(SWT.COLOR_BLUE);
39
    private Styler vocStyler;
40

    
41
    public TermLabelProvider() {
42
    }
43

    
44
    public TermLabelProvider(Styler vocStyler){
45
        this.vocStyler = vocStyler;
46
    }
47

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

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

    
56
        if (element instanceof TermVocabulary && text != null) {
57
            StyledString styledString = new StyledString(text, getVocabularyStyler());
58
            StyleRange[] styleRanges;
59
            styleRanges = styledString.getStyleRanges();
60
            cell.setStyleRanges(styleRanges);
61
        }
62
        super.update(cell);
63
    }
64

    
65
    public StyledString getStyledText(Object element) {
66

    
67
        if (element instanceof TermVocabulary) {
68
            new StyledString(getText(element), getVocabularyStyler());
69
        }
70
        return new StyledString(getText(element), StyledString.QUALIFIER_STYLER);
71
    }
72

    
73
    public String getText(Object element) {
74
    	if(element instanceof TermBase){
75
    		TermBase termBase = (TermBase)element;
76
    		Representation rep = termBase.getRepresentation(PreferencesUtil.getGlobalLanguage());
77
    		if (rep == null){
78
    			rep = termBase.getPreferredRepresentation(new ArrayList<Language>());
79
    		}
80
    		String label = rep != null? rep.getLabel() : termBase.getTitleCache();
81
    		if (element instanceof DefinedTermBase) {
82
    			DefinedTermBase<?> dtb = (DefinedTermBase<?>) element;
83
    			return CdmUtils.concat(" : ", dtb.getIdInVocabulary(), label);
84

    
85
    		} else {
86
    			return label;
87
    		}
88
    	}
89

    
90
        // FIXME : must throw an exception here
91
        return element.toString();
92
    }
93

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

    
106
}
(7-7/8)