Project

General

Profile

Download (4.18 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
    }
45

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

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

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

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

    
73
//    public StyledString getStyledText(Object element) {
74
//        if (element instanceof TermVocabulary) {
75
//            new StyledString(getText(element), getVocabularyStyler());
76
//        }
77
//        else if (element instanceof DefinedTermBase && ((DefinedTermBase) element).getKindOf()!=null) {
78
//            new StyledString(getText(element), getKindOfStyler());
79
//        }
80
//        return new StyledString(getText(element), StyledString.QUALIFIER_STYLER);
81
//    }
82

    
83
    public String getText(Object element) {
84
        String label = null;
85
    	if(element instanceof TermBase){
86
    		TermBase termBase = (TermBase)element;
87
    		Representation rep = termBase.getRepresentation(PreferencesUtil.getGlobalLanguage());
88
    		if (rep == null){
89
    			rep = termBase.getPreferredRepresentation(new ArrayList<Language>());
90
    		}
91
    		label = rep != null? rep.getLabel() : termBase.getTitleCache();
92
    		if (element instanceof DefinedTermBase && label!=null) {
93
    			DefinedTermBase<?> dtb = (DefinedTermBase<?>) element;
94
    			label = CdmUtils.concat(" : ", dtb.getIdInVocabulary(), label);
95
    		}
96
    	}
97
        // FIXME : must throw an exception here
98
    	if(label==null){
99
    	    label = element.toString();
100
    	}
101
        return label;
102
    }
103

    
104
    private Styler getVocabularyStyler() {
105
        if (vocStyler == null) {
106
            vocStyler = new Styler() {
107
                @Override
108
                public void applyStyles(TextStyle textStyle) {
109
                    textStyle.foreground = vocColor;
110
                }
111
            };
112
        }
113
        return vocStyler;
114
    }
115

    
116
    private Styler getKindOfStyler() {
117
        if (kindOfStyler == null) {
118
            kindOfStyler = new Styler() {
119
                @Override
120
                public void applyStyles(TextStyle textStyle) {
121
                    textStyle.foreground = kindOfColor;
122
                }
123
            };
124
        }
125
        return kindOfStyler;
126
    }
127

    
128
}
(7-7/8)