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 org.apache.commons.lang.StringUtils;
|
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.model.common.DefinedTermBase;
|
24
|
import eu.etaxonomy.cdm.model.common.TermBase;
|
25
|
import eu.etaxonomy.cdm.model.common.TermVocabulary;
|
26
|
|
27
|
/**
|
28
|
* @author l.morris
|
29
|
* @date 9 Dec 2011
|
30
|
*
|
31
|
*/
|
32
|
public class TermLabelProvider extends StyledCellLabelProvider {
|
33
|
|
34
|
private static Color vocColor = Display.getCurrent().getSystemColor(
|
35
|
SWT.COLOR_BLUE);
|
36
|
private Styler vocStyler;
|
37
|
/* (non-Javadoc)
|
38
|
* @see org.eclipse.jface.viewers.DelegatingStyledCellLabelProvider.IStyledLabelProvider#getStyledText(java.lang.Object)
|
39
|
*/
|
40
|
|
41
|
/*
|
42
|
* (non-Javadoc)
|
43
|
*
|
44
|
* @see
|
45
|
* org.eclipse.jface.viewers.StyledCellLabelProvider#update(org.eclipse.
|
46
|
* jface.viewers.ViewerCell)
|
47
|
*/
|
48
|
@Override
|
49
|
public void update(ViewerCell cell) {
|
50
|
Object element = cell.getElement();
|
51
|
int columnIndex = cell.getColumnIndex();
|
52
|
|
53
|
String text = getText(element);
|
54
|
cell.setText(text);
|
55
|
|
56
|
if (element instanceof TermVocabulary) {
|
57
|
StyledString styledString = new StyledString(text, getVocabularyStyler());
|
58
|
StyleRange[] styleRanges;
|
59
|
styleRanges = styledString.getStyleRanges();
|
60
|
cell.setStyleRanges(styleRanges);
|
61
|
}
|
62
|
super.update(cell);
|
63
|
}
|
64
|
|
65
|
public StyledString getStyledText(Object element) {
|
66
|
|
67
|
if(element instanceof TermVocabulary){
|
68
|
new StyledString(getText(element), getVocabularyStyler());
|
69
|
}
|
70
|
return new StyledString(getText(element), StyledString.QUALIFIER_STYLER);
|
71
|
}
|
72
|
|
73
|
/* (non-Javadoc)
|
74
|
* @see org.eclipse.jface.viewers.LabelProvider#getText(java.lang.Object)
|
75
|
*/
|
76
|
|
77
|
public String getText(Object element) {
|
78
|
|
79
|
if(element instanceof DefinedTermBase){
|
80
|
DefinedTermBase<?> dtb = (DefinedTermBase<?>)element;
|
81
|
if (StringUtils.isNotBlank(dtb.getIdInVocabulary())){
|
82
|
return dtb.getIdInVocabulary() + " : " + dtb.getTitleCache();
|
83
|
} else{
|
84
|
return dtb.getTitleCache();
|
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
|
}
|