Project

General

Profile

Download (4.27 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.generateTitle();
92
    		if (element instanceof DefinedTermBase && label!=null) {
93
    			DefinedTermBase<?> dtb = (DefinedTermBase<?>) element;
94
    			label = CdmUtils.concat(" : ", dtb.getIdInVocabulary(), label);
95
    		}
96
    		if(label==null){
97
    		    label = ((TermBase) element).generateTitle();
98
    		}
99
    	}
100
        // FIXME : must throw an exception here
101
    	if(label==null){
102
    	    label = element.toString();
103
    	}
104
        return label;
105
    }
106

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

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

    
131
}
(4-4/7)