Project

General

Profile

Download (4.51 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 static Color kindOfColor = Display.getCurrent().getSystemColor(SWT.COLOR_DARK_GRAY);
40
    private Styler vocStyler;
41
    private Styler kindOfStyler;
42

    
43
    public TermLabelProvider() {
44
        this.vocStyler = new Styler() {
45
            @Override
46
            public void applyStyles(TextStyle textStyle) {
47
                textStyle.foreground = Display.getCurrent().getSystemColor(SWT.COLOR_BLACK);
48
            }
49
        };
50
    }
51

    
52
    public TermLabelProvider(Styler vocStyler){
53
        this.vocStyler = vocStyler;
54
    }
55

    
56
    @Override
57
    public void update(ViewerCell cell) {
58
        Object element = cell.getElement();
59

    
60
        String text = getText(element);
61
        cell.setText(text);
62

    
63
        Styler styler = null;
64
        if (element instanceof TermVocabulary && text != null) {
65
            styler = getVocabularyStyler();
66
        }
67
        else if(element instanceof DefinedTermBase && ((DefinedTermBase) element).getKindOf()!=null){
68
            styler = getKindOfStyler();
69
        }
70
        if(styler!=null){
71
            StyledString styledString = new StyledString(text, styler);
72
            StyleRange[] styleRanges;
73
            styleRanges = styledString.getStyleRanges();
74
            cell.setStyleRanges(styleRanges);
75
        }
76
        super.update(cell);
77
    }
78

    
79
//    public StyledString getStyledText(Object element) {
80
//        if (element instanceof TermVocabulary) {
81
//            new StyledString(getText(element), getVocabularyStyler());
82
//        }
83
//        else if (element instanceof DefinedTermBase && ((DefinedTermBase) element).getKindOf()!=null) {
84
//            new StyledString(getText(element), getKindOfStyler());
85
//        }
86
//        return new StyledString(getText(element), StyledString.QUALIFIER_STYLER);
87
//    }
88

    
89
    public String getText(Object element) {
90
        String label = null;
91
    	if(element instanceof TermBase){
92
    		TermBase termBase = (TermBase)element;
93
    		Representation rep = termBase.getRepresentation(PreferencesUtil.getGlobalLanguage());
94
    		if (rep == null){
95
    			rep = termBase.getPreferredRepresentation(new ArrayList<Language>());
96
    		}
97
    		label = rep != null? rep.getLabel() : termBase.generateTitle();
98
    		if (element instanceof DefinedTermBase && label!=null) {
99
    			DefinedTermBase<?> dtb = (DefinedTermBase<?>) element;
100
    			label = CdmUtils.concat(" : ", dtb.getIdInVocabulary(), label);
101
    		}
102
    		if(label==null){
103
    		    label = ((TermBase) element).generateTitle();
104
    		}
105
    	}
106
        // FIXME : must throw an exception here
107
    	if(label==null){
108
    	    label = element.toString();
109
    	}
110
        return label;
111
    }
112

    
113
    protected Styler getVocabularyStyler() {
114
        if (vocStyler == null) {
115
            vocStyler = new Styler() {
116
                @Override
117
                public void applyStyles(TextStyle textStyle) {
118
                    textStyle.foreground = vocColor;
119
                }
120
            };
121
        }
122
        return vocStyler;
123
    }
124

    
125
    protected Styler getKindOfStyler() {
126
        if (kindOfStyler == null) {
127
            kindOfStyler = new Styler() {
128
                @Override
129
                public void applyStyles(TextStyle textStyle) {
130
                    textStyle.foreground = kindOfColor;
131
                }
132
            };
133
        }
134
        return kindOfStyler;
135
    }
136

    
137
}
(6-6/9)