Project

General

Profile

Download (3.29 KB) Statistics
| Branch: | Tag: | Revision:
1
// $Id$
2
/**
3
 * Copyright (C) 2009 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.editor.definedterm;
11

    
12
import java.util.ArrayList;
13

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

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

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

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

    
42
    public TermLabelProvider() {
43
    }
44

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

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

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

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

    
66
    public StyledString getStyledText(Object element) {
67

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

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

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

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

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

    
107
}
(7-7/8)