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) {
|
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
|
|
76
|
if (element instanceof DefinedTermBase) {
|
77
|
DefinedTermBase<?> dtb = (DefinedTermBase<?>) element;
|
78
|
|
79
|
Representation rep = dtb.getRepresentation(PreferencesUtil.getGlobalLanguage());
|
80
|
if ( rep == null){
|
81
|
rep = dtb.getPreferredRepresentation(new ArrayList<Language>());
|
82
|
}
|
83
|
String label = rep != null? rep.getLabel() : dtb.getTitleCache();
|
84
|
return CdmUtils.concat(" : ", dtb.getIdInVocabulary(), label);
|
85
|
|
86
|
} else if (element instanceof TermBase) {
|
87
|
return ((TermBase) element).getTitleCache();
|
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
|
}
|